Merge pull request #247 from prometheus/beorn7/deprecation

Remove deprecated features that are esay to replace
This commit is contained in:
Björn Rabenstein 2016-10-26 13:07:08 +02:00 committed by GitHub
commit 3fb8ace93b
5 changed files with 82 additions and 86 deletions

View File

@ -30,15 +30,6 @@ type Counter interface {
Metric Metric
Collector Collector
// Set is used to set the Counter to an arbitrary value. It is only used
// if you have to transfer a value from an external counter into this
// Prometheus metric. Do not use it for regular handling of a
// Prometheus counter (as it can be used to break the contract of
// monotonically increasing values).
//
// Deprecated: Use NewConstMetric to create a counter for an external
// value. A Counter should never be set.
Set(float64)
// Inc increments the counter by 1. // Inc increments the counter by 1.
Inc() Inc()
// Add adds the given value to the counter. It panics if the value is < // Add adds the given value to the counter. It panics if the value is <

View File

@ -62,7 +62,7 @@ func giveBuf(buf *bytes.Buffer) {
// //
// Deprecated: Please note the issues described in the doc comment of // Deprecated: Please note the issues described in the doc comment of
// InstrumentHandler. You might want to consider using promhttp.Handler instead // InstrumentHandler. You might want to consider using promhttp.Handler instead
// (which is non instrumented). // (which is not instrumented).
func Handler() http.Handler { func Handler() http.Handler {
return InstrumentHandler("prometheus", UninstrumentedHandler()) return InstrumentHandler("prometheus", UninstrumentedHandler())
} }
@ -245,23 +245,46 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
}, },
instLabels, instLabels,
) )
if err := Register(reqCnt); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqCnt = are.ExistingCollector.(*CounterVec)
} else {
panic(err)
}
}
opts.Name = "request_duration_microseconds" opts.Name = "request_duration_microseconds"
opts.Help = "The HTTP request latencies in microseconds." opts.Help = "The HTTP request latencies in microseconds."
reqDur := NewSummary(opts) reqDur := NewSummary(opts)
if err := Register(reqDur); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqDur = are.ExistingCollector.(Summary)
} else {
panic(err)
}
}
opts.Name = "request_size_bytes" opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes." opts.Help = "The HTTP request sizes in bytes."
reqSz := NewSummary(opts) reqSz := NewSummary(opts)
if err := Register(reqSz); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
reqSz = are.ExistingCollector.(Summary)
} else {
panic(err)
}
}
opts.Name = "response_size_bytes" opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes." opts.Help = "The HTTP response sizes in bytes."
resSz := NewSummary(opts) resSz := NewSummary(opts)
if err := Register(resSz); err != nil {
regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec) if are, ok := err.(AlreadyRegisteredError); ok {
regReqDur := MustRegisterOrGet(reqDur).(Summary) resSz = are.ExistingCollector.(Summary)
regReqSz := MustRegisterOrGet(reqSz).(Summary) } else {
regResSz := MustRegisterOrGet(resSz).(Summary) panic(err)
}
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now() now := time.Now()
@ -285,10 +308,10 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
method := sanitizeMethod(r.Method) method := sanitizeMethod(r.Method)
code := sanitizeCode(delegate.status) code := sanitizeCode(delegate.status)
regReqCnt.WithLabelValues(method, code).Inc() reqCnt.WithLabelValues(method, code).Inc()
regReqDur.Observe(elapsed) reqDur.Observe(elapsed)
regResSz.Observe(float64(delegate.written)) resSz.Observe(float64(delegate.written))
regReqSz.Observe(float64(<-out)) reqSz.Observe(float64(<-out))
}) })
} }

View File

