Repository navigation
feat: Add OpenTelemetry instrumentation for aggregate populate #47
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,73 @@ | ||
| defmodule Commanded.OpenTelemetry.AggregatePopulate do | ||
| @moduledoc false | ||
|
|
||
| alias Commanded.OpenTelemetry.CommandedAttributes | ||
| alias OpenTelemetry.SemConv.Incubating.CodeAttributes | ||
| alias OpenTelemetry.SemConv.Incubating.MessagingAttributes | ||
| alias OpenTelemetry.Span | ||
|
|
||
| @tracer_id __MODULE__ | ||
|
|
||
| def setup do | ||
| :ok = | ||
| :telemetry.attach_many( | ||
| {__MODULE__, :populate}, | ||
| [ | ||
| [:commanded, :aggregate, :populate, :start], | ||
| [:commanded, :aggregate, :populate, :stop] | ||
| ], | ||
| &__MODULE__.handle_telemetry_event/4, | ||
| %{} | ||
| ) | ||
| end | ||
|
|
||
| def handle_telemetry_event( | ||
| [:commanded, :aggregate, :populate, :start], | ||
| _measurements, | ||
| meta, | ||
| _config | ||
| ) do | ||
| attributes = [ | ||
| # OTel Messaging SemConv | ||
| {MessagingAttributes.messaging_system(), "commanded"}, | ||
| {MessagingAttributes.messaging_operation_type(), :receive}, | ||
| {MessagingAttributes.messaging_operation_name(), "populate"}, | ||
| # OTel Code SemConv | ||
| {CodeAttributes.code_function(), "populate"}, | ||
| # Commanded-specific | ||
| {CommandedAttributes.commanded_application(), meta.application}, | ||
| {CommandedAttributes.commanded_aggregate_uuid(), meta.aggregate_uuid}, | ||
| {CommandedAttributes.commanded_aggregate_version(), meta.aggregate_version} | ||
| ] | ||
|
|
||
| OpentelemetryTelemetry.start_telemetry_span( | ||
| @tracer_id, | ||
| "commanded.aggregate.populate", | ||
| meta, | ||
| %{ | ||
| kind: :internal, | ||
| attributes: attributes | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| def handle_telemetry_event( | ||
| [:commanded, :aggregate, :populate, :stop], | ||
| measurements, | ||
| meta, | ||
| _config | ||
| ) do | ||
| ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
|
||
| event_count = Map.get(measurements, :count, 0) | ||
| Span.set_attribute(ctx, CommandedAttributes.commanded_event_count(), event_count) | ||
|
|
||
| Span.set_attribute( | ||
| ctx, | ||
| CommandedAttributes.commanded_aggregate_version(), | ||
| meta.aggregate_version | ||
| ) | ||
|
|
||
| OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
| end | ||
|
yordis marked this conversation as resolved.
Resolved
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| defmodule Commanded.OpenTelemetry.AggregatePopulateTest do | ||
| @moduledoc """ | ||
| Tests for AggregatePopulate OpenTelemetry instrumentation. | ||
|
|
||
| Follows the same patterns as AggregateTest and EventHandlerTest. | ||
| """ | ||
|
|
||
| use Commanded.OpenTelemetryCase, async: false | ||
|
|
||
| alias Commanded.OpenTelemetry.AggregatePopulate | ||
| alias Commanded.TestSupport.Factory | ||
| alias Commanded.UUID | ||
|
|
||
| setup do | ||
| detach_populate_handlers() | ||
| AggregatePopulate.setup() | ||
|
|
||
| :ok | ||
| end | ||
|
|
||
| describe "setup/0" do | ||
| test "attaches telemetry handlers for aggregate populate events" do | ||
| detach_populate_handlers() | ||
|
|
||
| AggregatePopulate.setup() | ||
|
|
||
| for event <- [ | ||
| [:commanded, :aggregate, :populate, :start], | ||
| [:commanded, :aggregate, :populate, :stop] | ||
| ] do | ||
| handlers = :telemetry.list_handlers(event) | ||
|
|
||
| assert Enum.any?( | ||
| handlers, | ||
| &match?(%{id: {AggregatePopulate, :populate}}, &1) | ||
| ), | ||
| "Expected handler for event #{inspect(event)}" | ||
| end | ||
| end | ||
|
|
||
| test "calling setup twice raises MatchError (fail fast)" do | ||
| detach_populate_handlers() | ||
|
|
||
| :ok = AggregatePopulate.setup() | ||
|
|
||
| handlers = :telemetry.list_handlers([:commanded, :aggregate, :populate, :start]) | ||
| assert length(handlers) == 1 | ||
|
|
||
| assert_raise MatchError, fn -> | ||
| AggregatePopulate.setup() | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "attribute completeness" do | ||
| setup do | ||
| detach_populate_handlers() | ||
| AggregatePopulate.setup() | ||
| :ok | ||
| end | ||
|
|
||
| test "includes ALL required span attributes" do | ||
| aggregate_uuid = UUID.uuid4() | ||
|
|
||
| meta = | ||
| Factory.build_aggregate_populate_metadata( | ||
| aggregate_uuid: aggregate_uuid, | ||
| aggregate_version: 5 | ||
| ) | ||
|
|
||
| :telemetry.span([:commanded, :aggregate, :populate], meta, fn -> | ||
| {:ok, meta} | ||
| end) | ||
|
|
||
| assert_receive {:span, | ||
| span( | ||
| name: "commanded.aggregate.populate", | ||
| kind: :internal, | ||
| attributes: attributes | ||
| )}, | ||
| 1000 | ||
|
|
||
| attrs = :otel_attributes.map(attributes) | ||
|
|
||
| assert attrs == %{ | ||
| "messaging.system": "commanded", | ||
| "messaging.operation.type": :receive, | ||
| "messaging.operation.name": "populate", | ||
| "code.function": "populate", | ||
| "commanded.application": MockApp, | ||
| "commanded.aggregate.uuid": aggregate_uuid, | ||
| "commanded.aggregate.version": 5, | ||
| "commanded.event.count": 0 | ||
| } | ||
| end | ||
| end | ||
|
|
||
| describe "edge cases" do | ||
| setup do | ||
| detach_populate_handlers() | ||
| AggregatePopulate.setup() | ||
| :ok | ||
| end | ||
|
|
||
| test "handles zero events populated" do | ||
| aggregate_uuid = UUID.uuid4() | ||
|
|
||
| meta = | ||
| Factory.build_aggregate_populate_metadata( | ||
| aggregate_uuid: aggregate_uuid, | ||
| aggregate_version: 0 | ||
| ) | ||
|
|
||
| :telemetry.execute([:commanded, :aggregate, :populate, :start], %{}, meta) | ||
| :telemetry.execute([:commanded, :aggregate, :populate, :stop], %{count: 0}, meta) | ||
|
|
||
| assert_receive {:span, | ||
| span( | ||
| name: "commanded.aggregate.populate", | ||
| kind: :internal, | ||
| attributes: attributes | ||
| )}, | ||
| 1000 | ||
|
|
||
| assert :otel_attributes.map(attributes) == %{ | ||
| "messaging.system": "commanded", | ||
| "messaging.operation.type": :receive, | ||
| "messaging.operation.name": "populate", | ||
| "code.function": "populate", | ||
| "commanded.application": MockApp, | ||
| "commanded.aggregate.uuid": aggregate_uuid, | ||
| "commanded.aggregate.version": 0, | ||
| "commanded.event.count": 0 | ||
| } | ||
| end | ||
|
|
||
| test "handles multiple events populated" do | ||
| aggregate_uuid = UUID.uuid4() | ||
|
|
||
| meta = | ||
| Factory.build_aggregate_populate_metadata( | ||
| aggregate_uuid: aggregate_uuid, | ||
| aggregate_version: 0 | ||
| ) | ||
|
|
||
| :telemetry.execute([:commanded, :aggregate, :populate, :start], %{}, meta) | ||
|
|
||
| stop_meta = Map.put(meta, :aggregate_version, 10) | ||
| :telemetry.execute([:commanded, :aggregate, :populate, :stop], %{count: 10}, stop_meta) | ||
|
|
||
| assert_receive {:span, | ||
| span( | ||
| name: "commanded.aggregate.populate", | ||
| kind: :internal, | ||
| attributes: attributes | ||
| )}, | ||
| 1000 | ||
|
|
||
| assert :otel_attributes.map(attributes) == %{ | ||
| "messaging.system": "commanded", | ||
| "messaging.operation.type": :receive, | ||
| "messaging.operation.name": "populate", | ||
| "code.function": "populate", | ||
| "commanded.application": MockApp, | ||
| "commanded.aggregate.uuid": aggregate_uuid, | ||
| "commanded.aggregate.version": 10, | ||
| "commanded.event.count": 10 | ||
| } | ||
| end | ||
| end | ||
|
|
||
| defp detach_populate_handlers do | ||
| for event <- [ | ||
| [:commanded, :aggregate, :populate, :start], | ||
| [:commanded, :aggregate, :populate, :stop] | ||
| ] do | ||
| for handler <- :telemetry.list_handlers(event) do | ||
| :telemetry.detach(handler.id) | ||
| end | ||
| end | ||
| 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
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.