Instrumentation

Instrumentation is the process of making your code collects telemetry data. GL Observability supports several layers of instrumentation to provide comprehensive coverage.

Auto-Instrumentation

Our library provides seamless, low code instrumentation for FastAPIarrow-up-right, Requestsarrow-up-right, HTTPXarrow-up-right, Langchainarrow-up-right and python loggingarrow-up-right libraries. These are configured directly through the TelemetryConfig object.

from gl_observability import init_telemetry, TelemetryConfig, FastAPIConfig

app = FastAPI()
fastapi_config = FastAPIConfig(app=app)

otel_config = TelemetryConfig(
    ...
    fastapi_config=fastapi_config,
    use_langchain=True,
    use_httpx=True,
    use_requests=True,
    log_trace_context=True,
)
init_telemetry(otel_config)

Logging

To enable logging instrumentation, set the parameter log_trace_context=True in TelemetryConfig. This does not create a new span but injects trace context into the log statement, linking trace and log data. The following keys are added to log records:

  • otelSpanID: string - The span ID, default to "0" .

  • otelTraceID: string - The trace ID, default to "0" .

  • otelServiceName: string - The trace resource service.name.

  • otelTraceSampled: bool - True if the trace is sampled by trace sampler, otherwise False.

circle-info

The LoggerManager JSON format will automatically add the keys into the log.

Third Party Instrumentor

You can add more instrumentation by using the third party instrumentor library. For example, if you want to add instrumentation for Redis you can use official OpenTelemetry Redis Instrumentor library. This is the steps to add the instrumentation:

  1. install the OpenTelemetry instrumentation library.

  2. Initiliaze the instrumentation

This is the list of instrumentation supported by open telemetry SDK: open telemetry instrumentation listarrow-up-right

GL Observability Custom Instrumentors

GL Observability also provides additional custom instrumentors to help you capture telemetry from parts of your application that standard libraries might not cover.

FunctionsInstrumentor

This instrumentor allows you to wrap specific module-level or class-level functions to automatically create spans, capturing their inputs, outputs, and any exceptions. The instrumentor currently support:

  • Class functions (CustomClass.function)

  • Module functions (module.function)

  • Static methods (@staticmethod)

  • Class methods (@classmethod)

Example

This is the example of how to use Custom functions instrumentation. Let's say you have a functions inside functions.py file on the module folder that you want to instrument.

You also have this custom_class.py on the module/classes/custom_class folder that you want to instrument.

To instrument those functions:

HTTPClientInstrumentor

This instrumentor is to instrument low level http library. Right now, the OpenTelemetry SDK only provide instrumentator for high level http library such as requests, aiohttp, and httpx.

Manual Instrumentation

You can also add instrumentation to your own python code by following the tutorial from OpenTelemetryarrow-up-right.

Last updated

Was this helpful?