|
49 | 49 | eventsHit = promauto.NewCounterVec(
|
50 | 50 | prometheus.CounterOpts{
|
51 | 51 | Name: "k8s_event_logger_q_cache_event_total",
|
52 |
| - Help: "", |
| 52 | + Help: "Cache hit/miss events for object label fetching", |
53 | 53 | },
|
54 | 54 | []string{"cache_type"},
|
55 | 55 | )
|
@@ -141,22 +141,27 @@ func logEvent(obj interface{}, logger *log.Logger) {
|
141 | 141 | }
|
142 | 142 |
|
143 | 143 | 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 := "" |
155 | 151 |
|
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"] |
159 | 160 | }
|
| 161 | + |
| 162 | + eventsTotal. |
| 163 | + WithLabelValues(evt.Type, evt.Reason, evt.InvolvedObject.Kind, qoveryProjectId, qoveryEnvId, qoveryServiceId). |
| 164 | + Inc() |
160 | 165 | }
|
161 | 166 |
|
162 | 167 | type cacheKey string
|
@@ -234,7 +239,11 @@ func (f *ObjectLabelFetcher) LabelsForEvent(ctx context.Context, evt *corev1.Eve
|
234 | 239 | // Pick the correct dynamic ResourceInterface (namespaced or cluster-wide).
|
235 | 240 | var dr dynamic.ResourceInterface
|
236 | 241 | 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) |
238 | 247 | } else {
|
239 | 248 | dr = f.dynClient.Resource(mapping.Resource)
|
240 | 249 | }
|
|
0 commit comments