-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xabier Larrakoetxea <[email protected]>
- Loading branch information
Showing
11 changed files
with
224 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// EventType is the event type of a controller enqueued object. | ||
type EventType string | ||
|
||
const ( | ||
//AddEvent is the add event. | ||
AddEvent EventType = "add" | ||
// DeleteEvent is the delete event. | ||
DeleteEvent EventType = "delete" | ||
// RequeueEvent is a requeued event (unknown state when handling again). | ||
RequeueEvent EventType = "requeue" | ||
) | ||
|
||
// MetricsRecorder knows how to record metrics of a controller. | ||
type MetricsRecorder interface { | ||
// IncResourceEvent increments in one the metric records of a queued event. | ||
IncResourceEventQueued(ctx context.Context, controller string, eventType EventType) | ||
// IncResourceEventProcessed increments in one the metric records processed event. | ||
IncResourceEventProcessed(ctx context.Context, controller string, eventType EventType) | ||
// IncResourceEventProcessedError increments in one the metric records of a processed event in error. | ||
IncResourceEventProcessedError(ctx context.Context, controller string, eventType EventType) | ||
// ObserveDurationResourceEventProcessed measures the duration it took to process a event. | ||
ObserveDurationResourceEventProcessed(ctx context.Context, controller string, eventType EventType, start time.Time) | ||
} | ||
|
||
// DummyMetricsRecorder is a dummy metrics recorder that satisifies all . | ||
var DummyMetricsRecorder = dummy(0) | ||
|
||
type dummy int | ||
|
||
func (dummy) IncResourceEventQueued(_ context.Context, _ string, _ EventType) {} | ||
func (dummy) IncResourceEventProcessed(_ context.Context, _ string, _ EventType) {} | ||
func (dummy) IncResourceEventProcessedError(_ context.Context, _ string, _ EventType) {} | ||
func (dummy) ObserveDurationResourceEventProcessed(_ context.Context, _ string, _ EventType, _ time.Time) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/spotahome/kooper/controller" | ||
) | ||
|
||
const ( | ||
promNamespace = "kooper" | ||
promControllerSubsystem = "controller" | ||
) | ||
|
||
// Config is the Recorder Config. | ||
type Config struct { | ||
// Registerer is a prometheus registerer, e.g: prometheus.Registry. | ||
// By default will use Prometheus default registry. | ||
Registerer prometheus.Registerer | ||
// Buckets sets custom buckets for the duration/latency metrics. This should be used when | ||
// the default buckets don't work. This could happen when the time to process an event is not on the | ||
// range of 5ms-10s duration. | ||
// Check https://godoc.org/github.com/prometheus/client_golang/prometheus#pkg-variables | ||
Buckets []float64 | ||
} | ||
|
||
func (c *Config) defaults() { | ||
if c.Registerer == nil { | ||
c.Registerer = prometheus.DefaultRegisterer | ||
} | ||
|
||
if c.Buckets == nil || len(c.Buckets) == 0 { | ||
c.Buckets = prometheus.DefBuckets | ||
} | ||
} | ||
|
||
// Recorder implements the metrics recording in a prometheus registry. | ||
type Recorder struct { | ||
// Metrics | ||
queuedEvents *prometheus.CounterVec | ||
processedEvents *prometheus.CounterVec | ||
processedEventErrors *prometheus.CounterVec | ||
processedEventDuration *prometheus.HistogramVec | ||
} | ||
|
||
// New returns a new Prometheus implementaiton for a metrics recorder. | ||
func New(cfg Config) *Recorder { | ||
cfg.defaults() | ||
|
||
r := &Recorder{ | ||
queuedEvents: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: promNamespace, | ||
Subsystem: promControllerSubsystem, | ||
Name: "queued_events_total", | ||
Help: "Total number of events queued.", | ||
}, []string{"controller", "type"}), | ||
|
||
processedEvents: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: promNamespace, | ||
Subsystem: promControllerSubsystem, | ||
Name: "processed_events_total", | ||
Help: "Total number of successfuly processed events.", | ||
}, []string{"controller", "type"}), | ||
|
||
processedEventErrors: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: promNamespace, | ||
Subsystem: promControllerSubsystem, | ||
Name: "processed_event_errors_total", | ||
Help: "Total number of errors processing events.", | ||
}, []string{"controller", "type"}), | ||
|
||
processedEventDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: promNamespace, | ||
Subsystem: promControllerSubsystem, | ||
Name: "processed_event_duration_seconds", | ||
Help: "The duration for a successful event to be processed.", | ||
Buckets: cfg.Buckets, | ||
}, []string{"controller", "type"}), | ||
} | ||
|
||
// Register metrics. | ||
cfg.Registerer.MustRegister( | ||
r.queuedEvents, | ||
r.processedEvents, | ||
r.processedEventErrors, | ||
r.processedEventDuration) | ||
|
||
return r | ||
} | ||
|
||
// IncResourceEventQueued satisfies metrics.Recorder interface. | ||
func (r Recorder) IncResourceEventQueued(_ context.Context, controller string, eventType controller.EventType) { | ||
r.queuedEvents.WithLabelValues(controller, string(eventType)).Inc() | ||
} | ||
|
||
// IncResourceEventProcessed satisfies metrics.Recorder interface. | ||
func (r Recorder) IncResourceEventProcessed(_ context.Context, controller string, eventType controller.EventType) { | ||
r.processedEvents.WithLabelValues(controller, string(eventType)).Inc() | ||
} | ||
|
||
// IncResourceEventProcessedError satisfies metrics.Recorder interface. | ||
func (r Recorder) IncResourceEventProcessedError(_ context.Context, controller string, eventType controller.EventType) { | ||
r.processedEventErrors.WithLabelValues(controller, string(eventType)).Inc() | ||
} | ||
|
||
// ObserveDurationResourceEventProcessed satisfies metrics.Recorder interface. | ||
func (r Recorder) ObserveDurationResourceEventProcessed(_ context.Context, controller string, eventType controller.EventType, start time.Time) { | ||
secs := time.Now().Sub(start).Seconds() | ||
r.processedEventDuration.WithLabelValues(controller, string(eventType)).Observe(secs) | ||
} | ||
|
||
// Check we implement all the required metrics recorder interfaces. | ||
var _ controller.MetricsRecorder = &Recorder{} |
Oops, something went wrong.