Skip to content

Commit

Permalink
Merge pull request #2892 from newrelic/the_boy_and_the_dog
Browse files Browse the repository at this point in the history
increase db.statement threshold to 4096
  • Loading branch information
fallwith authored Oct 10, 2024
2 parents 4d42829 + 15b0df8 commit f7803d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions lib/new_relic/agent/span_event_primitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ module SpanEventPrimitive
DATASTORE_CATEGORY = 'datastore'
CLIENT = 'client'

DB_STATEMENT_MAX_BYTES = 4096

# Builds a Hash of error attributes as well as the Span ID when
# an error is present. Otherwise, returns nil when no error present.
def error_attributes(segment)
Expand Down Expand Up @@ -114,9 +116,9 @@ def for_datastore_segment(segment) # rubocop:disable Metrics/AbcSize
agent_attributes[DB_SYSTEM_KEY] = segment.product if allowed?(DB_SYSTEM_KEY)

if segment.sql_statement && allowed?(DB_STATEMENT_KEY)
agent_attributes[DB_STATEMENT_KEY] = truncate(segment.sql_statement.safe_sql, 2000)
agent_attributes[DB_STATEMENT_KEY] = truncate(segment.sql_statement.safe_sql, DB_STATEMENT_MAX_BYTES)
elsif segment.nosql_statement && allowed?(DB_STATEMENT_KEY)
agent_attributes[DB_STATEMENT_KEY] = truncate(segment.nosql_statement, 2000)
agent_attributes[DB_STATEMENT_KEY] = truncate(segment.nosql_statement, DB_STATEMENT_MAX_BYTES)
end

[intrinsics, custom_attributes(segment), agent_attributes.merge(agent_attributes(segment))]
Expand Down
18 changes: 12 additions & 6 deletions test/new_relic/agent/transaction/datastore_segment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def test_nosql_statement_added_to_span_event_if_present
end

def test_span_event_truncates_long_sql_statement
select = 'select * from '
with_config(:'transaction_tracer.record_sql' => 'raw') do
in_transaction('wat') do |txn|
txn.stubs(:sampled?).returns(true)
Expand All @@ -392,7 +393,7 @@ def test_span_event_truncates_long_sql_statement
operation: 'select'
)

sql_statement = "select * from #{'a' * 2500}"
sql_statement = "#{select}#{'a' * (SpanEventPrimitive::DB_STATEMENT_MAX_BYTES + 500)}"

segment.notice_sql(sql_statement)
segment.finish
Expand All @@ -401,30 +402,35 @@ def test_span_event_truncates_long_sql_statement

last_span_events = NewRelic::Agent.agent.span_event_aggregator.harvest![1]
_, _, agent_attributes = last_span_events[0]
ellipsis = '...'

assert_equal 2000, agent_attributes['db.statement'].bytesize
assert_equal "select * from #{'a' * 1983}...", agent_attributes['db.statement']
assert_equal SpanEventPrimitive::DB_STATEMENT_MAX_BYTES, agent_attributes['db.statement'].bytesize
assert_equal "#{select}#{'a' * (SpanEventPrimitive::DB_STATEMENT_MAX_BYTES - select.size - ellipsis.size)}#{ellipsis}",
agent_attributes['db.statement']
end

def test_span_event_truncates_long_nosql_statement
set_mykey = 'set mykey '
in_transaction('wat') do |txn|
txn.stubs(:sampled?).returns(true)

segment = NewRelic::Agent::Tracer.start_datastore_segment(
product: 'Redis',
operation: 'set'
)
statement = "set mykey #{'a' * 2500}"
statement = "#{set_mykey}#{'a' * (SpanEventPrimitive::DB_STATEMENT_MAX_BYTES + 500)}"

segment.notice_nosql_statement(statement)
segment.finish
end

last_span_events = NewRelic::Agent.agent.span_event_aggregator.harvest![1]
_, _, agent_attributes = last_span_events[0]
ellipsis = '...'

assert_equal 2000, agent_attributes['db.statement'].bytesize
assert_equal "set mykey #{'a' * 1987}...", agent_attributes['db.statement']
assert_equal SpanEventPrimitive::DB_STATEMENT_MAX_BYTES, agent_attributes['db.statement'].bytesize
assert_equal "#{set_mykey}#{'a' * (SpanEventPrimitive::DB_STATEMENT_MAX_BYTES - set_mykey.size - ellipsis.size)}#{ellipsis}",
agent_attributes['db.statement']
end

def test_span_event_truncates_long_attributes
Expand Down

0 comments on commit f7803d5

Please sign in to comment.