Skip to content

Commit 16c97e8

Browse files
authored
Allow dynamic service_name configuration (#5)
* Allow dynamic service_name configuration Originally, service_name was limited to a simple string value. Modify the middleware to allow a Callable that generates a service_name dynamically from attributes as an alternative option. * Version bump and changelog
1 parent 165095e commit 16c97e8

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.1.0]
8+
- Support for dynamic service names by setting `DatadogQueueBus.service_name` to a Callable
9+
710
## [2.0.0]
811
- Replaces ddtrace with the updated `datadog` gem
912

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ To override the default service name (which will be `queue-bus` if not set):
3232
DatadogQueueBus.service_name = 'my-queue-bus-service'
3333
```
3434

35+
You can also set the service name to a Callable that accepts the attributes hash and returns a String, granting you flexibility to monitor larger installations as separate services:
36+
37+
```ruby
38+
DatadogQueueBus.service_name = ->(attrs) do
39+
if attrs['bus_rider_app_key'].nil? || attrs['bus_rider_app_key'].empty?
40+
'default-queue-bus-service'
41+
else
42+
"#{attrs['bus_rider_app_key']}-queue-bus-service"
43+
end
44+
end
45+
```
46+
3547
## Development
3648

3749
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/datadog-queue-bus.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module DatadogQueueBus
1010
class << self
1111
# Sets the service_name that will be used when reporting queue-bus spans.
12+
# Set this to a String, or to a Callable that accepts an attributes hash and returns a String.
1213
attr_writer :service_name
1314

1415
# Returns the service name that is used when reporting queue-bus spans.

lib/datadog_queue_bus/middleware.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ def call(attrs)
1313
resource += " event=#{event_type}" if event_type
1414
resource += " sub=#{sub_key}" if sub_key
1515

16+
service_name = if DatadogQueueBus.service_name.respond_to?(:call)
17+
DatadogQueueBus.service_name.call(attrs)
18+
else
19+
DatadogQueueBus.service_name
20+
end
21+
1622
# def trace(name, continue_from: nil, **span_options, &block)
1723
Datadog::Tracing.trace('queue-bus.worker',
18-
service: DatadogQueueBus.service_name,
24+
service: service_name,
1925
resource: resource) do |span|
2026
attrs.keys.grep(/^bus_/).each do |key|
2127
span.set_tag("queue-bus.#{key}", attrs[key])

lib/datadog_queue_bus/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module DatadogQueueBus
4-
VERSION = '2.0.0'
4+
VERSION = '2.1.0'
55
end

spec/datadog_queue_bus/middleware_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
subject.call(attrs)
2121
end
2222

23-
context 'with a service name' do
23+
context 'with a static service name' do
2424
let(:name) { rand.to_s }
2525

2626
before do
@@ -35,6 +35,22 @@
3535
end
3636
end
3737

38+
context 'with a dynamic service name' do
39+
let(:name) { rand.to_s }
40+
let(:service_name) { ->(attrs) { name } }
41+
42+
before do
43+
DatadogQueueBus.service_name = service_name
44+
end
45+
46+
it 'sends the service name' do
47+
expect(tracer)
48+
.to receive(:trace)
49+
.with('queue-bus.worker', hash_including(service: name))
50+
subject.call(attrs)
51+
end
52+
end
53+
3854
context 'with an event type' do
3955
let(:attrs) do
4056
super().merge('bus_event_type' => 'my_event')

0 commit comments

Comments
 (0)