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
|
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 {
|
type metric interface {
|
||||||
marshalTo(prefix string, w io.Writer)
|
marshalTo(prefix string, w io.Writer)
|
||||||
metricType() string
|
metricType() string
|
||||||
@ -306,3 +298,20 @@ func writeMetricFloat64(w io.Writer, metricName, metricType string, value float6
|
|||||||
writeMetadataIfNeeded(w, metricName, metricType)
|
writeMetadataIfNeeded(w, metricName, metricType)
|
||||||
fmt.Fprintf(w, "%s %g\n", metricName, value)
|
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 := ""
|
prevMetricFamily := ""
|
||||||
for _, nm := range sa {
|
for _, nm := range sa {
|
||||||
metricFamily := nm.family()
|
metricFamily := getMetricFamily(nm.name)
|
||||||
if metricFamily != prevMetricFamily {
|
if metricFamily != prevMetricFamily {
|
||||||
// write meta info only once per metric family
|
// write meta info only once per metric family
|
||||||
metricType := nm.metric.metricType()
|
metricType := nm.metric.metricType()
|
||||||
writeMetadataIfNeeded(&bb, metricFamily, metricType)
|
writeMetadataIfNeeded(&bb, nm.name, metricType)
|
||||||
prevMetricFamily = metricFamily
|
prevMetricFamily = metricFamily
|
||||||
}
|
}
|
||||||
// Call marshalTo without the global lock, since certain metric types such as Gauge
|
// 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())
|
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.
|
// NewHistogram creates and returns new histogram in s with the given name.
|
||||||
//
|
//
|
||||||
// name must be valid Prometheus-compatible metric with possible labels.
|
// name must be valid Prometheus-compatible metric with possible labels.
|
||||||
|
Loading…
Reference in New Issue
Block a user