From 249069ec01b7af49aeb9cede0f267c90af125c81 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Wed, 3 Aug 2016 01:09:27 +0200 Subject: [PATCH] Unexport SelfCollector. This is most likely used nowhere and can be unexported to clean up the namespace. --- prometheus/collector.go | 12 ++--- prometheus/counter.go | 4 +- prometheus/example_selfcollector_test.go | 69 ------------------------ prometheus/histogram.go | 4 +- prometheus/summary.go | 4 +- prometheus/value.go | 8 +-- 6 files changed, 16 insertions(+), 85 deletions(-) delete mode 100644 prometheus/example_selfcollector_test.go diff --git a/prometheus/collector.go b/prometheus/collector.go index 5492ed3..d0f2969 100644 --- a/prometheus/collector.go +++ b/prometheus/collector.go @@ -50,26 +50,26 @@ type Collector interface { Collect(chan<- Metric) } -// SelfCollector implements Collector for a single Metric so that that the +// selfCollector implements Collector for a single Metric so that that the // Metric collects itself. Add it as an anonymous field to a struct that // implements Metric, and call Init with the Metric itself as an argument. -type SelfCollector struct { +type selfCollector struct { self Metric } -// Init provides the SelfCollector with a reference to the metric it is supposed +// init provides the selfCollector with a reference to the metric it is supposed // to collect. It is usually called within the factory function to create a // metric. See example. -func (c *SelfCollector) Init(self Metric) { +func (c *selfCollector) init(self Metric) { c.self = self } // Describe implements Collector. -func (c *SelfCollector) Describe(ch chan<- *Desc) { +func (c *selfCollector) Describe(ch chan<- *Desc) { ch <- c.self.Desc() } // Collect implements Collector. -func (c *SelfCollector) Collect(ch chan<- Metric) { +func (c *selfCollector) Collect(ch chan<- Metric) { ch <- c.self } diff --git a/prometheus/counter.go b/prometheus/counter.go index 7bcae03..bf44cc8 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -58,7 +58,7 @@ func NewCounter(opts CounterOpts) Counter { opts.ConstLabels, ) result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}} - result.Init(result) // Init self-collection. + result.init(result) // Init self-collection. return result } @@ -105,7 +105,7 @@ func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec { valType: CounterValue, labelPairs: makeLabelPairs(desc, lvs), }} - result.Init(result) // Init self-collection. + result.init(result) // Init self-collection. return result }, }, diff --git a/prometheus/example_selfcollector_test.go b/prometheus/example_selfcollector_test.go deleted file mode 100644 index 608deeb..0000000 --- a/prometheus/example_selfcollector_test.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2014 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package prometheus_test - -import ( - "runtime" - - "github.com/golang/protobuf/proto" - - dto "github.com/prometheus/client_model/go" - - "github.com/prometheus/client_golang/prometheus" -) - -func NewCallbackMetric(desc *prometheus.Desc, callback func() float64) *CallbackMetric { - result := &CallbackMetric{desc: desc, callback: callback} - result.Init(result) // Initialize the SelfCollector. - return result -} - -// TODO: Come up with a better example. - -// CallbackMetric is an example for a user-defined Metric that exports the -// result of a function call as a metric of type "untyped" without any -// labels. It uses SelfCollector to turn the Metric into a Collector so that it -// can be registered with Prometheus. -// -// Note that this example is pretty much academic as the prometheus package -// already provides an UntypedFunc type. -type CallbackMetric struct { - prometheus.SelfCollector - - desc *prometheus.Desc - callback func() float64 -} - -func (cm *CallbackMetric) Desc() *prometheus.Desc { - return cm.desc -} - -func (cm *CallbackMetric) Write(m *dto.Metric) error { - m.Untyped = &dto.Untyped{Value: proto.Float64(cm.callback())} - return nil -} - -func ExampleSelfCollector() { - m := NewCallbackMetric( - prometheus.NewDesc( - "runtime_goroutines_count", - "Total number of goroutines that currently exist.", - nil, nil, // No labels, these must be nil. - ), - func() float64 { - return float64(runtime.NumGoroutine()) - }, - ) - prometheus.MustRegister(m) -} diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 7a68910..160b63c 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -210,7 +210,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr // Finally we know the final length of h.upperBounds and can make counts. h.counts = make([]uint64, len(h.upperBounds)) - h.Init(h) // Init self-collection. + h.init(h) // Init self-collection. return h } @@ -222,7 +222,7 @@ type histogram struct { sumBits uint64 count uint64 - SelfCollector + selfCollector // Note that there is no mutex required. desc *Desc diff --git a/prometheus/summary.go b/prometheus/summary.go index cc97b2b..4ce13b2 100644 --- a/prometheus/summary.go +++ b/prometheus/summary.go @@ -227,12 +227,12 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary { } sort.Float64s(s.sortedObjectives) - s.Init(s) // Init self-collection. + s.init(s) // Init self-collection. return s } type summary struct { - SelfCollector + selfCollector bufMtx sync.Mutex // Protects hotBuf and hotBufExpTime. mtx sync.Mutex // Protects every other moving part. diff --git a/prometheus/value.go b/prometheus/value.go index b54ac11..a944c37 100644 --- a/prometheus/value.go +++ b/prometheus/value.go @@ -48,7 +48,7 @@ type value struct { // operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG valBits uint64 - SelfCollector + selfCollector desc *Desc valType ValueType @@ -68,7 +68,7 @@ func newValue(desc *Desc, valueType ValueType, val float64, labelValues ...strin valBits: math.Float64bits(val), labelPairs: makeLabelPairs(desc, labelValues), } - result.Init(result) + result.init(result) return result } @@ -113,7 +113,7 @@ func (v *value) Write(out *dto.Metric) error { // library to back the implementations of CounterFunc, GaugeFunc, and // UntypedFunc. type valueFunc struct { - SelfCollector + selfCollector desc *Desc valType ValueType @@ -134,7 +134,7 @@ func newValueFunc(desc *Desc, valueType ValueType, function func() float64) *val function: function, labelPairs: makeLabelPairs(desc, nil), } - result.Init(result) + result.init(result) return result }