|
19 | 19 | from opentelemetry.sdk.metrics.export import InMemoryMetricReader |
20 | 20 | from opentelemetry.sdk.trace import ReadableSpan |
21 | 21 | from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor |
22 | | -from opentelemetry.trace import StatusCode, get_tracer |
| 22 | +from opentelemetry.trace import INVALID_SPAN, StatusCode, get_current_span, get_tracer |
23 | 23 | from pydantic import BaseModel, __version__ as pydantic_version |
24 | 24 | from pydantic_core import ValidationError |
25 | 25 |
|
@@ -3323,3 +3323,70 @@ def test_default_id_generator(exporter: TestExporter) -> None: |
3323 | 3323 | export['attributes']['i'] for export in sorted(exported, key=lambda span: span['start_time']) |
3324 | 3324 | ] |
3325 | 3325 | assert sorted_by_trace_id == sorted_by_start_timestamp |
| 3326 | + |
| 3327 | + |
| 3328 | +def test_start_end_attach_detach(exporter: TestExporter, caplog: pytest.LogCaptureFixture): |
| 3329 | + span = logfire.span('test') |
| 3330 | + assert not span.is_recording() |
| 3331 | + assert not exporter.exported_spans |
| 3332 | + assert get_current_span() is INVALID_SPAN |
| 3333 | + |
| 3334 | + span._start() # type: ignore |
| 3335 | + span._start() # type: ignore |
| 3336 | + assert span.is_recording() |
| 3337 | + assert len(exporter.exported_spans) == 1 |
| 3338 | + assert get_current_span() is INVALID_SPAN |
| 3339 | + |
| 3340 | + span._attach() # type: ignore |
| 3341 | + span._attach() # type: ignore |
| 3342 | + assert get_current_span() is span._span # type: ignore |
| 3343 | + |
| 3344 | + span._detach() # type: ignore |
| 3345 | + span._detach() # type: ignore |
| 3346 | + assert span.is_recording() |
| 3347 | + assert len(exporter.exported_spans) == 1 |
| 3348 | + assert get_current_span() is INVALID_SPAN |
| 3349 | + |
| 3350 | + span._end() # type: ignore |
| 3351 | + span._end() # type: ignore |
| 3352 | + assert not span.is_recording() |
| 3353 | + assert len(exporter.exported_spans) == 2 |
| 3354 | + assert get_current_span() is INVALID_SPAN |
| 3355 | + |
| 3356 | + assert not caplog.messages |
| 3357 | + |
| 3358 | + assert exporter.exported_spans_as_dict(_include_pending_spans=True) == snapshot( |
| 3359 | + [ |
| 3360 | + { |
| 3361 | + 'name': 'test', |
| 3362 | + 'context': {'trace_id': 1, 'span_id': 2, 'is_remote': False}, |
| 3363 | + 'parent': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 3364 | + 'start_time': 1000000000, |
| 3365 | + 'end_time': 1000000000, |
| 3366 | + 'attributes': { |
| 3367 | + 'code.filepath': 'test_logfire.py', |
| 3368 | + 'code.function': 'test_start_end_attach_detach', |
| 3369 | + 'code.lineno': 123, |
| 3370 | + 'logfire.msg_template': 'test', |
| 3371 | + 'logfire.msg': 'test', |
| 3372 | + 'logfire.span_type': 'pending_span', |
| 3373 | + 'logfire.pending_parent_id': '0000000000000000', |
| 3374 | + }, |
| 3375 | + }, |
| 3376 | + { |
| 3377 | + 'name': 'test', |
| 3378 | + 'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 3379 | + 'parent': None, |
| 3380 | + 'start_time': 1000000000, |
| 3381 | + 'end_time': 2000000000, |
| 3382 | + 'attributes': { |
| 3383 | + 'code.filepath': 'test_logfire.py', |
| 3384 | + 'code.function': 'test_start_end_attach_detach', |
| 3385 | + 'code.lineno': 123, |
| 3386 | + 'logfire.msg_template': 'test', |
| 3387 | + 'logfire.msg': 'test', |
| 3388 | + 'logfire.span_type': 'span', |
| 3389 | + }, |
| 3390 | + }, |
| 3391 | + ] |
| 3392 | + ) |
0 commit comments