OpenTelemetry Erlang and Elixir Contrib
repository·main·Indexed 42 days ago
https://github.com/open-telemetry/opentelemetry-erlang-contribA collection of community-contributed OpenTelemetry instrumentations and utilities for the Erlang and Elixir ecosystems. It extends the core OpenTelemetry SDK with support for recording exceptions, custom span attributes, and specific instrumentations such as opentelemetry_absinthe, opentelemetry_phoenix, and opentelemetry_ecto.
What's inside OpenTelemetry Erlang Contrib
- OpentelemetryAbsinthe is an OpenTelemetry instrumentation library specifically designed for Absinthe, a GraphQL server implementation for Elixir. It allows you to capture and export telemetry data (such as traces) generated by your Absinthe GraphQL operations.
Overview of OpenTelemetry Erlang & Elixir Contrib
mainTheopentelemetry-erlang-contribrepository contains OpenTelemetry contribution libraries for Erlang and Elixir. These libraries provide additional functionality, such as specific instrumentations, that are not included in the core OpenTelemetry Erlang API and SDK repositories. Use this repository to extend your observability capabilities with specialized instrumentations for popular Erlang/Elixir frameworks and libraries.Use Opentelemetry Process Propagator for cross-process context propagation
mainTheopentelemetry_process_propagatorpackage provides functions to enable OpenTelemetry context propagation across different processes. This is essential for maintaining trace continuity when a request or operation moves from one Erlang/Elixir process to another (e.g., via message passing or distributed systems).Use OpentelemetryReq for Req instrumentation
mainTheopentelemetry_reqpackage provides an OpenTelemetry plugin for the Req HTTP client. It enables automatic instrumentation and trace propagation for HTTP requests made usingReq.Trace Oban job processing
mainBy default, onceOpentelemetryOban.setup()is called, a new trace is automatically started whenever an Oban job is processed.Understand Semantic Convention requirement levels
mainThe instrumentation packages in this repository map OpenTelemetry Semantic Convention requirement levels to specific configuration options:
SemConv Level Instrumentation Behavior User Control Required Always included None Recommended Included by default opt_out_attrsOpt-In Excluded by default opt_in_attrsExperimental Excluded by default opt_in_attrsNaming conventions for custom span attributes
mainTo avoid collisions and maintain standards, follow these naming rules for custom attribute keys:
Module Naming
Modules follow the pattern
Opentelemetry[PackageName].[ComponentName]Attributes.- Example:
OpentelemetryEcto.EctoAttributesorOpentelemetryMylib.MylibAttributes.
Attribute Key Naming
- Prefixing: Prefix every key with the component name using dot notation (e.g.,
mylib.). - Casing: Use
snake_casesegments separated by dots. - Reserved Prefixes: Do not start keys with
otel.as this prefix is reserved for OpenTelemetry. Do not use existing SemConv namespaces unless intentionally extending them.
Example Status Reason mylib.cache✅ Correct prefix and format mylib.queue_name✅ Correct prefix and format otel.mylib.cache❌ otel.prefix is reservedcache❌ Missing namespace - Example:
How OpentelemetryNebulex works
mainOpentelemetryNebulex integrates with Nebulex by attaching totelemetryevents. It listens for Nebulex command events and translates them intoOpenTelemetryspans. It supports both Nebulex v2 and v3 command events.How to record exceptions on spans
mainTo correctly record a failure on an OpenTelemetry span, you must use both
record_exceptionandset_status. These two functions are complementary and serve different purposes:record_exception: Adds an event to the span containing structured fields likeexception.type,exception.message, andexception.stacktrace. This is used by trace backends for detailed error inspection.set_status(status, description): Marks the span status as errored. This is used by dashboards and UIs to quickly identify failed spans.
Warning: Do not add other span events. New events should be handled via the Logs API once available in Erlang.
record_exceptionwithoutset_statuswill leave the span appearing successful, whileset_statuswithoutrecord_exceptionwill lose the structured error details.Understand trace export strategies
mainWhen using OpenTelemetry in a Phoenix/Ecto application, you can export traces using two primary architectural patterns:
Via OpenTelemetry Collector (Recommended for multi-destination): The application sends traces to a local or centralized OpenTelemetry Collector. The Collector is then configured to route those traces to one or more backends (e.g., Zipkin, Jaeger, Honeycomb, or Lightstep). Pattern:
Application --> OpenTelemetry Collector --> [Zipkin, Jaeger, etc.]Direct Export (OTLP): The application sends traces directly to an external service that supports the OTLP (OpenTelemetry Protocol). Pattern:
Application --> External Service
If you choose to export to external services like Honeycomb or Lightstep, ensure you update your
.envfile with the required credentials and consult the specific service's documentation for OTLP configuration.How OpentelemetryHTTPoison works with trace propagation
mainOpentelemetryHTTPoison is designed to work in tandem with 'server-side' instrumented libraries (like
opentelemetry_plug).- Server-side: A library receives a request, extracts trace context from headers, and creates a
SERVERspan. - Client-side:
OpentelemetryHTTPoisoncreates a span for the outgoing request and injects the trace context into the request headers.
This allows trace information to propagate across microservices through HTTP 'jumps'.
Critical Note on Process Boundaries: The Erlang OpenTelemetry SDK stores the active span in a
pdict(per-process dictionary). IfOpentelemetryHTTPoisonis called from a different process than the one that handled the initial request, it will not find the parent span and will create a new root span, breaking the trace. In such cases, you must manually pass the parent span to the HTTP client request.- Server-side: A library receives a request, extracts trace context from headers, and creates a
Understand context propagation precedence
mainWhen implementing or debugging:telemetryhandlers, be aware of the context propagation precedence: handlers must first check for an already-attached context before falling back to a$callersancestor. This prevents incorrect context association during event handling.