/
patcher_spec.rb
140 lines (111 loc) · 4.26 KB
/
patcher_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
require 'ddtrace/contrib/support/spec_helper'
require 'ddtrace/contrib/analytics_examples'
require 'ddtrace/contrib/integration_examples'
require 'ddtrace'
require 'elasticsearch-transport'
RSpec.describe Datadog::Contrib::Elasticsearch::Patcher do
let(:host) { ENV.fetch('TEST_ELASTICSEARCH_HOST', '127.0.0.1') }
let(:port) { ENV.fetch('TEST_ELASTICSEARCH_PORT', '9200').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
wait_http_server(server, 60)
end
around do |example|
# Reset before and after each example; don't allow global state to linger.
Datadog::Contrib::REGISTRY[:elasticsearch].reset_configuration!
example.run
Datadog::Contrib::REGISTRY[:elasticsearch].reset_configuration!
end
describe 'cluster health request' do
subject(:request) { client.perform_request 'GET', '_cluster/health' }
it 'creates a span' do
expect { request }.to change { fetch_spans.first }.to Datadog::Span
end
context 'inside a span' do
subject(:request_inside_a_span) do
tracer.trace('publish') do |span|
span.service = 'webapp'
span.resource = '/status'
request
end
end
it 'creates a child request span' do
expect { request_inside_a_span }.to change { fetch_spans.length }.to 2
end
it 'sets request span parent id and trace id' do
request_inside_a_span
child, parent = spans
expect(child.parent_id).to eq(parent.span_id)
expect(child.trace_id).to eq(parent.trace_id)
end
end
describe 'health request span' do
before { request }
it { expect(span.name).to eq('elasticsearch.query') }
it { expect(span.service).to eq('elasticsearch') }
it { expect(span.resource).to eq('GET _cluster/health') }
it { expect(span.span_type).to eq('elasticsearch') }
it { expect(span.parent_id).not_to be_nil }
it { expect(span.trace_id).not_to be_nil }
it_behaves_like 'a peer service span'
end
describe 'health request span' do
before do
request
end
it { expect(span.name).to eq('elasticsearch.query') }
it { expect(span.service).to eq('elasticsearch') }
it { expect(span.resource).to eq('GET _cluster/health') }
it { expect(span.span_type).to eq('elasticsearch') }
it { expect(span.parent_id).not_to be_nil }
it { expect(span.trace_id).not_to be_nil }
it_behaves_like 'a peer service span'
end
end
describe 'indexing request' do
let(:document_body) do
{
field: 'Test',
nested_object: {
value: 'x'
},
nested_array: %w[a b],
nested_object_array: [
{ a: 'a' },
{ b: 'b' }
]
}
end
let(:index_name) { 'some_index' }
let(:document_type) { 'type' }
let(:document_id) { 1 }
subject(:request) { client.perform_request 'PUT', "#{index_name}/#{document_type}/#{document_id}", {}, document_body }
it 'creates a span' do
expect { request }.to change { fetch_spans.first }.to Datadog::Span
end
describe 'index request span' do
before { request }
it_behaves_like 'analytics for integration' do
let(:analytics_enabled_var) { Datadog::Contrib::Elasticsearch::Ext::ENV_ANALYTICS_ENABLED }
let(:analytics_sample_rate_var) { Datadog::Contrib::Elasticsearch::Ext::ENV_ANALYTICS_SAMPLE_RATE }
end
it_behaves_like 'measured span for integration', false
it { expect(span.name).to eq('elasticsearch.query') }
it { expect(span.service).to eq('elasticsearch') }
it { expect(span.span_type).to eq('elasticsearch') }
it { expect(span.resource).to eq('PUT some_index/type/?') }
it { expect(span.parent_id).not_to be_nil }
it { expect(span.trace_id).not_to be_nil }
it 'tags span with quantized request body' do
expect(span.get_tag('elasticsearch.body'))
.to eq('{"field":"?","nested_object":{"value":"?"},"nested_array":["?"],"nested_object_array":[{"a":"?"},"?"]}')
end
it_behaves_like 'a peer service span'
end
end
end