Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0]
- Support for dynamic service names by setting `DatadogQueueBus.service_name` to a Callable

## [2.0.0]
- Replaces ddtrace with the updated `datadog` gem

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ To override the default service name (which will be `queue-bus` if not set):
DatadogQueueBus.service_name = 'my-queue-bus-service'
```

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:

```ruby
DatadogQueueBus.service_name = ->(attrs) do
if attrs['bus_rider_app_key'].nil? || attrs['bus_rider_app_key'].empty?
'default-queue-bus-service'
else
"#{attrs['bus_rider_app_key']}-queue-bus-service"
end
end
```

## Development

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.
Expand Down
1 change: 1 addition & 0 deletions lib/datadog-queue-bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module DatadogQueueBus
class << self
# Sets the service_name that will be used when reporting queue-bus spans.
# Set this to a String, or to a Callable that accepts an attributes hash and returns a String.
attr_writer :service_name

# Returns the service name that is used when reporting queue-bus spans.
Expand Down
8 changes: 7 additions & 1 deletion lib/datadog_queue_bus/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ def call(attrs)
resource += " event=#{event_type}" if event_type
resource += " sub=#{sub_key}" if sub_key

service_name = if DatadogQueueBus.service_name.respond_to?(:call)
DatadogQueueBus.service_name.call(attrs)
else
DatadogQueueBus.service_name
end

# def trace(name, continue_from: nil, **span_options, &block)
Datadog::Tracing.trace('queue-bus.worker',
service: DatadogQueueBus.service_name,
service: service_name,
resource: resource) do |span|
attrs.keys.grep(/^bus_/).each do |key|
span.set_tag("queue-bus.#{key}", attrs[key])
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog_queue_bus/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DatadogQueueBus
VERSION = '2.0.0'
VERSION = '2.1.0'
end
18 changes: 17 additions & 1 deletion spec/datadog_queue_bus/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
subject.call(attrs)
end

context 'with a service name' do
context 'with a static service name' do
let(:name) { rand.to_s }

before do
Expand All @@ -35,6 +35,22 @@
end
end

context 'with a dynamic service name' do
let(:name) { rand.to_s }
let(:service_name) { ->(attrs) { name } }

before do
DatadogQueueBus.service_name = service_name
end

it 'sends the service name' do
expect(tracer)
.to receive(:trace)
.with('queue-bus.worker', hash_including(service: name))
subject.call(attrs)
end
end

context 'with an event type' do
let(:attrs) do
super().merge('bus_event_type' => 'my_event')
Expand Down