Last active
October 5, 2021 09:18
-
-
Save joshnuss/445c1a646f70c1583867455aee33bde8 to your computer and use it in GitHub Desktop.
Setup Appsignal telemetry for Oban
This file contains hidden or 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
| # inside Application.start | |
| :telemetry.attach_many("appsignal-oban", [ | |
| [:oban, :job, :start], | |
| [:oban, :job, :stop], | |
| [:oban, :job, :exception] | |
| ], &MyApp.Appsignal.Oban.handle_event/4, nil) | |
| # also start a Registry in Application.start to track Appsignal's "spans" | |
| children = [ | |
| # .... | |
| {Registry, [ | |
| name: Appsignal.Registry, | |
| keys: :unique, | |
| partitions: System.schedulers_online() | |
| ]}, | |
| ] |
This file contains hidden or 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
| defmodule MyApp.Appsignal.Oban do | |
| alias Appsignal.Tracer | |
| alias Appsignal.Span | |
| require Logger | |
| @registry Appsignal.Registry | |
| def handle_event([:oban, :job, :start], measurement, meta, _) do | |
| span = Tracer.create_span("background_job") | |
| |> Span.set_name("#{meta.worker}#perform") | |
| |> Span.set_sample_data("args", meta.args) | |
| Registry.register(@registry, {meta.id, meta.attempt}, span) | |
| end | |
| def handle_event([:oban, :job, :stop], measurement, meta, _) do | |
| {meta.id, meta.attempt} | |
| |> get_span() | |
| |> Tracer.close_span() | |
| end | |
| def handle_event([:oban, :job, :exception], _measurement, meta, _) do | |
| error = meta.job.unsaved_error | |
| {meta.id, meta.attempt} | |
| |> get_span() | |
| |> Span.add_error(error.kind, error.reason, error.stacktrace) | |
| |> Tracer.close_span() | |
| end | |
| defp get_span(id) do | |
| [{_pid, span}] = Registry.lookup(@registry, id) | |
| :ok = Registry.unregister(@registry, id) | |
| span | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man! Thanks for this!