示例#1
0
def initialize_tracer(service_name,
                      async_transport=False,
                      host=None,
                      port=0,
                      sample_rate=1.0):
    if sample_rate == 1.0:
        # sample all traces
        sampler_config = {'type': 'const', 'param': 1}
    elif sample_rate == 0.0:
        # sample none traces
        sampler_config = {'type': 'const', 'param': 0}
    else:
        # random sampling decision with the probability
        sampler_config = {'type': 'probabilistic', 'param': sample_rate}

    tracer_config = {
        'sampler': sampler_config,
    }

    if host:
        tracer_config['local_agent'] = {
            'reporting_host': host,
            'reporting_port': port
        }

    config = Config(
        config=tracer_config,
        service_name=service_name,
        validate=True,
        scope_manager=AsyncioScopeManager() if async_transport else None,
    )

    return config.new_tracer()
示例#2
0
 def test_tracer_context_provider_config(self):
     tracer = ddtrace.opentracer.Tracer("mysvc",
                                        scope_manager=AsyncioScopeManager())
     assert isinstance(
         tracer._dd_tracer.context_provider,
         ddtrace.contrib.asyncio.provider.AsyncioContextProvider,
     )
示例#3
0
def ot_tracer(ot_tracer_factory):
    return ot_tracer_factory(
        "asyncio_svc",
        config={},
        scope_manager=AsyncioScopeManager(),
        context_provider=ddtrace.contrib.asyncio.context_provider,
    )
示例#4
0
def setup_opentracing(app):
    """
    Helper function to setup opentracing with Jaeger client during setup.
    Use during app startup as follows:

    .. code-block:: python

        app = FastAPI()

        @app.on_event('startup')
        async def startup():
            setup_opentracing(app)

    :param app: app object, instance of FastAPI
    :return: None
    """
    config = Config(config={
        "local_agent": {
            "reporting_host": settings.jaeger_host,
            "reporting_port": settings.jaeger_port
        },
        "sampler": {
            "type": settings.jaeger_sampler_type,
            "param": settings.jaeger_sampler_rate,
        },
        "trace_id_header": settings.trace_id_header
    },
                    service_name=settings.service_name,
                    validate=True,
                    scope_manager=AsyncioScopeManager())

    # this call also sets opentracing.tracer
    app.tracer = config.initialize_tracer()
def ot_tracer(request, ot_tracer_factory):  # noqa: F811
    # use the dummy asyncio ot tracer
    request.instance.ot_tracer = ot_tracer_factory(
        "asyncio_svc",
        config={},
        scope_manager=AsyncioScopeManager(),
        context_provider=ddtrace.contrib.asyncio.context_provider,
    )
    request.instance.ot_writer = request.instance.ot_tracer._dd_tracer.writer
    request.instance.dd_tracer = request.instance.ot_tracer._dd_tracer
示例#6
0
    def test_parenting_200_ot(self):
        """OpenTracing version of test_handler."""
        ot_tracer = init_tracer('aiohttp_svc', self.tracer, scope_manager=AsyncioScopeManager())

        with ot_tracer.start_active_span('aiohttp_op'):
            request = yield from self.client.request('GET', '/')
            eq_(200, request.status)
            text = yield from request.text()

        eq_("What's tracing?", text)
        traces = self.tracer.writer.pop_traces()
        self._assert_200_parenting(traces)
示例#7
0
    def test_parenting_200_ot(self):
        """OpenTracing version of test_handler."""
        ot_tracer = init_tracer("aiohttp_svc",
                                self.tracer,
                                scope_manager=AsyncioScopeManager())

        with ot_tracer.start_active_span("aiohttp_op"):
            request = yield from self.client.request("GET", "/")
            assert 200 == request.status
            text = yield from request.text()

        assert "What's tracing?" == text
        traces = self.pop_traces()
        self._assert_200_parenting(traces)
示例#8
0
def init_jaeger_tracer() -> Tracer:
    config = Config(config={
        'sampler': {
            'type': 'const',
            'param': 1,
        },
        'logging': True,
        'propagation': "b3",
        'local_agent': {
            'reporting_host': 'localhost'
        }
    },
                    service_name='service1',
                    validate=True,
                    scope_manager=AsyncioScopeManager())
    return config.initialize_tracer()
