Skip to content

Commit 0034888

Browse files
authored
Various observability fixes (#40)
- `cowboy_telemetry` both provides HTTP status like "200", "200 OK", or "403 forbidden", and they are converted to prometheus labels as `200`, `200_ok`, `403_forbidden`... In the PR, only the integer part is kept to only export labels like `200`, `403` ... - Excludes the health check, the metric export, and the favicon from the set of HTTP resources that are monitored. The goal is to have a better understanding of the behavior of HTTP resources related to message publishing.
1 parent 9904d56 commit 0034888

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

neurow/lib/neurow/observability/http_interfaces_stats.ex

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,40 @@ defmodule Neurow.Observability.HttpInterfacesStats do
2929
end
3030

3131
def handle_event([:cowboy, :request, :stop], measurements, metadata, _config) do
32-
endpoint =
33-
case metadata[:req][:ref] do
34-
Neurow.PublicApi.Endpoint.HTTP -> :public_api
35-
Neurow.InternalApi.Endpoint.HTTP -> :internal_api
36-
end
32+
if monitor_path?(metadata[:req][:path]) do
33+
interface =
34+
case metadata[:req][:ref] do
35+
Neurow.PublicApi.Endpoint.HTTP -> :public_api
36+
Neurow.InternalApi.Endpoint.HTTP -> :internal_api
37+
end
3738

38-
duration_ms = System.convert_time_unit(measurements[:duration], :native, :millisecond)
39-
resp_status = metadata[:resp_status]
39+
duration_ms = System.convert_time_unit(measurements[:duration], :native, :millisecond)
40+
resp_status = metadata[:resp_status]
4041

41-
Counter.inc(name: :http_request_count, labels: [endpoint, resp_status])
42-
Summary.observe([name: :http_request_duration_ms, labels: [endpoint]], duration_ms)
42+
Counter.inc(name: :http_request_count, labels: [interface, trim_http_status(resp_status)])
43+
Summary.observe([name: :http_request_duration_ms, labels: [interface]], duration_ms)
44+
end
45+
end
46+
47+
@unmonitored_request_paths [
48+
"/ping",
49+
"/favicon.ico",
50+
"/metrics"
51+
]
52+
53+
defp monitor_path?(path) do
54+
!Enum.member?(@unmonitored_request_paths, path)
55+
end
56+
57+
defp trim_http_status(http_status) when is_binary(http_status) do
58+
String.split(http_status, " ") |> Enum.at(0)
59+
end
60+
61+
defp trim_http_status(http_status) when is_integer(http_status) do
62+
Integer.to_string(http_status)
63+
end
64+
65+
defp trim_http_status(http_status) when is_atom(http_status) do
66+
Atom.to_string(http_status)
4367
end
4468
end

0 commit comments

Comments
 (0)