Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Created June 17, 2019 21:57
Show Gist options
  • Save lucasmazza/3ab7107eb2fb543c00e8499d39e06595 to your computer and use it in GitHub Desktop.
Save lucasmazza/3ab7107eb2fb543c00e8499d39e06595 to your computer and use it in GitHub Desktop.
defmodule Broker.Workers.Appsignal do
@moduledoc false
alias Appsignal.Error
alias Appsignal.Transaction
def handle_event([:oban, event], measurement, meta, _) when event in [:success, :failure] do
transaction = record_event(measurement, meta)
if event == :failure && meta.attempt >= meta.max_attempts do
{reason, message, stack} = normalize_error(meta)
Transaction.set_error(transaction, reason, message, stack)
end
Transaction.complete(transaction)
end
defp record_event(measurement, meta) do
metadata = %{"id" => meta.id, "queue" => meta.queue, "attempt" => meta.attempt}
transaction = Transaction.start(Transaction.generate_id(), :background_job)
transaction
|> Transaction.set_action("#{meta.worker}#perform")
|> Transaction.set_meta_data(metadata)
|> Transaction.set_sample_data("params", meta.args)
|> Transaction.record_event("worker.perform", "", "", measurement.duration, 0)
|> Transaction.finish()
transaction
end
defp normalize_error(%{kind: :error, error: error, stack: stack}) do
{reason, message} = Error.metadata(error)
{reason, message, stack}
end
defp normalize_error(%{kind: kind, error: error, stack: stack}) do
{kind, error, stack}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment