-
Notifications
You must be signed in to change notification settings - Fork 178
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: Support prepend SQL comment for PG instrumentation #690
feat: Support prepend SQL comment for PG instrumentation #690
Conversation
I'm reading the specification for what https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/database/#call-level-attributes
Given that it's a small simple one, I hope that it's okay to tweak like this (this really helps readability of the tracing so I'd love to have this personally), but I understand if that's not the case. I'd appreciate any inputs! |
Thanks for your contribution. We've got work already underway to address this problem in a separate PR #529 Would you be interested in helping us do some exploratory testing there? |
Ariel, thank you for sharing that PR. I wasn't aware of it, and I'm excited to see that there is already a PR for this! I'm more than happy to assist in any way I can. It appears that this PR is quite big and has a lot of history. You mentioned the need for some exploratory testing, but could you please provide more details about how I can help for this? Thanks! |
Hi @keiko713. The main outstanding issue with that PR is exactly what you've addressed here. See #529 (comment). I'd really appreciate it if you could port your fix to that PR. 🙏 |
Thanks for the pointer, @fbogsany ! I took a look that PR deeper and now have several questions but let me see if I can keep it short. First, let's see if I understand that PR properly:
Now, coming back to my PR and the current implementation of Postgres client
From here, my questions would be:
I'm sorry for many questions and a long comment! 🙈 |
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
Hi @keiko713, I'm so sorry about the delay in responding to this PR. Thanks for your patience, your great questions, and this awesome fix. Though the work is thematically similar to #529, as you correctly stated, the The MySQL helpers gem created in 529 is intended to reduce duplication between the trilogy and mysql2 instrumentation. Since we only instrument one Postgres gem, pg, we don't need to abstract this code out into a separate gem, nor should we try to combine it with the MySQL helpers gem. I've also added functionality to #529 to make MySQL statement extraction respect Active Record Query Logs, so both MySQL and Postgres instrumentation via OpenTelemetry should now be compatible with this feature. |
I don't really understand this need. Why would you enable this comment in app to remove it later during instrumentation? 🤔 |
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
@simi, the way I understand it, the comment isn't exactly being removed by the instrumentation. Instead, the comment is ignored when trying to identify what statement type is in the query (ex. SELECT). If the comment is not ignored, the wrong statement type will be chosen, making the span name less precise. The prepended comment will still be included in the statement if the user configured it to be included by using the default |
02555e8
to
676425c
Compare
@kaylareopelle ahh, thanks for explanation. Now it makes sense. I'm ok to move this forward. Once there will be universal SQL helpers gem, this can be moved in there. |
@@ -101,7 +101,9 @@ def span_attrs(kind, *args) | |||
|
|||
def extract_operation(sql) | |||
# From: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/9244a08a8d014afe26b82b91cf86e407c2599d73/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts#L35 | |||
sql.to_s.split[0].to_s.upcase | |||
# Ignores prepend comment | |||
comment_regex = %r{\A\/\*.*?\*\/}m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to move this into constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL Ruby freezes Regexes like it freezes strings.
Running a file named foo.rb
with the following ruby code:
# frozen_string_literal: true
def foo
%r{\A\/\*.*?\*\/}m.object_id
end
def bar
'hello'.object_id
end
puts foo
puts foo
puts foo
puts bar
puts bar
puts bar
has this result:
➜ ~ ruby foo.rb
60
60
60
80
80
80
So I'm going to merge this in without moving it to a constant.
sql.to_s.split[0].to_s.upcase | ||
# Ignores prepend comment | ||
comment_regex = %r{\A\/\*.*?\*\/}m | ||
sql.to_s.sub(comment_regex, '').split[0].to_s.upcase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm, we are only interested in removing the first set of long form comments (without newlines) like those generated in SQL Commenter format.
We are also not concerned about inline comments -- this is a comment
Is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's my understanding since the goal of this method is to grab the operation (ex. SELECT
), so any comments after the operation won't apply. I'm not sure what would happen if there were two comments back-to-back before the operation, but that seems pretty unlikely.
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
@keiko713 - I think the only thing holding this PR back is to move the Once that's fixed, we should be clear to merge. Thanks for your contribution and for your patience with the review process! |
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
Currently,
extract_operation
method is simply extracting the first word before the space as an operation (as commented, this was borrowed from opentelemetry-js-contrib's pg instrument).This works okay for most of queries, however, when Active Record Query Logs (known as marginalia) is used with the option of
ActiveRecord::QueryLogs.prepend_comment = true
, the query will start with a query comment and this extracting logic fails, making a span name as simply database name and leaving the operation empty.This PR adds a simple logic to ignore any prepend query comment (with using
/* comment */
way, not supporting-- comment
type comment).As Active Record Query Logs became part of Rails, we may see more and more folks use this likely with
prepend_comment = true
option, so it would be nice to support this with some minimal change.Fixes #452