Skip to content

Commit 2997d20

Browse files
committed
Add option for Mongo span resource as JSON
1 parent b9e1364 commit 2997d20

File tree

10 files changed

+149
-103
lines changed

10 files changed

+149
-103
lines changed

Matrixfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
},
122122
'mongodb' => {
123123
'mongo-latest' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby',
124-
# 'mongo-min' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby',
124+
'mongo-min' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby',
125125
},
126126
'mysql2' => {
127127
'relational_db' => '✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby'

docs/GettingStarted.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ require 'mongo'
11441144
require 'datadog'
11451145

11461146
Datadog.configure do |c|
1147-
c.tracing.instrument :mongo, **options
1147+
c.tracing.instrument :mongo, json_command: true, **options
11481148
end
11491149

11501150
# Create a MongoDB client and use it as usual
@@ -1164,6 +1164,7 @@ Datadog.configure_onto(client, **options)
11641164
| `service_name` | `DD_TRACE_MONGO_SERVICE_NAME` | `String` | Name of application running the `mongo` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `mongodb` |
11651165
| `peer_service` | `DD_TRACE_MONGO_PEER_SERVICE` | `String` | Name of external service the application connects to | `nil` |
11661166
| `quantize` | | `Hash` | 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] }` |
1167+
| `json_command` | `DD_TRACE_MONGO_JSON_COMMAND` | `Bool` | (Recommended) Serialize the MongoDB command as JSON, which enables full support for introspection in the Datadog App. | `false` |
11671168

11681169
**Configuring trace settings per connection**
11691170

lib/datadog/tracing/contrib/mongodb/configuration/settings.rb

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class Settings < Contrib::Configuration::Settings
4848
o.type :string, nilable: true
4949
o.env Ext::ENV_PEER_SERVICE
5050
end
51+
52+
# Serializes the command to JSON format, which is the desired format for the agent and Datadog UI.
53+
# Setting this to false is deprecated and does not have any advantages.
54+
option :json_command do |o|
55+
o.type :bool
56+
o.env Ext::ENV_JSON_COMMAND
57+
o.default false
58+
end
5159
end
5260
end
5361
end

lib/datadog/tracing/contrib/mongodb/ext.rb

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Ext
1313
# @!visibility private
1414
ENV_ANALYTICS_ENABLED = 'DD_TRACE_MONGO_ANALYTICS_ENABLED'
1515
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_MONGO_ANALYTICS_SAMPLE_RATE'
16+
ENV_JSON_COMMAND = 'DD_TRACE_MONGO_JSON_COMMAND'
1617
DEFAULT_PEER_SERVICE_NAME = 'mongodb'
1718
SPAN_COMMAND = 'mongo.cmd'
1819
SPAN_TYPE_COMMAND = 'mongodb'

lib/datadog/tracing/contrib/mongodb/subscribers.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'json'
4+
35
require_relative '../analytics'
46
require_relative 'ext'
57
require_relative '../ext'
@@ -30,7 +32,7 @@ def started(event)
3032

3133
# build a quantized Query using the Parser module
3234
query = MongoDB.query_builder(event.command_name, event.database_name, event.command)
33-
serialized_query = query.to_s
35+
serialized_query = serialize_query(query)
3436

3537
if datadog_configuration[:peer_service]
3638
span.set_tag(
@@ -109,6 +111,21 @@ def succeeded(event)
109111

110112
private
111113

114+
def serialize_query(query)
115+
if datadog_configuration[:json_command]
116+
query.to_json
117+
else
118+
# Incorrect Hash#to_s serialization. The Mongo command should only be encoded as JSON.
119+
# This code path should be removed, and is only kept to avoid a breaking change.
120+
Datadog::Core.log_deprecation(key: :mongo_json_command) do
121+
'MongoDB integration: `json_command: false` causes invalid command serialization. '\
122+
'Use `json_command: true` or `DD_TRACE_MONGO_JSON_COMMAND=1` instead.'
123+
end
124+
125+
query.to_s
126+
end
127+
end
128+
112129
def get_span(event)
113130
Thread.current[:datadog_mongo_span] \
114131
&& Thread.current[:datadog_mongo_span][event.request_id]

sig/datadog/tracing/contrib/mongodb/ext.rbs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Datadog
55
module Ext
66
ENV_ENABLED: "DD_TRACE_MONGO_ENABLED"
77

8+
ENV_JSON_COMMAND: "DD_TRACE_MONGO_JSON_COMMAND"
89
ENV_PEER_SERVICE: "DD_TRACE_MONGO_PEER_SERVICE"
910
ENV_SERVICE_NAME: "DD_TRACE_MONGO_SERVICE_NAME"
1011

sig/datadog/tracing/contrib/mongodb/parsers.rbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Datadog
88

99
DEFAULT_OPTIONS: { exclude: untyped, show: untyped }
1010
PLACEHOLDER: "?"
11-
def self?.query_builder: (untyped command_name, untyped database_name, untyped command) -> untyped
11+
def self?.query_builder: (untyped command_name, untyped database_name, untyped command) -> Hash[untyped, untyped]
1212

1313
def self?.quantization_options: () -> untyped
1414

sig/datadog/tracing/contrib/mongodb/subscribers.rbs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Datadog
1313

1414
def get_span: (untyped event) -> untyped
1515

16+
def serialize_query: (Hash[untyped, untyped] query) -> string
17+
1618
def set_span: (untyped event, untyped span) -> untyped
1719

1820
def clear_span: (untyped event) -> (nil | untyped)

0 commit comments

Comments
 (0)