Class: Ask::Observability::Metrics
- Inherits:
-
Object
- Object
- Ask::Observability::Metrics
- Defined in:
- lib/ask/observability/metrics.rb
Overview
Prometheus metric definitions for ask-instrumentation events.
Created lazily on a registry so gems/apps that never emit events don't pay for registry entries, and so tests can inject a fresh registry. The label set is fixed at creation (prometheus-client requires stable label keys per metric) — hence the metadata keys must be configured before the metrics are first touched.
Constant Summary collapse
- BASE_LABELS =
Label keys every metric carries.
%i[provider model kind].freeze
Class Method Summary collapse
-
.for(registry, metadata_label_keys: []) ⇒ Object
Memoized per (registry, metadata keys) pair.
- .instances ⇒ Object
-
.reset! ⇒ Object
For tests: forget every Metrics instance (fresh registries).
Instance Method Summary collapse
-
#calls_total ⇒ Object
ask_llm_calls_totalprovider,model,kind.
-
#duration_seconds ⇒ Object
ask_llm_duration_secondsprovider,model,kind.
-
#errors_total ⇒ Object
ask_llm_errors_totalprovider,kind.
-
#initialize(registry, metadata_label_keys: []) ⇒ Metrics
constructor
A new instance of Metrics.
-
#tokens_total ⇒ Object
ask_llm_tokens_totalprovider,model,kind,direction.
Constructor Details
#initialize(registry, metadata_label_keys: []) ⇒ Metrics
Returns a new instance of Metrics.
33 34 35 36 |
# File 'lib/ask/observability/metrics.rb', line 33 def initialize(registry, metadata_label_keys: []) @registry = registry @metadata_label_keys = end |
Class Method Details
.for(registry, metadata_label_keys: []) ⇒ Object
Memoized per (registry, metadata keys) pair.
18 19 20 21 |
# File 'lib/ask/observability/metrics.rb', line 18 def for(registry, metadata_label_keys: []) instances[[registry.object_id, ]] ||= new(registry, metadata_label_keys: ) end |
.instances ⇒ Object
23 24 25 |
# File 'lib/ask/observability/metrics.rb', line 23 def instances @instances ||= {} end |
.reset! ⇒ Object
For tests: forget every Metrics instance (fresh registries).
28 29 30 |
# File 'lib/ask/observability/metrics.rb', line 28 def reset! instances.clear end |
Instance Method Details
#calls_total ⇒ Object
ask_llm_calls_totalprovider,model,kind
39 40 41 42 43 44 45 |
# File 'lib/ask/observability/metrics.rb', line 39 def calls_total @calls_total ||= @registry.counter( :ask_llm_calls_total, docstring: 'LLM calls by provider, model, and kind', labels: label_keys(:provider, :model, :kind) ) end |
#duration_seconds ⇒ Object
ask_llm_duration_secondsprovider,model,kind
57 58 59 60 61 62 63 64 |
# File 'lib/ask/observability/metrics.rb', line 57 def duration_seconds @duration_seconds ||= @registry.histogram( :ask_llm_duration_seconds, docstring: 'LLM call duration in seconds', labels: label_keys(:provider, :model, :kind), buckets: [0.1, 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64] ) end |
#errors_total ⇒ Object
ask_llm_errors_totalprovider,kind
67 68 69 70 71 72 73 |
# File 'lib/ask/observability/metrics.rb', line 67 def errors_total @errors_total ||= @registry.counter( :ask_llm_errors_total, docstring: 'LLM calls that errored', labels: label_keys(:provider, :kind) ) end |
#tokens_total ⇒ Object
ask_llm_tokens_totalprovider,model,kind,direction
48 49 50 51 52 53 54 |
# File 'lib/ask/observability/metrics.rb', line 48 def tokens_total @tokens_total ||= @registry.counter( :ask_llm_tokens_total, docstring: 'LLM tokens by provider, model, kind, and direction', labels: label_keys(:provider, :model, :kind, :direction) ) end |