def create_function_execution_span( context, function_name, is_cold_start, trace_context, merge_xray_traces ): tags = {} if context: function_arn = (context.invoked_function_arn or "").lower() tk = function_arn.split(":") function_arn = ":".join(tk[0:7]) if len(tk) > 7 else function_arn function_version = tk[7] if len(tk) > 7 else "$LATEST" tags = { "cold_start": str(is_cold_start).lower(), "function_arn": function_arn, "function_version": function_version, "request_id": context.aws_request_id, "resource_names": context.function_name, "datadog_lambda": datadog_lambda_version, "dd_trace": ddtrace_version, } source = trace_context["source"] if source == TraceContextSource.XRAY and merge_xray_traces: tags["_dd.parent_source"] = source args = { "service": "aws.lambda", "resource": function_name, "span_type": "serverless", } tracer.set_tags({"_dd.origin": "lambda"}) span = tracer.trace("aws.lambda", **args) if span: span.set_tags(tags) return span
def create_function_execution_span(context, function_name, is_cold_start, trace_context): tags = {} if context: function_arn = (context.invoked_function_arn or "").lower() tags = { "cold_start": str(is_cold_start).lower(), "function_arn": function_arn, "request_id": context.aws_request_id, "resource_names": context.function_name, } source = trace_context["source"] if source != TraceContextSource.DDTRACE: tags["_dd.parent_source"] = source args = { "service": "aws.lambda", "resource": function_name, "span_type": "serverless", } tracer.set_tags({"_dd.origin": "lambda"}) span = tracer.trace("aws.lambda", **args) if span: span.set_tags(tags) return span
def test_ingest_request(self): # test that ingest request is created successfully worker = LightstepMetricsWorker(Mock()) req = worker._ingest_request() self.assertEqual(len(req.reporter.tags), 5) self.assertEqual(len(worker._labels), 3) # update tags tracer.set_tags({ SERVICE_NAME: "test-component", SERVICE_VERSION: "vTest" }) req = worker._ingest_request() self.assertEqual(len(req.reporter.tags), 5) self.assertEqual(len(worker._labels), 3) for tag in req.reporter.tags: if tag.key == COMPONENT_NAME: self.assertEqual(tag.string_value, "test-component") if tag.key == SERVICE_VERSION: self.assertEqual(tag.string_value, "vTest") for label in worker._labels: if label.key == COMPONENT_NAME: self.assertEqual(label.string_value, "test-component") if label.key == SERVICE_VERSION: self.assertEqual(label.string_value, "vTest") req = worker._ingest_request() self.assertEqual(len(req.reporter.tags), 5) self.assertEqual(len(worker._labels), 3)
def home_page(): parent_id = int(tracer.get_call_context().get_current_root_span().trace_id) app.logger.info(parent_id) tracer.set_tags({'parent_span': parent_id}) return render_template('index.html', image_url=dog_image(), dog_fact=dog_fact() )
def get_ls_tracer(): tracer.configure(http_propagator=B3HTTPPropagator, settings={}) tracer.set_tags( { "lightstep.service_name": os.getenv("LS_SERVICE_NAME"), "service.version": os.getenv("LS_SERVICE_VERSION"), "lightstep.access_token": os.getenv("LS_ACCESS_TOKEN"), } )
def api_entry(): start_time = time.time() app.logger.info('getting root endpoint') # return 'Entrypoint to the Application' name = flask_request.args.get('name', str) tracer.set_tags({'name': name}) mycursor.execute("SELECT Name, UUID, Number FROM kikeyama_table where name='%s'" % name) myresult = mycursor.fetchall() for x in myresult: result = json.dumps(x) return result duration = time.time() - start_time statsd.distribution('kikeyama.dogstatsd.distribution.latency', duration) statsd.histogram('kikeyama.dogstatsd.histogram.latency', duration)
tracer.configure(**opts) if logs_injection: EXTRA_PATCHED_MODULES.update({'logging': True}) if patch: update_patched_modules() from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) debug = os.environ.get('DATADOG_TRACE_DEBUG') if debug and debug.lower() == 'true': tracer.debug_logging = True if 'DATADOG_ENV' in os.environ: tracer.set_tags({'env': os.environ['DATADOG_ENV']}) if 'DD_TRACE_GLOBAL_TAGS' in os.environ: add_global_tags(tracer) # Ensure sitecustomize.py is properly called if available in application directories: # * exclude `bootstrap_dir` from the search # * find a user `sitecustomize.py` module # * import that module via `imp` bootstrap_dir = os.path.dirname(__file__) path = list(sys.path) if bootstrap_dir in path: path.remove(bootstrap_dir) try:
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from ddtrace import tracer from ddtrace.propagation.b3 import B3HTTPPropagator tracer.configure(http_propagator=B3HTTPPropagator) tracer.set_tags({ "lightstep.service_name": "recommendationservice", "lightstep.access_token": os.getenv("SECRET_ACCESS_TOKEN"), }) import random import time import traceback from concurrent import futures import googleclouddebugger import googlecloudprofiler import grpc from opencensus.trace.exporters import print_exporter from opencensus.trace.exporters import stackdriver_exporter from opencensus.trace.ext.grpc import server_interceptor from opencensus.trace.samplers import always_on
} ) config.django['service_name'] = 'nephrogo-api' config.django['instrument_databases'] = True config.django['instrument_caches'] = True config.django['trace_query_string'] = True config.django['analytics_enabled'] = True config.trace_headers([ 'user-agent', 'transfer-encoding', 'time-zone-name', 'accept-encoding', ]) tracer.set_tags({'env': 'production', 'version': GIT_COMMIT}) REDIS_URL = env.str('REDIS_URL', None) # Celery CELERY_BROKER_URL = REDIS_URL CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_RESULT_SERIALIZER = 'json' CELERY_TASK_SERIALIZER = 'json' CELERY_TIMEZONE = 'UTC' CELERY_BEAT_SCHEDULE = { 'sync_product_metrics': { 'task': 'core.tasks.sync_product_metrics', 'schedule': timedelta(minutes=15)
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY') AWS_STORAGE_BUCKET_NAME = 'feedsubs' AWS_DEFAULT_ACL = 'private' AWS_QUERYSTRING_EXPIRE = 7800 # 2h10, must be more than article cache from ddtrace import config as dc, tracer, patch_all tracer.configure(hostname=config('DD_AGENT_HOSTNAME', default='localhost'), port=config('DD_AGENT_PORT', 8126, cast=int), enabled=True) dc.django['service_name'] = 'feedsubs' dc.django['cache_service_name'] = 'feedsubs-cache' dc.django['database_service_name_prefix'] = 'feedsubs-' dc.django['instrument_databases'] = True dc.django['instrument_caches'] = True dc.django['trace_query_string'] = True dc.django['analytics_enabled'] = True tracer.set_tags({'env': 'prod'}) patch_all() import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from spinach.contrib.sentry_sdk_spinach import SpinachIntegration sentry_sdk.init(dsn=config('SENTRY_DSN'), environment='prod', release=pkg_resources.require("feedsubs")[0].version, send_default_pii=True, integrations=[DjangoIntegration(), SpinachIntegration()])
from ddtrace import patch_all, tracer # noqa patch(flask=True) patch_all() # noqa import signal import os import atexit import time import logging import threading from ddtrace import tracer from datadog import statsd, initialize from flask import Flask import requests tracer.configure(uds_path="/var/run/datadog/apm.socket") tracer.set_tags({"env": os.environ.get("ENV")}) initialize(statsd_socket_path="/var/run/datadog/dsd.socket") application = Flask(__name__) @application.route("/") def hello(): logging.info("root get") return "<h1 style='color:blue'>Hello There!</h1>" stopPrint = False def doStop(): logging.info("stop!")
if opts: tracer.configure(**opts) if not hasattr(sys, 'argv'): sys.argv = [''] if patch: update_patched_modules() from ddtrace import patch_all; patch_all(**EXTRA_PATCHED_MODULES) # noqa debug = os.environ.get("DATADOG_TRACE_DEBUG") if debug and debug.lower() == "true": tracer.debug_logging = True if 'DATADOG_ENV' in os.environ: tracer.set_tags({"env": os.environ["DATADOG_ENV"]}) # Ensure sitecustomize.py is properly called if available in application directories: # * exclude `bootstrap_dir` from the search # * find a user `sitecustomize.py` module # * import that module via `imp` bootstrap_dir = os.path.dirname(__file__) path = list(sys.path) path.remove(bootstrap_dir) try: (f, path, description) = imp.find_module('sitecustomize', path) except ImportError: pass else: # `sitecustomize.py` found, load it
if port: opts["port"] = int(port) if priority_sampling: opts["priority_sampling"] = asbool(priority_sampling) tracer.configure(**opts) if trace_enabled: update_patched_modules() from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) dd_env = os.getenv("DD_ENV") if dd_env: tracer.set_tags({constants.ENV_KEY: dd_env}) if "DD_TRACE_GLOBAL_TAGS" in os.environ: env_tags = os.getenv("DD_TRACE_GLOBAL_TAGS") tracer.set_tags(parse_tags_str(env_tags)) # instrumentation telemetry writer should be enabled/started after the global tracer and configs # are initialized if asbool(os.getenv("DD_INSTRUMENTATION_TELEMETRY_ENABLED")): telemetry_writer.enable() # Check for and import any sitecustomize that would have normally been used # had ddtrace-run not been used. bootstrap_dir = os.path.dirname(__file__) if bootstrap_dir in sys.path: index = sys.path.index(bootstrap_dir)
from ddtrace import tracer from ddtrace.propagation.b3 import B3HTTPPropagator tracer.configure(http_propagator=B3HTTPPropagator, hostname="ingest.lightstep.com", port=443, https=True) tracer.set_tags({ "lightstep.service_name": "lightstep-py", "lightstep.access_token": "<access-token>", }) from json import loads, dumps from uuid import uuid4 from flask import Flask, request, render_template # BLOCK 0 from kitchen_service import KitchenService from kitchen_consumer import KitchenConsumer from donut import Donut from status import NEW_ORDER # BLOCK 1 app = Flask(__name__) app.static_folder = 'static' # BLOCK 2
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'management.middleware.AssociateSheltersMiddleware', ] AUTH_USER_MODEL = 'web.User' # Datadog tracer.configure(hostname='ddagent', port=8126, enabled=not DEBUG) config.django['service_name'] = 'getpet-platform' config.django['instrument_databases'] = True config.django['instrument_caches'] = True config.django['trace_query_string'] = True config.django['analytics_enabled'] = True tracer.set_tags({'env': 'production'}) if DEBUG and ENABLE_DEBUG_DRAWER_IN_DEBUG: INSTALLED_APPS.append('debug_toolbar') MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware') DEBUG_TOOLBAR_PANELS = [ 'debug_toolbar.panels.versions.VersionsPanel', 'debug_toolbar.panels.timer.TimerPanel', 'debug_toolbar.panels.settings.SettingsPanel', 'debug_toolbar.panels.headers.HeadersPanel', 'debug_toolbar.panels.request.RequestPanel', 'debug_toolbar.panels.sql.SQLPanel', 'debug_toolbar.panels.staticfiles.StaticFilesPanel', 'debug_toolbar.panels.templates.TemplatesPanel', 'debug_toolbar.panels.cache.CachePanel', 'debug_toolbar.panels.signals.SignalsPanel',
from ddtrace import tracer from ddtrace.propagation.b3 import B3HTTPPropagator tracer.configure(http_propagator=B3HTTPPropagator, hostname="localhost", port=8181) tracer.set_tags({"lightstep.service_name": "lightstep-py"}) from flask import Flask from flask import request app = Flask(__name__) @app.route('/') def index(): print(request.headers) span = tracer.current_span() print(hex(span.context.trace_id)) print(hex(span.context.span_id)) return 'Hello world' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
if enabled and enabled.lower() == "false": opts["enabled"] = False patch = False if hostname: opts["hostname"] = hostname if port: opts["port"] = int(port) if priority_sampling: opts["priority_sampling"] = asbool(priority_sampling) opts["collect_metrics"] = asbool(os.environ.get(METRICS_ENABLED_ENV_VAR, True)) key_intersection = set(_LIGHTSTEP_ENV_VARS.keys()) & set(os.environ.keys()) if key_intersection: tracer.set_tags({_LIGHTSTEP_ENV_VARS[key]: os.environ[key] for key in key_intersection}) if "DD_TRACE_GLOBAL_TAGS" in os.environ: add_global_tags(tracer) if opts: tracer.configure(**opts) if patch: update_patched_modules() from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) if "DATADOG_ENV" in os.environ: tracer.set_tags({constants.ENV_KEY: os.environ["DATADOG_ENV"]})
LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' from ddtrace import config, tracer tracer.configure(hostname='datadog-agent', port=8126, enabled=True) config.django['service_name'] = 'my-django-app' config.django['cache_service_name'] = 'my-cache' config.django['database_service_name_prefix'] = 'my-' config.django['instrument_databases'] = True config.django['instrument_caches'] = True config.django['trace_query_string'] = True config.django['analytics_enabled'] = True config.django['analytics_sample_rate'] = 0.5 tracer.set_tags({'env': 'sandbox'}) from ddtrace import patch_all import django patch_all()
from ddtrace import patch_all, tracer patch_all() from flask import Flask import requests tracer.set_tags({'team': 'cake'}) app = Flask(__name__) @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello, World' @app.route('/api') def api(): response = requests.get('https://api.github.com') current_span = tracer.current_span() if current_span: current_span.set_tag('important_response_value', len(response.text)) text_length = len(response.text)
# ls-trace-run python client.py import os import random import requests import time from ddtrace import tracer from ddtrace.propagation.b3 import B3HTTPPropagator tracer.configure(http_propagator=B3HTTPPropagator) tracer.set_tags( { "lightstep.service_name": os.getenv("LIGHTSTEP_COMPONENT_NAME"), "service.version": os.getenv("LIGHTSTEP_SERVICE_VERSION"), "lightstep.access_token": os.getenv("LIGHTSTEP_ACCESS_TOKEN"), } ) if __name__ == "__main__": target = os.getenv("TARGET_URL", "http://localhost:8081") while True: url = f"{target}/content/{random.randint(1,1024)}" try: res = requests.get(url) print(f"Request to {url}, got {len(res.content)} bytes") except Exception as e: print(f"Request to {url} failed {e}") pass time.sleep(1)
# Respect DATADOG_* environment variables in global tracer configuration enabled = os.environ.get("DATADOG_TRACE_ENABLED") hostname = os.environ.get("DATADOG_TRACE_AGENT_HOSTNAME") port = os.environ.get("DATADOG_TRACE_AGENT_PORT") opts = {} if enabled and enabled.lower() == "false": opts["enabled"] = False patch = False if hostname: opts["hostname"] = hostname if port: opts["port"] = int(port) if opts: tracer.configure(**opts) if patch: from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) # noqa debug = os.environ.get("DATADOG_TRACE_DEBUG") if debug and debug.lower() == "true": tracer.debug_logging = True if 'DATADOG_ENV' in os.environ: tracer.set_tags({"env": os.environ["DATADOG_ENV"]}) except Exception as e: log.warn("error configuring Datadog tracing", exc_info=True)
res = requests.post( url, data='{"donuts":[{"flavor":"cinnamon","quantity":1}]}' ) else: res = requests.get(url) print(f"Request to {url}, got {len(res.content)} bytes") except Exception as e: print(f"Request to {url} failed {e}") if __name__ == "__main__": config_file = os.environ.get("INTEGRATION_CONFIG_FILE") if not config_file: raise Exception("Config file not specified!!") config_data = {} with open(config_file) as f: config_data = yaml.load(f, Loader=yaml.FullLoader) destinations = config_data.get("endpoints") tracer.set_tags( { "lightstep.service_name": env.str("LS_SERVICE_NAME"), "service.version": env.str("LS_SERVICE_VERSION"), "lightstep.access_token": env.str("LS_ACCESS_TOKEN"), } ) while True: send_requests(destinations) time.sleep(2)
opts['collect_metrics'] = asbool(get_env('runtime_metrics', 'enabled')) if opts: tracer.configure(**opts) if logs_injection: EXTRA_PATCHED_MODULES.update({'logging': True}) if patch: update_patched_modules() from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) if 'DATADOG_ENV' in os.environ: tracer.set_tags({constants.ENV_KEY: os.environ['DATADOG_ENV']}) if 'DD_TRACE_GLOBAL_TAGS' in os.environ: add_global_tags(tracer) # Ensure sitecustomize.py is properly called if available in application directories: # * exclude `bootstrap_dir` from the search # * find a user `sitecustomize.py` module # * import that module via `imp` bootstrap_dir = os.path.dirname(__file__) path = list(sys.path) if bootstrap_dir in path: path.remove(bootstrap_dir) try:
def create_app(debug=False): app = Flask(__name__) app.debug = debug options = { 'statsd_host': os.environ['DATADOG_AGENT_HOST_IP'], 'statsd_port': 8125 } initialize(**options) tracer.set_tags({"pod_name": os.environ.get("MY_POD_NAME")}) # Flask index route, get a random adlib puzzle @app.route('/', methods=['GET']) def view_index(): statsd.increment("doglib.index_web_counter", tags=['pod_name:' + os.environ['MY_POD_NAME']]) random_key = utils.get_random_key() if (random_key is None): return render_template("adlib-empty.html"), 200 else: id = random_key.id_or_name entity = utils.get_entity(id) return render_template("adlib-index.html", entity=entity, id=id), 200 # Flask adib solution @app.route('/<int:id>/solution/', methods=['GET', 'POST']) def view_adlib_solution(id): entity = utils.get_solution(id) if entity: if (request.method == 'POST'): words = request.form.getlist('word') utils.process_adlib(id, words) else: words = entity['popular_words'] return render_template("adlib-solution.html", entity=entity, word_data=words, id=id) else: return render_template("404.html"), 404 # Flask create adlib @app.route('/create/', methods=['GET', 'POST']) def create_adlib(): if (request.method == 'POST'): if ('title' in request.form and 'adlib' in request.form and 'entries' in request.form): utils.create_entity(request.form['title'], request.form['adlib'], request.form['entries']) else: return "error", 500 return render_template("adlib-create.html"), 200 return app
from flask import Flask, Response, jsonify from flask import request as flask_request from ddtrace import tracer, patch, config from ddtrace.contrib.flask import TraceMiddleware from bootstrap import create_app, db from models import Network, Sensor import random sensors = [] # Tracer configuration tracer.configure(hostname='agent') tracer.set_tags({'env': 'workshop'}) patch(requests=True) # enable distributed tracing for requests # to send headers (globally) config.requests['distributed_tracing'] = True app = create_app() traced_app = TraceMiddleware(app, tracer, service='sensors-api', distributed_tracing=True) @app.route('/') def hello():
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from ddtrace import tracer from ddtrace.propagation.b3 import B3HTTPPropagator tracer.configure( http_propagator=B3HTTPPropagator, hostname=os.environ['LIGHTSTEP_HOST'], port=os.environ['LIGHTSTEP_PORT'], https=os.environ['LIGHTSTEP_PLAINTEXT'] is not "true" ) tracer.set_tags( { "lightstep.service_name": "emailservice", "lightstep.access_token": os.getenv("LIGHTSTEP_ACCESS_TOKEN"), } ) from concurrent import futures import argparse import sys import time import grpc from jinja2 import Environment, FileSystemLoader, select_autoescape, TemplateError from google.api_core.exceptions import GoogleAPICallError import demo_pb2 import demo_pb2_grpc from grpc_health.v1 import health_pb2 from grpc_health.v1 import health_pb2_grpc
if priority_sampling: opts["priority_sampling"] = asbool(priority_sampling) opts["collect_metrics"] = asbool(get_env("runtime_metrics", "enabled")) if opts: tracer.configure(**opts) if patch: update_patched_modules() from ddtrace import patch_all patch_all(**EXTRA_PATCHED_MODULES) if "DATADOG_ENV" in os.environ: tracer.set_tags({constants.ENV_KEY: os.environ["DATADOG_ENV"]}) if "DD_TRACE_GLOBAL_TAGS" in os.environ: env_tags = os.getenv("DD_TRACE_GLOBAL_TAGS") tracer.set_tags(parse_tags_str(env_tags)) # Ensure sitecustomize.py is properly called if available in application directories: # * exclude `bootstrap_dir` from the search # * find a user `sitecustomize.py` module # * import that module via `imp` bootstrap_dir = os.path.dirname(__file__) path = list(sys.path) if bootstrap_dir in path: path.remove(bootstrap_dir)
datadog_version = env.str('DD_VERSION', default='') datadog_agent_hostname = env.str('DD_AGENT_SERVICE_HOST', default='localhost') datadog_agent_port = env.str('DD_AGENT_SERVICE_PORT', default='8125') datadog_statsd_port = env.str('DD_STATSD_SERVICE_PORT', default='8126') dd_tracer.configure( enabled=True, hostname=datadog_agent_hostname, port=datadog_agent_port, dogstatsd_url=f'udp://{datadog_agent_hostname}:{datadog_statsd_port}', ) dd_tracer.set_tags({ 'env': DATADOG_ENVIRONMENT, 'version': datadog_version, **dict(t.split(':') for t in DATADOG_TAGS), }) dd_config.analytics_enabled = True dd_config.health_metrics_enabled = True dd_config.celery['analytics_enabled'] = True dd_config.celery['distributed_tracing'] = True dd_config.celery[ 'producer_service_name'] = f'{DATADOG_SERVICE}-celery-queue' dd_config.celery['worker_service_name'] = f'{DATADOG_SERVICE}-celery' dd_config.django['analytics_enabled'] = True dd_config.django['cache_service_name'] = f'{DATADOG_SERVICE}-cache' dd_config.django['database_service_name_prefix'] = f'{DATADOG_SERVICE}-' dd_config.django['service_name'] = DATADOG_SERVICE dd_config.postgres['analytics_enabled'] = True dd_config.requests['analytics_enabled'] = True