Skip to content

Commit

Permalink
add Stripe instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
agrobbin committed Jan 16, 2023
1 parent f452b84 commit 41e9d45
Show file tree
Hide file tree
Showing 20 changed files with 391 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ elsif ruby_version?('2.3')
gem 'sidekiq'
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '~> 1.3.6'
gem 'stripe', '~> 5.15'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0', '< 2.0.0'
Expand Down Expand Up @@ -654,6 +655,7 @@ elsif ruby_version?('2.4')
gem 'sidekiq'
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '~> 1.3.6'
gem 'stripe', '~> 6.0'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0', '< 2.0.0'
Expand Down Expand Up @@ -916,6 +918,7 @@ elsif ruby_version?('2.5')
gem 'sidekiq'
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '~> 1.4.1', platform: :ruby
gem 'stripe', '~> 7.0'
gem 'jdbc-sqlite3', '>= 3.28', platform: :jruby
gem 'sucker_punch'
gem 'typhoeus'
Expand Down Expand Up @@ -1164,6 +1167,7 @@ elsif ruby_version?('2.6')
gem 'sidekiq', '~> 6.4.1' # TODO: Support sidekiq 6.5.8
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '~> 1.4.1', platform: :ruby
gem 'stripe', '~> 8.0'
gem 'jdbc-sqlite3', '>= 3.28', platform: :jruby
gem 'sucker_punch'
gem 'typhoeus'
Expand Down Expand Up @@ -1391,6 +1395,7 @@ elsif ruby_version?('2.7')
gem 'sidekiq', '~> 6' # TODO: Support sidekiq 7.x
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '~> 1.4.1'
gem 'stripe'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0'
Expand Down Expand Up @@ -1522,6 +1527,7 @@ elsif ruby_version?('3.0') || ruby_version?('3.1')
gem 'sidekiq', '~> 6' # TODO: Support sidekiq 7.x
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '>= 1.4.2', platform: :ruby
gem 'stripe'
gem 'jdbc-sqlite3', '>= 3.28', platform: :jruby
gem 'sucker_punch'
gem 'typhoeus'
Expand Down Expand Up @@ -1653,6 +1659,7 @@ elsif ruby_version?('3.2')
gem 'sidekiq'
gem 'sneakers', '>= 2.12.0'
gem 'sqlite3', '>= 1.4.2'
gem 'stripe'
gem 'sucker_punch'
gem 'typhoeus'
gem 'que', '>= 1.0.0'
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ namespace :spec do
:sidekiq,
:sinatra,
:sneakers,
:stripe,
:sucker_punch,
:suite
].each do |contrib|
Expand Down Expand Up @@ -314,6 +315,7 @@ task :ci do
declare '❌ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:shoryuken'
declare '✅ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:sidekiq'
declare '❌ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:sneakers'
declare '❌ 2.1 / ❌ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:stripe'
declare '✅ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:sucker_punch'
declare '✅ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ jruby' => 'bundle exec appraisal contrib rake spec:suite'

