Created
June 17, 2019 21:57
-
-
Save lucasmazza/3ab7107eb2fb543c00e8499d39e06595 to your computer and use it in GitHub Desktop.
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
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