-
Notifications
You must be signed in to change notification settings - Fork 226
feat: Instrument PG connect #1763
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,65 @@ module OpenTelemetry | |
| module Instrumentation | ||
| module PG | ||
| module Patches | ||
| # Utility methods for setting connection attributes from Connect module | ||
| module ConnectionHelper | ||
| module_function | ||
|
|
||
| def set_connection_attributes(span, conn, config) | ||
| attributes = { | ||
| 'db.system' => 'postgresql', | ||
| 'db.name' => conn.db, | ||
| 'db.user' => conn.user | ||
| } | ||
| attributes['peer.service'] = config[:peer_service] if config[:peer_service] | ||
|
|
||
| h = conn.host | ||
| if h&.start_with?('/') | ||
| attributes['net.sock.family'] = 'unix' | ||
| attributes['net.peer.name'] = h | ||
| else | ||
| attributes['net.transport'] = 'ip_tcp' | ||
| attributes['net.peer.name'] = h | ||
| attributes['net.peer.port'] = conn.port if defined?(::PG::DEF_PGPORT) | ||
| end | ||
|
|
||
| attributes.merge!(OpenTelemetry::Instrumentation::PG.attributes) | ||
| attributes.compact! | ||
|
|
||
| span.add_attributes(attributes) | ||
| end | ||
| end | ||
|
|
||
| # Module to prepend to PG::Connection singleton class for connection initialization | ||
| # We override `new` instead of `initialize` because PG::Connection.new is implemented | ||
| # as a Ruby method that calls the C-level connect_start, bypassing initialize. | ||
| # We also need to override the aliases (open, connect, async_connect) because they | ||
| # were aliased before our prepend, so they point to the original method. | ||
| # See: https://github.com/ged/ruby-pg/blob/master/lib/pg/connection.rb#L870 | ||
| module Connect | ||
| def new(...) | ||
| tracer = OpenTelemetry::Instrumentation::PG::Instrumentation.instance.tracer | ||
| config = OpenTelemetry::Instrumentation::PG::Instrumentation.instance.config | ||
|
|
||
| tracer.in_span('connect', kind: :client) do |span| | ||
| if block_given? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wanted to confirm that there wasn't a way to simply this. Does the block not get forwarded when calling super?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does get forwarded, but we want a special path for ^ the above only returns |
||
| super do |conn| | ||
| ConnectionHelper.set_connection_attributes(span, conn, config) | ||
| yield conn | ||
| end | ||
| else | ||
| conn = super | ||
| ConnectionHelper.set_connection_attributes(span, conn, config) | ||
| conn | ||
| end | ||
| end | ||
| end | ||
|
|
||
| PG::Constants::CONNECTION_METHODS.each do |method| | ||
| alias_method method, :new | ||
| end | ||
| end | ||
|
|
||
| # Module to prepend to PG::Connection for instrumentation | ||
| module Connection # rubocop:disable Metrics/ModuleLength | ||
| # Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.