Expand Down
22 changes: 22 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ To contribute, check out the [contribution guidelines][contribution docs] and [d
- [Sidekiq](#sidekiq)
- [Sinatra](#sinatra)
- [Sneakers](#sneakers)
- [Stripe](#stripe)
- [Sucker Punch](#sucker-punch)
- [Additional configuration](#additional-configuration)
- [Custom logging](#custom-logging)
Expand Down Expand Up @@ -515,6 +516,7 @@ For a list of available integrations, and their configuration options, please re
| Sidekiq | `sidekiq` | `>= 3.5.4` | `>= 3.5.4` | *[Link](#sidekiq)* | *[Link](https://github.com/mperham/sidekiq)* |
| Sinatra | `sinatra` | `>= 1.4` | `>= 1.4` | *[Link](#sinatra)* | *[Link](https://github.com/sinatra/sinatra)* |
| Sneakers | `sneakers` | `>= 2.12.0` | `>= 2.12.0` | *[Link](#sneakers)* | *[Link](https://github.com/jondot/sneakers)* |
| Stripe | `stripe` | `>= 5.15.0` | `>= 5.15.0` | *[Link](#stripe)* | *[Link](https://github.com/stripe/stripe-ruby)* |
| Sucker Punch | `sucker_punch` | `>= 2.0` | `>= 2.0` | *[Link](#sucker-punch)* | *[Link](https://github.com/brandonhilkert/sucker_punch)* |
#### CI Visibility
Expand Down Expand Up @@ -2031,6 +2033,26 @@ end
| `tag_body` | Enable tagging of job message. `true` for on, `false` for off. | `false` |
| `error_handler` | Custom error handler invoked when a job raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring transient errors. | `proc { |span, error| span.set_error(error) unless span.nil? }` |

### Stripe

The Stripe integration traces Stripe API requests.

You can enable it through `Datadog.configure`:

```ruby
require 'ddtrace'
Datadog.configure do |c|
c.tracing.instrument :stripe, **options
end
```

`options` are the following keyword arguments:

| Key | Description | Default |
| --- | ----------- | ------- |
| `enabled` | Defines whether Stripe should be traced. Useful for temporarily disabling tracing. `true` or `false` | `true` |

### Sucker Punch

The `sucker_punch` integration traces all scheduled jobs:
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/ruby_2.3.8_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_2.4.10_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_2.5.9_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_2.6.10_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_2.7.6_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_3.0.4_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gemfiles/ruby_3.1.2_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/datadog/tracing/contrib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ module Contrib
require_relative 'contrib/sidekiq/integration'
require_relative 'contrib/sinatra/integration'
require_relative 'contrib/sneakers/integration'
require_relative 'contrib/stripe/integration'
require_relative 'contrib/sucker_punch/integration'
33 changes: 33 additions & 0 deletions lib/datadog/tracing/contrib/stripe/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative '../../configuration/settings'
require_relative '../ext'

module Datadog
module Tracing
module Contrib
module Stripe
module Configuration
# Custom settings for the Stripe integration
# @public_api
class Settings < Contrib::Configuration::Settings
option :enabled do |o|
o.default { env_to_bool(Ext::ENV_ENABLED, true) }
o.lazy
end

option :analytics_enabled do |o|
o.default { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) }
o.lazy
end

option :analytics_sample_rate do |o|
o.default { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) }
o.lazy
end
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/datadog/tracing/contrib/stripe/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Datadog
module Tracing
module Contrib
module Stripe
# Stripe integration constants
# @public_api Changing resource names, tag names, or environment variables creates breaking changes.
module Ext
ENV_ENABLED = 'DD_TRACE_STRIPE_ENABLED'
ENV_ANALYTICS_ENABLED = 'DD_TRACE_STRIPE_ANALYTICS_ENABLED'
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_STRIPE_ANALYTICS_SAMPLE_RATE'
SPAN_REQUEST = 'stripe.request'
SPAN_TYPE_REQUEST = 'custom'
TAG_COMPONENT = 'stripe'
TAG_OPERATION_REQUEST = 'request'
TAG_REQUEST_HTTP_STATUS = 'stripe.request.http_status'
TAG_REQUEST_ID = 'stripe.request.id'
TAG_REQUEST_METHOD = 'stripe.request.method'
TAG_REQUEST_NUM_RETRIES = 'stripe.request.num_retries'
TAG_REQUEST_PATH = 'stripe.request.path'
end
end
end
end
end
43 changes: 43 additions & 0 deletions lib/datadog/tracing/contrib/stripe/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative '../integration'
require_relative 'configuration/settings'
require_relative 'patcher'

module Datadog
module Tracing
module Contrib
module Stripe
# Description of Stripe integration
class Integration
include Contrib::Integration

MINIMUM_VERSION = Gem::Version.new('5.15.0')

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :stripe

def self.version
Gem.loaded_specs['stripe'] && Gem.loaded_specs['stripe'].version
end

def self.loaded?
!defined?(::Stripe).nil?
end

def self.compatible?
super && version >= MINIMUM_VERSION
end

def new_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/datadog/tracing/contrib/stripe/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true
# typed: false

require_relative '../patcher'
require_relative 'request'

module Datadog
module Tracing
module Contrib
module Stripe
# Provides instrumentation for `stripe` through the Stripe instrumentation framework
module Patcher
include Contrib::Patcher

module_function

def target_version
Integration.version
end

def patch
::Stripe::Instrumentation.subscribe(:request_begin, :datadog_tracing) { |event| Request.start_span(event) }
::Stripe::Instrumentation.subscribe(:request_end, :datadog_tracing) { |event| Request.finish_span(event) }
end
end
end
end
end
end
Loading

0 comments on commit 41e9d45

Please sign in to comment.