Skip to content

Commit a1d215c

Browse files
authored
Merge pull request #157 from cedricgarcia-wttj/feat-add-telemetry-support
feat: add :telemetry support with HTTP client events
2 parents 2ce3982 + b7d74c8 commit a1d215c

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ fields can be accessed in the `resources` field of the structure.
106106
}
107107
```
108108

109+
### Telemetry Integration
110+
111+
Chargebeex now supports `:telemetry`, allowing users to instrument and monitor API calls using telemetry events. This enables integration with various observability tools.
112+
113+
#### Telemetry Events
114+
115+
Chargebeex emits the following telemetry events for each API request:
116+
117+
- `[:chargebeex, :request, :start]`: Emitted when an API request starts.
118+
- `[:chargebeex, :request, :stop]`: Emitted when an API request completes.
119+
120+
When making API calls, telemetry events will automatically be emitted:
121+
122+
```elixir
123+
Chargebeex.Client.get("/customers")
124+
```
125+
126+
This will emit :start and :stop telemetry events with metadata such as the HTTP method, URL, and status code, which can be processed by your telemetry handler.
127+
128+
For more information on `:telemetry`, visit the [official documentation](https://hexdocs.pm/telemetry/).
129+
109130
## Run tests
110131

111132
```sh

lib/chargebeex/client.ex

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,45 @@ defmodule Chargebeex.Client do
4646

4747
def get(path, params \\ %{}, opts \\ []) do
4848
site = Keyword.get(opts, :site, :default)
49-
5049
url = endpoint(site, path, params)
5150

52-
case apply(http_client(), :get, [url, "", default_headers(site, opts)]) do
53-
{:ok, status_code, headers, body} when status_code in 200..299 ->
54-
{:ok, status_code, headers, get_body_from_response(body)}
51+
:telemetry.span([:chargebeex, :request], %{method: :get, url: url, params: params}, fn ->
52+
{status, status_code, _, _} =
53+
result =
54+
case apply(http_client(), :get, [url, "", default_headers(site, opts)]) do
55+
{:ok, status_code, headers, body} when status_code in 200..299 ->
56+
{:ok, status_code, headers, get_body_from_response(body)}
5557

56-
{:ok, status_code, headers, body} ->
57-
{:error, status_code, headers, get_body_from_response(body)}
58-
end
58+
{:ok, status_code, headers, body} ->
59+
{:error, status_code, headers, get_body_from_response(body)}
60+
end
61+
62+
{result, %{status: status, status_code: status_code}}
63+
end)
5964
end
6065

6166
def post(path, params \\ %{}, opts \\ []) do
6267
site = Keyword.get(opts, :site, :default)
68+
url = endpoint(site, path)
6369

6470
body =
6571
params
6672
|> transform_arrays_for_chargebee
6773
|> Plug.Conn.Query.encode()
6874

69-
url = endpoint(site, path)
75+
:telemetry.span([:chargebeex, :request], %{method: :post, url: url, params: params}, fn ->
76+
{status, status_code, _, _} =
77+
result =
78+
case apply(http_client(), :post, [url, body, default_headers(site, opts, :post)]) do
79+
{:ok, status_code, headers, body} when status_code in 200..299 ->
80+
{:ok, status_code, headers, get_body_from_response(body)}
7081

71-
case apply(http_client(), :post, [url, body, default_headers(site, opts, :post)]) do
72-
{:ok, status_code, headers, body} when status_code in 200..299 ->
73-
{:ok, status_code, headers, get_body_from_response(body)}
82+
{:ok, status_code, headers, body} ->
83+
{:error, status_code, headers, get_body_from_response(body)}
84+
end
7485

75-
{:ok, status_code, headers, body} ->
76-
{:error, status_code, headers, get_body_from_response(body)}
77-
end
86+
{result, %{status: status, status_code: status_code}}
87+
end)
7888
end
7989

8090
defp get_body_from_response(body) do

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ defmodule Chargebeex.MixProject do
5454
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
5555
{:typed_struct, "~> 0.3.0"},
5656
{:dialyxir, ">= 0.0.0", only: [:dev], runtime: false},
57-
{:exconstructor, "~> 1.2.7"}
57+
{:exconstructor, "~> 1.2.7"},
58+
{:telemetry, "~> 1.3"}
5859
]
5960
end
6061

0 commit comments

Comments
 (0)