Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Better support for creating multiple tracers (#150)
Browse files Browse the repository at this point in the history
* Better support for creating multiple tracers

Signed-off-by: Nathan Ziebart <[email protected]>

* add test

Signed-off-by: Nathan Ziebart <[email protected]>
  • Loading branch information
nziebart authored and yurishkuro committed Apr 12, 2018
1 parent cc96d1b commit ca16098
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def init_jaeger_tracer(service_name='your-app-name'):
```

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

Expand Down
15 changes: 11 additions & 4 deletions jaeger_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ def initialize_tracer(self, io_loop=None):
return
Config._initialized = True

tracer = self.new_tracer(io_loop)

self._initialize_global_tracer(tracer=tracer)
return tracer

def new_tracer(self, io_loop=None):
"""
Create a new Jaeger Tracer based on the passed `jaeger_client.Config`.
Does not set `opentracing.tracer` global variable.
"""
channel = self._create_local_agent_channel(io_loop=io_loop)
sampler = self.sampler
if sampler is None:
Expand All @@ -321,14 +331,11 @@ def initialize_tracer(self, io_loop=None):
if self.logging:
reporter = CompositeReporter(reporter, LoggingReporter(logger))

tracer = self.create_tracer(
return self.create_tracer(
reporter=reporter,
sampler=sampler,
)

self._initialize_global_tracer(tracer=tracer)
return tracer

def create_tracer(self, reporter, sampler):
return Tracer(
service_name=self.service_name,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import absolute_import
import os
import unittest
import opentracing.tracer
from jaeger_client import Config, ConstSampler, ProbabilisticSampler, RateLimitingSampler
from jaeger_client.reporter import NullReporter
from jaeger_client import constants
Expand Down Expand Up @@ -104,3 +105,9 @@ def test_propagation(self):
def test_for_unexpected_config_entries(self):
with self.assertRaises(Exception):
_ = Config({"unexpected":"value"}, validate=True)

def test_initialize_tracer(self):
c = Config({}, service_name='x')
tracer = c.initialize_tracer()

assert opentracing.tracer == tracer

0 comments on commit ca16098

Please sign in to comment.