Skip to content

Commit 108029d

Browse files
committed
Revert "make configurable"
This reverts commit ea864d1.
1 parent ea864d1 commit 108029d

File tree

5 files changed

+382
-465
lines changed

5 files changed

+382
-465
lines changed

instrumentation/opentelemetry_oban/lib/opentelemetry_oban.ex

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,10 @@ defmodule OpentelemetryOban do
2626
@doc """
2727
Initializes and configures telemetry handlers.
2828
29-
Example:
29+
By default jobs and plugins are traced. If you wish to trace only jobs then
30+
use:
3031
31-
OpentelemetryOban.setup(trace: [:jobs])
32-
33-
Options:
34-
35-
* `:trace` - A list of events to trace. Supported values are `:jobs` and `:plugins`,
36-
defaults to [:jobs, :plugins].
37-
* `:time_unit` - a time unit used to convert the timing values, defaults
38-
to `:microsecond`.
32+
OpentelemetryOban.setup(trace: [:jobs])
3933
4034
Note that if you don't trace plugins, but inside the plugins, there are spans
4135
from other instrumentation libraries (e.g. ecto) then these will still be
@@ -45,15 +39,13 @@ defmodule OpentelemetryOban do
4539
@spec setup() :: :ok
4640
def setup(opts \\ []) do
4741
trace = Keyword.get(opts, :trace, [:jobs, :plugins])
48-
time_unit = Keyword.get(opts, :time_unit, :microsecond)
49-
config = %{time_unit: time_unit}
5042

5143
if Enum.member?(trace, :jobs) do
52-
OpentelemetryOban.JobHandler.attach(config)
44+
OpentelemetryOban.JobHandler.attach()
5345
end
5446

5547
if Enum.member?(trace, :plugins) do
56-
OpentelemetryOban.PluginHandler.attach(config)
48+
OpentelemetryOban.PluginHandler.attach()
5749
end
5850

5951
:ok

instrumentation/opentelemetry_oban/lib/opentelemetry_oban/job_handler.ex

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ defmodule OpentelemetryOban.JobHandler do
66

77
@tracer_id __MODULE__
88

9-
def attach(config) do
9+
def attach() do
1010
attach_job_start_handler()
11-
attach_job_stop_handler(config)
12-
attach_job_exception_handler(config)
11+
attach_job_stop_handler()
12+
attach_job_exception_handler()
1313
end
1414

1515
defp attach_job_start_handler() do
@@ -21,21 +21,21 @@ defmodule OpentelemetryOban.JobHandler do
2121
)
2222
end
2323

24-
defp attach_job_stop_handler(config) do
24+
defp attach_job_stop_handler() do
2525
:telemetry.attach(
2626
"#{__MODULE__}.job_stop",
2727
[:oban, :job, :stop],
2828
&__MODULE__.handle_job_stop/4,
29-
config
29+
[]
3030
)
3131
end
3232

33-
defp attach_job_exception_handler(config) do
33+
defp attach_job_exception_handler() do
3434
:telemetry.attach(
3535
"#{__MODULE__}.job_exception",
3636
[:oban, :job, :exception],
3737
&__MODULE__.handle_job_exception/4,
38-
config
38+
[]
3939
)
4040
end
4141

@@ -83,36 +83,33 @@ defmodule OpentelemetryOban.JobHandler do
8383
})
8484
end
8585

86-
def handle_job_stop(_event, measurements, metadata, config) do
87-
set_measurements_attributes(measurements, config)
86+
def handle_job_stop(_event, measurements, metadata, _config) do
87+
set_measurements_attributes(measurements)
8888
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, metadata)
8989
end
9090

9191
def handle_job_exception(
9292
_event,
9393
measurements,
9494
%{stacktrace: stacktrace, error: error} = metadata,
95-
config
95+
_config
9696
) do
9797
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, metadata)
9898

9999
# Record exception and mark the span as errored
100100
Span.record_exception(ctx, error, stacktrace)
101101
Span.set_status(ctx, OpenTelemetry.status(:error, ""))
102102

103-
set_measurements_attributes(measurements, config)
103+
set_measurements_attributes(measurements)
104104

105105
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, metadata)
106106
end
107107

108-
defp set_measurements_attributes(%{duration: duration, queue_time: queue_time}, %{
109-
time_unit: time_unit
110-
}) do
108+
defp set_measurements_attributes(%{duration: duration, queue_time: queue_time}) do
111109
OpenTelemetry.Tracer.set_attributes(%{
112-
:"messaging.oban.duration_#{time_unit}" =>
113-
System.convert_time_unit(duration, :native, time_unit),
114-
:"messaging.oban.queue_time_#{time_unit}" =>
115-
System.convert_time_unit(queue_time, :nanosecond, time_unit)
110+
:"messaging.oban.duration_us" => System.convert_time_unit(duration, :native, :microsecond),
111+
:"messaging.oban.queue_time_us" =>
112+
System.convert_time_unit(queue_time, :nanosecond, :microsecond)
116113
})
117114
end
118115
end

instrumentation/opentelemetry_oban/lib/opentelemetry_oban/plugin_handler.ex

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ defmodule OpentelemetryOban.PluginHandler do
33

44
@tracer_id __MODULE__
55

6-
def attach(config) do
6+
def attach() do
77
attach_plugin_start_handler()
8-
attach_plugin_stop_handler(config)
9-
attach_plugin_exception_handler(config)
8+
attach_plugin_stop_handler()
9+
attach_plugin_exception_handler()
1010
end
1111

1212
defp attach_plugin_start_handler() do
@@ -18,21 +18,21 @@ defmodule OpentelemetryOban.PluginHandler do
1818
)
1919
end
2020

21-
defp attach_plugin_stop_handler(config) do
21+
defp attach_plugin_stop_handler() do
2222
:telemetry.attach(
2323
"#{__MODULE__}.plugin_stop",
2424
[:oban, :plugin, :stop],
2525
&__MODULE__.handle_plugin_stop/4,
26-
config
26+
[]
2727
)
2828
end
2929

30-
defp attach_plugin_exception_handler(config) do
30+
defp attach_plugin_exception_handler() do
3131
:telemetry.attach(
3232
"#{__MODULE__}.plugin_exception",
3333
[:oban, :plugin, :exception],
3434
&__MODULE__.handle_plugin_exception/4,
35-
config
35+
[]
3636
)
3737
end
3838

@@ -45,32 +45,22 @@ defmodule OpentelemetryOban.PluginHandler do
4545
)
4646
end
4747

48-
def handle_plugin_stop(_event, measurements, metadata, config) do
49-
set_measurements_attributes(measurements, config)
48+
def handle_plugin_stop(_event, _measurements, metadata, _config) do
5049
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, metadata)
5150
end
5251

5352
def handle_plugin_exception(
5453
_event,
55-
measurements,
54+
_measurements,
5655
%{stacktrace: stacktrace, error: error} = metadata,
57-
config
56+
_config
5857
) do
5958
ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, metadata)
6059

6160
# Record exception and mark the span as errored
6261
Span.record_exception(ctx, error, stacktrace)
6362
Span.set_status(ctx, OpenTelemetry.status(:error, ""))
6463

65-
set_measurements_attributes(measurements, config)
66-
6764
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, metadata)
6865
end
69-
70-
defp set_measurements_attributes(%{duration: duration}, %{time_unit: time_unit}) do
71-
OpenTelemetry.Tracer.set_attributes(%{
72-
:"messaging.oban.duration_#{time_unit}" =>
73-
System.convert_time_unit(duration, :native, time_unit)
74-
})
75-
end
7666
end

instrumentation/opentelemetry_oban/test/opentelemetry_oban/plugin_handler_test.exs

Lines changed: 60 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -24,127 +24,89 @@ defmodule OpentelemetryOban.PluginHandlerTest do
2424
:application.start(:opentelemetry)
2525

2626
TestHelpers.remove_oban_handlers()
27+
OpentelemetryOban.setup()
2728

2829
:ok
2930
end
3031

31-
describe "with the default config" do
32-
setup do
33-
OpentelemetryOban.setup()
34-
end
35-
36-
test "does not create spans when tracing plugins is disabled" do
37-
TestHelpers.remove_oban_handlers()
38-
OpentelemetryOban.setup(trace: [:jobs])
39-
40-
:telemetry.execute(
41-
[:oban, :plugin, :start],
42-
%{system_time: System.system_time()},
43-
%{plugin: Elixir.Oban.Plugins.Stager}
44-
)
45-
46-
:telemetry.execute(
47-
[:oban, :plugin, :stop],
48-
%{duration: 444},
49-
%{plugin: Elixir.Oban.Plugins.Stager}
50-
)
51-
52-
refute_receive {:span, span(name: "Elixir.Oban.Plugins.Stager process")}
53-
end
54-
55-
test "records span on plugin execution" do
56-
:telemetry.execute(
57-
[:oban, :plugin, :start],
58-
%{system_time: System.system_time()},
59-
%{plugin: Elixir.Oban.Plugins.Stager}
60-
)
32+
test "does not create spans when tracing plugins is disabled" do
33+
TestHelpers.remove_oban_handlers()
34+
OpentelemetryOban.setup(trace: [:jobs])
6135

62-
:telemetry.execute(
63-
[:oban, :plugin, :stop],
64-
%{duration: 444},
65-
%{plugin: Elixir.Oban.Plugins.Stager}
66-
)
36+
:telemetry.execute(
37+
[:oban, :plugin, :start],
38+
%{system_time: System.system_time()},
39+
%{plugin: Elixir.Oban.Plugins.Stager}
40+
)
6741

68-
assert_receive {:span,
69-
span(name: "Elixir.Oban.Plugins.Stager process", attributes: attributes)}
42+
:telemetry.execute(
43+
[:oban, :plugin, :stop],
44+
%{duration: 444},
45+
%{plugin: Elixir.Oban.Plugins.Stager}
46+
)
7047

71-
assert %{
72-
"messaging.oban.duration_microsecond": _duration
73-
} = :otel_attributes.map(attributes)
74-
end
48+
refute_receive {:span, span(name: "Elixir.Oban.Plugins.Stager process")}
49+
end
7550

76-
test "records span on plugin error" do
77-
:telemetry.execute(
78-
[:oban, :plugin, :start],
79-
%{system_time: System.system_time()},
80-
%{plugin: Elixir.Oban.Plugins.Stager}
81-
)
51+
test "records span on plugin execution" do
52+
:telemetry.execute(
53+
[:oban, :plugin, :start],
54+
%{system_time: System.system_time()},
55+
%{plugin: Elixir.Oban.Plugins.Stager}
56+
)
8257

83-
:telemetry.execute(
84-
[:oban, :plugin, :exception],
85-
%{duration: 444},
86-
%{
87-
plugin: Elixir.Oban.Plugins.Stager,
88-
kind: :error,
89-
stacktrace: [
90-
{Some, :error, [], []}
91-
],
92-
error: %UndefinedFunctionError{
93-
arity: 0,
94-
function: :error,
95-
message: nil,
96-
module: Some,
97-
reason: nil
98-
}
99-
}
100-
)
58+
:telemetry.execute(
59+
[:oban, :plugin, :stop],
60+
%{duration: 444},
61+
%{plugin: Elixir.Oban.Plugins.Stager}
62+
)
10163

102-
expected_status = OpenTelemetry.status(:error, "")
103-
104-
assert_receive {:span,
105-
span(
106-
name: "Elixir.Oban.Plugins.Stager process",
107-
attributes: attributes,
108-
events: events,
109-
status: ^expected_status
110-
)}
111-
112-
assert %{
113-
"messaging.oban.duration_microsecond": _duration
114-
} = :otel_attributes.map(attributes)
115-
116-
[
117-
event(
118-
name: "exception",
119-
attributes: event_attributes
120-
)
121-
] = :otel_events.list(events)
122-
123-
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
124-
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
125-
end
64+
assert_receive {:span, span(name: "Elixir.Oban.Plugins.Stager process")}
12665
end
12766

128-
test "can configure time_unit" do
129-
OpentelemetryOban.setup(time_unit: :second)
130-
67+
test "records span on plugin error" do
13168
:telemetry.execute(
13269
[:oban, :plugin, :start],
13370
%{system_time: System.system_time()},
13471
%{plugin: Elixir.Oban.Plugins.Stager}
13572
)
13673

13774
:telemetry.execute(
138-
[:oban, :plugin, :stop],
75+
[:oban, :plugin, :exception],
13976
%{duration: 444},
140-
%{plugin: Elixir.Oban.Plugins.Stager}
77+
%{
78+
plugin: Elixir.Oban.Plugins.Stager,
79+
kind: :error,
80+
stacktrace: [
81+
{Some, :error, [], []}
82+
],
83+
error: %UndefinedFunctionError{
84+
arity: 0,
85+
function: :error,
86+
message: nil,
87+
module: Some,
88+
reason: nil
89+
}
90+
}
14191
)
14292

93+
expected_status = OpenTelemetry.status(:error, "")
94+
14395
assert_receive {:span,
144-
span(name: "Elixir.Oban.Plugins.Stager process", attributes: attributes)}
96+
span(
97+
name: "Elixir.Oban.Plugins.Stager process",
98+
events: events,
99+
status: ^expected_status
100+
)}
101+
102+
[
103+
event(
104+
name: "exception",
105+
attributes: event_attributes
106+
)
107+
] = :otel_events.list(events)
145108

146-
assert %{
147-
"messaging.oban.duration_second": _duration
148-
} = :otel_attributes.map(attributes)
109+
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
110+
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
149111
end
150112
end

0 commit comments

Comments
 (0)