Module: FlowChat::Instrumentation

Extended by:
ActiveSupport::Concern
Included in:
Context, Http::Gateway::Simple, FlowChat::Intercom::Client, FlowChat::Intercom::Gateway::IntercomApi, Processor, Session::CacheSessionStore, Session::Middleware, Telegram::Client, Telegram::Gateway::BotApi, Ussd::Gateway::Nalo, Ussd::Middleware::Pagination, Whatsapp::Client, Whatsapp::Gateway::CloudApi
Defined in:
lib/flow_chat/instrumentation.rb,
lib/flow_chat/instrumentation/setup.rb,
lib/flow_chat/instrumentation/log_subscriber.rb,
lib/flow_chat/instrumentation/metrics_collector.rb

Defined Under Namespace

Modules: Events, Setup Classes: LogSubscriber, MetricsCollector

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instrument(event_name, payload = {}, &block) ⇒ Object

Module-level method for direct calls like FlowChat::Instrumentation.instrument



28
29
30
31
32
33
34
35
36
# File 'lib/flow_chat/instrumentation.rb', line 28

def self.instrument(event_name, payload = {}, &block)
  full_event_name = "#{event_name}.flow_chat"

  enriched_payload = {
    timestamp: Time.current
  }.merge(payload || {}).compact

  ActiveSupport::Notifications.instrument(full_event_name, enriched_payload, &block)
end

.report_api_error(message, error: nil, **context) ⇒ Object

Shared helper for reporting API errors with instrumentation and Rails.error

Parameters:

  • message (String)

    Error message

  • error (Exception, nil) (defaults to: nil)

    Original exception if available

  • context (Hash)

    Platform-specific error context (must include :platform)



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flow_chat/instrumentation.rb', line 42

def self.report_api_error(message, error: nil, **context)
  error_context = context.compact

  # Instrument for custom subscribers
  instrument(Events::API_ERROR, error_context.merge(message: message))

  # Report to Rails.error if available
  if defined?(Rails) && Rails.respond_to?(:error) && Rails.error.respond_to?(:report)
    exception = error || StandardError.new(message)
    Rails.error.report(exception, handled: true, context: error_context)
  end
end

Instance Method Details

#instrument(event_name, payload = {}, &block) ⇒ Object

Instrument a block of code with the given event name and payload



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/flow_chat/instrumentation.rb', line 8

def instrument(event_name, payload = {}, &block)
  enriched_payload = payload&.dup || {}
  if respond_to?(:context) && context
    enriched_payload[:request_id] = context["request.id"] if context["request.id"]
    enriched_payload[:session_id] = context["session.id"] if context["session.id"]
    enriched_payload[:flow_name] = context["flow.name"] if context["flow.name"]
    enriched_payload[:gateway] = context["request.gateway"] if context["request.gateway"]
    enriched_payload[:platform] = context["request.platform"] if context["request.platform"]
  end

  self.class.instrument(event_name, enriched_payload, &block)
end