-
Notifications
You must be signed in to change notification settings - Fork 9
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
Added OTLP Attributes and testing #10
Changes from 7 commits
ec34633
13dbe73
50366f0
64ecf79
41f09e5
a0b759d
4a3aafb
87115bf
89a7035
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,48 +3,111 @@ defmodule OpentelemetryCommanded.EventStore do | |
|
||
require OpenTelemetry.Tracer | ||
|
||
alias OpenTelemetry.{Tracer, Span} | ||
import OpentelemetryCommanded.Util | ||
|
||
alias OpenTelemetry.Span | ||
|
||
@tracer_id __MODULE__ | ||
|
||
def setup do | ||
:telemetry.attach_many( | ||
{__MODULE__, :stop}, | ||
[ | ||
[:commanded, :event_store, :stream_forward, :stop], | ||
[:commanded, :event_store, :append_to_stream, :stop] | ||
], | ||
&__MODULE__.handle_stop/4, | ||
[] | ||
) | ||
~w( | ||
ack_event | ||
adapter | ||
append_to_stream | ||
delete_snapshot | ||
delete_subscription | ||
read_snapshot | ||
record_snapshot | ||
stream_forward | ||
stream_forward | ||
stream_forward | ||
subscribe | ||
subscribe_to | ||
subscribe_to | ||
unsubscribe | ||
)a | ||
|> Enum.each(fn event -> | ||
:telemetry.attach( | ||
{__MODULE__, :start}, | ||
[:commanded, :event_store, event, :start], | ||
&__MODULE__.handle_start/4, | ||
[] | ||
) | ||
|
||
:telemetry.attach_many( | ||
{__MODULE__, :exception}, | ||
[ | ||
[:commanded, :event_store, :stream_forward, :exception], | ||
[:commanded, :event_store, :append_to_stream, :exception] | ||
], | ||
&__MODULE__.handle_stop/4, | ||
[] | ||
) | ||
:telemetry.attach( | ||
{__MODULE__, :stop}, | ||
[:commanded, :event_store, event, :stop], | ||
&__MODULE__.handle_stop/4, | ||
[] | ||
) | ||
|
||
:telemetry.attach( | ||
{__MODULE__, :exception}, | ||
[:commanded, :event_store, event, :exception], | ||
&__MODULE__.handle_exception/4, | ||
[] | ||
) | ||
end) | ||
end | ||
|
||
def handle_stop([_, _, action, type], measurements, meta, _) do | ||
end_time = :opentelemetry.timestamp() | ||
start_time = end_time - measurements.duration | ||
attributes = meta |> Map.take([:application, :stream_uuid]) |> Enum.to_list() | ||
span_name = :"commanded.event_store.#{action}" | ||
def handle_start([_, _, action, _type], _measurements, meta, _) do | ||
event = meta.event | ||
|
||
safe_context_propagation(event.metadata["trace_ctx"]) | ||
|
||
attributes = [ | ||
"messaging.system": "commanded", | ||
"messaging.protocol": "cqrs", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"messaging.destination_kind": "event_store", | ||
# TODO does this need to change based on the event? Or maybe `internal`? | ||
"messaging.operation": "receive", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bryannaegele This doesn't seem to fit well. It seems an "internal" span w/i the CQRS messaging system. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know where CQRS is a bit of a hybrid between Messaging and RPC, so I'm not sure of all the specifics of how it should be treated. You can always solicit opinions in the |
||
"messaging.message_id": event.causation_id, | ||
"messaging.conversation_id": event.correlation_id, | ||
# "messaging.destination": meta.handler_module, | ||
"messaging.commanded.application": meta.application, | ||
"messaging.commanded.event": event.event_type, | ||
"messaging.commanded.event_id": event.event_id, | ||
"messaging.commanded.event_number": event.event_number, | ||
"messaging.commanded.stream_id": event.stream_id, | ||
"messaging.commanded.stream_version": event.stream_version | ||
] | ||
|
||
Tracer.start_span(span_name, %{start_time: start_time, attributes: attributes}) | ||
OpentelemetryTelemetry.start_telemetry_span( | ||
@tracer_id, | ||
"commanded.event_store.#{action}", | ||
meta, | ||
%{ | ||
kind: :consumer, | ||
attributes: attributes | ||
} | ||
) | ||
end | ||
|
||
if type == :exception do | ||
ctx = Tracer.current_span_ctx() | ||
reason = meta[:reason] | ||
stacktrace = meta[:stacktrace] | ||
def handle_stop(_event, _measurements, meta, _) do | ||
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
||
exception = Exception.normalize(meta[:kind], reason, stacktrace) | ||
Span.record_exception(ctx, exception, stacktrace) | ||
Span.set_status(ctx, OpenTelemetry.status(:error, "")) | ||
if error = meta[:error] do | ||
Span.set_status(ctx, OpenTelemetry.status(:error, inspect(error))) | ||
end | ||
|
||
Tracer.end_span() | ||
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
end | ||
|
||
def handle_exception( | ||
_event, | ||
_measurements, | ||
%{kind: kind, reason: reason, stacktrace: stacktrace} = meta, | ||
_config | ||
) do | ||
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
||
# try to normalize all errors to Elixir exceptions | ||
exception = Exception.normalize(kind, reason, stacktrace) | ||
|
||
# record exception and mark the span as errored | ||
Span.record_exception(ctx, exception, stacktrace) | ||
Span.set_status(ctx, OpenTelemetry.status(:error, inspect(reason))) | ||
|
||
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,22 +33,29 @@ defmodule OpentelemetryCommanded.ProcessManager do | |
end | ||
|
||
def handle_start(_event, _, meta, _) do | ||
event = meta.recorded_event | ||
trace_headers = decode_headers(event.metadata["trace_ctx"]) | ||
:otel_propagator_text_map.extract(trace_headers) | ||
recorded_event = meta.recorded_event | ||
safe_context_propagation(recorded_event.metadata["trace_ctx"]) | ||
|
||
attributes = [ | ||
application: meta.application, | ||
"process_manager.uuid": meta.process_uuid, | ||
"process_manager.name": meta.process_manager_name, | ||
"process_manager.module": meta.process_manager_module, | ||
"event.id": event.event_id, | ||
"event.number": event.event_number, | ||
"event.type": event.event_type, | ||
"correlation.id": event.correlation_id, | ||
"causation.id": event.causation_id, | ||
"stream.id": event.stream_id, | ||
"stream.version": event.stream_version | ||
"messaging.system": "commanded", | ||
"messaging.protocol": "cqrs", | ||
"messaging.destination_kind": "process_manager", | ||
"messaging.operation": "receive", | ||
"messaging.message_id": recorded_event.causation_id, | ||
"messaging.conversation_id": recorded_event.correlation_id, | ||
"messaging.destination": meta.process_manager_module, | ||
"messaging.commanded.application": meta.application, | ||
"messaging.commanded.event": recorded_event.event_type, | ||
"messaging.commanded.event_id": recorded_event.event_id, | ||
"messaging.commanded.event_number": recorded_event.event_number, | ||
"messaging.commanded.process_uuid": meta.process_uuid, | ||
"messaging.commanded.stream_id": recorded_event.stream_id, | ||
"messaging.commanded.stream_version": recorded_event.stream_version, | ||
"messaging.commanded.handler_name": meta.process_manager_name | ||
# TODO add back | ||
# consistency: meta.consistency, | ||
# TODO add this back into commanded | ||
# "event.last_seen": meta.last_seen_event | ||
Comment on lines
+56
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left @davydog187 's comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bryannaegele e.g.
should be
|
||
] | ||
|
||
OpentelemetryTelemetry.start_telemetry_span( | ||
|
@@ -67,7 +74,11 @@ defmodule OpentelemetryCommanded.ProcessManager do | |
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) | ||
|
||
commands = Map.get(meta, :commands, []) | ||
Span.set_attribute(ctx, :"command.count", Enum.count(commands)) | ||
Span.set_attribute(ctx, :"messaging.commanded.command_count", Enum.count(commands)) | ||
|
||
if error = meta[:error] do | ||
Span.set_status(ctx, OpenTelemetry.status(:error, inspect(error))) | ||
end | ||
|
||
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
end | ||
|
@@ -85,7 +96,7 @@ defmodule OpentelemetryCommanded.ProcessManager do | |
|
||
# record exception and mark the span as errored | ||
Span.record_exception(ctx, exception, stacktrace) | ||
Span.set_status(ctx, OpenTelemetry.status(:error, "")) | ||
Span.set_status(ctx, OpenTelemetry.status(:error, inspect(reason))) | ||
|
||
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still want these two fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aggregate.function": context.function
is still there, just massaged to fit naming for messaging attributes:I can restore
context.lifespan
but while testing, didn't see it's value as an attribute since it doesn't effect the processing of the current span. Thoughts?