-
Notifications
You must be signed in to change notification settings - Fork 226
feat: ActiveRecord instrumentation improvements #1738
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
Draft
arielvalentin
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
arielvalentin:active-record-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
743c663
feat: ActiveRecord instrumentation improvements
arielvalentin 68a6914
squash: remove unused param
arielvalentin 546e356
squash: bad move
arielvalentin b78645e
wip: I will come back to you
arielvalentin c60873e
squash: more hacking an AI slop cleanup
arielvalentin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
instrumentation/active_record/lib/opentelemetry/instrumentation/active_record/handlers.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require_relative 'handlers/sql_handler' | ||
|
|
||
| module OpenTelemetry | ||
| module Instrumentation | ||
| module ActiveRecord | ||
| # Module that contains custom event handlers for ActiveRecord notifications | ||
| module Handlers | ||
| module_function | ||
|
|
||
| # Subscribes Event Handlers to relevant ActiveRecord notifications | ||
| # | ||
| # The following events are recorded as spans: | ||
| # - sql.active_record | ||
| # | ||
| # @note this method is not thread safe and should not be used in a multi-threaded context | ||
| def subscribe | ||
| return unless Array(@subscriptions).empty? | ||
|
|
||
| config = ActiveRecord::Instrumentation.instance.config | ||
| return unless config[:enable_notifications_instrumentation] | ||
|
|
||
| sql_handler = Handlers::SqlHandler.new | ||
|
|
||
| @subscriptions = [ | ||
| ::ActiveSupport::Notifications.subscribe('sql.active_record', sql_handler) | ||
| ] | ||
| end | ||
|
|
||
| # Removes Event Handler Subscriptions for ActiveRecord notifications | ||
| # @note this method is not thread-safe and should not be used in a multi-threaded context | ||
| def unsubscribe | ||
| @subscriptions&.each { |subscriber| ::ActiveSupport::Notifications.unsubscribe(subscriber) } | ||
| @subscriptions = nil | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
69 changes: 69 additions & 0 deletions
69
...ion/active_record/lib/opentelemetry/instrumentation/active_record/handlers/sql_handler.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| module OpenTelemetry | ||
| module Instrumentation | ||
| module ActiveRecord | ||
| module Handlers | ||
| # SqlHandler handles sql.active_record ActiveSupport notifications | ||
| class SqlHandler | ||
| # Invoked by ActiveSupport::Notifications at the start of the instrumentation block | ||
| # | ||
| # @param name [String] of the Event | ||
| # @param id [String] of the event | ||
| # @param payload [Hash] containing SQL execution information | ||
| # @return [Hash] the payload passed as a method argument | ||
| def start(name, id, payload) | ||
| span = tracer.start_span( | ||
| payload[:name] || 'SQL', | ||
| kind: :internal | ||
| ) | ||
| token = OpenTelemetry::Context.attach( | ||
| OpenTelemetry::Trace.context_with_span(span) | ||
| ) | ||
| payload[:__opentelemetry_span] = span | ||
| payload[:__opentelemetry_ctx_token] = token | ||
| rescue StandardError => e | ||
| OpenTelemetry.handle_error(exception: e) | ||
| end | ||
|
|
||
| # Invoked by ActiveSupport::Notifications at the end of the instrumentation block | ||
| # | ||
| # @param name [String] of the Event | ||
| # @param id [String] of the event | ||
| # @param payload [Hash] containing SQL execution information | ||
| def finish(name, id, payload) | ||
| attributes = { | ||
| 'db.active_record.async' => payload[:async] == true, | ||
| 'db.active_record.cached' => payload[:cached] == true | ||
| } | ||
| span = payload.delete(:__opentelemetry_span) | ||
| span&.add_attributes(attributes) | ||
|
|
||
| token = payload.delete(:__opentelemetry_ctx_token) | ||
| return unless span && token | ||
|
|
||
| if (e = payload[:exception_object]) | ||
| span.record_exception(e) | ||
| span.status = OpenTelemetry::Trace::Status.error('Unhandled exception') | ||
| end | ||
|
|
||
| span.finish | ||
| OpenTelemetry::Context.detach(token) | ||
| rescue StandardError => e | ||
| OpenTelemetry.handle_error(exception: e) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def tracer | ||
| OpenTelemetry::Instrumentation::ActiveRecord::Instrumentation.instance.tracer | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thoughts about making this
rails.active_record.*for consistency with the semconv pattern of not putting product specific attributes in the domain ie db namespace.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.
Thanks for the feedback.
I wasn't sure what direction to head since there isn't any semconv guidance for ORM of database abstraction libraries.
Namespacing under rails works for me