Skip to content

Commit 2fc6510

Browse files
Rashmi-K-Arashmi_ka
authored andcommitted
adding support for Jaeger propagator
1 parent 4a66652 commit 2fc6510

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
import opentelemetry.trace as trace
18+
from opentelemetry.context import Context
19+
from opentelemetry.trace.propagation.textmap import (
20+
Getter,
21+
Setter,
22+
TextMapPropagator,
23+
TextMapPropagatorT,
24+
)
25+
from opentelemetry import baggage
26+
27+
class JaegerFormat(TextMapPropagator):
28+
"""Propagator for the Jaeger format.
29+
30+
See: https://www.jaegertracing.io/docs/1.19/client-libraries/#propagation-format
31+
"""
32+
33+
TRACE_ID_KEY = 'uber-trace-id'
34+
def inject(
35+
self,
36+
set_in_carrier: Setter[TextMapPropagatorT],
37+
carrier: TextMapPropagatorT,
38+
context: typing.Optional[Context] = None,
39+
) -> None:
40+
span = trace.get_current_span(context=context)
41+
span_context = span.get_context()
42+
if span_context == trace.INVALID_SPAN_CONTEXT:
43+
return
44+
45+
# set span identity
46+
span_parent_id = getattr(span, "parent", 0)
47+
set_in_carrier(
48+
carrier, self.TRACE_ID_KEY, _format_trace_id(span_context.trace_id, span_context.span_id, span_parent_id, span_context.trace_flags)
49+
)
50+
51+
# set span baggage, if any
52+
baggage_entries = baggage.get_all(context=context)
53+
if not baggage_entries:
54+
return
55+
56+
for key, value in baggage_entries.items():
57+
baggage_key = 'uberctx-{}'.format(key)
58+
set_in_carrier(
59+
carrier, baggage_key, value
60+
)
61+
62+
63+
def _format_trace_id(trace_id, span_id, parent_span_id, flags):
64+
return '{:032x}:{:016x}:{:016x}:{:02x}'.format(trace_id, span_id, parent_span_id, flags)
65+
66+
67+
68+

0 commit comments

Comments
 (0)