Skip to content

Commit 0802b87

Browse files
NSHkrNSHkr
authored andcommitted
fb
1 parent a46db9e commit 0802b87

File tree

7 files changed

+44
-31
lines changed

7 files changed

+44
-31
lines changed

lib/elixir_scope/foundation/config.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ defmodule ElixirScope.Foundation.Config do
123123
end
124124

125125
@impl Access
126+
@spec get_and_update(ElixirScope.Foundation.Config.t(), any(), (any() -> :pop | {any(), any()})) ::
127+
{any(), struct()}
126128
def get_and_update(%__MODULE__{} = config, key, function) do
127129
map_config = Map.from_struct(config)
128130

lib/elixir_scope/foundation/telemetry.ex

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule ElixirScope.Foundation.Telemetry do
77
"""
88

99
require Logger
10-
alias ElixirScope.Foundation.{Utils, Error, ErrorContext}
10+
alias ElixirScope.Foundation.{Utils} #, Error} #, ErrorContext}
1111

1212
@telemetry_events [
1313
# Configuration events
@@ -39,26 +39,34 @@ defmodule ElixirScope.Foundation.Telemetry do
3939

4040
@spec measure_event([atom(), ...], map(), (() -> t)) :: t when t: var
4141
def measure_event(event_name, metadata \\ %{}, fun) when is_function(fun, 0) do
42-
context = ErrorContext.new(__MODULE__, :measure_event,
43-
metadata: %{event_name: event_name, metadata: metadata})
44-
4542
start_time = Utils.monotonic_timestamp()
4643

47-
result = ErrorContext.with_context(context, fun)
44+
# Don't wrap in ErrorContext - let exceptions propagate
45+
try do
46+
result = fun.()
4847

49-
end_time = Utils.monotonic_timestamp()
50-
duration = end_time - start_time
48+
end_time = Utils.monotonic_timestamp()
49+
duration = end_time - start_time
5150

52-
measurements = %{duration: duration, timestamp: end_time}
53-
:telemetry.execute(event_name, measurements, metadata)
51+
measurements = %{duration: duration, timestamp: end_time}
52+
:telemetry.execute(event_name, measurements, metadata)
53+
54+
result
55+
rescue
56+
exception ->
57+
# Still measure the duration even if it failed
58+
end_time = Utils.monotonic_timestamp()
59+
duration = end_time - start_time
60+
61+
measurements = %{duration: duration, timestamp: end_time}
62+
error_metadata = Map.put(metadata, :exception, exception)
63+
64+
# Emit both the normal event and an error event
65+
:telemetry.execute(event_name, measurements, error_metadata)
66+
emit_error_event(event_name, metadata, {:error, exception})
5467

55-
# If there was an error in the function, we still measured it
56-
case result do
57-
{:error, _} = error ->
58-
emit_error_event(event_name, metadata, error)
59-
error
60-
other ->
61-
other
68+
# Re-raise the exception so the test can catch it
69+
reraise exception, __STACKTRACE__
6270
end
6371
end
6472

@@ -125,19 +133,18 @@ defmodule ElixirScope.Foundation.Telemetry do
125133
""")
126134
end
127135

128-
@spec emit_error_event([atom(), ...], map(), {:error, term()}) :: :ok
129-
defp emit_error_event(event_name, metadata, error) do
130-
error_metadata = case error do
131-
{:error, %Error{} = err} ->
132-
Map.merge(metadata, %{
133-
error_code: err.code,
134-
error_message: err.message
135-
})
136-
{:error, reason} ->
137-
Map.merge(metadata, %{
138-
error_type: :external_error,
139-
error_message: inspect(reason)
140-
})
136+
@spec emit_error_event([atom(), ...], map(), {:error, struct()}) :: :ok
137+
defp emit_error_event(event_name, metadata, {:error, err}) do
138+
error_metadata = if err.__struct__ == ElixirScope.Foundation.Error do
139+
Map.merge(metadata, %{
140+
error_code: err.code,
141+
error_message: err.message
142+
})
143+
else
144+
Map.merge(metadata, %{
145+
error_type: :external_error,
146+
error_message: inspect(err)
147+
})
141148
end
142149

143150
measurements = %{error_count: 1, timestamp: Utils.monotonic_timestamp()}

test/support/foundation_test_helpers.ex renamed to lib/elixir_scope/foundation/test_helpers.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ defmodule ElixirScope.Foundation.TestHelpers do
4646
original_config = Config.get()
4747

4848
# Apply overrides
49-
test_config = deep_merge_config(original_config, config_overrides)
49+
_test_config = deep_merge_config(original_config, config_overrides)
5050

5151
try do
5252
# This would require additional Config API in a real implementation

test/smoke/foundation_smoke_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule ElixirScope.Smoke.FoundationTest do
55
"""
66

77
use ExUnit.Case
8+
@moduletag :foundation
89

910
alias ElixirScope.Foundation
1011
alias ElixirScope.Foundation.{Config, Events, Utils, Telemetry, Error}

test/unit/foundation/config_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ElixirScope.Foundation.ConfigTest do
22
use ExUnit.Case, async: false # Config tests must be synchronous
3+
@moduletag :foundation
34

45
alias ElixirScope.Foundation.{Config, Error}
56
alias ElixirScope.Foundation.TestHelpers

test/unit/foundation/events_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule ElixirScope.Foundation.EventsTest do
22
use ExUnit.Case, async: true
3+
@moduletag :foundation
34

4-
alias ElixirScope.Foundation.{Events, Utils, Error}
5+
alias ElixirScope.Foundation.{Events}
56

67
describe "event creation" do
78
test "creates basic event with required fields" do

test/unit/foundation/utils_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ElixirScope.Foundation.UtilsTest do
22
use ExUnit.Case, async: true
3+
@moduletag :foundation
34

45
alias ElixirScope.Foundation.Utils
56

0 commit comments

Comments
 (0)