Skip to content

Instantly share code, notes, and snippets.

@bamorim
Last active March 28, 2023 08:42
Show Gist options
  • Save bamorim/5e1d9a4bc41121c4475c2ecdcc2736fc to your computer and use it in GitHub Desktop.
Save bamorim/5e1d9a4bc41121c4475c2ecdcc2736fc to your computer and use it in GitHub Desktop.
Opentelemetry Span <> Logger integration
import Config
config :opentelemetry,
processors: [
{OpentelemetrySpanLogger, []},
:batch
]
defmodule OpentelemetrySpanLogger do
require Record
@behaviour :otel_span_processor
@span_record Record.extract(:span, from_lib: "opentelemetry/include/otel_span.hrl")
Record.defrecord(:span, @span_record)
@impl :otel_span_processor
def force_flush(_config), do: :ok
@impl :otel_span_processor
def on_start(_ctx, span, _config) do
Logger.metadata([trace_id: span(span, :trace_id), span_id: span(span, :span_id)])
span
end
@impl :otel_span_processor
def on_end(_span, _config), do: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment