• This repository has been archived on 11/Jul/2022
  • Stars
    star
    408
  • Rank 105,946 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ›‘ This library is DEPRECATED!

Build Status Coverage Status PyPI Version Python Version FOSSA Status

πŸ›‘ This library is DEPRECATED!

There will be no new releases of this library.

We urge all users to migrate to OpenTelemetry. Please refer to the notice in the documentation for details.

Jaeger Bindings for Python OpenTracing API

This is a client-side library that can be used to instrument Python apps for distributed trace collection, and to send those traces to Jaeger. See the OpenTracing Python API for additional detail.

Contributing and Developing

Please see CONTRIBUTING.md.

Installation

pip install jaeger-client

Getting Started

import logging
import time
from jaeger_client import Config

if __name__ == "__main__":
    log_level = logging.DEBUG
    logging.getLogger('').handlers = []
    logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)

    config = Config(
        config={ # usually read from some yaml config
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'logging': True,
        },
        service_name='your-app-name',
        validate=True,
    )
    # this call also sets opentracing.tracer
    tracer = config.initialize_tracer()

    with tracer.start_span('TestSpan') as span:
        span.log_kv({'event': 'test message', 'life': 42})

        with tracer.start_span('ChildSpan', child_of=span) as child_span:
            child_span.log_kv({'event': 'down below'})

    time.sleep(2)   # yield to IOLoop to flush the spans - https://github.com/jaegertracing/jaeger-client-python/issues/50
    tracer.close()  # flush any buffered spans

NOTE: If you're using the Jaeger all-in-one Docker image (or similar) and want to run Jaeger in a separate container from your app, use the code below to define the host and port that the Jaeger agent is running on. Note that this is not recommended, as Jaeger sends spans over UDP and UDP does not guarantee delivery. (See this thread for more details.)

    config = Config(
        config={ # usually read from some yaml config
            'sampler': {
                'type': 'const',
                'param': 1,
            },
            'local_agent': {
                'reporting_host': 'your-reporting-host',
                'reporting_port': 'your-reporting-port',
            },
            'logging': True,
        },
        service_name='your-app-name',
        validate=True,
    )

Other Instrumentation

The OpenTracing Registry has many modules that provide explicit instrumentation support for popular frameworks like Django and Flask.

At Uber we are mostly using the opentracing_instrumentation module that provides:

  • explicit instrumentation for HTTP servers, and
  • implicit (monkey-patched) instrumentation for several popular libraries like urllib2, redis, requests, some SQL clients, etc.

Initialization & Configuration

