Skip to content

Commit 79ee2d6

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #306 from pedro/add-service-info-to-pin
Refactor `Pin` to automatically set service info
2 parents 690b189 + 37d6926 commit 79ee2d6

File tree

16 files changed

+63
-50
lines changed

16 files changed

+63
-50
lines changed

Diff for: lib/ddtrace/configuration/pin_setup.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def call
2222

2323
attr_reader :pin, :opts
2424

25-
ATTRS = [:service_name, :app, :tags, :app_type, :name, :tracer].freeze
25+
ATTRS = [:app, :tags, :app_type, :name, :tracer, :service_name].freeze
2626

2727
private_constant :ATTRS
2828
end

Diff for: lib/ddtrace/contrib/aws/patcher.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def patch
2323

2424
add_pin
2525
add_plugin(Seahorse::Client::Base, *loaded_constants)
26-
Datadog.tracer.set_service_info(get_option(:service_name), 'aws', Ext::AppTypes::WEB)
2726

2827
@patched = true
2928
rescue => e
@@ -38,9 +37,9 @@ def patched?
3837
private
3938

4039
def add_pin
41-
Pin.new(get_option(:service_name), app_type: Ext::AppTypes::WEB).tap do |pin|
42-
pin.onto(::Aws)
43-
end
40+
Pin
41+
.new(get_option(:service_name), app: 'aws', app_type: Ext::AppTypes::WEB)
42+
.onto(::Aws)
4443
end
4544

4645
def add_plugin(*targets)

Diff for: lib/ddtrace/contrib/dalli/patcher.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def patch
2222

2323
add_pin!
2424
Instrumentation.patch!
25-
Datadog.tracer.set_service_info(get_option(:service_name), 'dalli', Ext::AppTypes::CACHE)
2625

2726
@patched = true
2827
rescue => e
@@ -43,9 +42,9 @@ def compatible?
4342
end
4443

4544
def add_pin!
46-
Pin.new(get_option(:service_name), app_type: Ext::AppTypes::CACHE).tap do |pin|
47-
pin.onto(::Dalli)
48-
end
45+
Pin
46+
.new(get_option(:service_name), app: 'dalli', app_type: Ext::AppTypes::CACHE)
47+
.onto(::Dalli)
4948
end
5049
end
5150
end

Diff for: lib/ddtrace/contrib/elasticsearch/patcher.rb

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def patch
3434
require 'ddtrace/contrib/elasticsearch/quantize'
3535

3636
patch_elasticsearch_transport_client()
37-
Datadog.tracer.set_service_info(get_option(:service_name), 'elasticsearch', Ext::AppTypes::DB)
3837

3938
@patched = true
4039
rescue StandardError => e

Diff for: lib/ddtrace/contrib/faraday/patcher.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def patch
3131

3232
add_pin
3333
add_middleware
34-
register_service(get_option(:service_name))
3534

3635
@patched = true
3736
rescue => e
@@ -56,10 +55,12 @@ def compatible?
5655
end
5756

5857
def add_pin
59-
Pin.new(get_option(:service_name), app_type: Ext::AppTypes::WEB).tap do |pin|
60-
pin.onto(::Faraday)
61-
pin.tracer = get_option(:tracer)
62-
end
58+
Pin.new(
59+
get_option(:service_name),
60+
app: 'faraday',
61+
app_type: Ext::AppTypes::WEB,
62+
tracer: get_option(:tracer)
63+
).onto(::Faraday)
6364
end
6465

6566
def add_middleware

Diff for: lib/ddtrace/contrib/grape/patcher.rb

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ def patch
3535
service = get_option(:service_name)
3636
pin = Datadog::Pin.new(service, app: 'grape', app_type: Datadog::Ext::AppTypes::WEB)
3737
pin.onto(::Grape)
38-
if pin.tracer && pin.service
39-
pin.tracer.set_service_info(pin.service, 'grape', pin.app_type)
40-
end
4138

4239
# subscribe to ActiveSupport events
4340
Datadog::Contrib::Grape::Endpoint.subscribe()

Diff for: lib/ddtrace/contrib/mongodb/patcher.rb

-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ def initialize(*args, &blk)
6767
service = Datadog.configuration[:mongo][:service_name]
6868
pin = Datadog::Pin.new(service, app: APP, app_type: Datadog::Ext::AppTypes::DB)
6969
pin.onto(self)
70-
if pin.tracer && pin.service
71-
pin.tracer.set_service_info(pin.service, 'mongodb', pin.app_type)
72-
end
7370
end
7471

7572
def datadog_pin

Diff for: lib/ddtrace/contrib/redis/patcher.rb

-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def datadog_pin
5454
end
5555

5656
# rubocop:disable Metrics/MethodLength
57-
# rubocop:disable Metrics/BlockLength
5857
def patch_redis_client
5958
::Redis::Client.class_eval do
6059
alias_method :initialize_without_datadog, :initialize
@@ -66,9 +65,6 @@ def initialize(*args)
6665
service = Datadog.configuration[:redis][:service_name]
6766
pin = Datadog::Pin.new(service, app: 'redis', app_type: Datadog::Ext::AppTypes::DB)
6867
pin.onto(self)
69-
if pin.tracer && pin.service
70-
pin.tracer.set_service_info(pin.service, pin.app, pin.app_type)
71-
end
7268
initialize_without_datadog(*args)
7369
end
7470

Diff for: lib/ddtrace/contrib/resque/patcher.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def patched?
4040
private
4141

4242
def add_pin
43-
Pin.new(get_option(:service_name), app_type: Ext::AppTypes::WORKER).tap do |pin|
44-
pin.onto(::Resque)
45-
end
43+
Pin
44+
.new(get_option(:service_name), app: 'resque', app_type: Ext::AppTypes::WORKER)
45+
.onto(::Resque)
4646
end
4747
end
4848
end

