-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #463 from newrelic/vince/oban-instrumentation
Oban Instrumentation
- Loading branch information
Showing
18 changed files
with
330 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,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,26 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
oban_example-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
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,3 @@ | ||
# ObanExample | ||
|
||
An example app demonstrating auto-instrumentation of Oban |
15 changes: 15 additions & 0 deletions
15
examples/apps/oban_example/lib/oban_example/application.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,15 @@ | ||
defmodule ObanExample.Application do | ||
use Application | ||
|
||
def start(_type, _args) do | ||
config = [ | ||
notifier: Oban.Notifiers.PG, | ||
testing: :inline | ||
] | ||
|
||
children = [{Oban, config}] | ||
|
||
opts = [strategy: :one_for_one, name: ObanExample.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule ObanExample.Worker do | ||
use Oban.Worker | ||
|
||
@impl Oban.Worker | ||
def perform(%Oban.Job{args: %{"error" => message}}) do | ||
{:error, message} | ||
end | ||
|
||
def perform(%Oban.Job{args: _args}) do | ||
Process.sleep(:rand.uniform(50)) | ||
:ok | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule ObanExample.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :oban_example, | ||
version: "0.1.0", | ||
build_path: "../../_build", | ||
config_path: "../../config/config.exs", | ||
deps_path: "../../deps", | ||
lockfile: "../../mix.lock", | ||
elixir: "~> 1.16", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger], | ||
mod: {ObanExample.Application, []} | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
{:new_relic_agent, path: "../../../"}, | ||
{:test_support, in_umbrella: true}, | ||
{:oban, "~> 2.0"} | ||
] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule ObanExampleTest do | ||
use ExUnit.Case | ||
|
||
alias NewRelic.Harvest.Collector | ||
|
||
setup_all context, do: TestSupport.simulate_agent_enabled(context) | ||
setup_all context, do: TestSupport.simulate_agent_run(context) | ||
|
||
test "instruments a job" do | ||
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle) | ||
TestSupport.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle) | ||
|
||
ObanExample.Worker.new(%{some: "args"}, tags: ["foo", "bar"]) | ||
|> Oban.insert() | ||
|
||
metrics = TestSupport.gather_harvest(Collector.Metric.Harvester) | ||
[event | _] = TestSupport.gather_harvest(Collector.TransactionEvent.Harvester) | ||
|
||
assert TestSupport.find_metric( | ||
metrics, | ||
"OtherTransaction/Oban/default/ObanExample.Worker/perform", | ||
1 | ||
) | ||
|
||
assert [ | ||
%{:name => "OtherTransaction/Oban/default/ObanExample.Worker/perform"}, | ||
%{ | ||
:"oban.worker" => "ObanExample.Worker", | ||
:"oban.queue" => "default", | ||
:"oban.job.result" => "success", | ||
:"oban.job.tags" => "foo,bar" | ||
} | ||
] = event | ||
end | ||
|
||
test "instruments a failed job" do | ||
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle) | ||
TestSupport.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle) | ||
|
||
ObanExample.Worker.new(%{error: "error!"}, tags: ["foo", "bar"]) | ||
|> Oban.insert() | ||
|
||
metrics = TestSupport.gather_harvest(Collector.Metric.Harvester) | ||
[event | _] = TestSupport.gather_harvest(Collector.TransactionEvent.Harvester) | ||
|
||
assert TestSupport.find_metric( | ||
metrics, | ||
"OtherTransaction/Oban/default/ObanExample.Worker/perform", | ||
1 | ||
) | ||
|
||
assert [ | ||
%{:name => "OtherTransaction/Oban/default/ObanExample.Worker/perform"}, | ||
%{ | ||
:error => true, | ||
:error_kind => :error, | ||
:"oban.worker" => "ObanExample.Worker", | ||
:"oban.queue" => "default", | ||
:"oban.job.result" => "failure", | ||
:"oban.job.tags" => "foo,bar" | ||
} | ||
] = event | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |
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
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
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,131 @@ | ||
defmodule NewRelic.Telemetry.Oban do | ||
use GenServer | ||
|
||
@moduledoc """ | ||
Provides `Oban` instrumentation via `telemetry`. | ||
Oban jobs are auto-discovered and instrumented. | ||
We automatically gather: | ||
* Transaction metrics and events | ||
* Transaction Traces | ||
* Distributed Traces | ||
You can opt-out of this instrumentation via configuration. See `NewRelic.Config` for details. | ||
""" | ||
|
||
alias NewRelic.Transaction | ||
|
||
@doc false | ||
def start_link(_) do | ||
config = %{ | ||
enabled?: NewRelic.Config.feature?(:oban_instrumentation), | ||
handler_id: {:new_relic, :oban} | ||
} | ||
|
||
GenServer.start_link(__MODULE__, config, name: __MODULE__) | ||
end | ||
|
||
@oban_start [:oban, :job, :start] | ||
@oban_stop [:oban, :job, :stop] | ||
@oban_exception [:oban, :job, :exception] | ||
|
||
@oban_events [ | ||
@oban_start, | ||
@oban_stop, | ||
@oban_exception | ||
] | ||
|
||
@doc false | ||
def init(%{enabled?: false}), do: :ignore | ||
|
||
def init(%{enabled?: true} = config) do | ||
:telemetry.attach_many( | ||
config.handler_id, | ||
@oban_events, | ||
&__MODULE__.handle_event/4, | ||
config | ||
) | ||
|
||
Process.flag(:trap_exit, true) | ||
{:ok, config} | ||
end | ||
|
||
@doc false | ||
def terminate(_reason, %{handler_id: handler_id}) do | ||
:telemetry.detach(handler_id) | ||
end | ||
|
||
@doc false | ||
def handle_event( | ||
@oban_start, | ||
%{system_time: system_time}, | ||
meta, | ||
_config | ||
) do | ||
Transaction.Reporter.start_transaction(:other) | ||
NewRelic.DistributedTrace.start(:other) | ||
|
||
add_start_attrs(meta, system_time) | ||
end | ||
|
||
def handle_event( | ||
@oban_stop, | ||
%{duration: duration} = meas, | ||
meta, | ||
_config | ||
) do | ||
add_stop_attrs(meas, meta, duration) | ||
|
||
Transaction.Reporter.stop_transaction(:other) | ||
end | ||
|
||
def handle_event( | ||
@oban_exception, | ||
%{duration: duration} = meas, | ||
%{kind: kind} = meta, | ||
_config | ||
) do | ||
add_stop_attrs(meas, meta, duration) | ||
{reason, stack} = NewRelic.Util.Telemetry.reason_and_stack(meta) | ||
|
||
Transaction.Reporter.fail(%{kind: kind, reason: reason, stack: stack}) | ||
Transaction.Reporter.stop_transaction(:other) | ||
end | ||
|
||
def handle_event(_event, _measurements, _meta, _config) do | ||
:ignore | ||
end | ||
|
||
defp add_start_attrs(meta, system_time) do | ||
[ | ||
pid: inspect(self()), | ||
system_time: system_time, | ||
other_transaction_name: "Oban/#{meta.queue}/#{meta.worker}/perform", | ||
"oban.worker": meta.worker, | ||
"oban.queue": meta.queue, | ||
"oban.job.args": meta.job.args, | ||
"oban.job.tags": meta.job.tags |> Enum.join(","), | ||
"oban.job.attempt": meta.job.attempt, | ||
"oban.job.attempted_by": meta.job.attempted_by |> Enum.join("."), | ||
"oban.job.max_attempts": meta.job.max_attempts, | ||
"oban.job.priority": meta.job.priority | ||
] | ||
|> NewRelic.add_attributes() | ||
end | ||
|
||
@kb 1024 | ||
defp add_stop_attrs(meas, meta, duration) do | ||
info = Process.info(self(), [:memory, :reductions]) | ||
|
||
[ | ||
duration: duration, | ||
memory_kb: info[:memory] / @kb, | ||
reductions: info[:reductions], | ||
"oban.job.result": meta.state, | ||
"oban.job.queue_time": meas.queue_time | ||
] | ||
|> NewRelic.add_attributes() | ||
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
Oops, something went wrong.