Module: Google::Cloud::Trace::Notifications

Defined in:
lib/google/cloud/trace/notifications.rb

Overview

Utility methods for configuring ActiveSupport notifications to generate spans in the current trace.

Constant Summary collapse

DEFAULT_MAX_DATA_LENGTH =

The default max length for label data.

1024
DEFAULT_LABEL_NAMESPACE =

The default prefix for label keys

"/ruby/".freeze
REMOVE_NOTIFICATION_FRAMEWORK =

Stack truncation method that removes the ActiveSupport::Notifications calls from the top.

lambda do |frame|
  frame.absolute_path !~ %r{/lib/active_support/notifications}
end

Class Method Summary collapse

Class Method Details

.instrument(type, max_length: DEFAULT_MAX_DATA_LENGTH, label_namespace: DEFAULT_LABEL_NAMESPACE, capture_stack: false) ⇒ Object

Subscribes to the given event type or any type matching the given pattern. When an event is raised, a span is generated in the current thread's trace. The event payload is exposed as labels on the span. If there is no active trace for the current thread, then no span is generated.

Examples:


require "google/cloud/trace"
require "active_record"

Google::Cloud::Trace::Notifications.instrument "sql.activerecord"

trace_record = Google::Cloud::Trace::TraceRecord.new "my-project"
Google::Cloud::Trace.set trace_record

ActiveRecord::Base.connection.execute "SHOW TABLES"

Parameters:

  • type (String, Regex)

    A specific type or pattern to select notifications to listen for.

  • max_length (Integer) (defaults to: DEFAULT_MAX_DATA_LENGTH)

    The maximum length for label values. If a label value exceeds this length, it is truncated. If the length is nil, no truncation takes place.

  • label_namespace (String) (defaults to: DEFAULT_LABEL_NAMESPACE)

    A string to prepend to all label keys.

  • capture_stack (Boolean) (defaults to: false)

    Whether traces should include the call stack.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/google/cloud/trace/notifications.rb', line 69

def self.instrument type,
                    max_length: DEFAULT_MAX_DATA_LENGTH,
                    label_namespace: DEFAULT_LABEL_NAMESPACE,
                    capture_stack: false
  require "active_support/notifications"
  ActiveSupport::Notifications.subscribe type do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    handle_notification_event event, max_length, label_namespace,
                              capture_stack
  end
end