TelemetryTest (telemetry_test v0.1.0)

For example usage please refer to test/examples directory

examples

Examples

telemetry-event-sent-to-test-process

Telemetry event sent to test process

defmodule ExampleOneTest do
  use ExUnit.Case, async: false

  import TelemetryTest

  setup [:telemetry_listen]

  @tag telemetry_listen: [:sample, :event, :example_one]
  test "match on telemetry event" do
    assert :there = ExampleOne.hello()

    assert_receive {:telemetry_event,
                    %{
                      event: [:sample, :event, :example_one],
                      measurements: %{sample_value: :hello_there},
                      metadata: %{sample_metadata: true}
                    }}
  end
end

telemetry-event-being-passed-to-callback-after-test

Telemetry event being passed to callback after test

defmodule ExampleTwoTest do
  use ExUnit.Case, async: false

  import TelemetryTest

  setup [:telemetry_listen]

  @tag telemetry_listen: {[:sample, :event, :example_two], &__MODULE__.test_callback/1}
  test "match on telemetry event" do
    assert :there = ExampleTwo.hello()
  end

  def test_callback(telemetry) do
    assert %{
             event: [:sample, :event, :example_two],
             measurements: %{sample_value: :hello_there},
             metadata: %{sample_metadata: true}
           } = telemetry
  end
end

multiple-telemetry-events-sent-to-test-process

Multiple telemetry events sent to test process

defmodule ExampleThreeTest do
  use ExUnit.Case, async: false

  import TelemetryTest

  setup [:telemetry_listen]

  @tag telemetry_listen: [
         [:sample, :event, :example_three, :start],
         [:sample, :event, :example_three, :stop]
       ]
  test "match on telemetry events" do
    assert :there = ExampleThree.hello()

    assert_receive {:telemetry_event,
                    %{
                      event: [:sample, :event, :example_three, :start],
                      measurements: %{monotonic_time: _, system_time: _},
                      metadata: %{sample_metadata: true}
                    }}

    assert_receive {:telemetry_event,
                    %{
                      event: [:sample, :event, :example_three, :stop],
                      measurements: %{monotonic_time: _, duration: _},
                      metadata: %{more_metadata: true}
                    }}
  end
end

multiple-telemetry-events-being-passed-to-callback-after-test

Multiple telemetry events being passed to callback after test

defmodule ExampleFourTest do
  use ExUnit.Case, async: false

  import TelemetryTest

  setup [:telemetry_listen]

  @tag telemetry_listen: [
         {[:sample, :event, :example_four, :start], &__MODULE__.test_start_callback/1},
         {[:sample, :event, :example_four, :stop], &__MODULE__.test_stop_callback/1}
       ]
  test "match on telemetry events" do
    assert :there = ExampleFour.hello()
  end

  def test_start_callback(telemetry) do
    assert %{
             event: [:sample, :event, :example_four, :start],
             measurements: %{monotonic_time: _, system_time: _},
             metadata: %{sample_metadata: true, telemetry_span_context: _}
           } = telemetry
  end

  def test_stop_callback(telemetry) do
    assert %{
             event: [:sample, :event, :example_four, :stop],
             measurements: %{monotonic_time: _, duration: _},
             metadata: %{more_metadata: true, telemetry_span_context: _}
           } = telemetry
  end
end

multiple-telemetry-events-being-passed-to-callback-using-mfa

Multiple telemetry events being passed to callback using MFA

defmodule ExampleFourTest do
  use ExUnit.Case, async: false

  import TelemetryTest

  setup [:telemetry_listen]

  @tag telemetry_listen: [
         {[:sample, :event, :example_four, :start], &__MODULE__.test_start_callback/1},
         {[:sample, :event, :example_four, :stop], &__MODULE__.test_stop_callback/1}
       ]
  test "match on telemetry events" do
    assert :there = ExampleFour.hello()
  end

  def test_start_callback(telemetry) do
    assert %{
             event: [:sample, :event, :example_four, :start],
             measurements: %{monotonic_time: _, system_time: _},
             metadata: %{sample_metadata: true, telemetry_span_context: _}
           } = telemetry
  end

  def test_stop_callback(telemetry) do
    assert %{
             event: [:sample, :event, :example_four, :stop],
             measurements: %{monotonic_time: _, duration: _},
             metadata: %{more_metadata: true, telemetry_span_context: _}
           } = telemetry
  end
end

Link to this section Summary

Functions

ExUnit setup callback function which attaches to telemetry events

Link to this section Functions

Link to this function

telemetry_listen(context)

(since 0.1.0)

ExUnit setup callback function which attaches to telemetry events