示例#9
0
async def test_parenting_200_ot(app_tracer, aiohttp_client):
    """OpenTracing version of test_handler."""
    app, tracer = app_tracer
    client = await aiohttp_client(app)
    ot_tracer = init_tracer("aiohttp_svc",
                            tracer,
                            scope_manager=AsyncioScopeManager())

    with ot_tracer.start_active_span("aiohttp_op"):
        request = await client.request("GET", "/")
        assert 200 == request.status
        text = await request.text()

    assert "What's tracing?" == text
    traces = tracer.pop_traces()
    _assert_200_parenting(client, traces)
示例#10
0
def initialize_tracer(service_name):
    # pylint: disable=E0401
    from jaeger_client.config import Config
    from opentracing.scope_managers.asyncio import AsyncioScopeManager

    # pylint: enable=E0401

    config = Config(
        config={'sampler': {
            'type': 'const',
            'param': 1
        }},
        service_name=service_name,
        validate=True,
        scope_manager=AsyncioScopeManager(),
    )

    return config.initialize_tracer()
示例#11
0
def init_tracer(service_name: str):
    config = Config(
        config={
            "local_agent": {
                "reporting_host": JAEGER_HOST,
                "reporting_port": JAEGER_PORT,
            },
            "sampler": {
                "type": JAEGER_SAMPLER_TYPE,
                "param": JAEGER_SAMPLER_RATE
            },
            "trace_id_header": JAEGER_TRACE_ID_HEADER,
        },
        scope_manager=AsyncioScopeManager(),
        service_name=service_name,
        validate=True,
    )
    return config.initialize_tracer()
示例#12
0
def setup_opentracing(app):
    """
    Helper function to setup opentracing with Jaeger client during setup.
    Use during app startup as follows:

    .. code-block:: python

        app = FastAPI()

        @app.on_event('startup')
        async def startup():
            setup_opentracing(app)

    :param app: app object, instance of FastAPI
    :return: None
    """
    config = Config(config={
        "local_agent": {
            "reporting_host": settings.jaeger_host,
            "reporting_port": settings.jaeger_port
        },
        "sampler": {
            "type": settings.jaeger_sampler_type,
            "param": settings.jaeger_sampler_rate,
        },
        "trace_id_header": settings.trace_id_header
    },
                    service_name=settings.service_name,
                    validate=True,
                    scope_manager=AsyncioScopeManager())

    warnings.warn(
        """
        tracer object in request.app will be removed in favor of saving it in
        request.app.state in the next minor version 0.3.0
        """, FutureWarning)
    # this call also sets opentracing.tracer
    app.state.tracer = config.initialize_tracer()
    app.tracer = app.state.tracer
    def __init__(self,
                 config,
                 metrics=None,
                 service_name=None,
                 metrics_factory=None,
                 validate=False,
                 is_async=False,
                 enabled=True):
        if config is None:
            raise AttributeError("config cannot be empty")

        self.enabled = enabled
        if self.enabled:
            config = Config(
                config,
                metrics=metrics,
                service_name=service_name,
                metrics_factory=metrics_factory,
                validate=validate,
                scope_manager=AsyncioScopeManager() if is_async else None)
            self.tracer = config.initialize_tracer()
        else:
            self.tracer = EmptyTracer()
 def test_get_context_provider_for_scope_manager_asyncio(self):
     scope_manager = AsyncioScopeManager()
     ctx_prov = get_context_provider_for_scope_manager(scope_manager)
     assert isinstance(ctx_prov, ddtrace.contrib.asyncio.provider.AsyncioContextProvider)
