Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/opentelemetry/trace/propagation/__init__.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

14 statements  

1# Copyright The OpenTelemetry Authors 

2# SPDX-License-Identifier: Apache-2.0 

3 

4from opentelemetry.context import create_key, get_value, set_value 

5from opentelemetry.context.context import Context 

6from opentelemetry.trace.span import INVALID_SPAN, Span 

7 

8SPAN_KEY = "current-span" 

9_SPAN_KEY = create_key("current-span") 

10 

11 

12def set_span_in_context(span: Span, context: Context | None = None) -> Context: 

13 """Set the span in the given context. 

14 

15 Args: 

16 span: The Span to set. 

17 context: a Context object. if one is not passed, the 

18 default current context is used instead. 

19 """ 

20 ctx = set_value(_SPAN_KEY, span, context=context) 

21 return ctx 

22 

23 

24def get_current_span(context: Context | None = None) -> Span: 

25 """Retrieve the current span. 

26 

27 Args: 

28 context: A Context object. If one is not passed, the 

29 default current context is used instead. 

30 

31 Returns: 

32 The Span set in the context if it exists. INVALID_SPAN otherwise. 

33 """ 

34 span = get_value(_SPAN_KEY, context=context) 

35 if span is None or not isinstance(span, Span): 

36 return INVALID_SPAN 

37 return span