Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add row count and affected rows to Trilogy spans #1290

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def query(sql)
OpenTelemetry::Instrumentation::Trilogy.attributes
),
kind: :client
) do |_span, context|
) do |span, context|
if propagator && sql.frozen?
sql = +sql
propagator.inject(sql, context: context)
Expand All @@ -58,7 +58,14 @@ def query(sql)
propagator.inject(sql, context: context)
end

super
result = super

attributes = { 'db.response.returned_rows' => result.count }
attributes['db.response.affected_rows'] = result.affected_rows unless result.affected_rows.nil?

span.add_attributes(attributes)

result
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'DESELECT ?'
end

it 'includes row count' do
client.query('SELECT 1')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see SELECTs in this test file, so I'm not sure what the best way to test affected_rows is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to run an insert and subsequent update command to assert the behaviour here, no?


_(span.attributes['db.response.returned_rows']).must_equal(1)
end

it 'includes affected rows' do
client.query(<<~SQL)
CREATE TABLE products (
product_id int
)
SQL

client.query('INSERT INTO products (product_id) VALUES (1), (2), (3)')

_(exporter.finished_spans.last.attributes['db.response.affected_rows']).must_equal(3)
ensure
client.query('DROP TABLE IF EXISTS products')
end
end

describe 'when connecting' do
Expand Down
Loading