-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!(active_job): Use ActiveSupport instead of patches (#677)
* feat!(active_job): Use ActiveSupport instead of patches * refactor: Register discard job * refactor: independent registration * refactor: Use template methods for children * refactor: some more small fixes * refactor: a little clean up * refactor: a little more clarity * refactor: try to make sense of a few things * refactor: fix test directory structure * refactor: extract to files * add some docs * refactor: clean up and renames * refactor: split tests out per handler * refactor... maybe a little overengineering * fix: add examples of in-line instrumentation * Update README.md Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> * Update README.md Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> * Update default.rb Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> * Update perform.rb Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> * squash: Use Singleton Tracer * fix: Use top level namespaces When used in conjunction with the `OpenTelemetry::Instrumenation::ActiveSupport`, the classloader would mistakenly use the wrong namespace and raises a `NameError`. This change updates references to ensure we use top level namespaces to load the appropriate classes. * squash: Update instrumentation/active_job/README.md Co-authored-by: Robb Kidd <robb@thekidds.org> * squash: Update docs Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> * squash: Reduce Local Variable Allocation Co-authored-by: Robb Kidd <robb@thekidds.org> * squash: Safe navigation no longer required * squash: fix bad commit * squash: Remove possible PII from status message * squash: PR Feedback * squash: Use up to date semconv and remove unused attrs * squash: remove unused file * squash: fix test * squash: fix messaging id * squash: More semconv fixes * squash: fix ids * squash: feedback from Ruby SIG --------- Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com> Co-authored-by: Robb Kidd <robb@thekidds.org>
- Loading branch information
1 parent
02799c8
commit fb31945
Showing
22 changed files
with
1,077 additions
and
560 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/handlers.rb
This file contains 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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require_relative 'mappers/attribute' | ||
require_relative 'handlers/default' | ||
require_relative 'handlers/enqueue' | ||
require_relative 'handlers/perform' | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module ActiveJob | ||
# Module that contains custom event handlers, which are used to generate spans per event | ||
module Handlers | ||
module_function | ||
|
||
# Subscribes Event Handlers to relevant ActiveJob notifications | ||
# | ||
# The following events are recorded as spans: | ||
# - enqueue | ||
# - enqueue_at | ||
# - enqueue_retry | ||
# - perform | ||
# - retry_stopped | ||
# - discard | ||
# | ||
# Ingress and Egress spans (perform, enqueue, enqueue_at) use Messaging semantic conventions for naming the span, | ||
# while internal spans keep their ActiveSupport event name. | ||
# | ||
# @note this method is not thread safe and should not be used in a multi-threaded context | ||
# @note Why no perform_start? | ||
# This event causes much heartache as it is the first in a series of events that is triggered. | ||
# It should not be the ingress span because it does not measure anything. | ||
# https://github.com/rails/rails/blob/v6.1.7.6/activejob/lib/active_job/instrumentation.rb#L14 | ||
# https://github.com/rails/rails/blob/v7.0.8/activejob/lib/active_job/instrumentation.rb#L19 | ||
def subscribe | ||
return unless Array(@subscriptions).empty? | ||
|
||
mapper = Mappers::Attribute.new | ||
config = ActiveJob::Instrumentation.instance.config | ||
parent_span_provider = OpenTelemetry::Instrumentation::ActiveJob | ||
|
||
# TODO, use delegation instead of inheritance | ||
default_handler = Handlers::Default.new(parent_span_provider, mapper, config) | ||
enqueue_handler = Handlers::Enqueue.new(parent_span_provider, mapper, config) | ||
perform_handler = Handlers::Perform.new(parent_span_provider, mapper, config) | ||
|
||
handlers_by_pattern = { | ||
'enqueue' => enqueue_handler, | ||
'enqueue_at' => enqueue_handler, | ||
'enqueue_retry' => default_handler, | ||
'perform' => perform_handler, | ||
'retry_stopped' => default_handler, | ||
'discard' => default_handler | ||
} | ||
|
||
@subscriptions = handlers_by_pattern.map do |key, handler| | ||
::ActiveSupport::Notifications.subscribe("#{key}.active_job", handler) | ||
end | ||
end | ||
|
||
# Removes Event Handler Subscriptions for ActiveJob 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 |
Oops, something went wrong.