我编写了一个导出程序,用于使用Prometheus客户端导出应用程序中不同进程的CPU和内存使用情况等数据。一切都正常,然后我注意到,当我终止一个进程时,只要出口商在,客户端就会继续发送它收到的最后一个值。
我使用了gaugvec和Set
方法。我怀疑,由于Set
,客户端已经设置为该值,并且由于它没有接收到任何新值,所以它只是继续发送最后一个值。以下是我的代码:
var CpuPercentValue = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "MyExporter",
Name: "CpuPercentValue",
Help: "CpuPercentValue",
},
[]string{
"namespace",
"proc_qID",
"opID",
},
)
var MemoryValue = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "MyExporter",
Name: "MemoryValue",
Help: "MemoryValue",
},
[]string{
"namespace",
"proc_qID",
"opID",
},
)
var RunThreadsValue = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "MyExporter",
Name: "RunThreadsValue",
Help: "RunThreadsValue",
},
[]string{
"namespace",
"proc_qID",
"opID",
},
)
prometheus.MustRegister(CpuPercentValue)
prometheus.MustRegister(MemoryValue)
prometheus.MustRegister(RunThreadsValue)
go func() {
for d := range msgs {
// I get the labels from the RabbitMQ routingkey
routingkey = strings.Split(d.RoutingKey, ".")
procid = routingkey[0]
opid = routingkey[1]
// get the data
err := json.Unmarshal([]byte(d.Body), opst)
failOnError(err, "failed to Unmarshal the opstat")
// set the values
// namespace here is the different namespaces in a Kubernetes cluster
CpuPercentValue.With(prometheus.Labels{"namespace": namespace, "proc_qID": procid, "opID": opid}).Set(opst.CpuPercent.Value)
MemoryValue.With(prometheus.Labels{"namespace": namespace, "proc_qID": procid, "opID": opid}).Set(opst.Memory.Value)
RunThreadsValue.With(prometheus.Labels{"namespace": namespace, "proc_qID": procid, "opID": opid}).Set(opst.NumThreads.Value)
}
}()
我该怎么解决这个问题?
问题是我没有删除未发布的标签。我原以为普罗米修斯的客户在收到新值时只是出口,但这种假设/期望是错误的。