Note: do not initialize the tracer during import, it may cause a deadlock (see issues #31, #60). Instead define a function that returns a tracer (see example below) and call that function explicitly after all the imports are done.

Also note that using gevent.monkey in asyncio-based applications (python 3+) may need to pass current event loop explicitly (see issue #256):

from tornado import ioloop
from jaeger_client import Config

config = Config(config={}, service_name='your-app-name', validate=True)
config.initialize_tracer(io_loop=ioloop.IOLoop.current())

Production

The recommended way to initialize the tracer for production use:

from jaeger_client import Config

def init_jaeger_tracer(service_name='your-app-name'):
    config = Config(config={}, service_name=service_name, validate=True)
    return config.initialize_tracer()

Note that the call initialize_tracer() also sets the opentracing.tracer global variable. If you need to create additional tracers (e.g., to create spans on the client side for remote services that are not instrumented), use the new_tracer() method.

Prometheus metrics

This module brings a Prometheus integration to the internal Jaeger metrics. The way to initialize the tracer with Prometheus metrics:

from jaeger_client.metrics.prometheus import PrometheusMetricsFactory

config = Config(
        config={},
        service_name='your-app-name',
        validate=True,
        metrics_factory=PrometheusMetricsFactory(service_name_label='your-app-name')
)
tracer = config.initialize_tracer()

Note that the optional argument service_name_label to the factory constructor will force it to tag all Jaeger client metrics with a label service: your-app-name. This way you can distinguish Jaeger client metrics produced by different services.

Development

For development, some parameters can be passed via config dictionary, as in the Getting Started example above. For more details please see the Config class.

WSGI, multi-processing, fork(2)

When using this library in applications that fork child processes to handle individual requests, such as with WSGI / PEP 3333, care must be taken when initializing the tracer. When Jaeger tracer is initialized, it may start a new background thread. If the process later forks, it might cause issues or hang the application (due to exclusive lock on the interpreter). Therefore, it is recommended that the tracer is not initialized until after the child processes are forked. Depending on the WSGI framework you might be able to use @postfork decorator to delay tracer initialization (see also issues #31, #60).

Debug Traces (Forced Sampling)

Programmatically

The OpenTracing API defines a sampling.priority standard tag that can be used to affect the sampling of a span and its children:

from opentracing.ext import tags as ext_tags

span.set_tag(ext_tags.SAMPLING_PRIORITY, 1)

Via HTTP Headers

Jaeger Tracer also understands a special HTTP Header jaeger-debug-id, which can be set in the incoming request, e.g.

curl -H "jaeger-debug-id: some-correlation-id" http://myhost.com

When Jaeger sees this header in the request that otherwise has no tracing context, it ensures that the new trace started for this request will be sampled in the "debug" mode (meaning it should survive all downsampling that might happen in the collection pipeline), and the root span will have a tag as if this statement was executed:

span.set_tag('jaeger-debug-id', 'some-correlation-id')

This allows using Jaeger UI to find the trace by this tag.

Zipkin Compatibility

To use this library directly with other Zipkin libraries & backend, you can provide the configuration property propagation: 'b3' and the X-B3-* HTTP headers will be supported.

The B3 codec assumes it will receive lowercase HTTP headers, as this seems to be the standard in the popular frameworks like Flask and Django. Please make sure your framework does the same.

License

Apache 2.0 License.

More Repositories

1

jaeger

CNCF Jaeger, a Distributed Tracing Platform
Go
20,138
star
2

jaeger-client-go

πŸ›‘ This library is DEPRECATED!
Go
1,382
star
3

jaeger-ui

Web UI for Jaeger
JavaScript
1,110
star
4

jaeger-operator

Jaeger Operator for Kubernetes simplifies deploying and running Jaeger on Kubernetes.
Go
1,012
star
5

jaeger-client-node

πŸ›‘ This library is DEPRECATED!
JavaScript
552
star
6

jaeger-client-java

πŸ›‘ This library is DEPRECATED!
Java
491
star
7

jaeger-kubernetes

Support for deploying Jaeger into Kubernetes
Java
447
star
8

jaeger-client-csharp

πŸ›‘ This library is DEPRECATED!
C#
303
star
9

helm-charts

Helm Charts for Jaeger backend
Mustache
265
star
10

jaeger-clickhouse

Jaeger ClickHouse storage plugin implementation
Go
251
star
11

jaeger-client-cpp

πŸ›‘ This library is DEPRECATED!
C++
138
star
12

spark-dependencies

Spark job for dependency links
Java
121
star
13

docker-protobuf

An all-inclusive protoc Docker image for the Jaeger project
Dockerfile
93
star
14

jaeger-idl

A set of shared data model definitions used by Jaeger components.
Thrift
80
star
15

jaeger-analytics-flink

Big data analytics for Jaeger using Apache Flink
Java
68
star
16

documentation

Documentation/website for the Jaeger Distributed Tracing project.
HTML
67
star
17

jaeger-lib

A collection of shared infrastructure libraries used by different components of Jaeger.
Go
67
star
18

jaeger-openshift

Support for deploying Jaeger into OpenShift
Groovy
57
star
19

jaeger-analytics-java

Data analytics pipeline and models for tracing data
Java
45
star
20

jaeger-client-javascript

Note: this SDK is not implemented yet, see https://github.com/jaegertracing/jaeger-client-javascript/issues/1.
JavaScript
32
star
21

jaeger-performance

Home of the Jaeger Performance tests
Java
21
star
22

jaeger-opentelemetry-collector

Experimental: components needed to build Jaeger on top of OpenTelemetry Collector
Makefile
12
star
23

jaeger-otelcol

Jaeger's OpenTelemetry Collector distribution
Go
7
star
24

legacy-client-java

Legacy com.uber.jaeger java client
Java
5
star
25

jaeger-vscode

VSCode extension for Jaeger
TypeScript
5
star
26

security-audits

Jaeger security audits
3
star
27

jaeger-opentelemetry-releases

Go
3
star
28

vertx-create-span

Demo application used in e2e tests for the Jaeger Operator
Java
3
star
29

artwork

Jaeger Logo and Artwork
2
star