Skip to content

Commit

Permalink
feat: Handle when body is a string (for bulk requests)
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Mar 1, 2023
1 parent a6358b0 commit f68c970
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def perform_request(method, path, *args, &block)
obfuscate = config[:db_statement] == :obfuscate
unless omit
body = Sanitizer.sanitize(body, obfuscate, config[:sanitize_field_names])
attributes['db.statement'] = body.to_json if body
if body && !body.is_a?(String)
body = body.to_json
end
attributes['db.statement'] = body
end

attributes.compact!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def sanitize(query, obfuscate, key_patterns = [])
private

def sanitize!(obj, key_patterns, obfuscate)
return unless obj.is_a?(Hash)
return obj unless obj.is_a?(Hash)

obj.each_pair do |k, v|
case v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
instrumentation.install(config)
stub_request(:get, %r{http://localhost:9200/.*}).to_return(status: 200)
stub_request(:post, %r{http://localhost:9200/.*}).to_return(status: 200)
stub_request(:put, %r{http://localhost:9200/.*}).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
Expand Down Expand Up @@ -75,9 +76,9 @@

it 'omits the statement ' do
client.bulk(
body: {
index: { _index: 'users', data: { name: 'Emily' } }
}
body: [{
index: { _index: 'users', data: { name: 'Fernando' } }
}]
)

_(exporter.finished_spans.size).must_equal(1)
Expand Down Expand Up @@ -111,15 +112,15 @@

it 'includes the entire statement ' do
client.bulk(
body: {
body: [{
index: { _index: 'users', data: { name: 'Emily', password: 'top_secret' } }
}
}]
)

_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'POST _bulk'
_(span.attributes['db.statement']).must_equal(
"{\"index\":{\"_index\":\"users\",\"data\":{\"name\":\"Emily\",\"password\":\"top_secret\"}}}"
"{\"index\":{\"_index\":\"users\"}}\n{\"name\":\"Emily\",\"password\":\"top_secret\"}\n"
)
_(span.attributes['db.system']).must_equal "elasticsearch"
_(span.attributes['db.operation']).must_equal "POST"
Expand All @@ -144,15 +145,15 @@
it 'captures the span attributes' do
client.bulk(
refresh: true,
body: {
index: { _index: 'users', data: { name: 'Emily' } }
}
body: [{
index: { _index: 'users', data: { name: 'Fernando' } }
}]
)

_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'POST _bulk'
_(span.attributes['db.statement']).must_equal(
"{\"index\":{\"_index\":\"users\",\"data\":{\"name\":\"Emily\"}}}"
"{\"index\":{\"_index\":\"users\"}}\n{\"name\":\"Fernando\"}\n"
)
_(span.attributes['db.system']).must_equal "elasticsearch"
_(span.attributes['db.operation']).must_equal "POST"
Expand All @@ -175,20 +176,20 @@

describe 'sanitize body' do
it 'sanitizes certain fields' do
client.bulk(
body: {
index: { _index: 'users', data: { name: 'Emily', password: 'top_secret' } }
}
client.index(
index: 'users',
id: '1',
body: { name: 'Emily', password: 'top_secret' }
)

_(exporter.finished_spans.size).must_equal(1)
_(span.name).must_equal 'POST _bulk'
_(span.name).must_equal 'PUT users/_doc/1'
_(span.attributes['db.statement']).must_equal(
"{\"index\":{\"_index\":\"users\",\"data\":{\"name\":\"Emily\",\"password\":\"?\"}}}"
"{\"name\":\"Emily\",\"password\":\"?\"}"
)
_(span.attributes['db.system']).must_equal "elasticsearch"
_(span.attributes['db.operation']).must_equal "POST"
_(span.attributes['elasticsearch.method']).must_equal 'POST'
_(span.attributes['db.operation']).must_equal "PUT"
_(span.attributes['elasticsearch.method']).must_equal 'PUT'
_(span.attributes['net.transport']).must_equal 'ip_tcp'

_(span.attributes['net.peer.name']).must_equal 'localhost'
Expand All @@ -199,13 +200,12 @@
#_(span.attributes['elasticsearch.id']).must_equal # doc id
#_(span.attributes['elasticsearch.target']).must_equal '_search'
assert_requested(
:post,
'http://localhost:9200/_bulk'
:put,
'http://localhost:9200/users/_doc/1'
)
end
end


describe '#perform_request with exception' do
before do
stub_request(:get, %r{http://localhost:9200/.*})
Expand Down

0 comments on commit f68c970

Please sign in to comment.