Skip to content

Commit b866592

Browse files
reciprocoJavier Segura
andauthored
fix: Propagate muted flag to :error telemetry events (#174)
Repo.insert!/2 with on_conflict: [set: [...]] only round-trips the columns listed in the set clause (status, last_occurrence_at). As a result, the returned %Error{} struct always carried muted: false — the schema default — even when the DB row was muted. upsert_error!/5 already fetches muted from the DB in a pre-query, but never applied it to the in-memory struct before handing it to Telemetry.new_error/1 and Telemetry.unresolved_error/1. This caused subscribers of [:error_tracker, :error, :new] and [:error_tracker, :error, :unresolved] to always see muted: false, making it impossible to silence alerts for muted errors at the telemetry level. Fix: stamp the already-fetched muted value onto the struct right before telemetry dispatch. No new queries, no public API change — the muted field already exists on the Error schema, so existing subscribers start receiving the correct value without any changes on their side. Adds a regression test that mutes and resolves an error, re-triggers it, and asserts the :unresolved payload carries muted: true. Co-authored-by: Javier Segura <javier.segura@tamoe.es>
1 parent 3fd3e10 commit b866592

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/error_tracker.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ defmodule ErrorTracker do
371371
%Occurrence{} = occurrence
372372
occurrence = %{occurrence | error: error}
373373

374+
# `Repo.insert!(_, on_conflict: [set: [...]])` only round-trips the columns
375+
# it sets, so the in-memory struct keeps `muted: false` (the schema default)
376+
# even when the DB row is muted. Stamp it with the value we already fetched
377+
# so subscribers of `[:error_tracker, :error, :*]` see the real flag.
378+
error = %{error | muted: muted}
379+
374380
# If the error existed and was marked as resolved before this exception,
375381
# sent a Telemetry event
376382
# If it is a new error, sent a Telemetry event

test/error_tracker/telemetry_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,24 @@ defmodule ErrorTracker.TelemetryTest do
5151

5252
assert_receive {:telemetry_event, [:error_tracker, :error, :unresolved], _, %{error: %Error{}}}
5353
end
54+
55+
test "the :unresolved event carries the muted flag for muted errors" do
56+
{exception, stacktrace} =
57+
try do
58+
raise "This is a test"
59+
rescue
60+
e -> {e, __STACKTRACE__}
61+
end
62+
63+
%Occurrence{error: error = %Error{}} = ErrorTracker.report(exception, stacktrace)
64+
{:ok, error} = ErrorTracker.mute(error)
65+
{:ok, _resolved} = ErrorTracker.resolve(error)
66+
67+
# Trigger the same error again. It exists and is resolved, so the
68+
# :unresolved telemetry event fires — and its `error` struct must
69+
# reflect the muted flag stored in the DB.
70+
ErrorTracker.report(exception, stacktrace)
71+
72+
assert_receive {:telemetry_event, [:error_tracker, :error, :unresolved], _, %{error: %Error{muted: true}}}
73+
end
5474
end

0 commit comments

Comments
 (0)