Diff for: lib/ddtrace/contrib/resque/resque_job.rb

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def around_perform(*args)
2525
Resque.before_first_fork do
2626
pin = Datadog::Pin.get_from(Resque)
2727
next unless pin && pin.tracer
28-
pin.tracer.set_service_info(pin.service, 'resque', Datadog::Ext::AppTypes::WORKER)
2928

3029
# Create SyncWriter instance before forking
3130
sync_writer = Datadog::SyncWriter.new(transport: pin.tracer.writer.transport)

Diff for: lib/ddtrace/contrib/sucker_punch/patcher.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def patch
2424
add_pin!
2525
ExceptionHandler.patch!
2626
Instrumentation.patch!
27-
Datadog.tracer.set_service_info(get_option(:service_name), 'sucker_punch', Ext::AppTypes::WORKER)
2827

2928
@patched = true
3029
rescue => e
@@ -43,7 +42,7 @@ def compatible?
4342
end
4443

4544
def add_pin!
46-
Pin.new(get_option(:service_name), app_type: Ext::AppTypes::WORKER).tap do |pin|
45+
Pin.new(get_option(:service_name), app: 'sucker_punch', app_type: Ext::AppTypes::WORKER).tap do |pin|
4746
pin.onto(::SuckerPunch)
4847
end
4948
end

Diff for: lib/ddtrace/pin.rb

+17-11
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,22 @@ def self.get_from(obj)
1010
obj.datadog_pin
1111
end
1212

13-
attr_accessor :service
13+
attr_accessor :service_name
1414
attr_accessor :app
1515
attr_accessor :tags
1616
attr_accessor :app_type
1717
attr_accessor :name
1818
attr_accessor :tracer
1919
attr_accessor :config
2020

21-
alias service_name= service=
22-
23-
# [ruby19] named parameters would be more idiomatic here, but would break backward compatibility
24-
def initialize(service, options = { app: nil, tags: nil, app_type: nil, tracer: nil, config: nil })
25-
@service = service
26-
@app = options.fetch(:app, nil)
27-
@tags = options.fetch(:tags, nil)
28-
@app_type = options.fetch(:app_type, nil)
21+
def initialize(service_name, options = {})
22+
@app = options[:app]
23+
@tags = options[:tags]
24+
@app_type = options[:app_type]
2925
@name = nil # this would rarely be overriden as it's really span-specific
3026
@tracer = options[:tracer] || Datadog.tracer
31-
@config = options.fetch(:config, nil)
27+
@config = options[:config]
28+
self.service_name = service_name
3229
end
3330

3431
def enabled?
@@ -57,8 +54,17 @@ def datadog_pin
5754
obj.datadog_pin = self
5855
end
5956

57+
def service_name=(name)
58+
tracer.set_service_info(name, app, app_type) if name && app && app_type
59+
60+
@service_name = name
61+
end
62+
63+
alias service= service_name=
64+
alias service service_name
65+
6066
def to_s
61-
"Pin(service:#{@service},app:#{@app},app_type:#{@app_type},name:#{@name})"
67+
"Pin(service:#{service},app:#{app},app_type:#{app_type},name:#{name})"
6268
end
6369
end
6470
end

Diff for: test/configuration/pin_setup_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setup
1212
end
1313

1414
def test_setting_options
15-
custom_tracer = Object.new
15+
custom_tracer = get_test_tracer
1616

1717
custom_options = {
1818
service_name: 'my-service',

Diff for: test/contrib/aws/instrumentation_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setup
1717
def test_pin_defaults
1818
assert_equal('aws', pin.service)
1919
assert_equal('web', pin.app_type)
20-
assert_nil(pin.app)
20+
assert_equal('aws', pin.app)
2121
assert_nil(pin.name)
2222
assert_nil(pin.config)
2323
end

Diff for: test/contrib/redis/redis_test.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ def test_quantize
127127
end
128128

129129
def test_service_name
130-
service_name = 'test'
131130
driver = Redis.new(host: REDIS_HOST, port: REDIS_PORT, driver: :ruby)
132-
pin = Datadog::Pin.get_from(driver)
133-
pin.tracer = @tracer
134-
pin.service = service_name
135-
@tracer.writer.services() # empty queue
136-
@tracer.set_service_info("redis-#{service_name}", 'redis', Datadog::Ext::AppTypes::CACHE)
131+
@tracer.writer.services # empty queue
132+
Datadog.configure(
133+
driver,
134+
service_name: 'redis-test',
135+
tracer: @tracer,
136+
app_type: Datadog::Ext::AppTypes::CACHE
137+
)
137138
driver.set 'FOO', 'bar'
138139
services = @tracer.writer.services
139140
assert_equal(1, services.length)

Diff for: test/pin_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,24 @@ def test_custom_getter_setter
6767
pin.onto(obj)
6868
assert_equal('The PIN is set!', Datadog::Pin.get_from(obj))
6969
end
70+
71+
def test_service_info_update
72+
tracer = get_test_tracer
73+
Datadog::Pin.new('test-service', app: 'test-app', app_type: 'test-type', tracer: tracer)
74+
75+
assert(tracer.services.key?('test-service'))
76+
assert_equal(
77+
{ 'app' => 'test-app', 'app_type' => 'test-type' },
78+
tracer.services['test-service']
79+
)
80+
end
81+
82+
def test_service_info_update_with_missing_params
83+
tracer = get_test_tracer
84+
85+
# instantiating `Pin` without an `app` param (which is necessary for service info)
86+
Datadog::Pin.new('test-service', app_type: 'test-type', tracer: tracer)
87+
88+
assert(tracer.services.empty?)
89+
end
7090
end

0 commit comments

Comments
 (0)