Docker环境下如何通过Prometheus的relabel_config为NetApp Harvest2指标添加前缀?
Let's break down what's going wrong with your current setup and fix it step by step:
1. You're using the wrong relabel stage for renaming metrics
Your current config uses relabel_configs to modify the __name__ label (metric name), but that's not where this operation belongs:
relabel_configsruns before Prometheus scrapes metrics — it only handles target metadata likeinstance,job, or custom labels attached to scrape targets. At this point, no metric data exists yet, so trying to modify__name__here does nothing useful.- To modify actual metric names (and labels on scraped metrics), you need to use
metric_relabel_configs, which runs after metrics are collected from the target.
2. Your keep rule is filtering out all Harvest targets
You've repeated custom_labels four times in source_labels and used a regex that expects four non-empty values separated by semicolons. Unless every one of your Harvest targets has this label with exactly four values formatted that way, this rule will discard all targets entirely — which explains why the Harvest collector disappears from Prometheus/Grafana.
Corrected Prometheus Config
Here's the adjusted config that adds the netapp_ prefix correctly and fixes the target filtering issue:
job_name: harvest scrape_interval: 1m scrape_timeout: 1m metrics_path: /metrics # Remove or adjust the relabel_configs only if you need to filter targets based on metadata # relabel_configs: # - action: keep # source_labels: [custom_labels] # Only list the label once, even if it has multiple values # regex: '.+;.+;.+;.+' # Keep this only if your targets actually have 4 semicolon-separated values # Use metric_relabel_configs to rename metrics metric_relabel_configs: - action: replace source_labels: [__name__] regex: (.*) target_label: __name__ replacement: 'netapp_$1' file_sd_configs: - refresh_interval: 10s files: - targets/harvest.yml
Key Notes:
- If you don't actually need the
keeprule (e.g., you don't need to filter targets), just delete that entirerelabel_configsblock — it's likely the main reason your collector wasn't showing up. - If you do need to filter targets by
custom_labels, remember that Prometheus automatically joins multiple values of the same label with semicolons. You only need to list the label once insource_labels, not four times.
How to Verify:
- Update your Prometheus config file with the above changes
- Restart Prometheus to apply the config
- Check the Targets page in Prometheus (e.g.,
http://your-prometheus-ip:9090/targets) — theharvestjob should now show asUP - In Prometheus's query editor, search for metrics starting with
netapp_— you should see all Harvest metrics prefixed correctly
内容的提问来源于stack exchange,提问作者Philip

