def test_manual_wrap_extension_quote_ident(self): from ddtrace import patch_all patch_all() from psycopg2.extensions import quote_ident # NOTE: this will crash if it doesn't work. # TypeError: argument 2 must be a connection or a cursor conn = psycopg2.connect(**POSTGRES_CONFIG) quote_ident('foo', conn)
def test_manual_wrap_extension_quote_ident_standalone(): from ddtrace import patch_all patch_all() from psycopg2.extensions import quote_ident # NOTE: this will crash if it doesn't work. # TypeError: argument 2 must be a connection or a cursor conn = psycopg2.connect(**POSTGRES_CONFIG) quote_ident('foo', conn)
def test_gevent_auto_patching(): import ddtrace ddtrace.patch_all() # Patch on import import gevent # noqa from ddtrace.contrib.gevent import GeventContextProvider assert isinstance(ddtrace.tracer.context_provider, GeventContextProvider)
def create_app(test_config=None): # Grab DD environment variables for APM config. dd_environment = False try: agent_service_host = os.environ['DD_AGENT_SERVICE_HOST'] agent_service_port = os.environ['DD_AGENT_SERVICE_PORT'] if (agent_service_host and agent_service_port): dd_environment = True except: pass # If variables not set, skips APM configuration. if (dd_environment): patch_all() tracer.configure( hostname=os.environ['DD_AGENT_SERVICE_HOST'], port=os.environ['DD_AGENT_SERVICE_PORT'], ) # Create and configure the application app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'app.sqlite'), ) if test_config is None: # Load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # Load the test config if passed in app.config.from_mapping(test_config) # Ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # Attached DB from . import db db.init_app(app) # Attach routes from . import auth app.register_blueprint(auth.bp) from . import blog app.register_blueprint(blog.bp) app.add_url_rule('/', endpoint='index') return app
def test_user_specified_service(self): """ When a service name is specified by the user The algoliasearch integration should use it as the service name """ patch_all() Pin.override(self.index, tracer=self.tracer) self.perform_search("test search") spans = self.get_spans() self.reset() assert spans, spans assert len(spans) == 1 assert spans[0].service == "mysvc" unpatch()
def test_patch_all_auto_enable(self): patch_all() Pin.override(self.index, tracer=self.tracer) self.perform_search('test search') spans = self.get_spans() self.reset() assert spans, spans assert len(spans) == 1 unpatch() self.perform_search('test search') spans = self.get_spans() assert not spans, spans
def traced_wrapper(self, *args, **kwargs): if datadog_agent is None: return fn(self, *args, **kwargs) trace_check = is_affirmative(self.init_config.get('trace_check')) integration_tracing = is_affirmative( datadog_agent.get_config('integration_tracing')) if integration_tracing and trace_check: try: from ddtrace import patch_all, tracer patch_all() with tracer.trace(fn.__name__, service='{}-integration'.format(self.name), resource=fn.__name__): return fn(self, *args, **kwargs) except Exception: pass return fn(self, *args, **kwargs)
import os import ddtrace import django import psycopg2 import redis from django.core.wsgi import get_wsgi_application from gevent import monkey from whitenoise import WhiteNoise from common.apm import tracer ddtrace.patch_all() ddtrace.patch(gevent=True) ddtrace.Pin.override(django, tracer=tracer) ddtrace.Pin.override(psycopg2, tracer=tracer) ddtrace.Pin.override(redis, tracer=tracer) monkey.patch_all() os.environ.setdefault("DJANGO_SETTINGS_MODULE", "turnout.settings") application = get_wsgi_application() application = WhiteNoise(application) application.add_files("/app/static", prefix="static/") # type: ignore
from ddtrace import patch_all from .base.celery import app as celery_app from .base.wsgi import application patch_all(requests=True)
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 hostname: opts["hostname"] = hostname if port: opts["port"] = int(port) if priority_sampling: opts["priority_sampling"] = asbool(priority_sampling) 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)
def patch_all(request): if request.config.getoption("ddtrace-patch-all") or request.config.getini( "ddtrace-patch-all"): ddtrace.patch_all()
# 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)
from flask import render_template, flash, redirect, url_for, request, Response from app.forms import LoginForm, RegistrationForm from flask_login import logout_user, current_user, login_user, login_required from flask_limiter import Limiter from flask_limiter.util import get_remote_address from werkzeug.urls import url_parse from app.models import User, CrashLocationPoint, CrashDataPoint from ddtrace import tracer, config, patch_all; patch_all(logging = True) from datadog import initialize, statsd import redis, os, json, logging from app import app, db import ddtrace.profiling.auto from datetime import time import requests # ############## Environment Variables ##################### clientToken = os.environ["DD_CLIENT_TOKEN"] applicationId = os.environ["DD_APPLICATION_ID"] host = os.environ["DD_AGENT_HOST"] redis_port = 30001 tomcat_port = 30002 ############# DogStatsD & Tracer Configuration ############### options = { 'statsd_host': host, 'statsd_port':8125 } initialize(**options)
# 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: 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"]}) except Exception as e: log.warn("error configuring Datadog tracing", exc_info=True)
""" Bootstrapping code that is run when using the `ddtrace-run` Python entrypoint Add all monkey-patching that needs to run by default here """ import os import logging logging.basicConfig() log = logging.getLogger(__name__) try: from ddtrace import tracer # Respect DATADOG_* environment variables in global tracer configuration enabled = os.environ.get("DATADOG_TRACE_ENABLED") if enabled and enabled.lower() == "false": tracer.configure(enabled=False) else: from ddtrace import patch_all; patch_all(django=True, flask=True, pylons=True) # 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)
import warnings import MySQLdb from datadog import statsd from ddtrace import patch_all from ddtrace import tracer from connectors.DbConnector import DbConnector from utility import nb_logging from utility.NbConfig import NbConfig import rollbar logger = nb_logging.setup_logger('MySqlClient') patch_all() STATSD_PREFIX = 'nb.MySqlClient.' class MySqlClient(DbConnector): def __init__(self, host, user, password, db_name): DbConnector.__init__(self) self.host = host self.user = user self.password = password self.db_name = db_name logger.info("Attempt to connect to DB %s on %s as user %s", db_name, host, user) self.conn = MySQLdb.connect(host=self.host, user=self.user,
""" auto patch things. """ # manual test for monkey patching import logging import sys # project import ddtrace # allow logging logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) ddtrace.tracer.debug_logging = True # Patch nothing ddtrace.patch() # Patch all except Redis ddtrace.patch_all(redis=False) # Patch Redis ddtrace.patch(redis=True)
import pylibmc import pympler.tracker import psycopg2 import redis # project import ddtrace from tests.contrib import config # verbosity logging.basicConfig(stream=sys.stderr, level=logging.INFO) ddtrace.tracer.debug_logging = False ddtrace.patch_all() ddtrace.tracer.writer = None class KitchenSink(object): def __init__(self): self._redis = redis.Redis(**config.REDIS_CONFIG) self._pg = psycopg2.connect(**config.POSTGRES_CONFIG) url = "%s:%s" % ( config.MEMCACHED_CONFIG["host"], config.MEMCACHED_CONFIG["port"]) self._pylibmc = pylibmc.Client([url]) def ping(self, i):
"console": { "class": "log_formatter.CustomJsonFormatter", }, }, "handlers": { "console": { "level": "DEBUG", "class": "logging.StreamHandler", "formatter": "console", }, }, "loggers": { "ServiceA": { "handlers": ["console"], "level": "DEBUG", "propagate": False, }, "django.server": { "handlers": ["console"], "level": "DEBUG", "propagate": False, }, }, } patch_all(logging=True) tracer.configure(hostname=os.getenv("DATADOG_HOST"), port=8126, enabled=True) initialize(statsd_host=os.getenv("DATADOG_HOST"), statsd_port=8125, host_name="django3")
# Patching # DEV: This is here only for those who choose not to use `ddtrace-run` from ddtrace import patch_all; patch_all(flask=True, requests=True) # noqa # Datadog from ddtrace import tracer # Flask from flask import Flask, Response from flask import after_this_request from flask import abort, jsonify, render_template, url_for from flask.views import View from werkzeug.routing import Rule # Extensions from flask_caching import Cache from flask_cors import CORS # Utilities import requests # Internal from .blueprint import bp from .exceptions import AppException from .limiter import limiter from .signals import connect_signals # Create a new app app = Flask(__name__) # Register our blueprint and it's endpoints on our app
"""Initialize app.""" from flask import Flask from flask_login import LoginManager from flask_session import Session from flask_sqlalchemy import SQLAlchemy from ddtrace import patch_all db = SQLAlchemy() login_manager = LoginManager() sess = Session() patch_all() def create_app(): """Construct the core flask_session_tutorial.""" app = Flask(__name__, instance_relative_config=False) app.config.from_object("config.Config") # Initialize Plugins db.init_app(app) login_manager.init_app(app) sess.init_app(app) with app.app_context(): from . import routes from . import auth from .assets import compile_static_assets, compile_auth_assets app.register_blueprint(routes.main_bp) app.register_blueprint(auth.auth_bp)
from logging.config import dictConfig # Datadog Trace import ddtrace from ddtrace import tracer from ddtrace.contrib.flask import TraceMiddleware from ddtrace import Pin, patch, patch_all # DogStatsD from datadog import statsd # DB Config import db_config patch_all(logging=True, mysql=True, flask=False) with tracer.trace("web.request", service="kikeyama_service") as span: span.set_tag("source", "flask_apm") ## Have flask use stdout as the logger #patch(logging=True) FORMAT = ('%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] ' '[dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] ' '- %(message)s') dictConfig({ 'version': 1, 'formatters': {'default': { 'format': FORMAT, }},
""" Django settings for example project. Generated by 'django-admin startproject' using Django 3.0.1. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import logging logging.basicConfig() logging.getLogger('ddtrace').setLevel(logging.DEBUG) from ddtrace import patch_all; patch_all() import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'nw-_#bmvm$tk%5m29ev7moe$7%*pdt+g#sx=ama%9%m4f&_@1i' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True
opts["hostname"] = hostname if port: opts["port"] = int(port) 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)
from flask import Flask from ddtrace import patch_all; patch_all(logging=True) import ddtrace.profiling.auto from ddtrace import tracer import logging import sys # Have flask use stdout as the logger main_logger = logging.getLogger() main_logger.setLevel(logging.DEBUG) ddtrace.config.analytics_enabled = True c = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') c.setFormatter(formatter) main_logger.addHandler(c) app = Flask(__name__) @app.route('/') def api_entry(): return 'Entrypoint to the Application' @app.route('/api/apm') def apm_endpoint(): return 'Getting APM Started' @app.route('/api/trace') def trace_endpoint(): return 'Posting Traces'