-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathotel_config.py
More file actions
235 lines (184 loc) · 7.63 KB
/
otel_config.py
File metadata and controls
235 lines (184 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""OpenTelemetry configuration for Synapse Python Client."""
import os
import sys
from typing import Any, Dict, List, Optional
from opentelemetry import metrics, trace
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
ConsoleMetricExporter,
PeriodicExportingMetricReader,
)
from opentelemetry.sdk.resources import (
SERVICE_INSTANCE_ID,
SERVICE_NAME,
SERVICE_VERSION,
Resource,
)
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.trace import Span, SpanContext, get_current_span
# Default service name for the client
DEFAULT_SERVICE_NAME = "synapseclient"
CLIENT_VERSION = "unspecified"
SYNAPSE_SERVICE_VERSION = f"synapse.{SERVICE_VERSION}"
class AttributePropagatingSpanProcessor(SpanProcessor):
"""A custom span processor that propagates specific attributes from the parent span
to the child span when the child span is started.
It also propagates the attributes to the parent span when the child span ends.
Args:
attributes_to_propagate_to_child: List of attribute names to propagate from
parent to child span.
attributes_to_propagate_to_parent: List of attribute names to propagate from
child to parent span.
"""
def __init__(
self,
attributes_to_propagate_to_child: List[str] = None,
attributes_to_propagate_to_parent: List[str] = None,
) -> None:
self.attributes_to_propagate_to_child = attributes_to_propagate_to_child or []
self.attributes_to_propagate_to_parent = attributes_to_propagate_to_parent or []
def on_start(
self, span: Span, parent_context: Optional[SpanContext] = None
) -> None:
"""Propagates attributes from the parent span to the child span.
Arguments:
span: The child span to which the attributes should be propagated.
parent_context: The context of the parent span.
Returns:
None
"""
parent_span = get_current_span()
if parent_span is not None and parent_span.is_recording():
for attribute in self.attributes_to_propagate_to_child:
# Check if the attribute exists in the parent span's attributes
attribute_val = parent_span.attributes.get(attribute)
if attribute_val:
# Propagate the attribute to the current span
span.set_attribute(attribute, attribute_val)
def on_end(self, span: Span) -> None:
"""Propagates attributes from the child span back to the parent span"""
parent_span = get_current_span()
if parent_span is not None and parent_span.is_recording():
for attribute in self.attributes_to_propagate_to_parent:
child_val = span.attributes.get(attribute)
parent_val = parent_span.attributes.get(attribute)
if child_val and not parent_val:
# Propagate the attribute back to parent span
parent_span.set_attribute(attribute, child_val)
def shutdown(self) -> None:
"""No-op method that does nothing when the span processor is shut down."""
def force_flush(self, timeout_millis: int = 30000) -> None:
"""No-op method that does nothing when the span processor is forced to flush."""
def configure_traces(
resource_attributes: Optional[Dict[str, Any]] = None,
include_context: bool = True,
) -> TracerProvider:
"""
Configure OpenTelemetry tracing for the Synapse Python Client.
Args:
resource_attributes: Additional resource attributes to include
include_context: Whether to include contextual information about the runtime environment
Returns:
The configured TracerProvider
"""
resource_attrs = {
SERVICE_NAME: os.environ.get("OTEL_SERVICE_NAME", DEFAULT_SERVICE_NAME),
SYNAPSE_SERVICE_VERSION: CLIENT_VERSION,
SERVICE_INSTANCE_ID: os.environ.get(
"OTEL_SERVICE_INSTANCE_ID", "default_instance"
),
}
if include_context:
resource_attrs["python.version"] = ".".join(
str(v) for v in sys.version_info[:3]
)
resource_attrs["os.type"] = os.name
try:
from synapseclient import __version__ as client_version
resource_attrs[SYNAPSE_SERVICE_VERSION] = client_version
except ImportError:
pass
if resource_attributes:
resource_attrs.update(resource_attributes)
resource = Resource.create(resource_attrs)
provider = TracerProvider(resource=resource)
attribute_propagator = AttributePropagatingSpanProcessor(
attributes_to_propagate_to_child=[
"synapse.transfer.direction",
"synapse.operation.category",
]
)
provider.add_span_processor(attribute_propagator)
otlp_exporter = OTLPSpanExporter()
provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
if os.environ.get("OTEL_DEBUG_CONSOLE", "").lower() in (
"true",
"1",
):
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
# Set as global tracer provider
trace.set_tracer_provider(provider)
return provider
def configure_metrics(
resource_attributes: Optional[Dict[str, Any]] = None,
include_context: bool = True,
) -> MeterProvider:
"""
Configure OpenTelemetry metrics for the Synapse Python Client.
Args:
resource_attributes: Additional resource attributes to include
include_context: Whether to include contextual information about the runtime environment
Returns:
The configured MeterProvider
"""
resource_attrs = {
SERVICE_NAME: os.environ.get("OTEL_SERVICE_NAME", DEFAULT_SERVICE_NAME),
SYNAPSE_SERVICE_VERSION: CLIENT_VERSION,
}
if include_context:
resource_attrs["python.version"] = ".".join(
str(v) for v in sys.version_info[:3]
)
resource_attrs["os.type"] = os.name
try:
from synapseclient import __version__ as client_version
resource_attrs[SYNAPSE_SERVICE_VERSION] = client_version
except ImportError:
pass
if resource_attributes:
resource_attrs.update(resource_attributes)
resource = Resource.create(resource_attrs)
readers = []
otlp_metric_exporter = OTLPMetricExporter()
readers.append(PeriodicExportingMetricReader(otlp_metric_exporter))
if os.environ.get("OTEL_DEBUG_CONSOLE", "").lower() in (
"true",
"1",
):
console_metric_exporter = ConsoleMetricExporter()
readers.append(PeriodicExportingMetricReader(console_metric_exporter))
provider = MeterProvider(resource=resource, metric_readers=readers)
# Set as global meter provider
metrics.set_meter_provider(provider)
return provider
def get_tracer(name: Optional[str] = None) -> trace.Tracer:
"""
Get a tracer with the specified name or default to the service name.
Args:
name: Optional tracer name
Returns:
An OpenTelemetry tracer
"""
return trace.get_tracer(name or DEFAULT_SERVICE_NAME)
def get_meter(name: Optional[str] = None):
"""
Get a meter with the specified name or default to the service name.
Args:
name: Optional meter name
Returns:
An OpenTelemetry meter
"""
return metrics.get_meter(name or DEFAULT_SERVICE_NAME)