module Google::Cloud::Trace::Notifications
Utility methods for configuring ActiveSupport notifications to generate spans in the current trace.
Constants
- DEFAULT_LABEL_NAMESPACE
The default prefix for label keys
- DEFAULT_MAX_DATA_LENGTH
The default max length for label data.
- REMOVE_NOTIFICATION_FRAMEWORK
Stack truncation method that removes the ActiveSupport::Notifications calls from the top.
Public Class Methods
@private
# File lib/google/cloud/trace/notifications.rb, line 83 def self.handle_notification_event event, maxlen, label_namespace, capture_stack cur_span = Google::Cloud::Trace.get return unless cur_span && event.time && event.end labels = payload_to_labels event, maxlen, label_namespace if capture_stack Google::Cloud::Trace::LabelKey.set_stack_trace \ labels, skip_frames: 2, truncate_stack: REMOVE_NOTIFICATION_FRAMEWORK end cur_span.create_span event.name, start_time: event.time, end_time: event.end, labels: labels end
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.
@param [String, Regex] type A specific type or pattern to select
notifications to listen for.
@param [Integer] max_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.
@param [String] label_namespace A string to prepend to all label
keys.
@param [Boolean] capture_stack Whether traces should include the
call stack.
@example
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"
# 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
@private
# File lib/google/cloud/trace/notifications.rb, line 102 def self.payload_to_labels event, maxlen, label_namespace labels = {} event.payload.each do |k, v| if v.is_a? ::String v = "#{v[0, maxlen - 3]}..." if maxlen && v.size > maxlen labels["#{label_namespace}#{k}"] = v.to_s end end labels end