Quick Start
Disabled by Default
Metrics are disabled by default. To enable metrics collection, setMETRICS_ENABLED=true and start GoModel:
The rest of this guide shows just the variable being set. Apply it via
whichever launcher you picked above —
.env plus --env-file, an inline
-e flag, or export before ./bin/gomodel — and restart GoModel.Disable Metrics
Option 1: Environment VariableCustom Metrics Endpoint
Change the default/metrics path:
Configuration Options
Via Environment Variables
Via config.yaml
Examples
Production Setup (Metrics Enabled)
.envDevelopment Setup (Metrics Disabled)
.envCustom Endpoint for Internal Monitoring
config.yamlVerification
Check if Metrics are Enabled
Start the server and look for log messages: Metrics Enabled:Test Metrics Endpoint
When Enabled:Performance Impact
Metrics Enabled
- Minimal overhead: ~100ns per request for hook execution
- Memory: ~1MB for metric storage (depends on cardinality)
- CPU: Negligible impact (
<0.1%in benchmarks)
Metrics Disabled
- Zero overhead: no hooks registered, no collection
- The metrics library is still linked but inactive
- Recommended for maximum performance in non-production environments
Security Considerations
Exposing the Metrics Endpoint
By default the metrics endpoint skips authentication, even when a master key or managed API keys are configured, so Prometheus can scrape it without a credential. Anyone who can reach the gateway can then read it, including through an ingress that routes/ to GoModel.
Set METRICS_REQUIRE_AUTH=true (metrics.require_auth: true) to keep it behind
the gateway’s normal bearer authentication: the master key or any managed API
key is accepted. Prefer a dedicated managed key for Prometheus over the master
key. It has no effect when the gateway runs without authentication.
authorization.credentials referencing a Secret key); the Helm chart
does this with metrics.serviceMonitor.bearerTokenSecret. The Secret must be in
the ServiceMonitor’s namespace.
If you need to protect the metrics endpoint further:
-
Use a custom internal path:
-
Use network-level security:
- Configure firewall rules to allow only the Prometheus server
- Use a private network for metrics collection
- Deploy Prometheus in the same VPC/network
-
Reverse proxy with authentication:
Alerting on Empty Responses
A provider sometimes answers200 but returns no choices, no output, or no
token usage. gomodel_requests_total counts those calls as successes, so
GoModel also increments gomodel_empty_responses_total and logs a warning
(provider returned 200 with an empty response).
A chat completion without choices also returns
502 to the client, or fails
over when a failover target exists. A response with content but no usage is
still returned; it only breaks usage and cost tracking.
Alerting on Lost Usage
Usage is written to storage in batches. An entry that never reaches storage is missing from usage views, budgets and invoices, so GoModel counts it ingomodel_usage_entries_dropped_total and logs it.
Raise
USAGE_BUFFER_SIZE for buffer_full. For write_failed, the server log
has the write error. logger_closed counts usage recorded after the usage
logger shut down, such as by a request or session still running at shutdown;
the process may exit before Prometheus scrapes it, so the log is the record.
Alerting on Undelivered Budget Alerts
Budget alerts are delivered to a webhook, so a failing webhook is silent unless you watch for it.gomodel_budget_alerts_fired_total counts the alerts an instance raised, and
gomodel_budget_alert_delivery_failures_total the ones a destination did not
receive: a webhook that still failed after its retries, a sink that failed,
an alert dropped because that destination’s queue was full, or one abandoned
at shutdown. The server log has the reason.
Token and Cost Metrics
GoModel counts the tokens and the cost of every request it records usage for, so you can graph and alert on spend without querying the usage database. The counters are updated when the response ends; a streaming request is counted once, when its stream closes.
All four carry
provider (the provider name from your configuration) and
model (the model that answered), plus the caller labels.
The two token metrics add token_type:
The details are parts of
gomodel_tokens_total, not additions to it, which
is why they are a separate metric: sum(gomodel_tokens_total) is the token
total, and no sum over one metric counts a token twice. The same holds for the
two cost metrics. Providers report cached and reasoning tokens differently:
Anthropic reports cache reads on top of input, and xAI’s chat completions and
Gemini’s native API report reasoning on top of output. GoModel normalizes
them, so input and output mean the same for every provider.
- Usage tracking must be on (
USAGE_ENABLED=true, the default). The metrics are derived from the same entries the usage dashboard shows. - Cost needs pricing. A request for a model without a known price adds
tokens but no cost.
gomodel_billed_cost_usd_totalis recorded only when pricing markups are enabled. - Response-cache hits never reach a provider, so they add no tokens and no
provider cost. A billed cache hit (
pricing.bill_cache_hits) still adds togomodel_billed_cost_usd_total. - MCP tool calls record neither tokens nor cost, so they add nothing.
- A series appears once it has a non-zero value.
Caller Dimensions
By default the metrics say nothing about who made a request. Three opt-in dimensions add it:gomodel_caller_requests_total, a request counter with provider, model,
and status_type that exists only while a caller dimension is enabled. They
are not added to gomodel_requests_total, to the
gomodel_request_duration_seconds histogram, or to the in-flight gauge: every
caller value would multiply each of their series, and a histogram has one
series per bucket.
A dimension that is off, or has no value for a request, is exported as an
empty label, which Prometheus treats as absent. The user path always has a
value once enabled: a request without one is reported as /. The API key’s
secret and ID are never exported; a key that no longer exists is reported as
{unknown}.
Keeping Cardinality Bounded
Each enabled dimension multiplies the number of series, so turn on only what you query.- User path depth. With the default depth of 2,
/team/alice/laptopis reported as/team/alice. Lower it to group more, raise it only if the deeper levels are few. - Value cap. Each dimension keeps the first
METRICS_CALLER_MAX_VALUES(default 100) distinct values it sees. The cap is per dimension: with all three on, up to 300 values are kept. Later values share one series:{other}(/{other}for user paths). Admitted values keep their own series until GoModel restarts. - Rejected values. A value longer than 256 bytes, one that is not valid
UTF-8, or one spelled like a reserved name (
{other},/{other},{unknown},/{unknown}) is reported as{other}(/{other}for user paths) and takes no place under the cap. - Request labels are one value per label set.
prod,team-aandteam-aare different values, so label sets fill the cap much faster than labels: a client combining 7 labels freely can send 127 different sets, enough to fill the default cap on its own. Every later set, legitimate or not, is then reported as{other}until GoModel restarts. If clients send free-form labels, leave this dimension off. - Worst case. Each dimension adds at most its cap plus
{other}and the empty value (and{unknown}for API keys). With all three on at the default cap, one provider and model can reach 102 × 103 × 102 ≈ 1.07 million series per token type. Only combinations that occur are exported, but enable one dimension at a time and lowerMETRICS_CALLER_MAX_VALUESto what you expect.
Prometheus Configuration
Scrape Config
prometheus.ymlWith Custom Endpoint
Troubleshooting
Metrics Endpoint Returns 404
Cause: metrics are disabled (the default). Solution:METRICS_ENABLED=true via your .env file, an inline -e METRICS_ENABLED=true
flag, or export METRICS_ENABLED=true (see the Quick Start
for the three launch forms), then restart GoModel.
No Metrics Data Appearing
Cause: no requests have been made yet. Solution: make some requests to generate metrics:Custom Endpoint Not Working
Cause: the endpoint must start with/.
Incorrect:
Best Practices
Development
- Disable metrics for faster startup and reduced noise
- Enable only when testing observability features
Staging
- Enable metrics to test the monitoring setup
- Use a custom endpoint if needed for security
Production
- Enable metrics for full observability
- Set up Prometheus alerting
- Use Grafana dashboards for visualization
- Consider a custom endpoint for security
- Monitor metric cardinality to avoid explosion
See Also
- Configuration — full environment variable reference
- config.yaml — YAML configuration reference