@ -46,7 +46,7 @@ func TestInstrumentHandler(t *testing.T) {
ConstLabels: Labels{"handler": "test-handler"}, ConstLabels: Labels{"handler": "test-handler"},
} }
reqCnt := MustRegisterOrGet(NewCounterVec( reqCnt := NewCounterVec(
CounterOpts{ CounterOpts{
Namespace: opts.Namespace, Namespace: opts.Namespace,
Subsystem: opts.Subsystem, Subsystem: opts.Subsystem,
@ -55,19 +55,51 @@ func TestInstrumentHandler(t *testing.T) {
ConstLabels: opts.ConstLabels, ConstLabels: opts.ConstLabels,
}, },
instLabels, instLabels,
)).(*CounterVec) )
err := Register(reqCnt)
if err == nil {
t.Fatal("expected reqCnt to be registered already")
}
if are, ok := err.(AlreadyRegisteredError); ok {
reqCnt = are.ExistingCollector.(*CounterVec)
} else {
t.Fatal("unexpected registration error:", err)
}
opts.Name = "request_duration_microseconds" opts.Name = "request_duration_microseconds"
opts.Help = "The HTTP request latencies in microseconds." opts.Help = "The HTTP request latencies in microseconds."
reqDur := MustRegisterOrGet(NewSummary(opts)).(Summary) reqDur := NewSummary(opts)
err = Register(reqDur)
if err == nil {
t.Fatal("expected reqDur to be registered already")
}
if are, ok := err.(AlreadyRegisteredError); ok {
reqDur = are.ExistingCollector.(Summary)
} else {
t.Fatal("unexpected registration error:", err)
}
opts.Name = "request_size_bytes" opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes." opts.Help = "The HTTP request sizes in bytes."
MustRegisterOrGet(NewSummary(opts)) reqSz := NewSummary(opts)
err = Register(reqSz)
if err == nil {
t.Fatal("expected reqSz to be registered already")
}
if _, ok := err.(AlreadyRegisteredError); !ok {
t.Fatal("unexpected registration error:", err)
}
opts.Name = "response_size_bytes" opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes." opts.Help = "The HTTP response sizes in bytes."
MustRegisterOrGet(NewSummary(opts)) resSz := NewSummary(opts)
err = Register(resSz)
if err == nil {
t.Fatal("expected resSz to be registered already")
}
if _, ok := err.(AlreadyRegisteredError); !ok {
t.Fatal("unexpected registration error:", err)
}
reqCnt.Reset() reqCnt.Reset()

View File

@ -152,38 +152,6 @@ func MustRegister(cs ...Collector) {
DefaultRegisterer.MustRegister(cs...) DefaultRegisterer.MustRegister(cs...)
} }
// RegisterOrGet registers the provided Collector with the DefaultRegisterer and
// returns the Collector, unless an equal Collector was registered before, in
// which case that Collector is returned.
//
// Deprecated: RegisterOrGet is merely a convenience function for the
// implementation as described in the documentation for
// AlreadyRegisteredError. As the use case is relatively rare, this function
// will be removed in a future version of this package to clean up the
// namespace.
func RegisterOrGet(c Collector) (Collector, error) {
if err := Register(c); err != nil {
if are, ok := err.(AlreadyRegisteredError); ok {
return are.ExistingCollector, nil
}
return nil, err
}
return c, nil
}
// MustRegisterOrGet behaves like RegisterOrGet but panics instead of returning
// an error.
//
// Deprecated: This is deprecated for the same reason RegisterOrGet is. See
// there for details.
func MustRegisterOrGet(c Collector) Collector {
c, err := RegisterOrGet(c)
if err != nil {
panic(err)
}
return c
}
// Unregister removes the registration of the provided Collector from the // Unregister removes the registration of the provided Collector from the
// DefaultRegisterer. // DefaultRegisterer.
// //
@ -201,25 +169,6 @@ func (gf GathererFunc) Gather() ([]*dto.MetricFamily, error) {
return gf() return gf()
} }
// SetMetricFamilyInjectionHook replaces the DefaultGatherer with one that
// gathers from the previous DefaultGatherers but then merges the MetricFamily
// protobufs returned from the provided hook function with the MetricFamily
// protobufs returned from the original DefaultGatherer.
//
// Deprecated: This function manipulates the DefaultGatherer variable. Consider
// the implications, i.e. don't do this concurrently with any uses of the
// DefaultGatherer. In the rare cases where you need to inject MetricFamily
// protobufs directly, it is recommended to use a custom Registry and combine it
// with a custom Gatherer using the Gatherers type (see
// there). SetMetricFamilyInjectionHook only exists for compatibility reasons
// with previous versions of this package.
func SetMetricFamilyInjectionHook(hook func() []*dto.MetricFamily) {
DefaultGatherer = Gatherers{
DefaultGatherer,
GathererFunc(func() ([]*dto.MetricFamily, error) { return hook(), nil }),
}
}
// AlreadyRegisteredError is returned by the Register method if the Collector to // AlreadyRegisteredError is returned by the Register method if the Collector to
// be registered has already been registered before, or a different Collector // be registered has already been registered before, or a different Collector
// that collects the same metrics has been registered before. Registration fails // that collects the same metrics has been registered before. Registration fails

View File

@ -526,20 +526,21 @@ func TestRegisterWithOrGet(t *testing.T) {
}, },
[]string{"foo", "bar"}, []string{"foo", "bar"},
) )
if err := prometheus.Register(original); err != nil { var err error
if err = prometheus.Register(original); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := prometheus.Register(equalButNotSame); err == nil { if err = prometheus.Register(equalButNotSame); err == nil {
t.Fatal("expected error when registringe equal collector") t.Fatal("expected error when registringe equal collector")
} }
existing, err := prometheus.RegisterOrGet(equalButNotSame) if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
if err != nil { if are.ExistingCollector != original {
t.Fatal(err)
}
if existing != original {
t.Error("expected original collector but got something else") t.Error("expected original collector but got something else")
} }
if existing == equalButNotSame { if are.ExistingCollector == equalButNotSame {
t.Error("expected original callector but got new one") t.Error("expected original callector but got new one")
} }
} else {
t.Error("unexpected error:", err)
}
} }