示例#15
0
    def __init__(self, config, custom_reactor, tracer=opentracing.tracer):
        """
        Object that holds state for the entirety of a Sygnal instance.
        Args:
            config (dict): Configuration for this Sygnal
            custom_reactor: a Twisted Reactor to use.
            tracer (optional): an OpenTracing tracer. The default is the no-op tracer.
        """
        self.config = config
        self.reactor = custom_reactor
        self.pushkins = {}
        self.tracer = tracer

        logging_dict_config = config["log"]["setup"]
        logging.config.dictConfig(logging_dict_config)

        logger.debug("Started logging")

        observer = twisted_log.PythonLoggingObserver()
        observer.start()

        # Old format db config
        if config.get("db") is not None:
            logger.warning(
                "Config includes the legacy 'db' option, please migrate"
                " to 'database' instead")
            config["database"] = {
                "name": "sqlite3",
                "args": {
                    "dbfile": config["db"]["dbfile"]
                },
            }
        elif config.get("database") is None:
            config["database"] = {
                "name": "sqlite3",
                "args": {
                    "dbfile": "sygnal.db"
                },
            }

        sentrycfg = config["metrics"]["sentry"]
        if sentrycfg["enabled"] is True:
            import sentry_sdk

            logger.info("Initialising Sentry")
            sentry_sdk.init(sentrycfg["dsn"])

        promcfg = config["metrics"]["prometheus"]
        if promcfg["enabled"] is True:
            prom_addr = promcfg["address"]
            prom_port = int(promcfg["port"])
            logger.info("Starting Prometheus Server on %s port %d", prom_addr,
                        prom_port)

            prometheus_client.start_http_server(port=prom_port,
                                                addr=prom_addr or "")

        tracecfg = config["metrics"]["opentracing"]
        if tracecfg["enabled"] is True:
            if tracecfg["implementation"] == "jaeger":
                try:
                    import jaeger_client

                    jaeger_cfg = jaeger_client.Config(
                        config=tracecfg["jaeger"],
                        service_name=tracecfg["service_name"],
                        scope_manager=AsyncioScopeManager(),
                    )

                    self.tracer = jaeger_cfg.initialize_tracer()

                    logger.info("Enabled OpenTracing support with Jaeger")
                except ModuleNotFoundError:
                    logger.critical(
                        "You have asked for OpenTracing with Jaeger but do not have"
                        " the Python package 'jaeger_client' installed.")
                    raise
            else:
                logger.error("Unknown OpenTracing implementation: %s.",
                             tracecfg["impl"])
                sys.exit(1)

        db_name = config["database"]["name"]

        if db_name == "psycopg2":
            logger.info("Using postgresql database")
            self.database_engine = "postgresql"
            self.database = ConnectionPool(
                "psycopg2",
                cp_reactor=self.reactor,
                **config["database"].get("args"),
            )
        elif db_name == "sqlite3":
            logger.info("Using sqlite database")
            self.database_engine = "sqlite"
            self.database = ConnectionPool(
                "sqlite3",
                config["database"]["args"]["dbfile"],
                cp_reactor=self.reactor,
                cp_min=1,
                cp_max=1,
                check_same_thread=False,
            )
        else:
            raise Exception("Unsupported database 'name'")
示例#16
0
span_recorder = InstanaRecorder()

# The global OpenTracing compatible tracer used internally by
# this package.
#
# Usage example:
#
# import instana
# instana.tracer.start_span(...)
#
tracer = InstanaTracer(recorder=span_recorder)

if sys.version_info >= (3, 4):
    from opentracing.scope_managers.asyncio import AsyncioScopeManager
    async_tracer = InstanaTracer(scope_manager=AsyncioScopeManager(),
                                 recorder=span_recorder)

# Mock the tornado tracer until tornado is detected and instrumented first
tornado_tracer = tracer


def setup_tornado_tracer():
    global tornado_tracer
    from opentracing.scope_managers.tornado import TornadoScopeManager
    tornado_tracer = InstanaTracer(scope_manager=TornadoScopeManager(),
                                   recorder=span_recorder)


# Set ourselves as the tracer.
opentracing.tracer = tracer
示例#17
0
 def setUp(self):
     self.tracer = MockTracer(AsyncioScopeManager())
     self.loop = asyncio.get_event_loop()
示例#18
0
 def scope_manager(self):
     return AsyncioScopeManager()
示例#19
0
import sys
import opentracing

from .agent import Agent  # noqa
from .tracer import InstanaTracer  # noqa

# The Instana Agent which carries along with it a Sensor that collects metrics.
agent = Agent()

# The global OpenTracing compatible tracer used internally by
# this package.
#
# Usage example:
#
# import instana
# instana.tracer.start_span(...)
#
tracer = InstanaTracer()

if sys.version_info >= (3, 4):
    from opentracing.scope_managers.asyncio import AsyncioScopeManager
    async_tracer = InstanaTracer(AsyncioScopeManager())

