-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (43 loc) · 2.16 KB
/
main.py
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
from opentelemetry import trace, baggage
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPSpanGrpcExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource, HOST_NAME
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.trace import SpanKind
def inner_method():
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("child_span", kind=SpanKind.CLIENT):
print("hello world")
def outer_method():
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("parent_span", kind=SpanKind.SERVER):
inner_method()
def baggage_and_attribute_usage():
tracer = trace.get_tracer(__name__)
global_ctx = baggage.set_baggage("key", "value_from_global_ctx") # 使用baggage api,在不同span之间传递数据
with tracer.start_as_current_span(name='baggage_parent_span', attributes={'attribute_key': 'value'},
kind=SpanKind.SERVER):
parent_ctx = baggage.set_baggage("key", "value_from_parent_ctx")
with tracer.start_as_current_span(name='baggage_child_span', context=parent_ctx,
kind=SpanKind.INTERNAL):
child_ctx = baggage.set_baggage("key", "value_from_child_ctx")
print(baggage.get_baggage("key", global_ctx))
print(baggage.get_baggage("key", parent_ctx))
print(baggage.get_baggage("key", child_ctx))
def init_opentelemetry():
# 设置服务名、主机名
resource = Resource(attributes={
SERVICE_NAME: "PythonTest",
HOST_NAME: "MyComputer",
"token": "xxxxxxxxxx" # 替换成控制台上的 Token
})
# 使用GRPC协议上报
span_processor = BatchSpanProcessor(OTLPSpanGrpcExporter(
endpoint="http://ap-guangzhou.apm.tencentcs.com:4317", # 替换成控制台上的接入点
))
trace_provider = TracerProvider(resource=resource, active_span_processor=span_processor)
trace.set_tracer_provider(trace_provider)
if __name__ == '__main__':
init_opentelemetry()
outer_method()
baggage_and_attribute_usage()