O11y.TestHelper behaviour (O11y v0.1.1)
Provides a few helper functions to make testing with OpenTelemetry easier.
Example:
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.
Extracts the attributes from a span record as a map.
Extracts the events from a span record. Individual event records can be further inspected with the event
function.
Extracts the links from a span record. Individual link records can be further inspected with the link
function.
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.
Extracts the status atom from the status field on a span record.
Types
span_record()
@type span_record() :: tuple()
Callbacks
assert_span(t, t)
@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.
Example:
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)
attributes(tuple)
Extracts the attributes from a span record as a map.
events(tuple)
Extracts the events from a span record. Individual event records can be further inspected with the event
function.
links(tuple)
Extracts the links from a span record. Individual link records can be further inspected with the link
function.
otel_pid_reporter(any)
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]
status(tuple)
Extracts the status atom from the status field on a span record.