forked from opsgenie/kubernetes-event-exporter
-
Notifications
You must be signed in to change notification settings - Fork 186
feat: add prometheus receiver #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cherylfbrown
wants to merge
4
commits into
resmoio:master
Choose a base branch
from
cherylfbrown:prometheus-receiver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
package sinks | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"k8s.io/utils/strings/slices" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/resmoio/kubernetes-event-exporter/pkg/kube" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func newGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec { | ||
v := prometheus.NewGaugeVec(opts, labelNames) | ||
prometheus.MustRegister(v) | ||
return v | ||
} | ||
|
||
type PrometheusConfig struct { | ||
EventsMetricsNamePrefix string `yaml:"eventsMetricsNamePrefix"` | ||
ReasonFilter map[string][]string `yaml:"reasonFilter"` | ||
} | ||
|
||
type PrometheusGaugeVec interface { | ||
With(labels prometheus.Labels) prometheus.Gauge | ||
Delete(labels prometheus.Labels) bool | ||
} | ||
|
||
type PrometheusSink struct { | ||
cfg *PrometheusConfig | ||
kinds []string | ||
metricsByKind map[string]PrometheusGaugeVec | ||
} | ||
|
||
func NewPrometheusSink(config *PrometheusConfig) (Sink, error) { | ||
if config.EventsMetricsNamePrefix == "" { | ||
config.EventsMetricsNamePrefix = "event_exporter_" | ||
} | ||
|
||
metricsByKind := map[string]PrometheusGaugeVec{} | ||
|
||
log.Info().Msgf("Initializing new Prometheus sink...") | ||
kinds := []string{} | ||
for kind := range config.ReasonFilter { | ||
kinds = append(kinds, kind) | ||
metricName := config.EventsMetricsNamePrefix + strings.ToLower(kind) + "_event_count" | ||
metricLabels := []string{strings.ToLower(kind), "namespace", "reason"} | ||
metricsByKind[kind] = newGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: metricName, | ||
Help: "Event counts for " + kind + " resources.", | ||
}, metricLabels) | ||
|
||
log.Info().Msgf("Created metric: %s, will emit events: %v with additional labels: %v", kind, config.ReasonFilter[kind], metricLabels) | ||
} | ||
|
||
return &PrometheusSink{ | ||
cfg: config, | ||
kinds: kinds, | ||
metricsByKind: metricsByKind, | ||
}, nil | ||
} | ||
|
||
func (o *PrometheusSink) Send(_ context.Context, ev *kube.EnhancedEvent) error { | ||
kind := ev.InvolvedObject.Kind | ||
if slices.Contains(o.kinds, kind) { | ||
for _, reason := range o.cfg.ReasonFilter[kind] { | ||
if ev.Reason == reason { | ||
SetEventCount(o.metricsByKind[kind], ev.InvolvedObject, reason, ev.Count) | ||
} else { | ||
DeleteEventCount(o.metricsByKind[kind], ev.InvolvedObject, reason) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *PrometheusSink) Close() { | ||
// No-op | ||
} | ||
|
||
func getMetricLabels(obj kube.EnhancedObjectReference, reason string) prometheus.Labels { | ||
prometheusLabels := prometheus.Labels{ | ||
strings.ToLower(obj.Kind): obj.Name, | ||
"namespace": obj.Namespace, | ||
"reason": reason, | ||
} | ||
|
||
return prometheusLabels | ||
} | ||
|
||
func SetEventCount(metric PrometheusGaugeVec, obj kube.EnhancedObjectReference, reason string, count int32) { | ||
labels := getMetricLabels(obj, reason) | ||
log.Info().Msgf("Setting event count metric with labels: %v", labels) | ||
metric.With(labels).Set(float64(count)) | ||
} | ||
|
||
func DeleteEventCount(metric PrometheusGaugeVec, obj kube.EnhancedObjectReference, reason string) { | ||
labels := getMetricLabels(obj, reason) | ||
log.Info().Msgf("Deleting event count metric with labels: %v", labels) | ||
metric.Delete(labels) | ||
} |
This file contains hidden or 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,137 @@ | ||
package sinks | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/resmoio/kubernetes-event-exporter/pkg/kube" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type mockGauge struct { | ||
mock.Mock | ||
prometheus.Gauge | ||
} | ||
|
||
func (m *mockGauge) Set(count float64) { | ||
m.Called(count) | ||
} | ||
|
||
type mockGuageVec struct { | ||
mock.Mock | ||
*prometheus.GaugeVec | ||
} | ||
|
||
func (v *mockGuageVec) With(labels prometheus.Labels) prometheus.Gauge { | ||
withArgs := v.Called(labels) | ||
return withArgs.Get(0).(prometheus.Gauge) | ||
} | ||
|
||
func (v *mockGuageVec) Delete(labels prometheus.Labels) bool { | ||
deleteArgs := v.Called(labels) | ||
return deleteArgs.Get(0).(bool) | ||
} | ||
|
||
func mockEvent(kind string, name string, namespace string, reason string, count int32) *kube.EnhancedEvent { | ||
ev := &kube.EnhancedEvent{} | ||
ev.Reason = reason | ||
ev.Count = count | ||
ev.InvolvedObject.Kind = kind | ||
ev.InvolvedObject.Name = name | ||
ev.InvolvedObject.Namespace = namespace | ||
|
||
return ev | ||
} | ||
|
||
func TestPrometheusSink_Send(t *testing.T) { | ||
configKind := "Pod" | ||
configReason := "Starting" | ||
testEvent := mockEvent("Pod", "testpod", "testnamespace", "Starting", 1) | ||
|
||
tests := []struct { | ||
name string | ||
configKind string | ||
configReason string | ||
ev *kube.EnhancedEvent | ||
wantPrometheusLabels prometheus.Labels | ||
wantErr bool | ||
wantSetCalled bool | ||
wantDeleteCalled bool | ||
}{ | ||
{ | ||
name: "emits desired resource event", | ||
configKind: configKind, | ||
configReason: configReason, | ||
ev: testEvent, | ||
wantPrometheusLabels: prometheus.Labels{ | ||
strings.ToLower(configKind): testEvent.InvolvedObject.Name, | ||
"namespace": testEvent.InvolvedObject.Namespace, | ||
"reason": configReason, | ||
}, | ||
wantErr: false, | ||
wantSetCalled: true, | ||
wantDeleteCalled: false, | ||
}, | ||
{ | ||
name: "deletes desired resource event", | ||
configKind: configKind, | ||
configReason: "Creating", | ||
ev: testEvent, | ||
wantPrometheusLabels: prometheus.Labels{ | ||
strings.ToLower(configKind): testEvent.InvolvedObject.Name, | ||
"namespace": testEvent.InvolvedObject.Namespace, | ||
"reason": "Creating", | ||
}, | ||
wantErr: false, | ||
wantSetCalled: false, | ||
wantDeleteCalled: true, | ||
}, | ||
{ | ||
name: "does nothing if kind is not expected", | ||
configKind: "ReplicaSet", | ||
configReason: "SuccessfulCreate", | ||
ev: testEvent, | ||
wantPrometheusLabels: prometheus.Labels{}, | ||
wantErr: false, | ||
wantSetCalled: false, | ||
wantDeleteCalled: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
mockGauge := &mockGauge{} | ||
mockGauge.On("Set", mock.Anything).Return() | ||
mockPodMetric := &mockGuageVec{} | ||
mockPodMetric.On("With", mock.Anything).Return(mockGauge) | ||
mockPodMetric.On("Delete", mock.Anything).Return(true) | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
o := &PrometheusSink{ | ||
cfg: &PrometheusConfig{ | ||
EventsMetricsNamePrefix: "test_prefix_", | ||
ReasonFilter: map[string][]string{tt.configKind: {tt.configReason}}, | ||
}, | ||
kinds: []string{tt.configKind}, | ||
metricsByKind: map[string]PrometheusGaugeVec{tt.configKind: mockPodMetric}, | ||
} | ||
if err := o.Send(context.TODO(), tt.ev); (err != nil) != tt.wantErr { | ||
t.Errorf("PrometheusSink.Send() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
if tt.wantSetCalled { | ||
mockPodMetric.AssertCalled(t, "With", tt.wantPrometheusLabels) | ||
mockGauge.AssertCalled(t, "Set", float64(1)) | ||
} else { | ||
mockPodMetric.AssertNotCalled(t, "With") | ||
mockGauge.AssertNotCalled(t, "Set") | ||
} | ||
|
||
if tt.wantDeleteCalled { | ||
mockPodMetric.AssertCalled(t, "Delete", tt.wantPrometheusLabels) | ||
} else { | ||
mockPodMetric.AssertNotCalled(t, "Delete") | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.