BuildkiteTestCollector.Tracing (buildkite_test_collector v0.3.1)

Helpers for doing simple tracing in your tests.

Summary

Types

Valid trace types.

ExUnit test tags (specifically the module and test tags).

Functions

Measure the execution time of a function and add a trace to the test analytics.

Types

section()

@type section() :: :http | :sql | :sleep | :annotation

Valid trace types.

See the documentation for more information.

tags()

@type tags() :: %{:module => module(), :test => atom(), optional(atom()) => any()}

ExUnit test tags (specifically the module and test tags).

These are used to uniquely identify the test being executed.

Functions

measure(tags, section, detail \\ nil, callable)

@spec measure(tags(), section(), nil | String.t(), (-> result)) :: result
when result: any()

Measure the execution time of a function and add a trace to the test analytics.

Example

alias BuildkiteTestCollector.Tracing

test "it can measure an HTTP request", tags do
  assert {:ok, _} =
           Tracing.measure(tags, :http, "The koan of Github", fn ->
             Tesla.get("https://api.github.com/zen", headers: [{"user-agent", "Tesla"}])
           end)
end


test "it can measure a SQL query", tags do
  import Ecto.Query

  query =
    from(tt in "time_travellers",
      where: tt.born >= 1968 and tt.born <= 1970,
      select: tt.name,
      order_by: tt.name
    )

  assert {:ok, ["Marty McFly", "Theodore Logan, III", "William Stanley Preston, Esq."]} =
           Tracing.measure(tags, :sql, inspect(query), fn ->
             MyApp.all(query)
           end)
end