In a previous post I discussed how to setup your Elixir app to send metrics, logs, and traces to Grafana. In this post we will discuss adding custom metrics and traces.
Traces
Traces are very straightforward and we just do what the docs say:
require OpenTelemetry.Tracer, as: Tracer
Tracer.with_span "span-1" do
... do something ...
end
ElixirMetrics
Metrics are slightly more involved. First, we need to fire off :telemetry
events, then we need to setup a custom PromEx plugin to report the metrics.
Let’s say I want to count the number of requests to a specific controller. I’d add this to my controller:
:telemetry.execute([:http, :request, :mycontroller], %{count: 1})
Elixirand then create a custom PromEx
plugin
defmodule Observability.MycontrollerMetric do
use PromEx.Plugin
@impl true
def event_metrics(opts) do
Event.build(
:observability_mycontroller_metrics,
[
Telemetry.Metrics.counter("http.request.mycontroller.count")
]
)
end
end
ElixirAnd finally, add it to your PromEx
plugin list.
Note If you misconfigure the metric, it will not necessarily throw an error. For example, if you wrote http.request.mycontroller
(missing the .count
) then it will not work but it will also not crash.
That’s all!
Start the discussion at forum.stereowrench.com