client_golang/prometheus/timer.go

54 lines
1.2 KiB
Go
Raw Normal View History

2016-10-31 15:24:29 +03:00
// Copyright 2016 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
import "time"
// Observer is the interface that wraps the Observe method, used by Histogram
// and Summary to add observations.
type Observer interface {
2016-10-31 15:24:29 +03:00
Observe(float64)
}
type observerFunc func(float64)
func (o observerFunc) Observe(value float64) {
o(value)
}
type Timer struct {
begin time.Time
observer Observer
}
func StartTimer() *Timer {
return &Timer{begin: time.Now()}
}
func (t *Timer) With(o Observer) *Timer {
t.observer = o
return t
}
func (t *Timer) WithGauge(g Gauge) *Timer {
t.observer = observerFunc(g.Set)
return t
}
2016-10-31 15:24:29 +03:00
func (t *Timer) Stop() {
if t.observer != nil {
t.observer.Observe(time.Since(t.begin).Seconds())
}
2016-10-31 15:24:29 +03:00
}