Skip to content

Commit

Permalink
Expose WriteMetadataIfNeeded() function
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Dec 19, 2023
1 parent 2d7f9a1 commit efd161d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func writeGoMetrics(w io.Writer) {
}
phis := []float64{0, 0.25, 0.5, 0.75, 1}
quantiles := make([]float64, 0, len(phis))
writeMetadataIfNeeded(w, "go_gc_duration_seconds", "summary")
WriteMetadataIfNeeded(w, "go_gc_duration_seconds", "summary")
for i, q := range gcPauses.Quantiles(quantiles[:0], phis) {
fmt.Fprintf(w, `go_gc_duration_seconds{quantile="%g"} %g`+"\n", phis[i], q)
}
Expand All @@ -97,10 +97,10 @@ func writeGoMetrics(w io.Writer) {
WriteGaugeUint64(w, "go_threads", uint64(numThread))

// Export build details.
writeMetadataIfNeeded(w, "go_info", "gauge")
WriteMetadataIfNeeded(w, "go_info", "gauge")
fmt.Fprintf(w, "go_info{version=%q} 1\n", runtime.Version())

writeMetadataIfNeeded(w, "go_info_ext", "gauge")
WriteMetadataIfNeeded(w, "go_info_ext", "gauge")
fmt.Fprintf(w, "go_info_ext{compiler=%q, GOARCH=%q, GOOS=%q, GOROOT=%q} 1\n",
runtime.Compiler, runtime.GOARCH, runtime.GOOS, runtime.GOROOT())
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func writeRuntimeHistogramMetric(w io.Writer, name string, h *runtimemetrics.Flo

totalCount := uint64(0)
iNext := 0.0
writeMetadataIfNeeded(w, name, "histogram")
WriteMetadataIfNeeded(w, name, "histogram")
for i, count := range counts {
totalCount += count
if float64(i) >= iNext {
Expand Down
9 changes: 6 additions & 3 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,19 @@ func WriteCounterFloat64(w io.Writer, name string, value float64) {
}

func writeMetricUint64(w io.Writer, metricName, metricType string, value uint64) {
writeMetadataIfNeeded(w, metricName, metricType)
WriteMetadataIfNeeded(w, metricName, metricType)
fmt.Fprintf(w, "%s %d\n", metricName, value)
}

func writeMetricFloat64(w io.Writer, metricName, metricType string, value float64) {
writeMetadataIfNeeded(w, metricName, metricType)
WriteMetadataIfNeeded(w, metricName, metricType)
fmt.Fprintf(w, "%s %g\n", metricName, value)
}

func writeMetadataIfNeeded(w io.Writer, metricName, metricType string) {
// WriteMetadataIfNeeded writes HELP and TYPE metadata for the given metricName and metricType if this is globally enabled via ExposeMetadata().
//
// If the metadata exposition isn't enabled, then this function is no-op.
func WriteMetadataIfNeeded(w io.Writer, metricName, metricType string) {
if !isMetadataEnabled() {
return
}
Expand Down
2 changes: 1 addition & 1 deletion set.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *Set) WritePrometheus(w io.Writer) {
if metricFamily != prevMetricFamily {
// write meta info only once per metric family
metricType := nm.metric.metricType()
writeMetadataIfNeeded(&bb, nm.name, metricType)
WriteMetadataIfNeeded(&bb, nm.name, metricType)
prevMetricFamily = metricFamily
}
// Call marshalTo without the global lock, since certain metric types such as Gauge
Expand Down

0 comments on commit efd161d

Please sign in to comment.