Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
defmodule OpentelemetryObanTest do
|
|
|
|
use DataCase
|
|
|
|
|
|
|
|
doctest OpentelemetryOban
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "records span on job insertion" do
|
|
|
|
OpentelemetryOban.insert(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob send",
|
|
|
|
attributes: attributes,
|
|
|
|
parent_span_id: :undefined,
|
|
|
|
kind: :producer,
|
|
|
|
status: :undefined
|
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.destination": "events",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.destination_kind": :queue,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.oban.job_id": _job_id,
|
|
|
|
"messaging.oban.max_attempts": 1,
|
|
|
|
"messaging.oban.priority": 0,
|
|
|
|
"messaging.oban.worker": "TestJob",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.system": :oban
|
|
|
|
} = :otel_attributes.map(attributes)
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "job creation uses existing trace if present" do
|
|
|
|
OpenTelemetry.Tracer.with_span "test span" do
|
|
|
|
ctx = OpenTelemetry.Tracer.current_span_ctx()
|
|
|
|
root_trace_id = OpenTelemetry.Span.trace_id(ctx)
|
|
|
|
root_span_id = OpenTelemetry.Span.span_id(ctx)
|
|
|
|
|
|
|
|
OpentelemetryOban.insert(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob send",
|
|
|
|
attributes: _attributes,
|
|
|
|
trace_id: ^root_trace_id,
|
|
|
|
parent_span_id: ^root_span_id,
|
|
|
|
kind: :producer,
|
|
|
|
status: :undefined
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "keeps existing meta information" do
|
|
|
|
OpentelemetryOban.insert(TestJob.new(%{}, meta: %{foo: "bar"}))
|
|
|
|
|
|
|
|
assert [job] = all_enqueued()
|
|
|
|
assert job.meta["foo"] == "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "tracing information is propagated between send and process" do
|
|
|
|
OpentelemetryOban.insert(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob send",
|
|
|
|
attributes: _attributes,
|
|
|
|
trace_id: send_trace_id,
|
|
|
|
span_id: send_span_id,
|
|
|
|
kind: :producer,
|
|
|
|
status: :undefined
|
|
|
|
)}
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob process",
|
|
|
|
attributes: _attributes,
|
|
|
|
kind: :consumer,
|
|
|
|
status: :undefined,
|
|
|
|
trace_id: process_trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[link(trace_id: ^send_trace_id, span_id: ^send_span_id)] = :otel_links.list(links)
|
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
# Process is ran asynchronously so we create a new trace, but still link
|
|
|
|
# the traces together.
|
|
|
|
assert send_trace_id != process_trace_id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "no link is created on process when tracing info was not propagated" do
|
|
|
|
# Using regular Oban, instead of OpentelemetryOban
|
|
|
|
Oban.insert(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob process",
|
|
|
|
attributes: _attributes,
|
|
|
|
kind: :consumer,
|
|
|
|
status: :undefined,
|
|
|
|
trace_id: _trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
2021-12-28 23:39:06 +00:00
|
|
|
|
|
|
|
assert [] == :otel_links.list(links)
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "records spans for successful Oban jobs" do
|
|
|
|
OpentelemetryOban.insert(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob process",
|
|
|
|
attributes: attributes,
|
|
|
|
kind: :consumer,
|
|
|
|
status: :undefined
|
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.destination": "events",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.destination_kind": :queue,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.oban.attempt": 1,
|
|
|
|
"messaging.oban.inserted_at": _inserted_at,
|
|
|
|
"messaging.oban.job_id": _job_id,
|
|
|
|
"messaging.oban.max_attempts": 1,
|
|
|
|
"messaging.oban.priority": 0,
|
|
|
|
"messaging.oban.scheduled_at": _scheduled_at,
|
|
|
|
"messaging.oban.worker": "TestJob",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.operation": :process,
|
|
|
|
"messaging.system": :oban
|
|
|
|
} = :otel_attributes.map(attributes)
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "records spans for Oban jobs that stop with {:error, :something}" do
|
|
|
|
OpentelemetryOban.insert(TestJobThatReturnsError.new(%{}))
|
2022-11-16 00:48:50 +00:00
|
|
|
assert %{success: 0, discard: 1} = Oban.drain_queue(queue: :events)
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
|
|
|
|
expected_status = OpenTelemetry.status(:error, "")
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobThatReturnsError process",
|
|
|
|
attributes: attributes,
|
|
|
|
kind: :consumer,
|
2021-12-28 23:39:06 +00:00
|
|
|
events: events,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
status: ^expected_status
|
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.destination": "events",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.destination_kind": :queue,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.oban.attempt": 1,
|
|
|
|
"messaging.oban.inserted_at": _inserted_at,
|
|
|
|
"messaging.oban.job_id": _job_id,
|
|
|
|
"messaging.oban.max_attempts": 1,
|
|
|
|
"messaging.oban.priority": 0,
|
|
|
|
"messaging.oban.scheduled_at": _scheduled_at,
|
|
|
|
"messaging.oban.worker": "TestJobThatReturnsError",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.operation": :process,
|
|
|
|
"messaging.system": :oban
|
|
|
|
} = :otel_attributes.map(attributes)
|
|
|
|
|
|
|
|
[
|
|
|
|
event(
|
|
|
|
name: "exception",
|
|
|
|
attributes: event_attributes
|
|
|
|
)
|
|
|
|
] = :otel_events.list(events)
|
|
|
|
|
2023-08-25 20:11:23 +00:00
|
|
|
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
|
|
|
|
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "records spans for each retry" do
|
|
|
|
OpentelemetryOban.insert(TestJobThatReturnsError.new(%{}, max_attempts: 2))
|
|
|
|
|
2022-11-16 00:48:50 +00:00
|
|
|
assert %{success: 0, failure: 1, discard: 1} =
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
Oban.drain_queue(queue: :events, with_scheduled: true, with_recursion: true)
|
|
|
|
|
|
|
|
expected_status = OpenTelemetry.status(:error, "")
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobThatReturnsError send",
|
|
|
|
trace_id: send_trace_id,
|
|
|
|
span_id: send_span_id
|
|
|
|
)}
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobThatReturnsError process",
|
|
|
|
status: ^expected_status,
|
|
|
|
trace_id: first_process_trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: job_1_links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[link(trace_id: ^send_trace_id, span_id: ^send_span_id)] = :otel_links.list(job_1_links)
|
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobThatReturnsError process",
|
|
|
|
status: ^expected_status,
|
|
|
|
trace_id: second_process_trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: job_2_links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[link(trace_id: ^send_trace_id, span_id: ^send_span_id)] = :otel_links.list(job_2_links)
|
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
assert first_process_trace_id != second_process_trace_id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "records spans for Oban jobs that stop with an exception" do
|
|
|
|
OpentelemetryOban.insert(TestJobThatThrowsException.new(%{}))
|
2022-11-16 00:48:50 +00:00
|
|
|
assert %{success: 0, discard: 1} = Oban.drain_queue(queue: :events)
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
|
|
|
|
expected_status = OpenTelemetry.status(:error, "")
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobThatThrowsException process",
|
|
|
|
attributes: attributes,
|
|
|
|
kind: :consumer,
|
2021-12-28 23:39:06 +00:00
|
|
|
events: events,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
status: ^expected_status
|
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.destination": "events",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.destination_kind": :queue,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
"messaging.oban.attempt": 1,
|
|
|
|
"messaging.oban.inserted_at": _inserted_at,
|
|
|
|
"messaging.oban.job_id": _job_id,
|
|
|
|
"messaging.oban.max_attempts": 1,
|
|
|
|
"messaging.oban.priority": 0,
|
|
|
|
"messaging.oban.scheduled_at": _scheduled_at,
|
|
|
|
"messaging.oban.worker": "TestJobThatThrowsException",
|
2021-12-28 23:39:06 +00:00
|
|
|
"messaging.operation": :process,
|
|
|
|
"messaging.system": :oban
|
|
|
|
} = :otel_attributes.map(attributes)
|
|
|
|
|
|
|
|
[
|
|
|
|
event(
|
|
|
|
name: "exception",
|
|
|
|
attributes: event_attributes
|
|
|
|
)
|
|
|
|
] = :otel_events.list(events)
|
|
|
|
|
2023-08-25 20:11:23 +00:00
|
|
|
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
|
|
|
|
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "spans inside the job are associated with the job trace" do
|
|
|
|
OpentelemetryOban.insert(TestJobWithInnerSpan.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJobWithInnerSpan process",
|
|
|
|
kind: :consumer,
|
|
|
|
trace_id: trace_id,
|
|
|
|
span_id: process_span_id
|
|
|
|
)}
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "span inside the job",
|
|
|
|
kind: :internal,
|
|
|
|
trace_id: ^trace_id,
|
|
|
|
parent_span_id: ^process_span_id
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "OpentelemetryOban.insert!/2 returns job on successful insert" do
|
|
|
|
%Oban.Job{} = OpentelemetryOban.insert!(TestJob.new(%{}))
|
|
|
|
assert %{success: 1, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
assert_receive {:span, span(name: "TestJob send")}
|
|
|
|
assert_receive {:span, span(name: "TestJob process")}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "OpentelemetryOban.insert!/2 raises an error on failed insert" do
|
|
|
|
assert_raise(
|
|
|
|
Ecto.InvalidChangesetError,
|
|
|
|
fn -> OpentelemetryOban.insert!(TestJob.new(%{}, max_attempts: -1)) end
|
|
|
|
)
|
|
|
|
|
|
|
|
assert %{success: 0, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
expected_status = OpenTelemetry.status(:error, "")
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob send",
|
2021-12-28 23:39:06 +00:00
|
|
|
events: events,
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
status: ^expected_status
|
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[
|
|
|
|
event(
|
|
|
|
name: "exception",
|
|
|
|
attributes: event_attributes
|
|
|
|
)
|
|
|
|
] = :otel_events.list(events)
|
|
|
|
|
2023-08-25 20:11:23 +00:00
|
|
|
assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
|
|
|
|
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
|
2021-12-28 23:39:06 +00:00
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
refute_received {:span, span(name: "TestJob process")}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "tracing information is propagated when using insert_all/2" do
|
|
|
|
OpentelemetryOban.insert_all([
|
|
|
|
TestJob.new(%{}),
|
|
|
|
TestJob.new(%{})
|
|
|
|
])
|
|
|
|
|
|
|
|
assert %{success: 2, failure: 0} = Oban.drain_queue(queue: :events)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
2021-12-28 23:39:06 +00:00
|
|
|
name: :"Oban bulk insert",
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
attributes: _attributes,
|
|
|
|
trace_id: send_trace_id,
|
|
|
|
span_id: send_span_id,
|
|
|
|
kind: :producer,
|
|
|
|
status: :undefined
|
|
|
|
)}
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob process",
|
|
|
|
attributes: _attributes,
|
|
|
|
kind: :consumer,
|
|
|
|
status: :undefined,
|
|
|
|
trace_id: first_process_trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: job_1_links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[link(trace_id: ^send_trace_id, span_id: ^send_span_id)] = :otel_links.list(job_1_links)
|
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "TestJob process",
|
|
|
|
attributes: _attributes,
|
|
|
|
kind: :consumer,
|
|
|
|
status: :undefined,
|
|
|
|
trace_id: second_process_trace_id,
|
2021-12-28 23:39:06 +00:00
|
|
|
links: job_2_links
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
[link(trace_id: ^send_trace_id, span_id: ^send_span_id)] = :otel_links.list(job_2_links)
|
|
|
|
|
Add opentelemetry integration to Oban (#6)
By default a new trace is automatically started when a job is processed
by monitoring these events:
* `[:oban, :job, :start]` — at the point a job is fetched from the database and will execute
* `[:oban, :job, :stop]` — after a job succeeds and the success is recorded in the database
* `[:oban, :job, :exception]` — after a job fails and the failure is recorded in the database
To also record a span when a job is created and to link traces together
`Oban.insert/2` has to be replaced by `OpentelemetryOban.insert/2`.
Before:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> Oban.insert()
```
After:
```elixir
%{id: 1, in_the: "business", of_doing: "business"}
|> MyApp.Business.new()
|> OpentelemetryOban.insert()
```
Co-authored-by: Tristan Sloughter <t@crashfast.com>
2021-12-08 15:41:36 +00:00
|
|
|
# Process is ran asynchronously so we create a new trace, but still link
|
|
|
|
# the traces together.
|
|
|
|
assert send_trace_id != first_process_trace_id
|
|
|
|
assert send_trace_id != second_process_trace_id
|
|
|
|
assert first_process_trace_id != second_process_trace_id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "works with Oban.Testing.perform_job helper function" do
|
|
|
|
Oban.Testing.perform_job(TestJob, %{}, repo: TestRepo)
|
|
|
|
|
|
|
|
assert_receive {:span, span(name: "TestJob process")}
|
|
|
|
end
|
|
|
|
end
|