janiussyafiq commented on code in PR #13202:
URL: https://github.com/apache/apisix/pull/13202#discussion_r3382844653
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -107,6 +106,61 @@ local function extra_labels(name, ctx)
end
+local metric_label_map = {
+ http_status = {"code", "route", "matched_uri", "matched_host", "service",
"consumer", "node",
+ "request_type", "request_llm_model", "llm_model", "response_source"},
+ http_latency = {"type", "route", "service", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ bandwidth = {"type", "route", "service", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_latency = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_prompt_tokens = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_completion_tokens = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_active_connections = {"route", "route_id", "matched_uri",
"matched_host",
+ "service", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+}
+
+
+local function get_disabled_label_metric_map()
+ local attr = plugin.plugin_attr("prometheus")
+ local metrics_conf = attr and attr.metrics or {}
+ local disabled_label_metric_map = {}
+ for metric_name, metric_conf in pairs(metrics_conf) do
+ if metric_conf.disable_labels then
+ disabled_label_metric_map[metric_name] = {}
+ for _, label in ipairs(metric_conf.disable_labels) do
+ disabled_label_metric_map[metric_name][label] = true
+ end
+ end
+ end
Review Comment:
Addressed in the latest commit. Configuration moved from `plugin_attr` to
plugin **metadata** (`disabled_labels`), which is now validated by a
`metadata_schema` (`type: object`/`array`, per-metric `enum`,
`additionalProperties: false`). Invalid input — wrong types, unknown metric
keys, or non-disable-able labels — is now rejected at config time by the Admin
API instead of erroring per request in the datapath.
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -107,6 +106,61 @@ local function extra_labels(name, ctx)
end
+local metric_label_map = {
+ http_status = {"code", "route", "matched_uri", "matched_host", "service",
"consumer", "node",
+ "request_type", "request_llm_model", "llm_model", "response_source"},
+ http_latency = {"type", "route", "service", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ bandwidth = {"type", "route", "service", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_latency = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_prompt_tokens = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_completion_tokens = {"route_id", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
+ llm_active_connections = {"route", "route_id", "matched_uri",
"matched_host",
+ "service", "service_id", "consumer", "node",
+ "request_type", "request_llm_model", "llm_model"},
Review Comment:
Addressed. `metric_label_map` is now the single source of truth: every
metric is registered in `http_init` via `append_tables(metric_label_map.<name>,
extra_labels(...))` and the duplicated inline label lists were removed.
Registration and value-collapsing now read the same array, so their ordering
can no longer drift.
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -292,6 +347,7 @@ end
function _M.http_log(conf, ctx)
local vars = ctx.var
+ local disabled_label_metric_map = get_disabled_label_metric_map()
Review Comment:
Addressed. `get_disabled_label_metric_map()` now reads the plugin metadata
and caches the built lookup with `core.lrucache` keyed by
`metadata.modifiedIndex`, so it is rebuilt only when the configuration changes
rather than on every request — the same pattern used by
`error-log-logger`/`cors`.
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -771,12 +838,15 @@ local function inc_llm_active_connections(ctx, value)
matched_host = ctx.curr_req_matched._host or ""
end
+ local disabled_label_metric_map = get_disabled_label_metric_map()
+
Review Comment:
Addressed. `inc_llm_active_connections` calls the same cached
`get_disabled_label_metric_map()`, so it reuses the lrucache-cached map and no
longer allocates a fresh table on each increment/decrement.
##########
conf/config.yaml.example:
##########
@@ -635,17 +635,24 @@ plugin_attr: # Plugin attributes
refresh_interval: 15 # Set the interval for refreshing
cached metric data. unit: second.
# metrics: # Create extra labels from nginx variables:
https://nginx.org/en/docs/varindex.html
# http_status:
+ # disable_labels: # List of built-in label names to drop (reduces
cardinality).
+ # - node # e.g. drop the upstream node IP label
Review Comment:
Addressed. `disable_labels` was removed from `plugin_attr` in
`config.yaml.example` — the feature now lives in plugin metadata as
`disabled_labels` — and the documentation describes it as collapsing the label
*value* to an empty string while keeping the label registered. No
"drop"/removal wording remains.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]