Opencensus.TestSupport.SpanCaptureReporter (opencensus_elixir v0.5.0)

An :opencensus.reporter to capture spans for tests.

:oc_reporter can't unregister reporters, but :telemetry can detach handlers, so we configure :opencensus to send spans to use our reporter, in mix.exs:

if Mix.env() == :test do
  config :opencensus,
    send_interval_ms: 100,
    reporters: [{Opencensus.TestSupport.SpanCaptureReporter, []}]
end

It'll call :telemetry.execute/3 whenever spans are reported. If you've called attach/0, the handler will convert the spans to structs with Span.from/1 and deliver them to your process inbox. To collect them, call collect/0. When you're finished, call detach/0:

defmodule NyApp.SpanCaptureTest do
  use ExUnit.Case, async: false

  alias Opencensus.TestSupport.SpanCaptureReporter

  setup do
    SpanCaptureReporter.attach()
    on_exit(make_ref(), &SpanCaptureReporter.detach/0)
  end

  test "can gather spans" do
    :ocp.with_child_span("our span name")
    :ocp.finish_span()
    [span] = SpanCaptureReporter.collect()
    assert span.name == "our span name"
  end
end

Link to this section Summary

Functions

Attach the reporter to deliver spans to your process inbox.

Collect spans from your process inbox.

Detach the reporter to stop delivering spans to your process inbox.

Link to this section Functions

Attach the reporter to deliver spans to your process inbox.

@spec collect() :: [
  %Opencensus.Span{
    attributes: term(),
    child_span_count: term(),
    end_time: term(),
    kind: term(),
    links: term(),
    name: term(),
    parent_span_id: term(),
    same_process_as_parent_span: term(),
    span_id: term(),
    stack_trace: term(),
    start_time: term(),
    status: term(),
    time_events: term(),
    trace_id: term(),
    trace_options: term(),
    tracestate: term()
  }
]

Collect spans from your process inbox.

If still attached, triggers span delivery before collection.

Detach the reporter to stop delivering spans to your process inbox.

If still attached, triggers span delivery before detaching.