mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-06-19 07:43:46 +08:00
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>
46 lines
1.1 KiB
Ruby
Vendored
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
|