不能。VictoriaMetrics 不是 Prometheus 的 drop-in 替换,不运行 prometheus 进程,也不自带服务发现、告警评估或 Alertmanager;仅兼容其远程读写协议,需调整 Prometheus 配置而非 Go 代码。
不能。VictoriaMetrics 本身不是 Prometheus 的 drop-in 替换,它不运行 prometheus 进程,也不自带服务发现、告警规则评估或 Alertmanager 集成。所谓“无缝”,仅指它完全兼容 Prometheus 的 /api/v1/write 和 /api/v1/read 协议,以及远程读写(remote_write/remote_read)配置——这意味着你不用改 Go 服务里的 prometheus.Client 或 promhttp,只要把原来指向 localhost:9090/api/v1/write 的 remote_write endpoint 换成 VictoriaMetrics 的 http://vm-single:8428/api/v1/write 即可生效。
容易踩的坑:
• 误以为启动 VictoriaMetrics 后,原有 Prometheus server 可以直接关掉——其实不能,除非你已将 rule evaluation、alerting、UI 查询全部迁出;
• 在 Go 微服务中硬编码了 prometheus.PushCollector 推送地址,而没通过配置管理——这类推送路径必须显式更新,否则指标仍发给旧 Prometheus。
标准 prometheus 客户端库(如 github.com/prometheus/client_golang)无需更换,但 remote_write 配置必须调整。重点在 Prometheus server 的配置文件(prometheus.yml),而非 Go 代码本身:
remote_write 的 url 必须指向 VictoriaMetrics 的 /api/v1/write(单节点是 http://vm:8428/api/v1/write,集群版是 http://vminsert:8480/insert/0/prometheus/api/v1/write)remote_write 中加 basic_auth 块,Go 服务本身不感知该认证——它只和本地 Prometheus 通信queue_config:VictoriaMetrics 对写入吞吐更敏感,max_samples_per_send: 1000 和 capacity: 10000 是安全起点,避免 Prometheus 因写入失败堆积内存示例片段:
立即学习“go语言免费学习笔记(深入)”;
remote_write:- url: http://victoriametrics:8428/api/v1/write queue_config: max_samples_per_send: 1000 capacity: 10000 basic_auth: username: admin password: secret
不需要。Go 微服务仍只对接本地 Prometheus,Prometheus 再通过 remote_write 把数据推给 VictoriaMetrics 集群入口 vminsert。真正要改的是运维侧配置:
vminsert 是唯一写入入口,地址形如 http://vminsert:8480/insert/0/prometheus/api/v1/write,其中 0 是 tenant ID,多租户场景才需变vmstorage 或 vmselect——它们不提供写入 API,且无状态设计意味着写请求必须经 vminsert 负载均衡--push.disable-consistency-check 必须开启,并将其 remote_write 指向 vminsert,而非直接连 VM绝大多数情况不用。如果你的 Go 服务只是暴露指标(promhttp.Handler()),不主动查监控数据,那完全无关。但若服务内嵌了告警检查、SLI 计算或 dashboard 数据组装逻辑,调用的是 Prometheus API(如 http://prom:9090/api/v1/query),就必须切 endpoint:
http://prom:9090/api/v1/query 改成 http://vm:8428/api/v1/query
http://vmselect:8481/select/0/prometheus/api/v1/query(tenant ID 同样默认为 0)/api/v1/query 兼容 Prometheus 表达式语法,但部分高级函数(如 stddev_over_time)行为略有差异,线上验证前先跑 curl 对比结果真正容易被忽略的是时间范围精度——VictoriaMetrics 默认聚合精度为 10 秒(可通过 -search.latencyOffset 调整),而 Prometheus 是毫秒级。如果 Go 服务里写了依赖 sub-second 精度的查询(比如追踪某次 RPC 的 exact duration bucket),结果可能对不上。