-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathclient_test.rb
230 lines (205 loc) · 8.95 KB
/
client_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true
# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
require 'test_helper'
require_relative '../../../../lib/opentelemetry/instrumentation/http'
require_relative '../../../../lib/opentelemetry/instrumentation/http/patches/client'
describe OpenTelemetry::Instrumentation::HTTP::Patches::Client do
let(:instrumentation) { OpenTelemetry::Instrumentation::HTTP::Instrumentation.instance }
let(:exporter) { EXPORTER }
let(:span) { exporter.finished_spans.first }
let(:config) do
{
span_name_formatter: span_name_formatter
}
end
let(:span_name_formatter) { nil }
def reset
exporter.reset
@orig_propagation = OpenTelemetry.propagation
propagator = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
OpenTelemetry.propagation = propagator
# simulate a fresh install:
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install(config)
stub_request(:get, 'http://example.com/success').to_return(status: 200)
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
stub_request(:get, 'https://example.com/timeout').to_timeout
end
before do
reset
end
after do
# Force re-install of instrumentation
instrumentation.instance_variable_set(:@installed, false)
OpenTelemetry.propagation = @orig_propagation
end
describe '#perform' do
it 'traces a simple request' do
HTTP.get('http://example.com/success')
_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
assert_requested(
:get,
'http://example.com/success',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
it 'after request with failure code' do
HTTP.post('http://example.com/failure')
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'HTTP POST'
_(span.attributes['http.method']).must_equal 'POST'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 500
_(span.attributes['http.target']).must_equal '/failure'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
assert_requested(
:post,
'http://example.com/failure',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
it 'after request timeout' do
expect do
HTTP.get('https://example.com/timeout')
end.must_raise HTTP::TimeoutError
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'https'
_(span.attributes['http.status_code']).must_be_nil
_(span.attributes['http.target']).must_equal '/timeout'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 443
_(span.status.code).must_equal(
OpenTelemetry::Trace::Status::ERROR
)
_(span.status.description).must_equal(
'Unhandled exception of type: HTTP::TimeoutError'
)
assert_requested(
:get,
'https://example.com/timeout',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
it 'merges http client attributes' do
OpenTelemetry::Common::HTTP::ClientContext.with_attributes('peer.service' => 'foo') do
HTTP.get('http://example.com/success')
end
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
_(span.attributes['peer.service']).must_equal 'foo'
assert_requested(
:get,
'http://example.com/success',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
describe 'when span_name_formatter specified' do
let(:span_name_formatter) do
# demonstrate simple addition of path and string to span name:
lambda { |request_method, request_path|
"HTTP #{request_method} #{request_path} miniswan"
}
end
it 'enriches the span' do
OpenTelemetry::Common::HTTP::ClientContext.with_attributes('peer.service' => 'foo') do
HTTP.get('http://example.com/success')
end
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'HTTP GET /success miniswan'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
_(span.attributes['peer.service']).must_equal 'foo'
assert_requested(
:get,
'http://example.com/success',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
end
describe 'when span_formatter specified and it errors' do
let(:span_name_formatter) do
# demonstrate simple addition of path and string to span name:
lambda { |_request_method, _request_path|
raise 'Something Bad'
}
end
it 'provides a sane default' do
OpenTelemetry::Common::HTTP::ClientContext.with_attributes('peer.service' => 'foo') do
HTTP.get('http://example.com/success')
end
_(exporter.finished_spans.size).must_equal 1
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
_(span.attributes['peer.service']).must_equal 'foo'
assert_requested(
:get,
'http://example.com/success',
headers: { 'Traceparent' => "00-#{span.hex_trace_id}-#{span.hex_span_id}-01" }
)
end
end
describe 'Semantic conventions http stability' do
it 'reports stable http attributes when OTEL_SEMCONV_STABILITY_OPT_IN = `http`' do
OpenTelemetry::TestHelpers.with_env('OTEL_SEMCONV_STABILITY_OPT_IN' => 'http') do
reset
HTTP.get('http://example.com/success')
_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['url.scheme']).must_equal 'http'
_(span.attributes['http.response.status_code']).must_equal 200
_(span.attributes['url.path']).must_equal '/success'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['server.port']).must_equal 80
end
end
it 'reports stable http attributes and old http attributes when OTEL_SEMCONV_STABILITY_OPT_IN = `http/dup`' do
OpenTelemetry::TestHelpers.with_env('OTEL_SEMCONV_STABILITY_OPT_IN' => 'http/dup') do
reset
HTTP.get('http://example.com/success')
_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'HTTP GET'
_(span.attributes['http.method']).must_equal 'GET'
_(span.attributes['http.request.method']).must_equal 'GET'
_(span.attributes['http.scheme']).must_equal 'http'
_(span.attributes['url.scheme']).must_equal 'http'
_(span.attributes['http.status_code']).must_equal 200
_(span.attributes['http.response.status_code']).must_equal 200
_(span.attributes['http.target']).must_equal '/success'
_(span.attributes['url.path']).must_equal '/success'
_(span.attributes['net.peer.name']).must_equal 'example.com'
_(span.attributes['server.address']).must_equal 'example.com'
_(span.attributes['net.peer.port']).must_equal 80
_(span.attributes['server.port']).must_equal 80
end
end
end
end
end