/
transport_spec.rb
161 lines (130 loc) · 5 KB
/
transport_spec.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
require 'ddtrace/contrib/integration_examples'
require 'ddtrace/contrib/support/spec_helper'
require 'time'
require 'elasticsearch-transport'
require 'faraday'
require 'ddtrace'
RSpec.describe 'Elasticsearch::Transport::Client tracing' do
before do
WebMock.enable!
WebMock.disable_net_connect!
end
after do
WebMock.allow_net_connect!
WebMock.reset!
WebMock.disable!
end
let(:host) { ENV.fetch('TEST_ELASTICSEARCH_HOST', '127.0.0.1') }
let(:port) { ENV.fetch('TEST_ELASTICSEARCH_PORT', '1234').to_i }
let(:server) { "http://#{host}:#{port}" }
let(:client) { Elasticsearch::Client.new(url: server, adapter: :net_http) }
let(:configuration_options) { {} }
before do
Datadog.configure do |c|
c.use :elasticsearch, configuration_options
end
end
after { Datadog::Contrib::REGISTRY[:elasticsearch].reset_configuration! }
context 'when configured with middleware' do
let(:client) do
Elasticsearch::Client.new url: server do |c|
c.use middleware
end
end
let(:middleware) do
stub_const('MyFaradayMiddleware', Class.new(Faraday::Middleware) do
def call(env)
@app.call(env)
end
end)
end
describe 'the handlers' do
subject(:handlers) { client.transport.connections.first.connection.builder.handlers }
it { is_expected.to include(middleware) }
end
end
describe '#perform_request' do
context 'with a' do
context 'GET request' do
subject(:response) { client.perform_request(method, path) }
let(:method) { 'GET' }
let(:path) { '_cluster/health' }
before do
stub_request(:get, "#{server}/#{path}").to_return(status: 200)
expect(response.status).to eq(200)
end
it 'produces a well-formed trace' do
expect(WebMock).to have_requested(:get, "#{server}/#{path}")
expect(spans).to have(1).items
expect(span.name).to eq('elasticsearch.query')
expect(span.service).to eq('elasticsearch')
expect(span.resource).to eq('GET _cluster/health')
expect(span.get_tag('elasticsearch.url')).to eq('_cluster/health')
expect(span.get_tag('elasticsearch.method')).to eq('GET')
expect(span.get_tag('http.status_code')).to eq('200')
expect(span.get_tag('elasticsearch.params')).to be nil
expect(span.get_tag('elasticsearch.body')).to be nil
expect(span.get_tag('out.host')).to eq(host)
expect(span.get_tag('out.port')).to eq(port)
end
it_behaves_like 'a peer service span'
end
context 'PUT request' do
subject(:response) { client.perform_request(method, path, params, body) }
let(:method) { 'PUT' }
let(:path) { 'my/thing/1' }
let(:params) { { refresh: true } }
before do
stub_request(:put, "#{server}/#{path}?refresh=true").with(body: body).to_return(status: 201)
expect(response.status).to eq(201)
end
shared_examples_for 'a PUT request trace' do
it do
expect(WebMock).to have_requested(:put, "#{server}/#{path}?refresh=true")
expect(spans).to have(1).items
expect(span.name).to eq('elasticsearch.query')
expect(span.service).to eq('elasticsearch')
expect(span.resource).to eq('PUT my/thing/?')
expect(span.get_tag('elasticsearch.url')).to eq(path)
expect(span.get_tag('elasticsearch.method')).to eq('PUT')
expect(span.get_tag('http.status_code')).to eq('201')
expect(span.get_tag('elasticsearch.params')).to eq(params.to_json)
expect(span.get_tag('elasticsearch.body')).to eq('{"data1":"?","data2":"?"}')
expect(span.get_tag('out.host')).to eq(host)
expect(span.get_tag('out.port')).to eq(port)
end
it_behaves_like 'a peer service span'
end
context 'with Hash params' do
let(:body) { '{"data1":"D1","data2":"D2"}' }
it_behaves_like 'a PUT request trace'
end
context 'with encoded body' do
let(:body) { { data1: 'D1', data2: 'D2' } }
it_behaves_like 'a PUT request trace'
end
end
end
end
describe 'client Datadog::Pin' do
context 'when #service is overridden' do
before { Datadog::Pin.get_from(client).service = service_name }
let(:service_name) { 'bar' }
describe 'then a GET request' do
subject(:response) { client.perform_request(method, path) }
let(:method) { 'GET' }
let(:path) { '_cluster/health' }
before do
stub_request(:get, "#{server}/#{path}").to_return(status: 200)
expect(response.status).to eq(200)
end
it 'produces a well-formed trace' do
expect(WebMock).to have_requested(:get, "#{server}/#{path}")
expect(spans).to have(1).items
expect(span.name).to eq('elasticsearch.query')
expect(span.service).to eq(service_name)
end
end
end
end
end