Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: allow to configure the prefix for the telemtry events" #624

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions lib/tesla/middleware/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ if Code.ensure_loaded?(:telemetry) do
## Options

- `:metadata` - additional metadata passed to telemetry events
- `:prefix` - prefix for telemetry events. Defaults to `:tesla`

## Telemetry Events

Expand Down Expand Up @@ -92,10 +91,9 @@ if Code.ensure_loaded?(:telemetry) do
@impl Tesla.Middleware
def call(env, next, opts) do
metadata = opts[:metadata] || %{}
prefix = opts[:prefix] || :tesla
start_time = System.monotonic_time()

emit_start(prefix, Map.merge(metadata, %{env: env}))
emit_start(Map.merge(metadata, %{env: env}))

try do
Tesla.run(env, next)
Expand All @@ -105,7 +103,6 @@ if Code.ensure_loaded?(:telemetry) do
duration = System.monotonic_time() - start_time

emit_exception(
prefix,
duration,
Map.merge(metadata, %{env: env, kind: kind, reason: reason, stacktrace: stacktrace})
)
Expand All @@ -115,56 +112,56 @@ if Code.ensure_loaded?(:telemetry) do
{:ok, env} = result ->
duration = System.monotonic_time() - start_time

emit_stop(prefix, duration, Map.merge(metadata, %{env: env}))
emit_legacy_event(prefix, duration, result)
emit_stop(duration, Map.merge(metadata, %{env: env}))
emit_legacy_event(duration, result)

result

{:error, reason} = result ->
duration = System.monotonic_time() - start_time

emit_stop(prefix, duration, Map.merge(metadata, %{env: env, error: reason}))
emit_legacy_event(prefix, duration, result)
emit_stop(duration, Map.merge(metadata, %{env: env, error: reason}))
emit_legacy_event(duration, result)

result
end
end

defp emit_start(prefix, metadata) do
defp emit_start(metadata) do
:telemetry.execute(
[prefix, :request, :start],
[:tesla, :request, :start],
%{system_time: System.system_time()},
metadata
)
end

defp emit_stop(prefix, duration, metadata) do
defp emit_stop(duration, metadata) do
:telemetry.execute(
[prefix, :request, :stop],
[:tesla, :request, :stop],
%{duration: duration},
metadata
)
end

if @disable_legacy_event do
defp emit_legacy_event(_prefix, _duration, _result) do
defp emit_legacy_event(_duration, _result) do
:ok
end
else
defp emit_legacy_event(prefix, duration, result) do
defp emit_legacy_event(duration, result) do
duration = System.convert_time_unit(duration, :native, :microsecond)

:telemetry.execute(
[prefix, :request],
[:tesla, :request],
%{request_time: duration},
%{result: result}
)
end
end

defp emit_exception(prefix, duration, metadata) do
defp emit_exception(duration, metadata) do
:telemetry.execute(
[prefix, :request, :exception],
[:tesla, :request, :exception],
%{duration: duration},
metadata
)
Expand Down
Loading