Skip to content

Commit df0dd3c

Browse files
nickamorimrobertlaurin
authored andcommitted
feat: support meta protocol instrumentation (open-telemetry#1480)
* feat: support meta protocol instrumentation * test: dalli meta and binary protocol instrumentation support --------- Co-authored-by: Robert <[email protected]>
1 parent f148acb commit df0dd3c

File tree

3 files changed

+96
-89
lines changed

3 files changed

+96
-89
lines changed

instrumentation/dalli/lib/opentelemetry/instrumentation/dalli/instrumentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def add_patches
3434
::Dalli::Server.prepend(Patches::Server)
3535
else
3636
::Dalli::Protocol::Binary.prepend(Patches::Server)
37+
::Dalli::Protocol::Meta.prepend(Patches::Server) if defined?(::Dalli::Protocol::Meta)
3738
end
3839
end
3940
end

instrumentation/dalli/lib/opentelemetry/instrumentation/dalli/patches/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module OpenTelemetry
88
module Instrumentation
99
module Dalli
1010
module Patches
11-
# Module to prepend to Dalli::Server (or Dalli::Protocol::Binary in 3.0+) for instrumentation
11+
# Module to prepend to Dalli::Server (or Dalli::Protocol::Binary/Meta in 3.0+) for instrumentation
1212
module Server
1313
def request(op, *args) # rubocop:disable Naming/MethodParameterName
1414
operation = Utils.opname(op, multi?)

instrumentation/dalli/test/opentelemetry/instrumentation/dalli/instrumentation_test.rb

Lines changed: 94 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
let(:span) { exporter.finished_spans.first }
1616
let(:host) { ENV.fetch('TEST_MEMCACHED_HOST', '127.0.0.1') }
1717
let(:port) { ENV.fetch('TEST_MEMCACHED_PORT', 11_211).to_i }
18-
let(:dalli) { Dalli::Client.new("#{host}:#{port}", {}) }
1918

2019
before do
2120
exporter.reset
@@ -26,116 +25,123 @@
2625
instrumentation.instance_variable_set(:@installed, false)
2726
end
2827

29-
describe 'tracing' do
30-
before do
31-
instrumentation.install(db_statement: :include)
32-
end
33-
34-
it 'accepts peer service name from config' do
35-
instrumentation.instance_variable_set(:@installed, false)
36-
instrumentation.install(peer_service: 'readonly:memcached')
37-
dalli.set('foo', 'bar')
38-
39-
_(span.attributes['peer.service']).must_equal 'readonly:memcached'
40-
end
28+
[
29+
['binary protocol', { protocol: :binary }],
30+
['meta protocol', { protocol: :meta }]
31+
].each do |name, protocol|
32+
describe "tracing with #{name}" do
33+
let(:dalli) { Dalli::Client.new("#{host}:#{port}", protocol) }
4134

42-
it 'before request' do
43-
_(exporter.finished_spans.size).must_equal 0
44-
end
35+
before do
36+
instrumentation.install(db_statement: :include)
37+
end
4538

46-
it 'after dalli#set' do
47-
dalli.set('foo', 'bar')
39+
it 'accepts peer service name from config' do
40+
instrumentation.instance_variable_set(:@installed, false)
41+
instrumentation.install(peer_service: 'readonly:memcached')
42+
dalli.set('foo', 'bar')
4843

49-
_(exporter.finished_spans.size).must_equal 1
50-
_(span.name).must_equal 'set'
51-
_(span.attributes['db.system']).must_equal 'memcached'
52-
_(span.attributes['db.statement']).must_equal 'set foo bar 0 0'
53-
_(span.attributes['db.operation']).must_equal 'set'
54-
_(span.attributes['net.peer.name']).must_equal host
55-
_(span.attributes['net.peer.port']).must_equal port
56-
end
44+
_(span.attributes['peer.service']).must_equal 'readonly:memcached'
45+
end
5746

58-
it 'after dalli#set' do
59-
dalli.get('foo')
47+
it 'before request' do
48+
_(exporter.finished_spans.size).must_equal 0
49+
end
6050

61-
_(exporter.finished_spans.size).must_equal 1
62-
_(span.name).must_equal 'get'
63-
_(span.attributes['db.system']).must_equal 'memcached'
64-
_(span.attributes['db.statement']).must_equal 'get foo'
65-
_(span.attributes['db.operation']).must_equal 'get'
66-
_(span.attributes['net.peer.name']).must_equal host
67-
_(span.attributes['net.peer.port']).must_equal port
68-
end
51+
it 'after dalli#set' do
52+
dalli.set('foo', 'bar')
6953

70-
it 'after dalli#get_multi' do
71-
dalli.get_multi('foo', 'bar')
54+
_(exporter.finished_spans.size).must_equal 1
55+
_(span.name).must_equal 'set'
56+
_(span.attributes['db.system']).must_equal 'memcached'
57+
_(span.attributes['db.statement']).must_equal 'set foo bar 0 0'
58+
_(span.attributes['db.operation']).must_equal 'set'
59+
_(span.attributes['net.peer.name']).must_equal host
60+
_(span.attributes['net.peer.port']).must_equal port
61+
end
7262

73-
_(exporter.finished_spans.size).must_equal 1
74-
_(span.name).must_equal 'getkq'
75-
_(span.attributes['db.system']).must_equal 'memcached'
76-
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
77-
_(span.attributes['db.operation']).must_equal 'getkq'
78-
_(span.attributes['net.peer.name']).must_equal host
79-
_(span.attributes['net.peer.port']).must_equal port
80-
end
63+
it 'after dalli#get' do
64+
dalli.get('foo')
8165

82-
it 'after error' do
83-
dalli.set('foo', 'bar')
84-
exporter.reset
66+
_(exporter.finished_spans.size).must_equal 1
67+
_(span.name).must_equal 'get'
68+
_(span.attributes['db.system']).must_equal 'memcached'
69+
_(span.attributes['db.statement']).must_equal 'get foo'
70+
_(span.attributes['db.operation']).must_equal 'get'
71+
_(span.attributes['net.peer.name']).must_equal host
72+
_(span.attributes['net.peer.port']).must_equal port
73+
end
8574

86-
dalli.instance_variable_get(:@ring).servers.first.stub(:write, ->(_bytes) { raise Dalli::NetworkError }) do
75+
it 'after dalli#get_multi' do
8776
dalli.get_multi('foo', 'bar')
88-
end
8977

90-
if supports_retry_on_network_errors?
91-
_(exporter.finished_spans.size).must_equal 2
92-
else
9378
_(exporter.finished_spans.size).must_equal 1
79+
_(span.name).must_equal 'getkq'
80+
_(span.attributes['db.system']).must_equal 'memcached'
81+
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
82+
_(span.attributes['db.operation']).must_equal 'getkq'
83+
_(span.attributes['net.peer.name']).must_equal host
84+
_(span.attributes['net.peer.port']).must_equal port
9485
end
9586

96-
_(span.name).must_equal 'getkq'
97-
_(span.attributes['db.system']).must_equal 'memcached'
98-
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
99-
_(span.attributes['db.operation']).must_equal 'getkq'
100-
_(span.attributes['net.peer.name']).must_equal host
101-
_(span.attributes['net.peer.port']).must_equal port
102-
103-
span_event = span.events.first
104-
_(span_event.name).must_equal 'exception'
105-
_(span_event.attributes['exception.type']).must_equal 'Dalli::NetworkError'
106-
_(span_event.attributes['exception.message']).must_equal 'Dalli::NetworkError'
107-
end
87+
it 'after error' do
88+
dalli.set('foo', 'bar')
89+
exporter.reset
90+
91+
dalli.instance_variable_get(:@ring).servers.first.stub(:write, ->(_bytes) { raise Dalli::NetworkError }) do
92+
dalli.get_multi('foo', 'bar')
93+
end
94+
95+
if supports_retry_on_network_errors?
96+
_(exporter.finished_spans.size).must_equal 2
97+
else
98+
_(exporter.finished_spans.size).must_equal 1
99+
end
100+
101+
_(span.name).must_equal 'getkq'
102+
_(span.attributes['db.system']).must_equal 'memcached'
103+
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
104+
_(span.attributes['db.operation']).must_equal 'getkq'
105+
_(span.attributes['net.peer.name']).must_equal host
106+
_(span.attributes['net.peer.port']).must_equal port
107+
108+
span_event = span.events.first
109+
_(span_event.name).must_equal 'exception'
110+
_(span_event.attributes['exception.type']).must_equal 'Dalli::NetworkError'
111+
_(span_event.attributes['exception.message']).must_equal 'Dalli::NetworkError'
112+
end
108113

109-
it 'omits db.statement' do
110-
instrumentation.instance_variable_set(:@installed, false)
111-
instrumentation.install(db_statement: :omit)
114+
it 'omits db.statement' do
115+
instrumentation.instance_variable_set(:@installed, false)
116+
instrumentation.install(db_statement: :omit)
112117

113-
dalli.set('foo', 'bar')
118+
dalli.set('foo', 'bar')
114119

115-
_(exporter.finished_spans.size).must_equal 1
116-
_(span.name).must_equal 'set'
117-
_(span.attributes).wont_include 'db.statement'
118-
end
120+
_(exporter.finished_spans.size).must_equal 1
121+
_(span.name).must_equal 'set'
122+
_(span.attributes).wont_include 'db.statement'
123+
end
119124

120-
it 'obfuscates db.statement' do
121-
instrumentation.instance_variable_set(:@installed, false)
122-
instrumentation.install(db_statement: :obfuscate)
125+
it 'obfuscates db.statement' do
126+
instrumentation.instance_variable_set(:@installed, false)
127+
instrumentation.install(db_statement: :obfuscate)
123128

124-
dalli.set('foo', 'bar')
129+
dalli.set('foo', 'bar')
125130

126-
_(exporter.finished_spans.size).must_equal 1
127-
_(span.name).must_equal 'set'
128-
_(span.attributes['db.statement']).must_equal 'set ?'
129-
end
131+
_(exporter.finished_spans.size).must_equal 1
132+
_(span.name).must_equal 'set'
133+
_(span.attributes['db.statement']).must_equal 'set ?'
134+
end
130135

131-
it 'supports gat' do
132-
skip unless dalli.respond_to?(:gat)
136+
it 'supports gat' do
137+
skip unless dalli.respond_to?(:gat)
133138

134-
dalli.gat('foo')
139+
dalli.gat('foo')
135140

136-
_(exporter.finished_spans.size).must_equal 1
137-
_(span.name).must_equal 'gat'
138-
_(span.attributes['db.statement']).must_equal 'gat foo 0'
141+
_(exporter.finished_spans.size).must_equal 1
142+
_(span.name).must_equal 'gat'
143+
_(span.attributes['db.statement']).must_equal 'gat foo 0'
144+
end
139145
end
140146
end
141147

0 commit comments

Comments
 (0)