Prometheus Metric Integration#
HugeGraph collects per-request HTTP metrics and exposes them in Prometheus text format via the /metrics REST endpoint. Metric names are derived from normalized request paths + HTTP methods and must be sanitized to conform to Prometheus naming rules (only [a-zA-Z0-9_:] allowed). The core sanitization and format-writing logic lives in MetricsUtil.java.
Metric Name Construction#
Per-request metric names are assembled in AccessLogFilter, a JAX-RS ContainerResponseFilter that runs after every HTTP response:
- Normalize the path β
normalizePath()replaces dynamic URI segments (graph names, IDs) with their parameter placeholder names so that/hugegraph/vertices/abc123becomes something stable likegraphs/graph/vertices/vertexId. - Join path + method β the metric key is built as
<normalizedPath>/<HTTP_METHOD>. - Append suffix β one of four suffixes defined in
MetricsUtilis appended :TOTAL_COUNTERSUCCESS_COUNTERFAILED_COUNTERRESPONSE_TIME_HISTOGRAM
The resulting raw key (e.g., graphs/hugegraph/vertices/vertexId/GET/TOTAL_COUNTER) is registered with the Codahale MetricRegistry .
Sanitization Rules#
Before writing to Prometheus output, every metric key is run through replaceDotDashInKey(), which performs four character substitutions in sequence:
| Character | Replacement |
|---|---|
. (dot) | _ |
- (dash) | _ |
/ (slash) | _ |
$ (dollar) | _ |
This is the fix introduced in PR #2462 to resolve issue #2354, where slashes and dollar signs in raw metric names were causing invalid Prometheus output.
A second helper, replaceSlashInKey(), performs only slash replacement and is available for narrower use cases.
Prometheus Output (writePrometheusFormat)#
MetricsUtil.writePrometheusFormat() iterates all four Codahale metric types from the registry and renders them as Prometheus text:
- Gauges β
gaugetype - Histograms β
histogramtype with count + snapshot percentiles (p50βp999, min, max, mean, stddev) - Meters β
histogramtype with count + mean/1m/5m/15m rates - Timers β
histogramtype with rate + snapshot
Each metric gets a # HELP and # TYPE header line using the sanitized name. Snapshot percentiles are rendered via exportSnapshot().
writePrometheusFormat() is called from MetricsAPI.baseMetricPrometheusAll() , which is the handler backing the /metrics and /metrics/all REST endpoints. The output format is selected by the type query parameter β any value other than "json" returns Prometheus text.
Key Files#
| File | Role |
|---|---|
MetricsUtil.java | Sanitization helpers + Prometheus text serializer |
AccessLogFilter.java | Builds and registers per-request metric keys |
MetricsAPI.java | /metrics REST endpoint; calls writePrometheusFormat() |
| PR #2462 | Bug fix: added / and $ substitution to replaceDotDashInKey() |