Skip to content

Commit efcdfc3

Browse files
static mapping instead of if statements
1 parent 6f5f597 commit efcdfc3

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

src/sap_cloud_sdk/core/telemetry/span_processors/runtime_context_processor.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,39 @@
77
from opentelemetry.sdk.trace import SpanProcessor, ReadableSpan
88
from opentelemetry.trace import Span
99

10+
from sap_cloud_sdk.core.runtime_context import APP_TENANT_ID, GLOBAL_TENANT_ID, USER_ID
1011
from sap_cloud_sdk.core.telemetry.constants import (
1112
ATTR_SAP_TENANT_ID,
1213
ATTR_USER_ID,
1314
)
1415

1516
logger = logging.getLogger(__name__)
1617

18+
_CONTEXT_TO_SPAN_ATTR = {
19+
APP_TENANT_ID: ATTR_SAP_TENANT_ID,
20+
GLOBAL_TENANT_ID: ATTR_SAP_TENANT_ID,
21+
USER_ID: ATTR_USER_ID,
22+
}
23+
1724

1825
class RuntimeContextSpanProcessor(SpanProcessor):
1926
"""Injects tenant and user identity from the SDK runtime context into every span.
2027
21-
Reads TENANT_ID, GLOBAL_TENANT_ID, and USER_ID from the runtime context populated
22-
by bootstrap() providers (e.g. IASContextProvider) and stamps them as span attributes
23-
on every span at start time.
28+
Reads APP_TENANT_ID, GLOBAL_TENANT_ID, and USER_ID from the runtime context
29+
populated by bootstrap() providers (e.g. IASContextProvider) and stamps them
30+
as span attributes on every span at start time.
2431
"""
2532

2633
def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None:
2734
if not span.is_recording():
2835
return
2936
try:
30-
from sap_cloud_sdk.core.runtime_context import (
31-
get_context,
32-
APP_TENANT_ID,
33-
GLOBAL_TENANT_ID,
34-
USER_ID,
35-
)
37+
from sap_cloud_sdk.core.runtime_context import get_context
3638

3739
ctx = get_context()
38-
if app_tenant_id := ctx.get(APP_TENANT_ID):
39-
span.set_attribute(ATTR_SAP_TENANT_ID, app_tenant_id)
40-
if global_tenant_id := ctx.get(GLOBAL_TENANT_ID):
41-
span.set_attribute(ATTR_SAP_TENANT_ID, global_tenant_id)
42-
if user_id := ctx.get(USER_ID):
43-
span.set_attribute(ATTR_USER_ID, user_id)
40+
for context_key, span_attr in _CONTEXT_TO_SPAN_ATTR.items():
41+
if value := ctx.get(context_key):
42+
span.set_attribute(span_attr, value)
4443
except Exception as exc:
4544
logger.debug(
4645
"RuntimeContextSpanProcessor: error injecting context into span %r: %s",

0 commit comments

Comments
 (0)