-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add W3C Trace Context propagation middleware for OpenTelemetry #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,66 @@ | ||
| if Code.ensure_loaded?(:otel_propagator_text_map) do | ||
| defmodule Commanded.Middleware.TraceContextPropagator do | ||
| @moduledoc """ | ||
| A middleware for propagating W3C Trace Context to Event Handlers. | ||
|
|
||
| This middleware captures the current span context and stores it in event metadata | ||
| following the [W3C Trace Context](https://www.w3.org/TR/trace-context-2/) specification, | ||
| allowing event handlers to create proper span links or parent-child relationships. | ||
|
|
||
| ## W3C Trace Context | ||
|
|
||
| The middleware stores trace context using the standard W3C header names: | ||
|
|
||
| * `traceparent` - Contains version, trace-id, parent-id (span-id), and trace-flags | ||
| * `tracestate` - Vendor-specific key-value pairs (optional, only set if present) | ||
|
|
||
| Example traceparent: `00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| This middleware requires `opentelemetry_api` to be installed: | ||
|
|
||
| {:opentelemetry_api, "~> 1.0"} | ||
|
|
||
| ## Usage | ||
|
|
||
| ```elixir | ||
| defmodule BankRouter do | ||
| use Commanded.Commands.Router | ||
|
|
||
| middleware Commanded.Middleware.TraceContextPropagator | ||
|
|
||
| dispatch [OpenAccount, DepositMoney], | ||
| to: BankAccount, | ||
| identity: :account_number | ||
| end | ||
| ``` | ||
| """ | ||
|
|
||
| @behaviour Commanded.Middleware | ||
|
|
||
| alias Commanded.Middleware.Pipeline | ||
|
|
||
| @doc false | ||
| def before_dispatch(%Pipeline{} = pipeline) do | ||
| case :otel_propagator_text_map.inject([]) do | ||
| [] -> | ||
| pipeline | ||
|
|
||
| headers -> | ||
| pipeline | ||
| |> maybe_assign("traceparent", List.keyfind(headers, "traceparent", 0)) | ||
| |> maybe_assign("tracestate", List.keyfind(headers, "tracestate", 0)) | ||
| end | ||
| end | ||
|
|
||
| defp maybe_assign(pipeline, _key, nil), do: pipeline | ||
|
|
||
| defp maybe_assign(pipeline, key, {_, value}), | ||
| do: Pipeline.assign_metadata(pipeline, key, value) | ||
|
|
||
| def after_dispatch(pipeline), do: pipeline | ||
|
|
||
| def after_failure(pipeline), do: pipeline | ||
| end | ||
| end | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,151 @@ | ||
| defmodule Commanded.Middleware.TraceContextPropagatorTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias Commanded.Middleware.Commands.Fail | ||
| alias Commanded.Middleware.Pipeline | ||
| alias Commanded.Middleware.TraceContextPropagator | ||
|
|
||
| require OpenTelemetry.Tracer, as: Tracer | ||
|
|
||
| @trace_id "0af7651916cd43dd8448eb211c80319c" | ||
| @span_id "b7ad6b7169203331" | ||
| @traceparent "00-#{@trace_id}-#{@span_id}-01" | ||
| @tracestate "vendor1=value1,vendor2=value2" | ||
|
|
||
| describe "before_dispatch/1" do | ||
| test "captures traceparent when span context is active" do | ||
| set_trace_context(@traceparent) | ||
|
|
||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.metadata == %{"traceparent" => @traceparent} | ||
| end | ||
|
|
||
| test "does not modify metadata when no span is active" do | ||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.metadata == %{} | ||
| end | ||
|
|
||
| test "preserves existing metadata" do | ||
| set_trace_context(@traceparent) | ||
|
|
||
| pipeline = %Pipeline{ | ||
| command: %Fail{}, | ||
| metadata: %{"user_id" => "123", "tenant" => "acme"} | ||
| } | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.metadata == %{ | ||
| "user_id" => "123", | ||
| "tenant" => "acme", | ||
| "traceparent" => @traceparent | ||
| } | ||
| end | ||
|
|
||
| test "does not set tracestate when span has no tracestate" do | ||
| set_trace_context(@traceparent) | ||
|
|
||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.metadata == %{"traceparent" => @traceparent} | ||
| end | ||
|
|
||
| test "captures tracestate when present in span context" do | ||
| set_trace_context(@traceparent, @tracestate) | ||
|
|
||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.metadata == %{ | ||
| "traceparent" => @traceparent, | ||
| "tracestate" => @tracestate | ||
| } | ||
| end | ||
|
|
||
| test "child span inherits trace_id and tracestate from parent" do | ||
| set_trace_context(@traceparent, "vendor=parentvalue") | ||
|
|
||
| Tracer.with_span "child.span" do | ||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| # Extract child's span_id (dynamic) | ||
| "00-" <> @trace_id <> "-" <> rest = result.metadata["traceparent"] | ||
| [child_span_id, "01"] = String.split(rest, "-") | ||
|
|
||
| assert result.metadata == %{ | ||
| "traceparent" => "00-#{@trace_id}-#{child_span_id}-01", | ||
| "tracestate" => "vendor=parentvalue" | ||
| } | ||
|
|
||
| # Span ID should be different from parent's | ||
| refute child_span_id == @span_id | ||
| end | ||
| end | ||
|
|
||
| test "does not modify pipeline command" do | ||
| set_trace_context(@traceparent) | ||
|
|
||
| command = %Fail{} | ||
| pipeline = %Pipeline{command: command, metadata: %{}} | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.command == command | ||
| assert result.metadata == %{"traceparent" => @traceparent} | ||
| end | ||
|
|
||
| test "does not modify pipeline assigns" do | ||
| set_trace_context(@traceparent) | ||
|
|
||
| pipeline = %Pipeline{ | ||
| command: %Fail{}, | ||
| metadata: %{}, | ||
| assigns: %{existing: "value"} | ||
| } | ||
|
|
||
| result = TraceContextPropagator.before_dispatch(pipeline) | ||
|
|
||
| assert result.assigns == %{existing: "value"} | ||
| assert result.metadata == %{"traceparent" => @traceparent} | ||
| end | ||
| end | ||
|
|
||
| describe "after_dispatch/1" do | ||
| test "returns pipeline unchanged" do | ||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{"test" => "value"}} | ||
|
|
||
| assert TraceContextPropagator.after_dispatch(pipeline) == pipeline | ||
| end | ||
| end | ||
|
|
||
| describe "after_failure/1" do | ||
| test "returns pipeline unchanged" do | ||
| pipeline = %Pipeline{command: %Fail{}, metadata: %{"test" => "value"}} | ||
|
|
||
| assert TraceContextPropagator.after_failure(pipeline) == pipeline | ||
| end | ||
| end | ||
|
|
||
| defp set_trace_context(traceparent) when is_binary(traceparent) do | ||
| :otel_propagator_text_map.extract([{"traceparent", traceparent}]) | ||
| end | ||
|
|
||
| defp set_trace_context(traceparent, tracestate) | ||
| when is_binary(traceparent) and is_binary(tracestate) do | ||
| :otel_propagator_text_map.extract([ | ||
| {"traceparent", traceparent}, | ||
| {"tracestate", tracestate} | ||
| ]) | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.