Skip to content

Commit deb8a69

Browse files
committed
Add ability to specify service names by domain
1 parent bcfeb95 commit deb8a69

File tree

7 files changed

+42
-2
lines changed

7 files changed

+42
-2
lines changed

docs/GettingStarted.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ Where `options` is an optional `Hash` that accepts the following parameters:
685685
| `error_handler` | A `Proc` that accepts a `response` parameter. If it evaluates to a *truthy* value, the trace span is marked as an error. By default only sets 5XX responses as errors. | `nil` |
686686
| `service_name` | Service name for Excon instrumentation. When provided to middleware for a specific connection, it applies only to that connection object. | `'excon'` |
687687
| `split_by_domain` | Uses the request domain as the service name when set to `true`. | `false` |
688+
| `split_by_domain_map` | When splitting by domain, provide a hash of regex => service name to override the service name for matched domains. | `{}` |
688689
| `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
689690

690691
**Configuring connections to use different settings**
@@ -743,6 +744,7 @@ Where `options` is an optional `Hash` that accepts the following parameters:
743744
| `error_handler` | A `Proc` that accepts a `response` parameter. If it evaluates to a *truthy* value, the trace span is marked as an error. By default only sets 5XX responses as errors. | `nil` |
744745
| `service_name` | Service name for Faraday instrumentation. When provided to middleware for a specific connection, it applies only to that connection object. | `'faraday'` |
745746
| `split_by_domain` | Uses the request domain as the service name when set to `true`. | `false` |
747+
| `split_by_domain_map` | When splitting by domain, provide a hash of regex => service name to override the service name for matched domains. | `{}` |
746748
| `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
747749
748750
### Grape

lib/ddtrace/contrib/excon/configuration/settings.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Settings < Contrib::Configuration::Settings
2121
option :error_handler
2222
option :service_name, default: Ext::SERVICE_NAME
2323
option :split_by_domain, default: false
24+
option :split_by_domain_map, default: {}
2425
end
2526
end
2627
end

lib/ddtrace/contrib/excon/middleware.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,15 @@ def propagate!(span, datum)
146146

147147
def service_name(datum)
148148
# TODO: Change this to implement more sensible multiplexing
149-
split_by_domain? ? datum[:host] : @options[:service_name]
149+
if split_by_domain?
150+
host = datum[:host]
151+
@options[:split_by_domain_map].each do |matcher, service_name|
152+
return service_name if matcher =~ host
153+
end
154+
return host
155+
end
156+
157+
@options[:service_name]
150158
end
151159
end
152160
end

lib/ddtrace/contrib/faraday/configuration/settings.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Settings < Contrib::Configuration::Settings
2626
option :error_handler, default: DEFAULT_ERROR_HANDLER
2727
option :service_name, default: Ext::SERVICE_NAME
2828
option :split_by_domain, default: false
29+
option :split_by_domain_map, default: {}
2930
end
3031
end
3132
end

lib/ddtrace/contrib/faraday/middleware.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ def tracer
6767
end
6868

6969
def service_name(env)
70-
return env[:url].host if options[:split_by_domain]
70+
if options[:split_by_domain]
71+
host = env[:url].host
72+
options[:split_by_domain_map].each do |matcher, service_name|
73+
return service_name if matcher =~ host
74+
end
75+
return host
76+
end
7177

7278
options[:service_name]
7379
end

spec/ddtrace/contrib/excon/instrumentation_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@
163163
end
164164
end
165165

166+
context 'when split by domain with a domain map' do
167+
subject!(:response) { connection.get(path: '/success') }
168+
let(:configuration_options) { super().merge(split_by_domain: true, split_by_domain_map: { /example\.com/ => 'bar' }) }
169+
after(:each) do
170+
Datadog.configuration[:excon][:split_by_domain] = false
171+
Datadog.configuration[:excon][:split_by_domain_map] = {}
172+
end
173+
174+
it 'overrides the domain from the map' do
175+
expect(request_span.service).to eq('bar')
176+
end
177+
end
178+
166179
context 'default request headers' do
167180
subject!(:response) do
168181
expect_any_instance_of(Datadog::Contrib::Excon::Middleware).to receive(:request_call)

spec/ddtrace/contrib/faraday/middleware_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@
140140
end
141141
end
142142

143+
context 'when split by domain with a domain map' do
144+
subject!(:response) { client.get('/success') }
145+
let(:middleware_options) { { split_by_domain: true, split_by_domain_map: { /example\.com/ => 'bar' } } }
146+
147+
it 'overrides the domain from the map' do
148+
expect(request_span.service).to eq('bar')
149+
end
150+
end
151+
143152
context 'default request headers' do
144153
subject(:response) { client.get('/success') }
145154

0 commit comments

Comments
 (0)