2025-03-31 19:41:11 +05:30
|
|
|
package clickhouseprometheus
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2025-06-16 19:42:17 +05:30
|
|
|
"github.com/prometheus/common/model"
|
2025-03-31 19:41:11 +05:30
|
|
|
"github.com/prometheus/prometheus/prompb"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Unmarshals JSON into Prometheus labels. It does not preserve order.
|
|
|
|
|
func unmarshalLabels(s string) ([]prompb.Label, string, error) {
|
|
|
|
|
var metricName string
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &m); err != nil {
|
|
|
|
|
return nil, metricName, err
|
|
|
|
|
}
|
|
|
|
|
res := make([]prompb.Label, 0, len(m))
|
|
|
|
|
for n, v := range m {
|
|
|
|
|
if n == "__name__" {
|
|
|
|
|
metricName = v
|
2025-06-16 19:42:17 +05:30
|
|
|
} else {
|
|
|
|
|
if !model.IsValidLegacyMetricName(n) {
|
|
|
|
|
n = `"` + n + `"`
|
|
|
|
|
}
|
2025-03-31 19:41:11 +05:30
|
|
|
}
|
2025-06-16 19:42:17 +05:30
|
|
|
|
2025-03-31 19:41:11 +05:30
|
|
|
res = append(res, prompb.Label{
|
|
|
|
|
Name: n,
|
|
|
|
|
Value: v,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return res, metricName, nil
|
|
|
|
|
}
|