O11y.TestHelper behaviour (O11y v0.2.10)
Provides a few helper functions to make testing with OpenTelemetry easier.
Examples:
defmodule MyTest do
use ExUnit.Case, async: true
use O11y.TestHelper
test "appends the given attribute to the span" do
Tracer.with_span "checkout" do
O11y.set_attribute(:id, 123)
end
assert_span("checkout", attributes: %{id: 123})
end
end
Summary
Callbacks
Asserts that a span with the given name was exported and optionally checks the status and attributes.
Sets up OpenTelemetry to use the pid exporter and ensures it is stopped after the test.
When spans are exported, a message is sent to the test pid allowing you to assert_receive
on a tuple with the span.
Types
@type span_record() :: tuple()
Callbacks
@callback assert_span(String.t(), Keyword.t()) :: span_record()
Asserts that a span with the given name was exported and optionally checks the status and attributes.
Examples:
assert_span("checkout", attributes: %{"id" => 123})
assert_span("checkout", status: :error)
It returns the matched span record so you can make additional assertions. The easiest way is to pattern match by creating a span record and bind the properties you want to assert on. You can use the included span, link, and event records to help with this, as well as the status, links, events, and attributes functions.
Examples:
span(status: status, events: events) = assert_span("checkout")
assert status(status) == :error
assert [event(name: "exception", attributes: attributes)] = events(events)
assert %{"exception.message": "something went wrong"} = attributes(attributes)
Sets up OpenTelemetry to use the pid exporter and ensures it is stopped after the test.
When spans are exported, a message is sent to the test pid allowing you to assert_receive
on a tuple with the span.
This is already setup for you when you use O11y.TestHelper
.
setup [:otel_pid_reporter]