-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metadata to telemetry events
Added telemetry support to webhook handler and changed metadata included in API requests. API requests metadata contains some low cardinality fields that can be useful when visualising these metrics in other tools. For example using 'stripe.request.stop' event we can infer metrics for RED method and have visibility into all Stripe API endpoints. 'url' passed in to 'do_perform_request_and_retry' contains query params which can contain sensitive data such as emails so it is URI-parsed and all params are dropped. Webhooks are often important part of integration with Stripe and having telemetry in place for that is good idea too imo.
- Loading branch information
Showing
4 changed files
with
178 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ defmodule Stripe.APITest do | |
import Mox | ||
use Stripe.StripeCase | ||
|
||
def telemetry_handler_fn(name, measurements, metadata, _config) do | ||
send(self(), {:telemetry_event, name, measurements, metadata}) | ||
end | ||
|
||
test "works with non existent responses without issue" do | ||
{:error, %Stripe.Error{extra: %{http_status: 404}}} = | ||
Stripe.API.request(%{}, :get, "/", %{}, []) | ||
|
@@ -87,6 +91,41 @@ defmodule Stripe.APITest do | |
end | ||
end | ||
|
||
describe "telemetry" do | ||
test "requests emit :start, :stop telemetry events", %{test: test} do | ||
:telemetry.attach_many( | ||
"#{test}", | ||
[[:stripe, :request, :start], [:stripe, :request, :stop]], | ||
&__MODULE__.telemetry_handler_fn/4, | ||
nil | ||
) | ||
|
||
%{query: ~s|email: "[email protected]"|} | ||
|> Stripe.API.request(:get, "/v1/customers/search", %{}, []) | ||
|
||
assert_received({ | ||
:telemetry_event, | ||
[:stripe, :request, :start], | ||
%{monotonic_time: _}, | ||
%{telemetry_span_context: _} | ||
}) | ||
|
||
assert_received({ | ||
:telemetry_event, | ||
[:stripe, :request, :stop], | ||
%{monotonic_time: _, duration: _}, | ||
%{ | ||
telemetry_span_context: _, | ||
endpoint: "/v1/customers/search", | ||
attempt: 0, | ||
method: :get, | ||
status: 200, | ||
stripe_api_version: _ | ||
} | ||
}) | ||
end | ||
end | ||
|
||
@tag :skip | ||
test "gets default api version" do | ||
Stripe.API.request(%{}, :get, "products", %{}, []) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters