Tracing conformance with real sentry-elixir (OpenTelemetry) and sentry-python 3

e9331a4 · gmorell · 2026-12-02 02:37

6 files +207 -0

Files changed

added test/support/sdk/sentry_elixir_trace.exs
+77 −0
@@ -0,0 +1,77 @@
1 +# Sends a traced request (a server span with a nested db span) and an error
2 +# inside it with the real sentry-elixir and OpenTelemetry, as a Phoenix app
3 +# set up per sentry-elixir's docs would. Run by
4 +# VigilWeb.SdkTracingConformanceTest. The DSN comes from SENTRY_DSN, which
5 +# sentry-elixir reads itself, so Mix.install's config (and its cache)
6 +# doesn't change from run to run.
7 +Mix.install(
8 + [
9 + {:sentry, path: System.fetch_env!("SENTRY_ELIXIR_PATH")},
10 + {:jason, "~> 1.4"},
11 + {:finch, "~> 0.21"},
12 + {:opentelemetry, "~> 1.5"},
13 + {:opentelemetry_api, "~> 1.4"},
14 + # sentry-elixir enables tracing only with these too (its VersionChecker).
15 + {:opentelemetry_exporter, "~> 1.8"},
16 + {:opentelemetry_semantic_conventions, "~> 1.27"}
17 + ],
18 + config: [
19 + sentry: [
20 + environment_name: "test",
21 + release: "vigil-trace@1.0",
22 + traces_sample_rate: 1.0
23 + ],
24 + opentelemetry: [
25 + traces_exporter: :none,
26 + span_processor: {Sentry.OpenTelemetry.SpanProcessor, []},
27 + sampler: {Sentry.OpenTelemetry.Sampler, []}
28 + ]
29 + ]
30 +)
31 +
32 +# The functional OpenTelemetry API: the Tracer macros can't be used in a
33 +# script that installs them (it's compiled before Mix.install runs).
34 +tracer = :opentelemetry.get_tracer(:vigil_trace)
35 +
36 +:otel_tracer.with_span(
37 + tracer,
38 + "GET /orders/:id",
39 + %{
40 + kind: :server,
41 + attributes: %{
42 + "http.request.method" => "GET",
43 + "http.route" => "/orders/:id",
44 + "url.path" => "/orders/42"
45 + }
46 + },
47 + fn _ctx ->
48 + :otel_tracer.with_span(
49 + tracer,
50 + "orders.repo.query",
51 + %{
52 + kind: :client,
53 + attributes: %{
54 + "db.system" => "postgresql",
55 + "db.statement" => "SELECT * FROM orders WHERE id = $1"
56 + }
57 + },
58 + fn _ -> Process.sleep(15) end
59 + )
60 +
61 + exception =
62 + try do
63 + raise ArgumentError, "order 42 is gone"
64 + rescue
65 + e -> e
66 + end
67 +
68 + IO.puts("exception: #{inspect(Sentry.capture_exception(exception, result: :sync))}")
69 + Process.sleep(5)
70 + end
71 +)
72 +
73 +Sentry.flush(timeout: 10_000)
74 +# The transaction is sent when its root span ends, asynchronously.
75 +Process.sleep(500)
76 +Sentry.flush(timeout: 10_000)
77 +IO.puts("done")
added test/vigil_web/sdk_tracing_conformance_test.exs
+130 −0
@@ -0,0 +1,130 @@
1 +defmodule VigilWeb.SdkTracingConformanceTest do
2 + @moduledoc """
3 + Slice 6's exit, with the real SDKs against a running server:
4 + sentry-elixir with OpenTelemetry (a `transaction` with nested spans,
5 + `test/support/sdk/sentry_elixir_trace.exs`) and sentry-python 3 (span
6 + streaming, `span` v2 items). Each becomes transaction groups and a
7 + trace, and an error raised inside the trace links to it. Skipped when a
8 + checkout is missing (see `VigilWeb.SdkConformanceTest`).
9 + """
10 + use VigilWeb.ConnCase, async: false
11 +
12 + import Ecto.Query
13 + import Vigil.AccountsFixtures
14 + import Vigil.OrganizationsFixtures
15 +
16 + alias Vigil.{Performance, Projects, Repo}
17 + alias Vigil.Issues.IssueEvent
18 + alias Vigil.Performance.Span
19 +
20 + @elixir_sdk System.get_env(
21 + "SENTRY_ELIXIR_PATH",
22 + Path.expand("~/Development/local/sentry_oss_2019/sentry-elixir")
23 + )
24 +
25 + @python_sdk System.get_env(
26 + "SENTRY_PYTHON_PATH",
27 + Path.expand("~/Development/local/sentry_oss_2019/sentry-python")
28 + )
29 +
30 + setup do
31 + owner = user_fixture()
32 + org = organization_fixture(owner)
33 + project = project_fixture(org_scope(owner, org))
34 + [key] = Projects.list_keys(project)
35 +
36 + server = start_supervised!({Bandit, plug: VigilWeb.Endpoint, port: 0, ip: :loopback})
37 + {:ok, {_ip, port}} = ThousandIsland.listener_info(server)
38 +
39 + hex = String.replace(key.public_key, "-", "")
40 + %{project: project, dsn: "http://#{hex}@127.0.0.1:#{port}/#{project.dsn_id}"}
41 + end
42 +
43 + @tag :sdk
44 + @tag timeout: 300_000
45 + test "sentry-elixir with OpenTelemetry: a transaction, its spans, and the error in it", ctx do
46 + if File.exists?(Path.join(@elixir_sdk, "mix.exs")) do
47 + script = Path.expand("../support/sdk/sentry_elixir_trace.exs", __DIR__)
48 +
49 + {out, status} =
50 + System.cmd("elixir", [script],
51 + env: [
52 + {"SENTRY_DSN", ctx.dsn},
53 + {"SENTRY_ELIXIR_PATH", @elixir_sdk},
54 + {"MIX_ENV", "prod"}
55 + ],
56 + stderr_to_stdout: true
57 + )
58 +
59 + assert status == 0, out
60 + assert out =~ "done", out
61 +
62 + Vigil.Ingest.Drain.drain(ctx.project.id)
63 +
64 + assert [group] = Performance.list_groups(ctx.project.id), out
65 + assert group.name =~ "/orders"
66 + assert group.stats.count == 1
67 +
68 + [segment] = Repo.all(from s in Span, where: s.is_segment)
69 + children = Repo.all(from s in Span, where: not s.is_segment)
70 + assert Enum.any?(children, &(&1.parent_span_id == segment.span_id)), inspect(children)
71 +
72 + trace = Performance.get_trace([ctx.project.id], segment.trace_id)
73 + assert length(trace.spans) >= 2
74 + assert [%IssueEvent{}] = trace.errors, "the error should carry the trace id\n#{out}"
75 + else
76 + IO.puts("skipping sentry-elixir tracing: no checkout at #{@elixir_sdk}")
77 + end
78 + end
79 +
80 + @tag :sdk
81 + test "sentry-python 3 span streaming: segments, children, and the error in the trace", ctx do
82 + if File.dir?(Path.join(@python_sdk, "sentry_sdk")) and System.find_executable("python3") do
83 + script = """
84 + import time
85 + import sentry_sdk
86 + sentry_sdk.init(dsn=#{inspect(ctx.dsn)}, release="py@1.0", environment="test",
87 + traces_sample_rate=1.0)
88 + with sentry_sdk.start_span(name="/api/checkout",
89 + attributes={"sentry.op": "http.server",
90 + "http.request.method": "POST"}):
91 + with sentry_sdk.start_span(name="SELECT * FROM carts WHERE id = %s",
92 + attributes={"sentry.op": "db"}):
93 + time.sleep(0.01)
94 + try:
95 + 1 / 0
96 + except ZeroDivisionError:
97 + sentry_sdk.capture_exception()
98 + sentry_sdk.flush(timeout=10)
99 + """
100 +
101 + {out, status} =
102 + System.cmd("python3", ["-c", script],
103 + env: [{"PYTHONPATH", @python_sdk}],
104 + stderr_to_stdout: true
105 + )
106 +
107 + assert status == 0, out
108 + Vigil.Ingest.Drain.drain(ctx.project.id)
109 +
110 + assert [%{name: "/api/checkout", op: "http.server", method: "POST"} = group] =
111 + Performance.list_groups(ctx.project.id),
112 + out
113 +
114 + assert group.stats.count == 1
115 +
116 + [segment] = Repo.all(from s in Span, where: s.is_segment)
117 + assert segment.environment == "test" and segment.release == "py@1.0"
118 +
119 + # (sentry-python also traces its own envelope POSTs, as http.client.)
120 + children = Repo.all(from s in Span, where: not s.is_segment)
121 + assert %{parent_span_id: parent} = Enum.find(children, &(&1.op == "db")), inspect(children)
122 + assert parent == segment.span_id
123 +
124 + trace = Performance.get_trace([ctx.project.id], segment.trace_id)
125 + assert [%IssueEvent{}] = trace.errors, out
126 + else
127 + IO.puts("skipping sentry-python tracing: no checkout at #{@python_sdk}")
128 + end
129 + end
130 +end

Parents: 0750f91