Skip to content

Commit

Permalink
Pass set variables to perform_method instead
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Jul 11, 2023
1 parent d1ed119 commit d4ddf3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 53 deletions.
14 changes: 6 additions & 8 deletions lib/elastic/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,13 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =
span_name = opts[:endpoint] || method
@otel.tracer.in_span(span_name) do |span|
span['http.request.method'] = method
if opts[:endpoint] && opts[:path_templates]
@otel.path_params(opts[:endpoint], opts[:path_templates], path)&.each do |k, v|
span["db.elasticsearch.path_parts.#{k}"] = v
end
if body_as_json = @otel.process_body(body, opts[:endpoint])
span['db.statement'] = body_as_json
end
span['db.operation'] = opts[:endpoint]
opts[:defined_params]&.each do |k, v|
span["db.elasticsearch.path_parts.#{k}"] = v
end
if body_as_json = @otel.process_body(body, opts[:endpoint])
span['db.statement'] = body_as_json
end
span['db.operation'] = opts[:endpoint] if opts[:endpoint]
transport.perform_request(method, path, params || {}, body, headers)
end
else
Expand Down
78 changes: 33 additions & 45 deletions spec/elastic/transport/opentelemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,18 @@

context 'when path parameters' do
before do
client.perform_request(
'DELETE', '/users', nil, nil, nil, path_templates: ["/{index}"],
endpoint: 'delete'
)
client.perform_request('DELETE', '/users', nil, nil, nil)
rescue
end
after do
client.perform_request(
'DELETE', '/users', nil, nil, nil, path_templates: ["/{index}"],
endpoint: 'delete'
)
client.perform_request('DELETE', '/users', nil, nil, nil)
rescue
end

it 'creates a span with path parameters' do
client.perform_request(
'POST', '/users/_create/abc', nil, { name: 'otel-test' }, nil,
path_templates: ["/{index}/_create/{id}"], endpoint: 'create'
defined_params: {'index' => 'users', 'id' => 'abc'}, endpoint: 'create'
)

span = exporter.finished_spans.find { |s| s.name == 'create' }
Expand All @@ -66,39 +60,38 @@
end
end

describe '#path_regexps' do
let(:endpoint) { 'search' }
let(:path_templates) { ["/_search", "/{index}/_search"] }

it 'caches the regexps' do
expect(described_class::ENDPOINT_PATH_REGEXPS['search']).to be_nil
expect(otel.path_regexps(endpoint, path_templates)).to eq([/^\/_search$/, /^\/(?<index>[^\/]+)\/_search$/])
expect(described_class::ENDPOINT_PATH_REGEXPS['search'][0]).to eq(/^\/_search$/)
end

context 'path parts' do
let(:endpoint) { 'nodes.info' }
let(:path_templates) { ["/_nodes", "/_nodes/{node_id}", "/_nodes/{metric}", "/_nodes/{node_id}/{metric}"] }

it 'extracts the path parameters' do
path = "/_nodes/123"
matching_regexp = otel.path_regexps(endpoint, path_templates).find do |r|
path.match?(r)
end

expect(path.match(matching_regexp)&.named_captures).to eq('node_id' => '123')
end
end
end
# describe '#path_regexps' do
# let(:endpoint) { 'search' }
# let(:path_templates) { ["/_search", "/{index}/_search"] }
#
# it 'caches the regexps' do
# expect(described_class::ENDPOINT_PATH_REGEXPS['search']).to be_nil
# expect(otel.path_regexps(endpoint, path_templates)).to eq([/^\/_search$/, /^\/(?<index>[^\/]+)\/_search$/])
# expect(described_class::ENDPOINT_PATH_REGEXPS['search'][0]).to eq(/^\/_search$/)
# end
#
# context 'path parts' do
# let(:endpoint) { 'nodes.info' }
# let(:path_templates) { ["/_nodes", "/_nodes/{node_id}", "/_nodes/{metric}", "/_nodes/{node_id}/{metric}"] }
#
# it 'extracts the path parameters' do
# path = "/_nodes/123"
# matching_regexp = otel.path_regexps(endpoint, path_templates).find do |r|
# path.match?(r)
# end
#
# expect(path.match(matching_regexp)&.named_captures).to eq('node_id' => '123')
# end
# end
# end

context 'when a request is instrumented' do
let(:body) do
{ query: { match: { password: { query: 'secret'} } } }
end

it 'creates a span and omits db.statement' do
client.perform_request('GET', '/_search', nil, body, nil,
path_templates: ["/_search", "/{index}/_search"], endpoint: 'search')
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')

expect(span.name).to eql('search')
expect(span.attributes['db.operation']).to eq('search')
Expand All @@ -122,8 +115,7 @@
end

it 'sanitizes the body' do
client.perform_request('GET', '/_search', nil, body, nil,
path_templates: ["/_search", "/{index}/_search"], endpoint: 'search')
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')

expect(span.attributes['db.statement']).to eq(sanitized_body.to_json)
end
Expand Down Expand Up @@ -152,8 +144,7 @@
end

it 'sanitizes the body' do
client.perform_request('GET', '/_search', nil, body, nil,
path_templates: ["/_search", "/{index}/_search"], endpoint: 'search')
client.perform_request('GET', '/_search', nil, body, nil, endpoint: 'search')

expect(span.attributes['db.statement']).to eq(sanitized_body.to_json)
end
Expand All @@ -167,8 +158,7 @@

it 'does not capture db.statement' do
client.perform_request(
'POST', '_all/_delete_by_query', nil, body, nil,
path_templates: ["/{index}/_delete_by_query"], endpoint: 'delete_by_query'
'POST', '_all/_delete_by_query', nil, body, nil, endpoint: 'delete_by_query'
)

expect(span.attributes['db.statement']).to be_nil
Expand All @@ -186,8 +176,7 @@
end

it 'instruments' do
client.perform_request('GET', '/_search', nil, nil, nil,
path_templates: ["/_search", "/{index}/_search"], endpoint: 'search')
client.perform_request('GET', '/_search', nil, nil, nil, endpoint: 'search')
expect(span.name).to eq('search')
end
end
Expand All @@ -201,8 +190,7 @@
end

it 'instruments' do
client.perform_request('GET', '/_search', nil, nil, nil,
path_templates: ["/_search", "/{index}/_search"], endpoint: 'search')
client.perform_request('GET', '/_search', nil, nil, nil, endpoint: 'search')
expect(span).to be_nil
end
end
Expand Down

0 comments on commit d4ddf3c

Please sign in to comment.