|
| 1 | +import asyncio |
| 2 | +from functools import wraps |
| 3 | + |
| 4 | +from opentelemetry import metrics, trace |
| 5 | +from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter |
| 6 | +from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter |
| 7 | +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 8 | +from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
| 9 | +from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor |
| 10 | +from opentelemetry.sdk._configuration import _init_logging as init_otel_logging |
| 11 | +from opentelemetry.sdk.metrics import MeterProvider |
| 12 | +from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader |
| 13 | +from opentelemetry.sdk.resources import Resource |
| 14 | +from opentelemetry.sdk.trace import TracerProvider |
| 15 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 16 | +from opentelemetry_instrumentor_dramatiq import DramatiqInstrumentor |
| 17 | + |
| 18 | +from .config import AppConfig |
| 19 | + |
| 20 | +# Get the _tracer instance (You can set your own _tracer name) |
| 21 | +tracer = trace.get_tracer(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def trace_function(trace_attributes: bool = True, trace_result: bool = True): |
| 25 | + """ |
| 26 | + Decorator to trace callables using OpenTelemetry spans. |
| 27 | +
|
| 28 | + Parameters: |
| 29 | + - trace_attributes (bool): If False, disables adding function arguments to the span. |
| 30 | + - trace_result (bool): If False, disables adding the function's result to the span. |
| 31 | + """ |
| 32 | + |
| 33 | + def decorator(func): |
| 34 | + @wraps(func) |
| 35 | + async def async_wrapper(*args, **kwargs): |
| 36 | + with tracer.start_as_current_span(func.__name__) as span: |
| 37 | + try: |
| 38 | + # Set function arguments as attributes |
| 39 | + if trace_attributes: |
| 40 | + span.set_attribute("function.args", str(args)) |
| 41 | + span.set_attribute("function.kwargs", str(kwargs)) |
| 42 | + |
| 43 | + result = await func(*args, **kwargs) |
| 44 | + # Add result to span |
| 45 | + if trace_result: |
| 46 | + span.set_attribute("function.result", str(result)) |
| 47 | + return result |
| 48 | + except Exception as e: |
| 49 | + # Record the exception in the span |
| 50 | + span.record_exception(e) |
| 51 | + span.set_status(trace.status.Status(trace.status.StatusCode.ERROR)) |
| 52 | + raise |
| 53 | + |
| 54 | + @wraps(func) |
| 55 | + def sync_wrapper(*args, **kwargs): |
| 56 | + with tracer.start_as_current_span(func.__name__) as span: |
| 57 | + try: |
| 58 | + # Set function arguments as attributes |
| 59 | + if trace_attributes: |
| 60 | + span.set_attribute("function.args", str(args)) |
| 61 | + span.set_attribute("function.kwargs", str(kwargs)) |
| 62 | + |
| 63 | + result = func(*args, **kwargs) |
| 64 | + # Add result to span |
| 65 | + if trace_result: |
| 66 | + span.set_attribute("function.result", str(result)) |
| 67 | + return result |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + # Record the exception in the span |
| 71 | + span.record_exception(e) |
| 72 | + span.set_status(trace.status.Status(trace.status.StatusCode.ERROR)) |
| 73 | + raise |
| 74 | + |
| 75 | + return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper |
| 76 | + |
| 77 | + return decorator |
| 78 | + |
| 79 | + |
| 80 | +""" |
| 81 | +Manual instrumentation bring several benefits: |
| 82 | +- We don't need to use `opentelemetry-instrument` command, which |
| 83 | + gives us more control over the application running process. |
| 84 | +- It is more performant |
| 85 | +- It works with uvicorn reloader |
| 86 | +- Avoids duplicating environment variables (i.e. OTEL_SERVICE_NAME is already defined in the config) |
| 87 | +""" |
| 88 | + |
| 89 | + |
| 90 | +def instrument_opentelemetry(config: AppConfig): # pragma: no cover |
| 91 | + """ |
| 92 | + Configures OpenTelemetry instrumentation for tracing, metrics, and logging. |
| 93 | +
|
| 94 | + This function sets up OpenTelemetry components, including span processors, metric |
| 95 | + exporters, and log exporters, based on the provided application configuration. |
| 96 | +
|
| 97 | + Parameters: |
| 98 | + config (AppConfig): Configuration object containing application-specific settings |
| 99 | + required for initializing OpenTelemetry instrumentation. |
| 100 | + """ |
| 101 | + |
| 102 | + resource = Resource.create( |
| 103 | + { |
| 104 | + "service.name": config.APP_NAME, |
| 105 | + "deployment.environment": config.ENVIRONMENT, |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + """ |
| 110 | + The exporters can be still configured using OTEL_* environment variables, |
| 111 | + we capture and check the variables so we can avoid instrumenting if we don't have |
| 112 | + any endpoints configured. This will avoid instrumenting the application when |
| 113 | + running locally or during unit tests. |
| 114 | + """ |
| 115 | + traces_endpoint = config.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT or config.OTEL_EXPORTER_OTLP_ENDPOINT |
| 116 | + metrics_endpoint = config.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT or config.OTEL_EXPORTER_OTLP_ENDPOINT |
| 117 | + logs_endpoint = config.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT or config.OTEL_EXPORTER_OTLP_ENDPOINT |
| 118 | + |
| 119 | + # Traces |
| 120 | + if traces_endpoint: |
| 121 | + span_exporter = OTLPSpanExporter(endpoint=traces_endpoint) |
| 122 | + tracer_provider = TracerProvider(resource=resource) |
| 123 | + tracer_provider.add_span_processor(BatchSpanProcessor(span_exporter)) |
| 124 | + trace.set_tracer_provider(tracer_provider) |
| 125 | + |
| 126 | + # Metrics |
| 127 | + if metrics_endpoint: |
| 128 | + metrics_exporter = OTLPMetricExporter(endpoint=metrics_endpoint) |
| 129 | + metrics_provider = MeterProvider( |
| 130 | + resource=resource, metric_readers=[PeriodicExportingMetricReader(metrics_exporter)] |
| 131 | + ) |
| 132 | + metrics.set_meter_provider(metrics_provider) |
| 133 | + |
| 134 | + # Logs |
| 135 | + """ |
| 136 | + Log instrumentation is still experimental, so we borrow a private instrumentation |
| 137 | + function, which should allow us to keep it working as expected with upcoming changes. |
| 138 | + When logs instrumentation will be stable this should be revisited. |
| 139 | + We still don't support passing the custom endpoint as parameter but it will |
| 140 | + be configured using OTEL_* environment variables. |
| 141 | + """ |
| 142 | + if logs_endpoint: |
| 143 | + init_otel_logging(resource=resource, exporters={"otel": OTLPLogExporter}) |
| 144 | + |
| 145 | + |
| 146 | +def instrument_third_party(): |
| 147 | + """ |
| 148 | + Instrument third-party libraries for monitoring and tracing. |
| 149 | +
|
| 150 | + This function initializes and instruments various third-party libraries |
| 151 | + that are commonly used in applications. It configures them to work with |
| 152 | + monitoring and tracing systems to collect performance metrics and |
| 153 | + distributed trace data. |
| 154 | +
|
| 155 | + Raises: |
| 156 | + This function does not explicitly raise exceptions, but exceptions |
| 157 | + may propagate from the individual instrumentor methods if the |
| 158 | + instrumentation process fails. |
| 159 | + """ |
| 160 | + DramatiqInstrumentor().instrument() |
| 161 | + HTTPXClientInstrumentor().instrument() |
| 162 | + SQLAlchemyInstrumentor().instrument() |
0 commit comments