chore: remove redundant function

Signed-off-by: Casper Guldbech Nielsen <scni@novonordisk.com>
This commit is contained in:
Casper Guldbech Nielsen 2025-05-06 03:17:23 -07:00
parent bb41d12344
commit d7708061a5
No known key found for this signature in database
GPG Key ID: B004583B52B9A446
2 changed files with 55 additions and 32 deletions

View File

@ -3,5 +3,4 @@ from .otel import (
async_span_decorator,
span_decorator,
extract_otel_context,
restore_otel_context,
)

View File

@ -2,7 +2,9 @@ from logging import Logger
from typing import Any, Union
import functools
import logging
from opentelemetry import trace
from opentelemetry._logs import set_logger_provider
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
@ -147,27 +149,10 @@ class DaprAgentsOTel:
return endpoint
def span_decorator(name):
"""Decorator that creates an OpenTelemetry span for a method."""
def decorator(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
# Access tracer from instance at runtime
tracer = getattr(self, "_tracer", None)
if tracer:
with tracer.start_as_current_span(name):
return func(self, *args, **kwargs)
else:
# Fallback if no tracer is available
return func(self, *args, **kwargs)
return wrapper
return decorator
# Global propagator instance
_propagator = TraceContextTextMapPropagator()
# For async methods
def async_span_decorator(name):
"""Decorator that creates an OpenTelemetry span for an async method."""
@ -178,16 +163,25 @@ def async_span_decorator(name):
if not tracer:
return await func(self, *args, **kwargs)
# Extract context from kwargs if available
# Extract OpenTelemetry context from kwargs if available
otel_context = kwargs.pop("otel_context", None)
context = None
# Create new context or use current one
ctx = None
if otel_context:
carrier = otel_context
context = TraceContextTextMapPropagator().extract(carrier=carrier)
try:
# Create new context from carrier
ctx = _propagator.extract(carrier=otel_context)
logging.debug(f"Restored context for span '{name}': {otel_context}")
except Exception as e:
logging.warning(f"Failed to extract context for span '{name}': {e}")
# Start span with parent context if available
with tracer.start_as_current_span(name, context=context):
# Start span with context
with tracer.start_as_current_span(name, context=ctx) as span:
# Add common attributes
span.set_attribute("function.name", func.__name__)
# Execute the function
return await func(self, *args, **kwargs)
return wrapper
@ -195,14 +189,44 @@ def async_span_decorator(name):
return decorator
def span_decorator(name):
"""Decorator that creates an OpenTelemetry span for a synchronous method."""
def decorator(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
tracer = getattr(self, "_tracer", None)
if not tracer:
return func(self, *args, **kwargs)
# Extract OpenTelemetry context from kwargs if available
otel_context = kwargs.pop("otel_context", None)
# Create new context or use current one
ctx = None
if otel_context:
try:
# Create new context from carrier
ctx = _propagator.extract(carrier=otel_context)
logging.debug(f"Restored context for span '{name}': {otel_context}")
except Exception as e:
logging.warning(f"Failed to extract context for span '{name}': {e}")
# Start span with context
with tracer.start_as_current_span(name, context=ctx) as span:
# Add common attributes
span.set_attribute("function.name", func.__name__)
# Execute the function
return func(self, *args, **kwargs)
return wrapper
return decorator
def extract_otel_context():
"""Extract current OpenTelemetry context for cross-boundary propagation"""
carrier: dict[Any, Any] = {}
TraceContextTextMapPropagator().inject(carrier)
return carrier
def restore_otel_context(carrier):
"""Restore OpenTelemetry context from carrier"""
context = TraceContextTextMapPropagator().extract(carrier)
return context