mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-11 18:56:23 +00:00
98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
"""
|
|
Utility functions for OpenTelemetry instrumentation with graceful degradation.
|
|
|
|
This module provides a safe way to import and use OpenTelemetry decorators and metrics
|
|
that gracefully degrades when OpenTelemetry packages are not installed.
|
|
"""
|
|
|
|
|
|
# Define no-op implementations first (always available for testing)
|
|
def _no_op_trace_decorator(operation_name):
|
|
"""No-op decorator when OpenTelemetry is not available"""
|
|
|
|
def decorator(func):
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
class NoOpMetrics:
|
|
"""No-op metrics class when OpenTelemetry is not available"""
|
|
|
|
def record_avatar_generated(self, *args, **kwargs):
|
|
pass
|
|
|
|
def record_avatar_request(self, *args, **kwargs):
|
|
pass
|
|
|
|
def record_cache_hit(self, *args, **kwargs):
|
|
pass
|
|
|
|
def record_cache_miss(self, *args, **kwargs):
|
|
pass
|
|
|
|
def record_external_request(self, *args, **kwargs):
|
|
pass
|
|
|
|
def record_file_upload(self, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
# Safe import pattern for OpenTelemetry
|
|
try:
|
|
from .opentelemetry_middleware import (
|
|
trace_avatar_operation,
|
|
trace_file_upload,
|
|
trace_authentication,
|
|
get_avatar_metrics,
|
|
)
|
|
|
|
# Get the actual metrics instance
|
|
avatar_metrics = get_avatar_metrics()
|
|
|
|
# OpenTelemetry is available
|
|
TELEMETRY_AVAILABLE = True
|
|
|
|
except ImportError:
|
|
# OpenTelemetry packages not installed - use no-op implementations
|
|
trace_avatar_operation = _no_op_trace_decorator
|
|
trace_file_upload = _no_op_trace_decorator
|
|
trace_authentication = _no_op_trace_decorator
|
|
|
|
avatar_metrics = NoOpMetrics()
|
|
|
|
# OpenTelemetry is not available
|
|
TELEMETRY_AVAILABLE = False
|
|
|
|
|
|
def get_telemetry_decorators():
|
|
"""
|
|
Get all telemetry decorators in a single call.
|
|
|
|
Returns:
|
|
tuple: (trace_avatar_operation, trace_file_upload, trace_authentication)
|
|
"""
|
|
return trace_avatar_operation, trace_file_upload, trace_authentication
|
|
|
|
|
|
def get_telemetry_metrics():
|
|
"""
|
|
Get the telemetry metrics instance.
|
|
|
|
Returns:
|
|
AvatarMetrics or NoOpMetrics: The metrics instance
|
|
"""
|
|
return avatar_metrics
|
|
|
|
|
|
def is_telemetry_available():
|
|
"""
|
|
Check if OpenTelemetry is available and working.
|
|
|
|
Returns:
|
|
bool: True if OpenTelemetry is available, False otherwise
|
|
"""
|
|
# Check the actual metrics instance type rather than relying on
|
|
# the module-level variable which can be affected by test mocking
|
|
return not isinstance(avatar_metrics, NoOpMetrics)
|