Class: Ask::Observability::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/observability/subscriber.rb

Overview

Turns Ask::Instrumentation events into Prometheus metrics:

ask_llm_calls_total{provider,model,kind}
ask_llm_tokens_total{provider,model,kind,direction}
ask_llm_duration_seconds{provider,model,kind}
ask_llm_errors_total{provider,kind}

Metadata attached via Ask::Instrumentation.with_metadata is merged into every event payload, so configured metadata keys (see Configuration#label_metadata) are read straight off the payload — missing values render as "unknown".

Constant Summary collapse

KIND_BY_EVENT =

Event name → metric kind (mirrors ask-opentelemetry's span map).

{
  'chat.ask' => 'chat',
  'chat.stream.ask' => 'chat',
  'tool.ask' => 'tool',
  'embedding.ask' => 'embedding',
  'image.ask' => 'image'
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object

Invoked by ActiveSupport::Notifications for each matching event.

Parameters:

  • event (ActiveSupport::Notifications::Event)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ask/observability/subscriber.rb', line 29

def call(event)
  kind = KIND_BY_EVENT[event.name]
  return unless kind

  payload = event.payload
  metrics = Ask::Observability.metrics
  labels = base_labels(payload, kind).merge((payload))

  metrics.calls_total.increment(labels: labels)
  metrics.duration_seconds.observe(event.duration / 1000.0, labels: labels)
  record_tokens(metrics, labels, payload)
  record_error(metrics, labels, payload) if payload[:error]
end