forked from open-telemetry/opentelemetry-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation_test.rb
122 lines (96 loc) · 3.99 KB
/
instrumentation_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true
# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
require 'test_helper'
require_relative '../../../../lib/opentelemetry/instrumentation/dalli'
require_relative '../../../../lib/opentelemetry/instrumentation/dalli/patches/server'
describe OpenTelemetry::Instrumentation::Dalli::Instrumentation do
let(:instrumentation) { OpenTelemetry::Instrumentation::Dalli::Instrumentation.instance }
let(:exporter) { EXPORTER }
let(:span) { exporter.finished_spans.first }
let(:host) { ENV.fetch('TEST_MEMCACHED_HOST') { '127.0.0.1' } }
let(:port) { (ENV.fetch('TEST_MEMCACHED_PORT') { 11_211 }).to_i }
let(:dalli) { ::Dalli::Client.new("#{host}:#{port}", {}) }
before do
exporter.reset
end
after do
# Force re-install of instrumentation
instrumentation.instance_variable_set(:@installed, false)
end
describe 'tracing' do
before do
instrumentation.install
end
it 'accepts peer service name from config' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(peer_service: 'readonly:memcached')
dalli.set('foo', 'bar')
_(span.attributes['peer.service']).must_equal 'readonly:memcached'
end
it 'before request' do
_(exporter.finished_spans.size).must_equal 0
end
it 'after dalli#set' do
dalli.set('foo', 'bar')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'set'
_(span.attributes['db.system']).must_equal 'memcached'
_(span.attributes['db.statement']).must_equal 'set foo bar 0 0'
_(span.attributes['net.peer.name']).must_equal host
_(span.attributes['net.peer.port']).must_equal port
end
it 'after dalli#set' do
dalli.get('foo')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'get'
_(span.attributes['db.system']).must_equal 'memcached'
_(span.attributes['db.statement']).must_equal 'get foo'
_(span.attributes['net.peer.name']).must_equal host
_(span.attributes['net.peer.port']).must_equal port
end
it 'after dalli#get_multi' do
dalli.get_multi('foo', 'bar')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'getkq'
_(span.attributes['db.system']).must_equal 'memcached'
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
_(span.attributes['net.peer.name']).must_equal host
_(span.attributes['net.peer.port']).must_equal port
end
it 'after error' do
dalli.set('foo', 'bar')
exporter.reset
dalli.instance_variable_get(:@ring).servers.first.stub(:write, ->(_bytes) { raise Dalli::NetworkError }) do
dalli.get_multi('foo', 'bar')
end
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'getkq'
_(span.attributes['db.system']).must_equal 'memcached'
_(span.attributes['db.statement']).must_equal 'getkq foo bar'
_(span.attributes['net.peer.name']).must_equal host
_(span.attributes['net.peer.port']).must_equal port
span_event = span.events.first
_(span_event.name).must_equal 'exception'
_(span_event.attributes['exception.type']).must_equal 'Dalli::NetworkError'
_(span_event.attributes['exception.message']).must_equal 'Dalli::NetworkError'
end
it 'omits db.statement' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(db_statement: :omit)
dalli.set('foo', 'bar')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'set'
_(span.attributes).wont_include 'db.statement'
end
it 'obfuscates db.statement' do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(db_statement: :obfuscate)
dalli.set('foo', 'bar')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'set'
_(span.attributes['db.statement']).must_equal 'set ?'
end
end
end unless ENV['OMIT_SERVICES']