# Set ourselves as the tracer.
opentracing.tracer = tracer
 def setUp(self):
     self.tracer = MockTracer(AsyncioScopeManager())
     self.loop = asyncio.get_event_loop()
     self.client = Client(RequestHandler(self.tracer), self.loop)
示例#21
0
    def __init__(
        self,
        config: Dict[str, Any],
        custom_reactor: SygnalReactor,
        tracer: Tracer = opentracing.tracer,
    ):
        """
        Object that holds state for the entirety of a Sygnal instance.
        Args:
            config: Configuration for this Sygnal
            custom_reactor: a Twisted Reactor to use.
            tracer (optional): an OpenTracing tracer. The default is the no-op tracer.
        """
        self.config = config
        self.reactor = custom_reactor
        self.pushkins: Dict[str, Pushkin] = {}
        self.tracer = tracer

        logging_dict_config = config["log"]["setup"]
        logging.config.dictConfig(logging_dict_config)

        logger.debug("Started logging")

        observer = twisted_log.PythonLoggingObserver()
        observer.start()

        proxy_url = config.get("proxy")
        if proxy_url is not None:
            logger.info("Using proxy configuration from Sygnal configuration file")
        else:
            proxy_url = os.getenv("HTTPS_PROXY")
            if proxy_url:
                logger.info(
                    "Using proxy configuration from HTTPS_PROXY environment variable."
                )
                config["proxy"] = proxy_url

        sentrycfg = config["metrics"]["sentry"]
        if sentrycfg["enabled"] is True:
            import sentry_sdk

            logger.info("Initialising Sentry")
            sentry_sdk.init(sentrycfg["dsn"])

        if config.get("db") is not None:
            logger.warning(
                "Config includes the legacy 'db' option and will be ignored"
                " as Sygnal no longer uses a database, this field can be removed"
            )

        if config.get("database") is not None:
            logger.warning(
                "Config includes the legacy 'database' option and will be ignored"
                " as Sygnal no longer uses a database, this field can be removed"
            )

        promcfg = config["metrics"]["prometheus"]
        if promcfg["enabled"] is True:
            prom_addr = promcfg["address"]
            prom_port = int(promcfg["port"])
            logger.info(
                "Starting Prometheus Server on %s port %d", prom_addr, prom_port
            )

            prometheus_client.start_http_server(port=prom_port, addr=prom_addr or "")

        tracecfg = config["metrics"]["opentracing"]
        if tracecfg["enabled"] is True:
            if tracecfg["implementation"] == "jaeger":
                try:
                    import jaeger_client

                    jaeger_cfg = jaeger_client.Config(
                        config=tracecfg["jaeger"],
                        service_name=tracecfg["service_name"],
                        scope_manager=AsyncioScopeManager(),
                    )

                    jaeger_tracer = jaeger_cfg.initialize_tracer()
                    assert jaeger_tracer is not None
                    self.tracer = jaeger_tracer

                    logger.info("Enabled OpenTracing support with Jaeger")
                except ModuleNotFoundError:
                    logger.critical(
                        "You have asked for OpenTracing with Jaeger but do not have"
                        " the Python package 'jaeger_client' installed."
                    )
                    raise
            else:
                raise RuntimeError(
                    "Unknown OpenTracing implementation: %s.", tracecfg["impl"]
                )
示例#22
0
    @param new_agent: agent to replace current singleton
    @return: None
    """
    global agent
    agent = new_agent


# The global OpenTracing compatible tracer used internally by
# this package.
tracer = InstanaTracer(recorder=span_recorder)

if sys.version_info >= (3, 4):
    try:
        from opentracing.scope_managers.asyncio import AsyncioScopeManager
        async_tracer = InstanaTracer(scope_manager=AsyncioScopeManager(), recorder=span_recorder)
    except Exception:
        logger.debug("Error setting up async_tracer:", exc_info=True)


# Mock the tornado tracer until tornado is detected and instrumented first
tornado_tracer = tracer


def setup_tornado_tracer():
    global tornado_tracer
    from opentracing.scope_managers.tornado import TornadoScopeManager
    tornado_tracer = InstanaTracer(scope_manager=TornadoScopeManager(), recorder=span_recorder)


# Set ourselves as the tracer.