discourse/plugins/discourse-ai/spec/lib/completions/llm_metric_spec.rb
Rafael dos Santos Silva 4fb6767c83
FEATURE: Add Prometheus metrics for LLM API calls (#35636)
Adds Prometheus metrics for the discourse-ai plugin to track LLM usage,
performance, and costs.

When discourse-prometheus is installed, exports 4 metrics for all LLM
API calls:

- `discourse_ai_llm_calls_total` - Total API calls with success/error
tracking
- `discourse_ai_llm_request_tokens_total` - Input tokens sent to LLMs
- `discourse_ai_llm_response_tokens_total` - Output tokens received from
LLMs
- `discourse_ai_llm_duration_seconds` - API call latency

All metrics include labels: `db`, `provider`, `model_name`, `feature`,
`seeded`, `status`

Implementation follows the SpamMetric pattern with instrumentation in
the base endpoint class to capture all providers.

---------

Co-authored-by: Keegan George <kgeorge13@gmail.com>
2025-10-28 14:10:42 -03:00

46 lines
1.1 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseAi::Completions::LlmMetric do
fab!(:llm_model)
describe ".record" do
it "does not raise an error when DiscoursePrometheus is not defined" do
expect do
described_class.record(
llm_model: llm_model,
feature_name: "test_feature",
request_tokens: 100,
response_tokens: 50,
duration_ms: 1500,
status: :success,
)
end.not_to raise_error
end
it "accepts nil feature_name" do
expect do
described_class.record(
llm_model: llm_model,
feature_name: nil,
request_tokens: 100,
response_tokens: 50,
duration_ms: 1500.0,
status: :success,
)
end.not_to raise_error
end
it "accepts error status" do
expect do
described_class.record(
llm_model: llm_model,
feature_name: "test_feature",
request_tokens: 0,
response_tokens: 0,
duration_ms: 500.0,
status: :error,
)
end.not_to raise_error
end
end
end