Skip to content

Commit

Permalink
[+] mongo service name resolver for multi cluster mongo (#1423)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Costa <marco.costa@datadoghq.com>
  • Loading branch information
skcc321 and marcotc authored Nov 4, 2021
1 parent 3995004 commit 93ab4ae
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
31 changes: 31 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,37 @@ Where `options` is an optional `Hash` that accepts the following parameters:
| `quantize` | Hash containing options for quantization. May include `:show` with an Array of keys to not quantize (or `:all` to skip quantization), or `:exclude` with Array of keys to exclude entirely. | `{ show: [:collection, :database, :operation] }` |
| `service_name` | Service name used for `mongo` instrumentation | `'mongodb'` |

**Configuring trace settings per connection**

You can configure trace settings per connection by using the `describes` option:

```ruby
# Provide a `:describes` option with a connection key.
# Any of the following keys are acceptable and equivalent to one another.
# If a block is provided, it yields a Settings object that
# accepts any of the configuration options listed above.
Datadog.configure do |c|
# Network connection string
c.use :mongo, describes: '127.0.0.1:27017', service_name: 'mongo-primary'
# Network connection regular expression
c.use :mongo, describes: /localhost.*/, service_name: 'mongo-secondary'
end
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'artists')
collection = client[:people]
collection.insert_one({ name: 'Steve' })
# Traced call will belong to `mongo-primary` service
client = Mongo::Client.new([ 'localhost:27017' ], :database => 'artists')
collection = client[:people]
collection.insert_one({ name: 'Steve' })
# Traced call will belong to `mongo-secondary` service
```

When multiple `describes` configurations match a connection, the latest configured rule that matches will be applied.

### MySQL2

The MySQL2 integration traces any SQL command sent through `mysql2` gem.
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace/contrib/mongodb/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.included(base)
module InstanceMethods
def datadog_pin
@datadog_pin ||= begin
service = Datadog.configuration[:mongo][:service_name]
service = Datadog.configuration[:mongo, seed][:service_name]

Datadog::Pin.new(
service,
Expand Down
5 changes: 5 additions & 0 deletions lib/ddtrace/contrib/mongodb/integration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# typed: false
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/configuration/resolvers/pattern_resolver'
require 'ddtrace/contrib/mongodb/configuration/settings'
require 'ddtrace/contrib/mongodb/patcher'

Expand Down Expand Up @@ -33,6 +34,10 @@ def default_configuration
def patcher
Patcher
end

def resolver
@resolver ||= Contrib::Configuration::Resolvers::PatternResolver.new
end
end
end
end
Expand Down
54 changes: 54 additions & 0 deletions spec/ddtrace/contrib/mongodb/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@
it_behaves_like 'a peer service span'
end

context 'with a different service name using describes option' do
let(:primary_service) { 'mongodb-primary' }
let(:secondary_service) { 'mongodb-secondary' }
let(:secondary_client) { Mongo::Client.new(["#{secondary_host}:#{port}"], client_options) }
let(:secondary_host) { 'localhost' }

before do
Datadog.configure do |c|
c.use :mongo, describes: /#{host}/ do |mongo|
mongo.service_name = primary_service
end

c.use :mongo, describes: /#{secondary_host}/ do |mongo|
mongo.service_name = secondary_service
end
end
end

context 'primary client' do
subject { client[collection].insert_one(name: 'FKA Twigs') }

it 'produces spans with the correct service' do
subject
expect(spans).to have(1).items
expect(spans.first.service).to eq(primary_service)
end

it_behaves_like 'a peer service span'
end

context 'secondary client' do
around do |example|
without_warnings do
# Reset before and after each example; don't allow global state to linger.
Datadog.registry[:mongo].reset_configuration!
example.run
Datadog.registry[:mongo].reset_configuration!
secondary_client.database.drop if drop_database?
secondary_client.close
end
end

subject { secondary_client[collection].insert_one(name: 'FKA Twigs') }

it 'produces spans with the correct service' do
subject
expect(spans).to have(1).items
expect(spans.first.service).to eq(secondary_service)
end

it_behaves_like 'a peer service span'
end
end

context 'to disable the tracer' do
before { tracer.enabled = false }

Expand Down

0 comments on commit 93ab4ae

Please sign in to comment.