-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use traceparent header from incoming shape requests to set pare…
…nt span (#2000) Fix #1914 - open-telemetry/opentelemetry-erlang#518 --------- Co-authored-by: Ilia Borovitinov <[email protected]>
- Loading branch information
1 parent
68540c8
commit 584c4f5
Showing
3 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@core/sync-service": patch | ||
--- | ||
|
||
use traceparent header from incoming shape requests to set parent span |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/sync-service/lib/electric/plug/trace_context_plug.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Electric.Plug.TraceContextPlug do | ||
@moduledoc """ | ||
A plug that extracts trace context from incoming HTTP headers and sets it as the parent span. | ||
""" | ||
@behaviour Plug | ||
|
||
require Logger | ||
|
||
def init(opts), do: opts | ||
|
||
def call(%Plug.Conn{req_headers: headers} = conn, _opts) do | ||
# Extract function expects a list of headers as tuples and knows | ||
# the expected header keys, so we don't have to prefilter. | ||
ctx = :otel_propagator_text_map.extract_to(:otel_ctx.new(), headers) | ||
|
||
# Get the span context from the extracted context | ||
case :otel_tracer.current_span_ctx(ctx) do | ||
:undefined -> | ||
# No parent, continue as-is | ||
conn | ||
|
||
span_ctx -> | ||
# Parent found, set as current span | ||
:otel_tracer.set_current_span(span_ctx) | ||
conn | ||
end | ||
end | ||
end |