Skip to content

Commit 1051959

Browse files
committed
test: dalli meta and binary protocol instrumentation support
1 parent 4fe2450 commit 1051959

File tree

2 files changed

+98
-88
lines changed

2 files changed

+98
-88
lines changed

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: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
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}", {}) }
18+
let(:protocol) { { protocol: :binary } }
19+
let(:dalli) { Dalli::Client.new("#{host}:#{port}", protocol) }
1920

2021
before do
2122
exporter.reset
@@ -26,116 +27,125 @@
2627
instrumentation.instance_variable_set(:@installed, false)
2728
end
2829

29-
describe 'tracing' do
30-
before do
31-
instrumentation.install(db_statement: :include)
32-
end
30+
[
31+
['default protocol', {}],
32+
['meta protocol', { protocol: :meta }]
33+
].each do |name, protocol|
34+
describe "tracing with #{name}" do
35+
let(:protocol) { protocol }
3336

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')
37+
before do
38+
instrumentation.install(db_statement: :include)
39+
end
3840

39-
_(span.attributes['peer.service']).must_equal 'readonly:memcached'
40-
end
41+
it 'accepts peer service name from config' do
42+
instrumentation.instance_variable_set(:@installed, false)
43+
instrumentation.install(peer_service: 'readonly:memcached')
44+
dalli.set('foo', 'bar')
45+
puts dalli.inspect
4146

42-
it 'before request' do
43-
_(exporter.finished_spans.size).must_equal 0
44-
end
4547

46-
it 'after dalli#set' do
47-
dalli.set('foo', 'bar')
48-
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
48+
_(span.attributes['peer.service']).must_equal 'readonly:memcached'
49+
end
5750

58-
it 'after dalli#set' do
59-
dalli.get('foo')
51+
it 'before request' do
52+
_(exporter.finished_spans.size).must_equal 0
53+
end
6054

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
55+
it 'after dalli#set' do
56+
dalli.set('foo', 'bar')
6957

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

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
67+
it 'after dalli#get' do
68+
dalli.get('foo')
8169

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

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

90-
if supports_retry_on_network_errors?
91-
_(exporter.finished_spans.size).must_equal 2
92-
else
9382
_(exporter.finished_spans.size).must_equal 1
83+
_(span.name).must_equal 'getkq'
84+
_(span.attributes['db.system']).must_equal 'memcached'
85+
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
86+
_(span.attributes['db.operation']).must_equal 'getkq'
87+
_(span.attributes['net.peer.name']).must_equal host
88+
_(span.attributes['net.peer.port']).must_equal port
9489
end
9590

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

109-
it 'omits db.statement' do
110-
instrumentation.instance_variable_set(:@installed, false)
111-
instrumentation.install(db_statement: :omit)
118+
it 'omits db.statement' do
119+
instrumentation.instance_variable_set(:@installed, false)
120+
instrumentation.install(db_statement: :omit)
112121

113-
dalli.set('foo', 'bar')
122+
dalli.set('foo', 'bar')
114123

115-
_(exporter.finished_spans.size).must_equal 1
116-
_(span.name).must_equal 'set'
117-
_(span.attributes).wont_include 'db.statement'
118-
end
124+
_(exporter.finished_spans.size).must_equal 1
125+
_(span.name).must_equal 'set'
126+
_(span.attributes).wont_include 'db.statement'
127+
end
119128

120-
it 'obfuscates db.statement' do
121-
instrumentation.instance_variable_set(:@installed, false)
122-
instrumentation.install(db_statement: :obfuscate)
129+
it 'obfuscates db.statement' do
130+
instrumentation.instance_variable_set(:@installed, false)
131+
instrumentation.install(db_statement: :obfuscate)
123132

124-
dalli.set('foo', 'bar')
133+
dalli.set('foo', 'bar')
125134

126-
_(exporter.finished_spans.size).must_equal 1
127-
_(span.name).must_equal 'set'
128-
_(span.attributes['db.statement']).must_equal 'set ?'
129-
end
135+
_(exporter.finished_spans.size).must_equal 1
136+
_(span.name).must_equal 'set'
137+
_(span.attributes['db.statement']).must_equal 'set ?'
138+
end
130139

131-
it 'supports gat' do
132-
skip unless dalli.respond_to?(:gat)
140+
it 'supports gat' do
141+
skip unless dalli.respond_to?(:gat)
133142

134-
dalli.gat('foo')
143+
dalli.gat('foo')
135144

136-
_(exporter.finished_spans.size).must_equal 1
137-
_(span.name).must_equal 'gat'
138-
_(span.attributes['db.statement']).must_equal 'gat foo 0'
145+
_(exporter.finished_spans.size).must_equal 1
146+
_(span.name).must_equal 'gat'
147+
_(span.attributes['db.statement']).must_equal 'gat foo 0'
148+
end
139149
end
140150
end
141151

0 commit comments

Comments
 (0)