Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions lib/commanded/opentelemetry/event_store.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Commanded.OpenTelemetry.EventStore do
@moduledoc false

alias Commanded.Application, as: CommandedApplication
alias Commanded.OpenTelemetry.CommandedAttributes
alias Commanded.OpenTelemetry.Helpers
alias OpenTelemetry.SemConv.ErrorAttributes
Expand Down Expand Up @@ -49,6 +50,7 @@ defmodule Commanded.OpenTelemetry.EventStore do
) do
operation_type = operation_type_for(action)
action_name = to_string(action)
destination_name = event_store_destination_name(meta)
source_uuid = extract_source_uuid(meta)

attributes =
Expand All @@ -59,18 +61,15 @@ defmodule Commanded.OpenTelemetry.EventStore do
{CommandedAttributes.commanded_application(), meta[:application]}
]
|> maybe_add_operation_type(operation_type)
|> maybe_add_destination_name(destination_name)
|> maybe_add_stream_uuid(meta[:stream_uuid])
|> maybe_add_expected_version(meta[:expected_version])
|> maybe_add_subscription_name(meta[:subscription_name])
|> maybe_add_source_uuid(source_uuid)
|> maybe_add_start_from(meta[:start_from])

# Use application as destination to maintain consistency with other Commanded modules
# and provide useful grouping in multi-application deployments.
application_name = Helpers.module_name(meta[:application])

span_name =
case application_name do
case destination_name do
nil -> action_name
name -> "#{action_name} #{name}"
end
Expand Down Expand Up @@ -153,6 +152,11 @@ defmodule Commanded.OpenTelemetry.EventStore do
defp maybe_add_operation_type(attrs, type),
do: [{MessagingAttributes.messaging_operation_type(), type} | attrs]

defp maybe_add_destination_name(attrs, nil), do: attrs

defp maybe_add_destination_name(attrs, destination_name),
do: [{MessagingAttributes.messaging_destination_name(), destination_name} | attrs]

defp maybe_add_stream_uuid(attrs, nil), do: attrs

defp maybe_add_stream_uuid(attrs, stream_uuid),
Expand Down Expand Up @@ -188,6 +192,48 @@ defmodule Commanded.OpenTelemetry.EventStore do
defp extract_source_uuid(%{snapshot: %{source_uuid: uuid}}) when is_binary(uuid), do: uuid
defp extract_source_uuid(_), do: nil

defp event_store_destination_name(meta) do
meta[:application]
|> lookup_event_store_name()
|> to_destination_name()
end

defp lookup_event_store_name(application) do
application
|> fetch_event_store_adapter_meta()
|> event_store_name()
end

defp fetch_event_store_adapter_meta(application) do
{_adapter, adapter_meta} = CommandedApplication.event_store_adapter(application)
adapter_meta
rescue
error ->
:telemetry.execute(
[:commanded, :opentelemetry, :warning],
%{count: 1},
%{
message:
"Failed to resolve event store adapter metadata, leaving event store destination unset",
application: application,
error: error,
tracer_id: @tracer_id
}
)

nil
end
Comment thread
yordis marked this conversation as resolved.

defp event_store_name(adapter_meta) when is_map(adapter_meta),
do: Map.get(adapter_meta, :name) || Map.get(adapter_meta, :event_store)

defp event_store_name(_), do: nil

defp to_destination_name(nil), do: nil
defp to_destination_name(name) when is_binary(name), do: name
defp to_destination_name(name) when is_atom(name), do: inspect(name)
defp to_destination_name(_), do: nil

defp to_start_from_attr(nil), do: nil
defp to_start_from_attr(atom) when is_atom(atom), do: to_string(atom)
defp to_start_from_attr(other), do: other
Expand Down
Loading