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

Example of using instrumentation in code #1467

Open
rlgreen91 opened this issue Oct 4, 2024 · 2 comments
Open

Example of using instrumentation in code #1467

rlgreen91 opened this issue Oct 4, 2024 · 2 comments

Comments

@rlgreen91
Copy link

Is your feature request related to a problem? Please describe.

How exactly do I use the instrumentation option in my code? I see the information in the docs noting the feature and with an example of what the configuration for the instrumentation might be, but where do I actually place this in my code? It doesn't appear to be a configuration option.

Describe the solution you'd like

A full example of where to place the instrumentation code relative to using the gem.

Describe alternatives you've considered

No response

Additional context

No response

@prathmesh-stripe
Copy link
Contributor

Place it before you make a call to Stripe API. As long as Stripe module is accessible from your script and you can make API calls to Stripe, this would work.

Stripe::Instrumentation.subscribe(:request_end) do |request_event|
  # Filter out high-cardinality ids from `path`
  path_parts = request_event.path.split("/").drop(2)
  resource = path_parts.map { |part| part.match?(/\A[a-z_]+\z/) ? part : ":id" }.join("/")

  tags = {
    method: request_event.method,
    resource: resource,
    code: request_event.http_status,
    retries: request_event.num_retries
  }
  StatsD.distribution('stripe_request', request_event.duration, tags: tags)
end

Stripe-ruby will invoke whatever is in the block when a request starts or ends depending on the hook you've subscribed to.

@rlgreen91
Copy link
Author

Thanks @prathmesh-stripe ! To clarify, would I need to list this block before every API call? Or could I put this in say, an initializer file in Rails and it will automatically execute the block for every API call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants