Logs conformance: sentry-elixir logger handler and sentry-python logger into the tail

5608707 · gmorell · 2026-12-02 03:22

6 files +140 -0

Files changed

added test/support/sdk/sentry_elixir_logs.exs
+29 −0
@@ -0,0 +1,29 @@
1 +# Logs through sentry-elixir's logger handler, set up as its docs say
2 +# (`config :sentry, logs: [level: :info, ...]` attaches the handler), to
3 +# SENTRY_DSN. Run by VigilWeb.SdkLogsConformanceTest.
4 +Mix.install(
5 + [
6 + {:sentry, path: System.fetch_env!("SENTRY_ELIXIR_PATH")},
7 + {:jason, "~> 1.4"},
8 + {:finch, "~> 0.21"}
9 + ],
10 + config: [
11 + sentry: [
12 + environment_name: "test",
13 + release: "vigil-logs@1.0",
14 + logs: [level: :info, metadata: [:order_id]]
15 + ]
16 + ]
17 +)
18 +
19 +require Logger
20 +
21 +Logger.info("order placed", order_id: 42)
22 +Logger.warning("payment retry for order 42")
23 +Logger.error("payment failed")
24 +Logger.debug("not sent: below the logs level")
25 +
26 +# Logs are batched by the SDK's telemetry processor; flush before exiting.
27 +Process.sleep(200)
28 +Sentry.flush(timeout: 10_000)
29 +IO.puts("done")
added test/vigil_web/sdk_logs_conformance_test.exs
+111 −0
@@ -0,0 +1,111 @@
1 +defmodule VigilWeb.SdkLogsConformanceTest do
2 + @moduledoc """
3 + Slice 9's exit with the real SDKs against a running server: sentry-elixir's
4 + logger handler and sentry-python's logger stream into the live tail
5 + (`Vigil.Logs.subscribe/1`) and are searchable. Skipped when a checkout is
6 + missing.
7 + """
8 + use VigilWeb.ConnCase, async: false
9 +
10 + import Vigil.AccountsFixtures
11 + import Vigil.OrganizationsFixtures
12 +
13 + alias Vigil.{Logs, Projects}
14 + alias Vigil.Logs.LogEvent
15 +
16 + @elixir_sdk System.get_env(
17 + "SENTRY_ELIXIR_PATH",
18 + Path.expand("~/Development/local/sentry_oss_2019/sentry-elixir")
19 + )
20 +
21 + @python_sdk System.get_env(
22 + "SENTRY_PYTHON_PATH",
23 + Path.expand("~/Development/local/sentry_oss_2019/sentry-python")
24 + )
25 +
26 + setup do
27 + owner = user_fixture()
28 + org = organization_fixture(owner)
29 + project = project_fixture(org_scope(owner, org))
30 + [key] = Projects.list_keys(project)
31 +
32 + server = start_supervised!({Bandit, plug: VigilWeb.Endpoint, port: 0, ip: :loopback})
33 + {:ok, {_ip, port}} = ThousandIsland.listener_info(server)
34 +
35 + hex = String.replace(key.public_key, "-", "")
36 + Logs.subscribe(project.id)
37 + %{project: project, dsn: "http://#{hex}@127.0.0.1:#{port}/#{project.dsn_id}"}
38 + end
39 +
40 + defp tail_bodies(acc \\ []) do
41 + receive do
42 + {:logs, events} -> tail_bodies(acc ++ Enum.map(events, & &1.body))
43 + after
44 + 0 -> acc
45 + end
46 + end
47 +
48 + @tag :sdk
49 + @tag timeout: 300_000
50 + test "sentry-elixir's logger handler streams into the tail", ctx do
51 + if File.exists?(Path.join(@elixir_sdk, "mix.exs")) do
52 + script = Path.expand("../support/sdk/sentry_elixir_logs.exs", __DIR__)
53 +
54 + {out, status} =
55 + System.cmd("elixir", [script],
56 + env: [
57 + {"SENTRY_DSN", ctx.dsn},
58 + {"SENTRY_ELIXIR_PATH", @elixir_sdk},
59 + {"MIX_ENV", "prod"}
60 + ],
61 + stderr_to_stdout: true
62 + )
63 +
64 + assert status == 0, out
65 + Vigil.Ingest.Drain.drain(ctx.project.id)
66 +
67 + bodies = tail_bodies()
68 + assert "order placed" in bodies, "tail: #{inspect(bodies)}\n#{out}"
69 + assert "payment failed" in bodies
70 + refute "not sent: below the logs level" in bodies
71 +
72 + [placed] = Logs.search(ctx.project.id, Logs.parse_query(~s("order placed")))
73 + assert %LogEvent{level: "info", environment: "test", release: "vigil-logs@1.0"} = placed
74 +
75 + assert [%{body: "payment retry for order 42"}] =
76 + Logs.search(ctx.project.id, Logs.parse_query("level:warn"))
77 + else
78 + IO.puts("skipping sentry-elixir logs: no checkout at #{@elixir_sdk}")
79 + end
80 + end
81 +
82 + @tag :sdk
83 + test "sentry-python's logger streams into the tail", ctx do
84 + if File.dir?(Path.join(@python_sdk, "sentry_sdk")) and System.find_executable("python3") do
85 + script = """
86 + import sentry_sdk
87 + sentry_sdk.init(dsn=#{inspect(ctx.dsn)}, environment="test")
88 + sentry_sdk.logger.info("user {user} signed in", user="ada")
89 + sentry_sdk.logger.error("queue backed up", attributes={"queue.depth": 9000})
90 + sentry_sdk.flush(timeout=10)
91 + """
92 +
93 + {out, status} =
94 + System.cmd("python3", ["-c", script],
95 + env: [{"PYTHONPATH", @python_sdk}],
96 + stderr_to_stdout: true
97 + )
98 +
99 + assert status == 0, out
100 + Vigil.Ingest.Drain.drain(ctx.project.id)
101 +
102 + bodies = tail_bodies()
103 + assert "user ada signed in" in bodies, "tail: #{inspect(bodies)}\n#{out}"
104 +
105 + assert [%LogEvent{level: "error", attributes: %{"queue.depth" => 9000}}] =
106 + Logs.search(ctx.project.id, Logs.parse_query("queue"))
107 + else
108 + IO.puts("skipping sentry-python logs: no checkout at #{@python_sdk}")
109 + end
110 + end
111 +end

Parents: 60e74af