1
- """Module to configure OpenTelemetry agent to work with SolarWinds backend"""
1
+ """Module to configure OpenTelemetry to work with SolarWinds backend"""
2
2
3
3
import logging
4
4
from os import environ
5
- from pkg_resources import iter_entry_points
6
5
7
- from opentelemetry import trace
8
- from opentelemetry .environment_variables import OTEL_PROPAGATORS , OTEL_TRACES_EXPORTER
6
+ from opentelemetry .environment_variables import (
7
+ OTEL_PROPAGATORS ,
8
+ OTEL_TRACES_EXPORTER
9
+ )
9
10
from opentelemetry .instrumentation .distro import BaseDistro
10
- from opentelemetry .instrumentation .propagators import set_global_response_propagator
11
- from opentelemetry .propagate import set_global_textmap
12
- from opentelemetry .propagators .composite import CompositePropagator
13
- from opentelemetry .sdk .trace import TracerProvider
14
- from opentelemetry .sdk .trace .export import BatchSpanProcessor
15
-
16
- from opentelemetry_distro_solarwinds .response_propagator import SolarWindsTraceResponsePropagator
17
- from opentelemetry_distro_solarwinds .sampler import ParentBasedSwSampler
11
+ from opentelemetry .sdk .environment_variables import OTEL_TRACES_SAMPLER
18
12
19
13
logger = logging .getLogger (__name__ )
20
14
21
15
class SolarWindsDistro (BaseDistro ):
22
- """SolarWinds custom distro for OpenTelemetry agents. """
16
+ """OpenTelemetry Distro for SolarWinds reporting environment """
23
17
24
- _DEFAULT_OTEL_EXPORTER = "solarwinds_exporter"
25
- _DEFAULT_OTEL_PROPAGATORS = [
26
- "tracecontext" ,
18
+ _TRACECONTEXT_PROPAGATOR = "tracecontext"
19
+ _SW_PROPAGATOR = "solarwinds_propagator"
20
+ _DEFAULT_SW_PROPAGATORS = [
21
+ _TRACECONTEXT_PROPAGATOR ,
27
22
"baggage" ,
28
- "solarwinds_propagator" ,
23
+ _SW_PROPAGATOR ,
29
24
]
25
+ _DEFAULT_SW_TRACES_EXPORTER = "solarwinds_exporter"
30
26
31
27
def _configure (self , ** kwargs ):
32
- # Automatically use custom SolarWinds sampler
33
- trace .set_tracer_provider (
34
- TracerProvider (sampler = ParentBasedSwSampler ()))
35
-
36
- # Customize Exporter else default to SolarWindsSpanExporter
37
- environ_exporter = environ .get (
38
- OTEL_TRACES_EXPORTER ,
39
- self ._DEFAULT_OTEL_EXPORTER
40
- )
41
- try :
42
- exporter = next (
43
- iter_entry_points (
44
- "opentelemetry_traces_exporter" ,
45
- environ_exporter
46
- )).load ()()
47
- except :
48
- logger .exception (
49
- "Failed to load configured exporter `%s`" , environ_exporter
50
- )
51
- raise
52
-
53
- span_exporter = BatchSpanProcessor (exporter )
54
- trace .get_tracer_provider ().add_span_processor (span_exporter )
55
-
56
- # Configure context propagators to always include
57
- # tracecontext,baggage,solarwinds -- first and in that order
58
- # -- plus any others specified by env var
28
+ environ .setdefault (OTEL_TRACES_EXPORTER , self ._DEFAULT_SW_TRACES_EXPORTER )
29
+
59
30
environ_propagators = environ .get (
60
31
OTEL_PROPAGATORS ,
61
- "," .join (self ._DEFAULT_OTEL_PROPAGATORS )
32
+ "," .join (self ._DEFAULT_SW_PROPAGATORS )
62
33
).split ("," )
63
- if environ_propagators != self ._DEFAULT_OTEL_PROPAGATORS :
64
- for default in self ._DEFAULT_OTEL_PROPAGATORS :
65
- while default in environ_propagators :
66
- environ_propagators .remove (default )
67
- environ_propagators = self ._DEFAULT_OTEL_PROPAGATORS + environ_propagators
34
+ # If not using the default propagators,
35
+ # can any arbitrary list BUT
36
+ # (1) must include tracecontext and solarwinds_propagator
37
+ # (2) tracecontext must be before solarwinds_propagator
38
+ if environ_propagators != self ._DEFAULT_SW_PROPAGATORS :
39
+ if not self ._TRACECONTEXT_PROPAGATOR in environ_propagators or \
40
+ not self ._SW_PROPAGATOR in environ_propagators :
41
+ raise ValueError ("Must include tracecontext and solarwinds_propagator in OTEL_PROPAGATORS to use SolarWinds Observability." )
42
+
43
+ if environ_propagators .index (self ._SW_PROPAGATOR ) \
44
+ < environ_propagators .index (self ._TRACECONTEXT_PROPAGATOR ):
45
+ raise ValueError ("tracecontext must be before solarwinds_propagator in OTEL_PROPAGATORS to use SolarWinds Observability." )
68
46
environ [OTEL_PROPAGATORS ] = "," .join (environ_propagators )
69
47
70
- # Init and set CompositePropagator globally, like OTel API
71
- propagators = []
72
- for propagator in environ_propagators :
73
- try :
74
- propagators .append (
75
- next (
76
- iter_entry_points ("opentelemetry_propagator" , propagator )
77
- ).load ()()
78
- )
79
- except Exception :
80
- logger .exception (
81
- "Failed to load configured propagator `%s`" , propagator
82
- )
83
- raise
84
- set_global_textmap (CompositePropagator (propagators ))
85
-
86
- # Set global HTTP response propagator
87
- set_global_response_propagator (SolarWindsTraceResponsePropagator ())
48
+ logger .debug ("Configured SolarWindsDistro: {}, {}, {}" .format (
49
+ environ .get (OTEL_TRACES_SAMPLER ),
50
+ environ .get (OTEL_TRACES_EXPORTER ),
51
+ environ .get (OTEL_PROPAGATORS )
52
+ ))
0 commit comments