Skip to content

Commit 96090a5

Browse files
authored
[Core] Add start_time kwargs to start_span methods (#41106)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent b9bdfe2 commit 96090a5

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- Added a `start_time` keyword argument to the `start_span` and `start_as_current_span` methods in the `OpenTelemetryTracer` class. This allows users to specify a custom start time for created spans. #41106
8+
79
### Breaking Changes
810

911
### Bugs Fixed

sdk/core/azure-core/azure/core/tracing/opentelemetry.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def start_span(
8181
kind: SpanKind = _SpanKind.INTERNAL,
8282
attributes: Optional[Attributes] = None,
8383
links: Optional[Sequence[Link]] = None,
84+
start_time: Optional[int] = None,
8485
) -> Span:
8586
"""Starts a span without setting it as the current span in the context.
8687
@@ -92,6 +93,8 @@ def start_span(
9293
:paramtype attributes: Mapping[str, AttributeValue]
9394
:keyword links: Links to add to the span.
9495
:paramtype links: list[~azure.core.tracing.Link]
96+
:keyword start_time: The start time of the span in nanoseconds since the epoch.
97+
:paramtype start_time: Optional[int]
9598
:return: The span that was started
9699
:rtype: ~opentelemetry.trace.Span
97100
"""
@@ -103,6 +106,7 @@ def start_span(
103106
kind=otel_kind,
104107
attributes=attributes,
105108
links=otel_links,
109+
start_time=start_time,
106110
record_exception=False,
107111
)
108112

@@ -116,6 +120,7 @@ def start_as_current_span(
116120
kind: SpanKind = _SpanKind.INTERNAL,
117121
attributes: Optional[Attributes] = None,
118122
links: Optional[Sequence[Link]] = None,
123+
start_time: Optional[int] = None,
119124
end_on_exit: bool = True,
120125
) -> Iterator[Span]:
121126
"""Context manager that starts a span and sets it as the current span in the context.
@@ -134,12 +139,14 @@ def start_as_current_span(
134139
:paramtype attributes: Optional[Attributes]
135140
:keyword links: Links to add to the span.
136141
:paramtype links: Optional[Sequence[Link]]
142+
:keyword start_time: The start time of the span in nanoseconds since the epoch.
143+
:paramtype start_time: Optional[int]
137144
:keyword end_on_exit: Whether to end the span when exiting the context manager. Defaults to True.
138145
:paramtype end_on_exit: bool
139146
:return: The span that was started
140147
:rtype: Iterator[~opentelemetry.trace.Span]
141148
"""
142-
span = self.start_span(name, kind=kind, attributes=attributes, links=links)
149+
span = self.start_span(name, kind=kind, attributes=attributes, links=links, start_time=start_time)
143150
with trace.use_span( # pylint: disable=not-context-manager
144151
span, record_exception=False, end_on_exit=end_on_exit
145152
) as span:

sdk/core/azure-core/tests/test_tracer_otel.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,22 @@ def test_tracer_set_span_error(tracing_helper):
376376

377377
assert finished_spans[1].status.status_code == OtelStatusCode.ERROR
378378
assert finished_spans[1].status.description == "This is an error"
379+
380+
381+
def test_start_span_with_start_time(tracing_helper):
382+
"""Test that a span can be started with a custom start time."""
383+
tracer = get_tracer()
384+
assert tracer
385+
start_time = 1234567890
386+
with tracer.start_as_current_span(name="foo-span", start_time=start_time) as span:
387+
assert span.start_time == start_time
388+
finished_spans = tracing_helper.exporter.get_finished_spans()
389+
assert len(finished_spans) == 1
390+
assert finished_spans[0].start_time == start_time
391+
392+
span = tracer.start_span(name="foo-span", start_time=start_time)
393+
assert span.start_time == start_time
394+
span.end()
395+
finished_spans = tracing_helper.exporter.get_finished_spans()
396+
assert len(finished_spans) == 2
397+
assert finished_spans[1].start_time == start_time

0 commit comments

Comments
 (0)