tetiana

tetiana

Using System.stacktrace() within exq middleware behaviour implementation

Before Elixir 1.7 we were happy to send errors to appsignal when exq job failed to complete.
We have implemented a middleware behaviour to achieve that.

defmodule Exq.Middleware.AppSignal do
  @moduledoc false

  @behaviour Exq.Middleware.Behaviour

  alias Exq.Middleware.Pipeline
  import Pipeline

  def before_work(pipeline) do
    transaction =
      Appsignal.Transaction.generate_id()
      |> Appsignal.Transaction.start(:background_job)
      |> Appsignal.Transaction.set_action("Exq/#{pipeline.assigns.worker_module}")
      |> Appsignal.Transaction.set_sample_data(
        "environment",
        %{job_id: pipeline.assigns.job.jid}
      )

    assign(pipeline, :appsignal_transaction, transaction)
  end

  def after_processed_work(pipeline) do
    transaction = pipeline.assigns.appsignal_transaction
    Appsignal.Transaction.finish(transaction)
    Appsignal.Transaction.complete(transaction)

    pipeline
  end

  def after_failed_work(pipeline) do
    transaction = pipeline.assigns.appsignal_transaction

    _ =
      Appsignal.Transaction.set_error(
        transaction,
        "Exq job failed with exception",
        pipeline.assigns.error_message,
        System.stacktrace()   # <---- DEPRECATED!
      )

    Appsignal.Transaction.finish(transaction)
    Appsignal.Transaction.complete(transaction)

    pipeline
  end
end

In Elixir 1.7.2 System.stacktrace() is deprecated, so we are wondering what would be the best way to report errors to appsignal from our middleware.

We can pass stacktrace as [ ]. That is not ideal, as it would be great to have stacktrace.

As compilation warning suggests, use __STACKTRACE__ in try/rescue block and once we have stacktrace pass it to after_failed_work callback as second parameter? But that would mean that we have to suggest to change Exq.Middleware.Behaviour definition to allow optional stacktrace argument.

If you have an idea of how we can solve this, don’t hesitate to share and discuss.

First Post!

OvermindDL1

OvermindDL1

Ugly and slow, but you could maybe ‘make’ your own stacktrace? by just raising and catching your own error? Though getting a stacktrace in the old versions was not fast either so eh. ^.^;

Last Post!

dideler

dideler

We have the same issue, that is, using System.stacktrace/0 without an exception and therefore no rescue/catch.

We have some code where we handle error tuples by collecting information, which includes the stacktrace, and send the data to a monitoring service.

def set_error(transaction, name, message, reason) do backtrace = ([reason] ++ 
  System.stacktrace()) |&gt; Enum.map(&amp;inspect/1) 
  Appsignal.Transaction.set_error(transaction, name, message, backtrace)
end

I’ve considered creating our own fake exception but that changes the stacktrace.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement