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

Add response HTTP status code to telemetry #113

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/snap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ defmodule Snap do
* `headers` - a list of the headers sent
* `body` - the body sent
* `result` - the result returned to the user
* `status` - the HTTP status code of the response

## Testing

Expand Down
10 changes: 8 additions & 2 deletions lib/snap/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule Snap.Request do
response_time = System.monotonic_time() - start_time

result = parse_response(response, json_library)
status = parse_status(response)

decode_time = System.monotonic_time() - response_time - start_time
total_time = response_time + decode_time
Expand All @@ -47,6 +48,7 @@ defmodule Snap.Request do
"""
Elasticsearch #{http_method_to_string(method)} request \
path=#{path} \
status=#{status} \
response=#{format_time_to_ms(response_time)}ms \
decode=#{format_time_to_ms(decode_time)}ms \
total=#{format_time_to_ms(total_time)}ms\
Expand All @@ -63,7 +65,7 @@ defmodule Snap.Request do

uri = URI.parse(url)

metadata = telemetry_metadata(method, uri, headers, body, result)
metadata = telemetry_metadata(method, uri, headers, body, status, result)

:telemetry.execute(event, measurements, metadata)

Expand Down Expand Up @@ -94,6 +96,9 @@ defmodule Snap.Request do
end
end

defp parse_status({:ok, %HTTPClient.Response{status: status}}), do: status
defp parse_status({:error, _}), do: nil

defp telemetry_prefix(cluster) do
config = cluster.config()

Expand All @@ -103,11 +108,12 @@ defmodule Snap.Request do
end)
end

defp telemetry_metadata(method, uri, _headers, body, result) do
defp telemetry_metadata(method, uri, _headers, body, status, result) do
method_str = http_method_to_string(method)

%{
method: method_str,
status: status,
host: uri.host,
port: uri.port,
path: uri.path,
Expand Down
3 changes: 2 additions & 1 deletion test/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule Snap.TelemetryTest do
path: "/_cluster/health",
host: "localhost",
port: 9200,
body: nil
body: nil,
status: 200
} = metadata

assert measurements.total_time ==
Expand Down
Loading