-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b9491d
commit 350dc46
Showing
5 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
64 changes: 64 additions & 0 deletions
64
instrumentation/opentelemetry_oban/lib/opentelemetry_oban/internal_handler.ex
This file contains 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,64 @@ | ||
defmodule OpentelemetryOban.InternalHandler do | ||
alias OpenTelemetry.Span | ||
alias OpenTelemetry.SemanticConventions.Trace | ||
|
||
require Trace | ||
|
||
@tracer_id __MODULE__ | ||
|
||
def attach() do | ||
:telemetry.attach_many( | ||
{__MODULE__, :internal}, | ||
Enum.flat_map( | ||
[ | ||
[:engine, :init], | ||
[:engine, :refresh], | ||
[:engine, :put_meta], | ||
[:engine, :check_available], | ||
[:engine, :cancel_all_jobs], | ||
[:engine, :fetch_jobs], | ||
[:engine, :insert_all_jobs], | ||
[:engine, :prune_all_jobs], | ||
[:engine, :stage_jobs], | ||
[:engine, :cancel_job], | ||
[:engine, :complete_job], | ||
[:engine, :discard_job], | ||
[:engine, :error_job], | ||
[:engine, :insert_job], | ||
[:engine, :snooze_job], | ||
[:notifier, :notify], | ||
[:peer, :election] | ||
], | ||
fn event -> | ||
[ | ||
[:oban | event ++ [:start]], | ||
[:oban | event ++ [:stop]], | ||
[:oban | event ++ [:exception]] | ||
] | ||
end | ||
), | ||
&__MODULE__.handle_oban_event/4, | ||
[] | ||
) | ||
end | ||
|
||
def handle_oban_event(event, _measurements, metadata, _config) do | ||
[op | rest] = Enum.reverse(event) | ||
|
||
case op do | ||
:start -> | ||
OpentelemetryTelemetry.start_telemetry_span(__MODULE__, Enum.join(Enum.reverse(rest), "."), metadata, %{kind: :consumer}) | ||
|
||
:stop -> | ||
OpentelemetryTelemetry.end_telemetry_span(__MODULE__, metadata) | ||
|
||
:exception -> | ||
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, metadata) | ||
|
||
Span.record_exception(ctx, metadata.reason, metadata.stacktrace) | ||
Span.set_status(ctx, OpenTelemetry.status(:error, Exception.message(metadata.reason))) | ||
|
||
OpentelemetryTelemetry.end_telemetry_span(__MODULE__, metadata) | ||
end | ||
end | ||
end |
This file contains 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 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
137 changes: 137 additions & 0 deletions
137
instrumentation/opentelemetry_oban/test/opentelemetry_oban/internal_handler_test.exs
This file contains 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,137 @@ | ||
defmodule OpentelemetryOban.InternalHandlerTest do | ||
use DataCase | ||
|
||
require OpenTelemetry.Tracer | ||
require OpenTelemetry.Span | ||
require Record | ||
|
||
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do | ||
Record.defrecord(name, spec) | ||
end | ||
|
||
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry_api/include/opentelemetry.hrl") do | ||
Record.defrecord(name, spec) | ||
end | ||
|
||
@events [ | ||
[:engine, :init], | ||
[:engine, :refresh], | ||
[:engine, :put_meta], | ||
[:engine, :check_available], | ||
[:engine, :cancel_all_jobs], | ||
[:engine, :fetch_jobs], | ||
[:engine, :insert_all_jobs], | ||
[:engine, :prune_all_jobs], | ||
[:engine, :stage_jobs], | ||
[:engine, :cancel_job], | ||
[:engine, :complete_job], | ||
[:engine, :discard_job], | ||
[:engine, :error_job], | ||
[:engine, :insert_job], | ||
[:engine, :snooze_job], | ||
[:notifier, :notify], | ||
[:peer, :election] | ||
] | ||
|
||
setup do | ||
:application.stop(:opentelemetry) | ||
:application.set_env(:opentelemetry, :tracer, :otel_tracer_default) | ||
|
||
:application.set_env(:opentelemetry, :processors, [ | ||
{:otel_batch_processor, %{scheduled_delay_ms: 1, exporter: {:otel_exporter_pid, self()}}} | ||
]) | ||
|
||
:application.start(:opentelemetry) | ||
|
||
TestHelpers.remove_oban_handlers() | ||
OpentelemetryOban.setup(trace: [:internal]) | ||
|
||
:ok | ||
end | ||
|
||
test "does not create spans when internal tracing is disabled" do | ||
TestHelpers.remove_oban_handlers() | ||
OpentelemetryOban.setup(trace: []) | ||
|
||
execute_internal_event([:peer, :election]) | ||
|
||
refute_receive {:span, span(name: "oban.peer.election")} | ||
end | ||
|
||
test "records span on internal execution" do | ||
execute_internal_event([:peer, :election]) | ||
|
||
assert_receive {:span, span(name: "oban.peer.election")} | ||
end | ||
|
||
test "records span on error" do | ||
:telemetry.execute( | ||
[:oban, :peer, :election, :start], | ||
%{system_time: System.system_time()}, | ||
%{} | ||
) | ||
|
||
exception = %UndefinedFunctionError{ | ||
arity: 0, | ||
function: :error, | ||
message: nil, | ||
module: Some, | ||
reason: nil | ||
} | ||
|
||
:telemetry.execute( | ||
[:oban, :peer, :election, :exception], | ||
%{duration: 444}, | ||
%{ | ||
kind: :error, | ||
stacktrace: [ | ||
{Some, :error, [], []} | ||
], | ||
reason: exception | ||
} | ||
) | ||
|
||
expected_status = OpenTelemetry.status(:error, Exception.message(exception)) | ||
|
||
assert_receive {:span, | ||
span( | ||
name: "oban.peer.election", | ||
events: events, | ||
status: ^expected_status | ||
)} | ||
|
||
[ | ||
event( | ||
name: :exception, | ||
attributes: event_attributes | ||
) | ||
] = :otel_events.list(events) | ||
|
||
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] == | ||
Enum.sort(Map.keys(:otel_attributes.map(event_attributes))) | ||
end | ||
|
||
for event <- @events do | ||
test "#{inspect([:oban | event])} spans" do | ||
execute_internal_event(unquote(event)) | ||
|
||
assert_receive {:span, span(name: "oban.#{unquote(Enum.join(event, "."))}")} | ||
|
||
:ok | ||
end | ||
end | ||
|
||
defp execute_internal_event(event) do | ||
:telemetry.execute( | ||
[:oban | event ++ [:start]], | ||
%{system_time: System.system_time()}, | ||
%{} | ||
) | ||
|
||
:telemetry.execute( | ||
[:oban | event ++ [:stop]], | ||
%{duration: 42069}, | ||
%{} | ||
) | ||
end | ||
end |