Skip to content

Commit ccf8fa5

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

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,17 @@ 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+
if matcher =~ host
153+
return service_name
154+
end
155+
end
156+
return host
157+
end
158+
159+
@options[:service_name]
150160
end
151161
end
152162
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ 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+
if matcher =~ host
74+
return service_name
75+
end
76+
end
77+
return host
78+
end
7179

7280
options[:service_name]
7381
end

spec/ddtrace/contrib/excon/instrumentation_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@
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) { Datadog.configuration[:excon][:split_by_domain] = false; Datadog.configuration[:excon][:split_by_domain_map] = {} }
170+
171+
it "overrides the domain from the map" do
172+
expect(request_span.service).to eq('bar')
173+
end
174+
end
175+
166176
context 'default request headers' do
167177
subject!(:response) do
168178
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)