Repository navigation
feat: implement OpenTelemetry instrumentation for application #46
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
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,125 @@ | ||
| defmodule Commanded.OpenTelemetry.Application do | ||
| @moduledoc false | ||
|
|
||
| alias Commanded.OpenTelemetry.CommandedAttributes | ||
| alias Commanded.OpenTelemetry.Helpers | ||
| alias OpenTelemetry.SemConv.ErrorAttributes | ||
| alias OpenTelemetry.SemConv.Incubating.CodeAttributes | ||
| alias OpenTelemetry.SemConv.Incubating.MessagingAttributes | ||
| alias OpenTelemetry.Span | ||
|
|
||
| @tracer_id __MODULE__ | ||
|
|
||
| def setup do | ||
| :ok = | ||
| :telemetry.attach_many( | ||
| {__MODULE__, :dispatch}, | ||
| [ | ||
| [:commanded, :application, :dispatch, :start], | ||
| [:commanded, :application, :dispatch, :stop], | ||
| [:commanded, :application, :dispatch, :exception] | ||
| ], | ||
| &__MODULE__.handle_telemetry_event/4, | ||
| %{} | ||
| ) | ||
| end | ||
|
|
||
| def handle_telemetry_event( | ||
| [:commanded, :application, :dispatch, :start], | ||
| _measurements, | ||
| meta, | ||
| _config | ||
| ) do | ||
| context = meta.execution_context | ||
|
|
||
| # Attach trace context from command metadata if present. | ||
| # The TraceContextPropagator middleware injects W3C traceparent/tracestate headers | ||
| # into metadata, allowing explicit context propagation when needed. | ||
| Helpers.attach_ctx(context.metadata) | ||
|
|
||
| handler_module_name = Helpers.module_name(context.handler) | ||
|
|
||
| attributes = [ | ||
| # OTel Messaging SemConv | ||
| {MessagingAttributes.messaging_system(), "commanded"}, | ||
| {MessagingAttributes.messaging_operation_type(), :receive}, | ||
| {MessagingAttributes.messaging_operation_name(), "dispatch"}, | ||
| {MessagingAttributes.messaging_destination_name(), handler_module_name}, | ||
| {MessagingAttributes.messaging_message_id(), context.causation_id}, | ||
| {MessagingAttributes.messaging_message_conversation_id(), context.correlation_id}, | ||
| # OTel Code SemConv | ||
| {CodeAttributes.code_function(), to_string(context.function)}, | ||
| {CodeAttributes.code_namespace(), handler_module_name}, | ||
| # Commanded-specific | ||
| {CommandedAttributes.commanded_handler_kind(), "command_handler"}, | ||
| {CommandedAttributes.commanded_application(), meta.application}, | ||
| {CommandedAttributes.commanded_command(), Helpers.struct_name(context.command)}, | ||
| {CommandedAttributes.commanded_correlation_id(), context.correlation_id}, | ||
| {CommandedAttributes.commanded_causation_id(), context.causation_id} | ||
| ] | ||
|
|
||
| # OTel semconv: span name = "{operation.name} {destination.name}" | ||
| span_name = "dispatch #{handler_module_name}" | ||
|
|
||
| OpentelemetryTelemetry.start_telemetry_span( | ||
| @tracer_id, | ||
| span_name, | ||
| meta, | ||
| %{ | ||
| kind: :consumer, | ||
| attributes: attributes | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| def handle_telemetry_event( | ||
| [:commanded, :application, :dispatch, :stop], | ||
| _measurements, | ||
| meta, | ||
| _config | ||
| ) do | ||
| ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
|
||
| if error = meta[:error] do | ||
| Span.set_attribute( | ||
| ctx, | ||
| ErrorAttributes.error_type(), | ||
| Helpers.to_error_type(error, @tracer_id) | ||
| ) | ||
|
|
||
| Span.set_status(ctx, OpenTelemetry.status(:error, Helpers.format_error(error))) | ||
| end | ||
|
|
||
| OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
| end | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| def handle_telemetry_event( | ||
| [:commanded, :application, :dispatch, :exception], | ||
| _measurements, | ||
| %{kind: kind, reason: reason, stacktrace: stacktrace} = meta, | ||
| _config | ||
| ) do | ||
| ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
|
||
| # TODO: follow up with OTEL team to add exception.kind SemConv attribute | ||
| Span.set_attribute(ctx, :"erlang.exception.kind", kind) | ||
|
|
||
| # Normalize all errors to Elixir exceptions | ||
| exception = Exception.normalize(kind, reason, stacktrace) | ||
|
|
||
| Span.set_attribute( | ||
| ctx, | ||
| ErrorAttributes.error_type(), | ||
| Helpers.to_error_type(exception, @tracer_id) | ||
| ) | ||
|
|
||
| Span.record_exception(ctx, exception, stacktrace) | ||
|
|
||
| Span.set_status( | ||
| ctx, | ||
| OpenTelemetry.status(:error, Exception.format_banner(kind, reason, stacktrace)) | ||
| ) | ||
|
|
||
| OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
| 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
Oops, something went wrong.
Oops, something went wrong.
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.