Skip to content

Commit 30517a4

Browse files
committed
robustify
1 parent 5b98c17 commit 30517a4

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

main.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var (
4949
eventsHit = promauto.NewCounterVec(
5050
prometheus.CounterOpts{
5151
Name: "k8s_event_logger_q_cache_event_total",
52-
Help: "",
52+
Help: "Cache hit/miss events for object label fetching",
5353
},
5454
[]string{"cache_type"},
5555
)
@@ -141,22 +141,27 @@ func logEvent(obj interface{}, logger *log.Logger) {
141141
}
142142

143143
func recordMetric(evt *corev1.Event, fetcher *ObjectLabelFetcher) {
144-
if *metricsEnabled {
145-
qoveryProjectId := ""
146-
qoveryEnvId := ""
147-
qoveryServiceId := ""
148-
149-
labels, err := fetcher.LabelsForEvent(context.Background(), evt)
150-
if err == nil {
151-
qoveryProjectId = labels["qovery.com/project-id"]
152-
qoveryEnvId = labels["qovery.com/environment-id"]
153-
qoveryServiceId = labels["qovery.com/service-id"]
154-
}
144+
if !*metricsEnabled {
145+
return
146+
}
147+
148+
qoveryProjectId := ""
149+
qoveryEnvId := ""
150+
qoveryServiceId := ""
155151

156-
eventsTotal.
157-
WithLabelValues(evt.Type, evt.Reason, evt.InvolvedObject.Kind, qoveryProjectId, qoveryEnvId, qoveryServiceId).
158-
Inc()
152+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
153+
defer cancel()
154+
155+
labels, err := fetcher.LabelsForEvent(ctx, evt)
156+
if err == nil {
157+
qoveryProjectId = labels["qovery.com/project-id"]
158+
qoveryEnvId = labels["qovery.com/environment-id"]
159+
qoveryServiceId = labels["qovery.com/service-id"]
159160
}
161+
162+
eventsTotal.
163+
WithLabelValues(evt.Type, evt.Reason, evt.InvolvedObject.Kind, qoveryProjectId, qoveryEnvId, qoveryServiceId).
164+
Inc()
160165
}
161166

162167
type cacheKey string
@@ -234,7 +239,11 @@ func (f *ObjectLabelFetcher) LabelsForEvent(ctx context.Context, evt *corev1.Eve
234239
// Pick the correct dynamic ResourceInterface (namespaced or cluster-wide).
235240
var dr dynamic.ResourceInterface
236241
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
237-
dr = f.dynClient.Resource(mapping.Resource).Namespace(evt.InvolvedObject.Namespace)
242+
ns := evt.InvolvedObject.Namespace
243+
if ns == "" {
244+
ns = evt.Namespace
245+
}
246+
dr = f.dynClient.Resource(mapping.Resource).Namespace(ns)
238247
} else {
239248
dr = f.dynClient.Resource(mapping.Resource)
240249
}

0 commit comments

Comments
 (0)