2.1 KiB · text History e9331a4
# Sends a traced request (a server span with a nested db span) and an error
# inside it with the real sentry-elixir and OpenTelemetry, as a Phoenix app
# set up per sentry-elixir's docs would. Run by
# VigilWeb.SdkTracingConformanceTest. The DSN comes from SENTRY_DSN, which
# sentry-elixir reads itself, so Mix.install's config (and its cache)
# doesn't change from run to run.
Mix.install(
  [
    {:sentry, path: System.fetch_env!("SENTRY_ELIXIR_PATH")},
    {:jason, "~> 1.4"},
    {:finch, "~> 0.21"},
    {:opentelemetry, "~> 1.5"},
    {:opentelemetry_api, "~> 1.4"},
    # sentry-elixir enables tracing only with these too (its VersionChecker).
    {:opentelemetry_exporter, "~> 1.8"},
    {:opentelemetry_semantic_conventions, "~> 1.27"}
  ],
  config: [
    sentry: [
      environment_name: "test",
      release: "vigil-trace@1.0",
      traces_sample_rate: 1.0
    ],
    opentelemetry: [
      traces_exporter: :none,
      span_processor: {Sentry.OpenTelemetry.SpanProcessor, []},
      sampler: {Sentry.OpenTelemetry.Sampler, []}
    ]
  ]
)

# The functional OpenTelemetry API: the Tracer macros can't be used in a
# script that installs them (it's compiled before Mix.install runs).
tracer = :opentelemetry.get_tracer(:vigil_trace)

:otel_tracer.with_span(
  tracer,
  "GET /orders/:id",
  %{
    kind: :server,
    attributes: %{
      "http.request.method" => "GET",
      "http.route" => "/orders/:id",
      "url.path" => "/orders/42"
    }
  },
  fn _ctx ->
    :otel_tracer.with_span(
      tracer,
      "orders.repo.query",
      %{
        kind: :client,
        attributes: %{
          "db.system" => "postgresql",
          "db.statement" => "SELECT * FROM orders WHERE id = $1"
        }
      },
      fn _ -> Process.sleep(15) end
    )

    exception =
      try do
        raise ArgumentError, "order 42 is gone"
      rescue
        e -> e
      end

    IO.puts("exception: #{inspect(Sentry.capture_exception(exception, result: :sync))}")
    Process.sleep(5)
  end
)

Sentry.flush(timeout: 10_000)
# The transaction is sent when its root span ends, asynchronously.
Process.sleep(500)
Sentry.flush(timeout: 10_000)
IO.puts("done")