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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
"""Tests for the OpenTelemetry tracer and span classes."""
from concurrent.futures import ThreadPoolExecutor, wait
import sys
import threading
import pytest
from azure.core.instrumentation import get_tracer
from azure.core.tracing._models import SpanKind, Link
from azure.core.tracing.opentelemetry import OpenTelemetryTracer
from azure.core.tracing.common import with_current_context
from opentelemetry.trace import (
Tracer as OtelTracer,
Span as OtelSpan,
StatusCode as OtelStatusCode,
NonRecordingSpan,
format_span_id,
format_trace_id,
)
def test_tracer(tracing_helper):
"""Test basic usage of a Instrumentation."""
tracer = get_tracer(
library_name="my-library",
library_version="1.0.0",
schema_url="https://test.schema",
attributes={"namespace": "Sample.Namespace"},
)
assert isinstance(tracer, OpenTelemetryTracer)
assert isinstance(tracer._tracer, OtelTracer)
with tracer.start_as_current_span("test_span") as span:
assert isinstance(span, OtelSpan)
span.set_attribute("foo", "bar")
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].name == "test_span"
assert finished_spans[0].attributes["foo"] == "bar"
assert finished_spans[0].status.status_code == OtelStatusCode.UNSET
assert finished_spans[0].instrumentation_scope.schema_url == "https://test.schema"
assert finished_spans[0].instrumentation_scope.name == "my-library"
assert finished_spans[0].instrumentation_scope.version == "1.0.0"
assert finished_spans[0].instrumentation_scope.attributes.get("namespace") == "Sample.Namespace"
def test_default_instrumentation():
"""Test that the default tracer manager returns an OpenTelemetryTracer."""
tracer = get_tracer()
assert isinstance(tracer, OpenTelemetryTracer)
assert isinstance(tracer._tracer, OtelTracer)
def test_start_span_with_links(tracing_helper):
tracer = get_tracer()
assert tracer
trace_context_headers = {}
with tracer.start_as_current_span(name="foo-span") as span:
trace_context_headers = tracer.get_trace_context()
link = Link(headers=trace_context_headers, attributes={"foo": "bar"})
with tracer.start_as_current_span(name="bar-span", links=[link]) as span:
pass
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 2
assert finished_spans[0].name == "foo-span"
assert finished_spans[1].name == "bar-span"
assert finished_spans[1].links[0].context.trace_id == finished_spans[0].context.trace_id
assert finished_spans[1].links[0].context.span_id == finished_spans[0].context.span_id
assert finished_spans[1].links[0].attributes["foo"] == "bar"
def test_start_span_with_attributes(tracing_helper):
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span(name="foo-span", attributes={"foo": "bar", "biz": 123}) as span:
pass
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].attributes["foo"] == "bar"
assert finished_spans[0].attributes["biz"] == 123
def test_start_as_current_span_no_exit(tracing_helper):
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span(name="foo-span", kind=SpanKind.INTERNAL, end_on_exit=False) as span:
assert span.is_recording()
assert tracer.get_current_span() == span
assert span.is_recording()
span.end()
assert not span.is_recording()
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
def test_tracer_get_current_span():
"""Test that the tracer can get the current OpenTelemetry span instance."""
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span("test_span") as span:
with tracer.start_as_current_span("test_span2", kind=SpanKind.CLIENT) as span2:
current_span = tracer.get_current_span()
assert current_span == span2
current_span = tracer.get_current_span()
assert current_span == span
current_span = tracer.get_current_span()
assert isinstance(current_span, NonRecordingSpan)
def test_end_span(tracing_helper):
tracer = get_tracer()
assert tracer
span = tracer.start_span(name="foo-span", kind=SpanKind.INTERNAL)
assert span.is_recording()
assert span.start_time is not None
assert span.end_time is None
span.end()
assert not span.is_recording()
assert span.end_time is not None
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].name == "foo-span"
def test_use_span(tracing_helper):
tracer = get_tracer()
assert tracer
assert isinstance(tracer.get_current_span(), NonRecordingSpan)
with tracer.start_as_current_span(name="root", kind=SpanKind.INTERNAL) as root:
span = tracer.start_span(name="foo-span", kind=SpanKind.INTERNAL)
assert span.is_recording()
assert tracer.get_current_span() == root
with tracer.use_span(span):
assert tracer.get_current_span() == span
assert not span.is_recording()
span2 = tracer.start_span(name="bar-span", kind=SpanKind.INTERNAL)
assert span2.is_recording()
assert tracer.get_current_span() == root
with tracer.use_span(span2, end_on_exit=False):
assert tracer.get_current_span() == span2
assert span2.is_recording()
assert span2.is_recording()
span2.end()
assert not span2.is_recording()
assert tracer.get_current_span() == root
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 3
def test_get_trace_context():
"""Test that the tracer can get the trace context and it contains the traceparent header."""
tracer = get_tracer()
assert tracer
assert not tracer.get_trace_context()
with tracer.start_as_current_span(name="foo-span") as span:
trace_context = tracer.get_trace_context()
span_context = span.get_span_context()
assert "traceparent" in trace_context
assert trace_context["traceparent"].startswith("00-")
assert trace_context["traceparent"].split("-")[1] == format_trace_id(span_context.trace_id)
assert trace_context["traceparent"].split("-")[2] == format_span_id(span_context.span_id)
def test_with_current_context_util_function(tracing_helper):
result = []
tracer = get_tracer()
assert tracer
def get_span_from_thread(output):
current_span = tracer.get_current_span()
output.append(current_span)
with tracing_helper.tracer.start_as_current_span(name="TestSpan") as span:
thread = threading.Thread(target=with_current_context(get_span_from_thread), args=(result,))
thread.start()
thread.join()
assert span is result[0]
def test_nest_span_with_thread_pool_executor(tracing_helper):
tracer = get_tracer(
library_name="my-library",
library_version="1.0.0",
schema_url="https://test.schema",
attributes={"namespace": "Sample.Namespace"},
)
assert tracer
def nest_spans():
with tracer.start_as_current_span(name="outer-span", kind=SpanKind.INTERNAL) as outer_span:
with tracer.start_as_current_span(name="inner-span", kind=SpanKind.INTERNAL) as inner_span:
assert isinstance(inner_span, OtelSpan)
assert tracer.get_current_span() == inner_span
futures = []
with ThreadPoolExecutor() as executor:
for _ in range(3):
futures.append(executor.submit(tracer.with_current_context(nest_spans)))
wait(futures)
# Each thread should produce 2 spans, so we should have 6 spans.
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 6
for span in finished_spans:
assert "-span" in span.name
assert span.instrumentation_scope.schema_url == "https://test.schema"
assert span.instrumentation_scope.name == "my-library"
assert span.instrumentation_scope.version == "1.0.0"
assert span.instrumentation_scope.attributes.get("namespace") == "Sample.Namespace"
def test_set_span_status(tracing_helper):
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span(name="foo-span") as span:
span.set_status(OtelStatusCode.OK)
with tracer.start_as_current_span(name="bar-span") as span:
span.set_status(OtelStatusCode.ERROR, "This is an error")
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 2
assert finished_spans[0].status.status_code == OtelStatusCode.OK
assert finished_spans[0].status.description is None
assert finished_spans[1].status.status_code == OtelStatusCode.ERROR
assert finished_spans[1].status.description == "This is an error"
def test_add_event(tracing_helper):
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span(name="foo-span") as span:
span.add_event("test-event", attributes={"foo": "bar"})
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert len(finished_spans[0].events) == 1
assert finished_spans[0].events[0].name == "test-event"
assert finished_spans[0].events[0].attributes["foo"] == "bar"
def test_span_exception(tracing_helper):
tracer = get_tracer()
assert tracer
with pytest.raises(ValueError):
with tracer.start_as_current_span(name="foo-span") as span:
raise ValueError("This is an error")
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].status.status_code == OtelStatusCode.ERROR
def test_span_exception_without_current_context(tracing_helper):
tracer = get_tracer()
assert tracer
span = tracer.start_span(name="foo-span")
assert span.is_recording()
with pytest.raises(ValueError):
with span:
raise ValueError("This is an error")
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].status.status_code == OtelStatusCode.ERROR
def test_span_exception_exit(tracing_helper):
tracer = get_tracer()
assert tracer
span = tracer.start_span(name="foo-span")
assert span.is_recording()
try:
raise ValueError("This is an error")
except ValueError as e:
exc_info = sys.exc_info()
span.__exit__(*exc_info)
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].status.status_code == OtelStatusCode.ERROR
def test_tracer_caching():
"""Test that get_tracer caches the OpenTelemetryTracer."""
tracer1 = get_tracer()
tracer2 = get_tracer()
assert tracer1 is tracer2
def test_tracer_caching_different_args():
"""Test that get_tracer caches the OpenTelemetryTracer."""
tracer1 = get_tracer(
library_name="my-library",
library_version="1.0.0",
schema_url="https://test.schema",
attributes={"namespace": "Sample.Namespace"},
)
tracer2 = get_tracer(
library_name="my-library",
library_version="1.0.0",
schema_url="https://test.schema",
attributes={"namespace": "Sample.Namespace"},
)
tracer3 = get_tracer(
library_name="my-library-2",
library_version="2.0.0",
schema_url="https://test.schema",
attributes={"namespace": "Sample.Namespace"},
)
assert tracer1 is tracer2
assert tracer1 is not tracer3
def test_tracer_set_span_error(tracing_helper):
"""Test that the tracer's set_span_status method works correctly."""
tracer = get_tracer()
assert tracer
with tracer.start_as_current_span(name="ok-span") as span:
tracer.set_span_error_status(span)
with tracer.start_as_current_span(name="ok-span") as span:
tracer.set_span_error_status(span, "This is an error")
# Verify status on finished spans
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 2
assert finished_spans[0].status.status_code == OtelStatusCode.ERROR
assert finished_spans[0].status.description is None
assert finished_spans[1].status.status_code == OtelStatusCode.ERROR
assert finished_spans[1].status.description == "This is an error"
def test_start_span_with_start_time(tracing_helper):
"""Test that a span can be started with a custom start time."""
tracer = get_tracer()
assert tracer
start_time = 1234567890
with tracer.start_as_current_span(name="foo-span", start_time=start_time) as span:
assert span.start_time == start_time
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 1
assert finished_spans[0].start_time == start_time
span = tracer.start_span(name="foo-span", start_time=start_time)
assert span.start_time == start_time
span.end()
finished_spans = tracing_helper.exporter.get_finished_spans()
assert len(finished_spans) == 2
assert finished_spans[1].start_time == start_time
|