Add OpenTelemetry tracing support for Stripe API calls #2177
+506
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Users cannot trace Stripe API calls through their observability systems without manually wrapping every API call. The SDK formats URLs like
/v1/charges/ch_123but OpenTelemetry requires URL templates like/v1/charges/{id}to prevent cardinality explosion in metrics.Issue #2104 requested built-in OpenTelemetry integration with proper
url.templatesemantic convention support.Solution
Add opt-in OpenTelemetry instrumentation that automatically creates spans for all Stripe HTTP operations. URL templates are tracked separately from formatted paths and propagated via context to comply with OpenTelemetry semantic conventions v1.24.
Users enable tracing with one line:
All subsequent API calls automatically create spans with proper attributes (
http.method,http.status_code,url.template,stripe.request_id, etc.).Changes
New files:
otel.go- OpenTelemetry instrumentation (134 lines)otel_test.go- Test coverage (203 lines, 9 test functions)Modified files:
params.go- AddedURLTemplatefield for template trackingstripe.go- InstrumentedDo(),DoStreaming(),CallRaw(),CallStreaming()+ addedFormatURLPathWithTemplate()helperexample_test.go- AddedExampleSetTracerProvider()andExampleFormatURLPathWithTemplate()go.mod/go.sum- Added OpenTelemetry dependencies (v1.24.0)Testing
Tests cover TracerProvider configuration, span creation with attributes, context propagation, error handling, streaming operations, and URL template formatting.
Breaking Changes
None. Implementation is opt-in; backward compatible and non-intrusive.
Closes #2104