Experiments >

Mail telemetry

Experiment #22025th July, 2021by Joshua Nussbaum

I’ve been working on sending on my metrics to AppSignal so I can have dashbaords for my SaaS project.

One area that didn’t have an existing solution was for tracking email. I use swoosh to send and it didn’t have any telemetry integration.

Adding telemetry

So I opened a PR, all it took was using :telemetry.span to output :start, :stop and :exception events.

Before

def deliver(email, config) do
  Mailer.deliver(email, parse_config(config))
end

After

def deliver(email, config) do
  metadata = %{email: email, config: config}

  # wrap with instrumentation
  instrument(:deliver, metadata, fn ->
    Mailer.deliver(email, parse_config(config))
  end)
end

defp instrument(key, metadata, fun) do
  metadata = Map.merge(metadata, %{mailer: __MODULE__})

  # start a span to capture how long it takes
  :telemetry.span([:swoosh, key], metadata, fn ->
    # run callback function and update metadata
    case fun.() do
      {:ok, result} -> {{:ok, result}, Map.put(metadata, :result, result)}
      {:error, error} -> {{:error, error}, Map.put(metadata, :error, error)}
    end
  end)
end

Code

swoosh/swoosh#614

view all experiments

Stay tuned in

Learn how to add more experimentation to your workflow