Skip to content

Commit a7b3346

Browse files
committed
feat: add ut
1 parent b2a92c5 commit a7b3346

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

tests/test_tracing.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@
2929
)
3030
from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer
3131

32+
from opentelemetry import trace as trace_api
33+
from opentelemetry.sdk import trace as trace_sdk
34+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
35+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
36+
OTLPSpanExporter,
37+
)
38+
3239
APP_NAME = "app"
3340
USER_ID = "testuser"
3441
SESSION_ID = "testsession"
3542

36-
37-
@pytest.mark.asyncio
38-
async def test_tracing():
43+
def init_exporters():
3944
cozeloop_exporter = CozeloopExporter(
4045
config=CozeloopExporterConfig(
4146
endpoint="http://localhost:8000",
@@ -61,12 +66,52 @@ async def test_tracing():
6166
secret_key="test_secret_key",
6267
)
6368
)
69+
return [cozeloop_exporter, apmplus_exporter, tls_exporter]
6470

65-
exporters = [cozeloop_exporter, apmplus_exporter, tls_exporter]
71+
def gen_span_processor(endpoint: str):
72+
otlp_exporter = OTLPSpanExporter(
73+
endpoint=endpoint,
74+
)
75+
span_processor = BatchSpanProcessor(otlp_exporter)
76+
return span_processor
77+
78+
@pytest.mark.asyncio
79+
async def test_tracing():
80+
exporters = init_exporters()
6681
tracer = OpentelemetryTracer(exporters=exporters)
6782

6883
assert len(tracer.exporters) == 5 # with extra 2 built-in exporters
6984

7085
# TODO: Ensure the tracing provider is set correctly after loading SDK
7186
# TODO: Ensure the tracing provider is set correctly after loading SDK
7287
# TODO: Ensure the tracing provider is set correctly after loading SDK
88+
89+
@pytest.mark.asyncio
90+
async def test_tracing_with_global_provider():
91+
exporters = init_exporters()
92+
# set global tracer provider before init OpentelemetryTracer
93+
trace_api.set_tracer_provider(trace_sdk.TracerProvider())
94+
tracer_provider = trace_api.get_tracer_provider()
95+
tracer_provider.add_span_processor(gen_span_processor("http://localhost:8000"))
96+
trace_api.set_tracer_provider(tracer_provider)
97+
#
98+
tracer = OpentelemetryTracer(exporters=exporters)
99+
100+
assert len(tracer.exporters) == 5 # with extra 2 built-in exporters
101+
102+
103+
@pytest.mark.asyncio
104+
async def test_tracing_with_apmplus_global_provider():
105+
exporters = init_exporters()
106+
# add apmplus exporter to global tracer provider before init OpentelemetryTracer
107+
trace_api.set_tracer_provider(trace_sdk.TracerProvider())
108+
tracer_provider = trace_api.get_tracer_provider()
109+
tracer_provider.add_span_processor(gen_span_processor("http://apmplus-region.com"))
110+
111+
# init OpentelemetryTracer
112+
tracer = OpentelemetryTracer(exporters=exporters)
113+
114+
# apmplus exporter won't init again
115+
assert len(tracer.exporters) == 4 # with extra 2 built-in exporters
116+
117+

0 commit comments

Comments
 (0)