|
|
|
@ -455,18 +455,30 @@ func WritePrometheusExpvar(w io.Writer, kv expvar.KeyValue) {
|
|
|
|
|
writePromExpVar(w, "", kv)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
|
|
|
|
|
key := kv.Key
|
|
|
|
|
type prometheusMetricDetails struct {
|
|
|
|
|
Name string
|
|
|
|
|
Type string
|
|
|
|
|
Label string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var prometheusMetricCache sync.Map // string => *prometheusMetricDetails
|
|
|
|
|
|
|
|
|
|
func prometheusMetric(prefix string, key string) (string, string, string) {
|
|
|
|
|
cachekey := prefix + key
|
|
|
|
|
if v, ok := prometheusMetricCache.Load(cachekey); ok {
|
|
|
|
|
d := v.(*prometheusMetricDetails)
|
|
|
|
|
return d.Name, d.Type, d.Label
|
|
|
|
|
}
|
|
|
|
|
var typ string
|
|
|
|
|
var label string
|
|
|
|
|
switch {
|
|
|
|
|
case strings.HasPrefix(kv.Key, gaugePrefix):
|
|
|
|
|
case strings.HasPrefix(key, gaugePrefix):
|
|
|
|
|
typ = "gauge"
|
|
|
|
|
key = strings.TrimPrefix(kv.Key, gaugePrefix)
|
|
|
|
|
key = strings.TrimPrefix(key, gaugePrefix)
|
|
|
|
|
|
|
|
|
|
case strings.HasPrefix(kv.Key, counterPrefix):
|
|
|
|
|
case strings.HasPrefix(key, counterPrefix):
|
|
|
|
|
typ = "counter"
|
|
|
|
|
key = strings.TrimPrefix(kv.Key, counterPrefix)
|
|
|
|
|
key = strings.TrimPrefix(key, counterPrefix)
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(key, labelMapPrefix) {
|
|
|
|
|
key = strings.TrimPrefix(key, labelMapPrefix)
|
|
|
|
@ -474,7 +486,18 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
|
|
|
|
|
label, key = a, b
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
name := strings.ReplaceAll(prefix+key, "-", "_")
|
|
|
|
|
d := &prometheusMetricDetails{
|
|
|
|
|
Name: strings.ReplaceAll(prefix+key, "-", "_"),
|
|
|
|
|
Type: typ,
|
|
|
|
|
Label: label,
|
|
|
|
|
}
|
|
|
|
|
prometheusMetricCache.Store(cachekey, d)
|
|
|
|
|
return d.Name, d.Type, d.Label
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
|
|
|
|
|
key := kv.Key
|
|
|
|
|
name, typ, label := prometheusMetric(prefix, key)
|
|
|
|
|
|
|
|
|
|
switch v := kv.Value.(type) {
|
|
|
|
|
case PrometheusVar:
|
|
|
|
|