Skip to content

Commit 500e7f9

Browse files
committed
Add OTel integration test
1 parent fe909f4 commit 500e7f9

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

elasticsearch/Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ end
2929
if ENV['TRANSPORT_VERSION'] == 'main'
3030
gem 'elastic-transport', git: 'https://github.com/elastic/elastic-transport-ruby.git', branch: 'main'
3131
end
32+
33+
if RUBY_VERSION >= '3.0'
34+
gem 'opentelemetry-sdk', require: false
35+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
if ENV['TEST_WITH_OTEL'] == 'true'
19+
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
20+
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
21+
22+
require 'spec_helper'
23+
24+
context 'OpenTelemetry' do
25+
let(:exporter) { EXPORTER }
26+
before { exporter.reset }
27+
after { exporter.reset }
28+
let(:span) { exporter.finished_spans[0] }
29+
30+
let(:client) do
31+
Elasticsearch::Client.new(
32+
host: ELASTICSEARCH_URL,
33+
user: 'elastic',
34+
password: 'changeme'
35+
)
36+
end
37+
38+
after do
39+
client.delete(index: 'myindex', id: 1); rescue
40+
end
41+
42+
context 'when a request is instrumented' do
43+
it 'sets the span name to the endpoint id' do
44+
client.search(body: { query: { match: {a: 1} } })
45+
expect(span.name).to eq 'search'
46+
end
47+
48+
it 'sets the path parts' do
49+
client.index(index: 'myindex', id: 1, body: { title: 'Test' })
50+
expect(span.attributes['db.elasticsearch.path_parts.index']).to eq 'myindex'
51+
expect(span.attributes['db.elasticsearch.path_parts.id']).to eq 1
52+
end
53+
end
54+
end
55+
end

elasticsearch/spec/spec_helper.rb

+12
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ def meta_version
2929
def jruby?
3030
defined?(JRUBY_VERSION)
3131
end
32+
33+
if ENV['TEST_WITH_OTEL'] == 'true'
34+
require 'opentelemetry-sdk'
35+
EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
36+
span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)
37+
38+
OpenTelemetry::SDK.configure do |c|
39+
c.error_handler = ->(exception:, message:) { raise(exception || message) }
40+
c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym)
41+
c.add_span_processor span_processor
42+
end
43+
end

elasticsearch/spec/unit/opaque_id_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
it 'uses x-opaque-id on a request' do
3030
client.search(opaque_id: '12345')
3131
expect(transport).to have_received(:perform_request)
32-
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => '12345' })
32+
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => '12345' }, {:endpoint=>"search"})
3333
end
3434
end
3535

@@ -42,7 +42,7 @@
4242
it 'uses x-opaque-id on a request' do
4343
expect { client.search(opaque_id: '12345') }.not_to raise_error
4444
expect(transport).to have_received(:perform_request)
45-
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => 'elastic_cloud12345' })
45+
.with('GET', '_search', {}, nil, { 'X-Opaque-Id' => 'elastic_cloud12345' }, {:endpoint=>"search"})
4646
end
4747
end
4848
end

0 commit comments

Comments
 (0)