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

Commit ca16098

Browse files
nziebartyurishkuro
authored andcommitted
Better support for creating multiple tracers (#150)
* Better support for creating multiple tracers Signed-off-by: Nathan Ziebart <[email protected]> * add test Signed-off-by: Nathan Ziebart <[email protected]>
1 parent cc96d1b commit ca16098

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def init_jaeger_tracer(service_name='your-app-name'):
8080
```
8181

8282
Note that the call `initialize_tracer()` also sets the `opentracing.tracer` global variable.
83+
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.
8384

8485
#### Prometheus metrics
8586

jaeger_client/config.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ def initialize_tracer(self, io_loop=None):
296296
return
297297
Config._initialized = True
298298

299+
tracer = self.new_tracer(io_loop)
300+
301+
self._initialize_global_tracer(tracer=tracer)
302+
return tracer
303+
304+
def new_tracer(self, io_loop=None):
305+
"""
306+
Create a new Jaeger Tracer based on the passed `jaeger_client.Config`.
307+
Does not set `opentracing.tracer` global variable.
308+
"""
299309
channel = self._create_local_agent_channel(io_loop=io_loop)
300310
sampler = self.sampler
301311
if sampler is None:
@@ -321,14 +331,11 @@ def initialize_tracer(self, io_loop=None):
321331
if self.logging:
322332
reporter = CompositeReporter(reporter, LoggingReporter(logger))
323333

324-
tracer = self.create_tracer(
334+
return self.create_tracer(
325335
reporter=reporter,
326336
sampler=sampler,
327337
)
328338

329-
self._initialize_global_tracer(tracer=tracer)
330-
return tracer
331-
332339
def create_tracer(self, reporter, sampler):
333340
return Tracer(
334341
service_name=self.service_name,

tests/test_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import absolute_import
1616
import os
1717
import unittest
18+
import opentracing.tracer
1819
from jaeger_client import Config, ConstSampler, ProbabilisticSampler, RateLimitingSampler
1920
from jaeger_client.reporter import NullReporter
2021
from jaeger_client import constants
@@ -104,3 +105,9 @@ def test_propagation(self):
104105
def test_for_unexpected_config_entries(self):
105106
with self.assertRaises(Exception):
106107
_ = Config({"unexpected":"value"}, validate=True)
108+
109+
def test_initialize_tracer(self):
110+
c = Config({}, service_name='x')
111+
tracer = c.initialize_tracer()
112+
113+
assert opentracing.tracer == tracer

0 commit comments

Comments
 (0)