Skip to content

Commit ee6012f

Browse files
committed
Updated package initialization; automatic tracer instantiation
1 parent 7643103 commit ee6012f

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

instana/__init__.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
from __future__ import absolute_import
2+
import opentracing
3+
from .sensor import Sensor
4+
from .tracer import InstanaTracer
5+
from .options import Options
6+
17
"""
2-
Instana sensor and tracer. It consists of two modules that can be used as entry points:
8+
The Instana package has two core components: the sensor and the tracer.
9+
10+
The sensor is individual to each python process and handles process metric
11+
collection and reporting.
312
4-
- sensor: activates the meter to collect and transmit all kind of built-in metrics
5-
- tracer: OpenTracing tracer implementation. It implicitly activates the meter
13+
The tracer upholds the OpenTracing API and is responsible for reporting
14+
span data to Instana.
615
"""
716

817
__author__ = 'Instana Inc.'
@@ -13,4 +22,30 @@
1322
__maintainer__ = 'Peter Giacomo Lombardo'
1423
__email__ = 'peter.lombardo@instana.com'
1524

16-
__all__ = ['sensor', 'tracer']
25+
# For any given Python process, we only want one sensor as multiple would
26+
# collect/report metrics in duplicate, triplicate etc..
27+
#
28+
# Usage example:
29+
#
30+
# import instana
31+
# instana.global_sensor
32+
#
33+
global_sensor = Sensor(Options())
34+
35+
# The global OpenTracing compatible tracer used internally by
36+
# this package.
37+
#
38+
# Usage example:
39+
#
40+
# import instana
41+
# instana.internal_tracer.start_span(...)
42+
#
43+
internal_tracer = InstanaTracer()
44+
45+
# Set ourselves as the tracer.
46+
opentracing.tracer = internal_tracer
47+
48+
49+
def load_instrumentation():
50+
# Import & initialize instrumentation
51+
from .instrumentation import urllib3

instana/tracer.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@
22
from basictracer import BasicTracer
33
import instana.recorder as r
44
import opentracing as ot
5+
import instana
56
import instana.options as o
6-
import instana.sensor as s
77

88
from basictracer.context import SpanContext
99
from basictracer.span import BasicSpan
1010
from instana.http_propagator import HTTPPropagator
1111
from instana.text_propagator import TextPropagator
1212
from instana.util import generate_id
1313

14-
# In case a user or app creates multiple tracers, we limit to just
15-
# one sensor per process otherwise metrics collection is duplicated,
16-
# triplicated etc.
17-
gSensor = None
18-
1914

2015
class InstanaTracer(BasicTracer):
2116
sensor = None
2217

2318
def __init__(self, options=o.Options()):
24-
global gSensor
25-
if gSensor is None:
26-
self.sensor = s.Sensor(options)
27-
gSensor = self.sensor
28-
else:
29-
self.sensor = gSensor
19+
self.sensor = instana.global_sensor
3020
super(InstanaTracer, self).__init__(
3121
r.InstanaRecorder(self.sensor), r.InstanaSampler())
3222

tests/__init__.py

-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
import os
33
import time
44
import threading
5-
import opentracing
6-
import instana.tracer
75
from .apps.flaskalino import app as flaskalino
86
os.environ["INSTANA_TEST"] = "true"
97

10-
opentracing.global_tracer = instana.tracer.InstanaTracer()
11-
128
# Spawn our background Flask app that the tests will throw
139
# requests at. Don't continue until the test app is fully
1410
# up and running.

0 commit comments

Comments
 (0)