|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "active_support/log_subscriber" |
| 4 | + |
| 5 | +module Sentry |
| 6 | + module Rails |
| 7 | + # Base class for Sentry log subscribers that extends ActiveSupport::LogSubscriber |
| 8 | + # to provide structured logging capabilities for Rails components. |
| 9 | + # |
| 10 | + # This class follows Rails' LogSubscriber pattern and provides common functionality |
| 11 | + # for capturing Rails instrumentation events and logging them through Sentry's |
| 12 | + # structured logging system. |
| 13 | + # |
| 14 | + # @example Creating a custom log subscriber |
| 15 | + # class MySubscriber < Sentry::Rails::LogSubscriber |
| 16 | + # attach_to :my_component |
| 17 | + # |
| 18 | + # def my_event(event) |
| 19 | + # log_structured_event( |
| 20 | + # message: "My event occurred", |
| 21 | + # level: :info, |
| 22 | + # attributes: { |
| 23 | + # duration_ms: event.duration, |
| 24 | + # custom_data: event.payload[:custom_data] |
| 25 | + # } |
| 26 | + # ) |
| 27 | + # end |
| 28 | + # end |
| 29 | + class LogSubscriber < ActiveSupport::LogSubscriber |
| 30 | + class << self |
| 31 | + if ::Rails.version.to_f < 6.0 |
| 32 | + # Rails 5.x does not provide detach_from |
| 33 | + def detach_from(namespace, notifications = ActiveSupport::Notifications) |
| 34 | + listeners = public_instance_methods(false) |
| 35 | + .flat_map { |key| |
| 36 | + notifications.notifier.listeners_for("#{key}.#{namespace}") |
| 37 | + } |
| 38 | + .select { |listener| listener.instance_variable_get(:@delegate).is_a?(self) } |
| 39 | + |
| 40 | + listeners.map do |listener| |
| 41 | + notifications.notifier.unsubscribe(listener) |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + protected |
| 48 | + |
| 49 | + # Log a structured event using Sentry's structured logger |
| 50 | + # |
| 51 | + # @param message [String] The log message |
| 52 | + # @param level [Symbol] The log level (:trace, :debug, :info, :warn, :error, :fatal) |
| 53 | + # @param attributes [Hash] Additional structured attributes to include |
| 54 | + def log_structured_event(message:, level: :info, attributes: {}) |
| 55 | + Sentry.logger.public_send(level, message, **attributes) |
| 56 | + rescue => e |
| 57 | + # Silently handle any errors in logging to avoid breaking the application |
| 58 | + Sentry.configuration.sdk_logger.debug("Failed to log structured event: #{e.message}") |
| 59 | + end |
| 60 | + |
| 61 | + # Check if an event should be excluded from logging |
| 62 | + # |
| 63 | + # @param event [ActiveSupport::Notifications::Event] The event to check |
| 64 | + # @return [Boolean] true if the event should be excluded |
| 65 | + def excluded_event?(event) |
| 66 | + # Skip Rails' internal events |
| 67 | + return true if event.name.start_with?("!") |
| 68 | + |
| 69 | + false |
| 70 | + end |
| 71 | + |
| 72 | + # Calculate duration in milliseconds from an event |
| 73 | + # |
| 74 | + # @param event [ActiveSupport::Notifications::Event] The event |
| 75 | + # @return [Float] Duration in milliseconds |
| 76 | + def duration_ms(event) |
| 77 | + event.duration.round(2) |
| 78 | + end |
| 79 | + |
| 80 | + # Determine log level based on duration (for performance-sensitive events) |
| 81 | + # |
| 82 | + # @param duration_ms [Float] Duration in milliseconds |
| 83 | + # @param slow_threshold [Float] Threshold in milliseconds to consider "slow" |
| 84 | + # @return [Symbol] Log level (:info or :warn) |
| 85 | + def level_for_duration(duration_ms, slow_threshold = 1000.0) |
| 86 | + duration_ms > slow_threshold ? :warn : :info |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments