allow passing metric names with labels into Write{Counter,Gauge}{Uint64,Float64} functions
This commit is contained in:
parent
9dc7358869
commit
2d7f9a140e
25
metrics.go
25
metrics.go
@ -28,14 +28,6 @@ type namedMetric struct {
|
||||
isAux bool
|
||||
}
|
||||
|
||||
func (nm *namedMetric) family() string {
|
||||
n := strings.IndexByte(nm.name, '{')
|
||||
if n < 0 {
|
||||
return nm.name
|
||||
}
|
||||
return nm.name[:n]
|
||||
}
|
||||
|
||||
type metric interface {
|
||||
marshalTo(prefix string, w io.Writer)
|
||||
metricType() string
|
||||
@ -306,3 +298,20 @@ func writeMetricFloat64(w io.Writer, metricName, metricType string, value float6
|
||||
writeMetadataIfNeeded(w, metricName, metricType)
|
||||
fmt.Fprintf(w, "%s %g\n", metricName, value)
|
||||
}
|
||||
|
||||
func writeMetadataIfNeeded(w io.Writer, metricName, metricType string) {
|
||||
if !isMetadataEnabled() {
|
||||
return
|
||||
}
|
||||
metricFamily := getMetricFamily(metricName)
|
||||
fmt.Fprintf(w, "# HELP %s\n", metricFamily)
|
||||
fmt.Fprintf(w, "# TYPE %s %s\n", metricFamily, metricType)
|
||||
}
|
||||
|
||||
func getMetricFamily(metricName string) string {
|
||||
n := strings.IndexByte(metricName, '{')
|
||||
if n < 0 {
|
||||
return metricName
|
||||
}
|
||||
return metricName[:n]
|
||||
}
|
||||
|
12
set.go
12
set.go
@ -49,11 +49,11 @@ func (s *Set) WritePrometheus(w io.Writer) {
|
||||
|
||||
prevMetricFamily := ""
|
||||
for _, nm := range sa {
|
||||
metricFamily := nm.family()
|
||||
metricFamily := getMetricFamily(nm.name)
|
||||
if metricFamily != prevMetricFamily {
|
||||
// write meta info only once per metric family
|
||||
metricType := nm.metric.metricType()
|
||||
writeMetadataIfNeeded(&bb, metricFamily, metricType)
|
||||
writeMetadataIfNeeded(&bb, nm.name, metricType)
|
||||
prevMetricFamily = metricFamily
|
||||
}
|
||||
// Call marshalTo without the global lock, since certain metric types such as Gauge
|
||||
@ -63,14 +63,6 @@ func (s *Set) WritePrometheus(w io.Writer) {
|
||||
w.Write(bb.Bytes())
|
||||
}
|
||||
|
||||
func writeMetadataIfNeeded(w io.Writer, metricFamily, metricType string) {
|
||||
if !isMetadataEnabled() {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "# HELP %s\n", metricFamily)
|
||||
fmt.Fprintf(w, "# TYPE %s %s\n", metricFamily, metricType)
|
||||
}
|
||||
|
||||
// NewHistogram creates and returns new histogram in s with the given name.
|
||||
//
|
||||
// name must be valid Prometheus-compatible metric with possible labels.
|
||||
|
Loading…
x
Reference in New Issue
Block a user