Skip to content

Commit bc5a77b

Browse files
committed
Ref #34197: catch exceptions in timers
1 parent 387b561 commit bc5a77b

1 file changed

Lines changed: 55 additions & 47 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/backend/dispatcher/proxysharing/ProxySharingMicrometer.java

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@
2929
import eu.openanalytics.containerproxy.model.runtime.Proxy;
3030
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.BackendContainerName;
3131
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.BackendContainerNameKey;
32+
import eu.openanalytics.containerproxy.service.StructuredLogger;
3233
import eu.openanalytics.containerproxy.stat.IStatCollector;
3334
import io.micrometer.core.instrument.Gauge;
3435
import io.micrometer.core.instrument.MeterRegistry;
3536
import io.micrometer.core.instrument.Tags;
3637
import io.micrometer.core.instrument.search.MeterNotFoundException;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
3740
import org.springframework.beans.factory.annotation.Autowired;
3841
import org.springframework.context.event.EventListener;
3942

@@ -72,6 +75,7 @@ public class ProxySharingMicrometer implements IStatCollector {
7275
);
7376

7477
private Cache<String, String> recentProxies;
78+
private final Logger logger = LoggerFactory.getLogger(getClass());
7579

7680
/**
7781
* Wraps a function that returns an Integer into a function that returns a double.
@@ -138,59 +142,63 @@ public void onNewProxyEvent(NewProxyEvent event) {
138142
}
139143

140144
private void updateDelegateAppInfo() {
141-
Map<String, Gauge> existingGauges = getDelegateAppInfoGauges();
142-
for (ProxySharingScaler scaler : proxySharingScalers) {
143-
String specId = scaler.getSpec().getId();
144-
for (DelegateProxy delegateProxy : scaler.getAllDelegateProxies()) {
145-
Proxy proxy = delegateProxy.getProxy();
146-
recentProxies.put(proxy.getId(), proxy.getId());
147-
Gauge existingGauge = existingGauges.remove(proxy.getId());
148-
if (existingGauge != null && existingGauge.value() == PROXY_STATUS_TO_INTEGER.get(delegateProxy.getDelegateProxyStatus())) {
149-
// gauge already exists and value is correct
150-
continue;
151-
}
152-
if (existingGauge != null) {
153-
registry.remove(existingGauge);
145+
try {
146+
Map<String, Gauge> existingGauges = getDelegateAppInfoGauges();
147+
for (ProxySharingScaler scaler : proxySharingScalers) {
148+
String specId = scaler.getSpec().getId();
149+
for (DelegateProxy delegateProxy : scaler.getAllDelegateProxies()) {
150+
Proxy proxy = delegateProxy.getProxy();
151+
recentProxies.put(proxy.getId(), proxy.getId());
152+
Gauge existingGauge = existingGauges.remove(proxy.getId());
153+
if (existingGauge != null && existingGauge.value() == PROXY_STATUS_TO_INTEGER.get(delegateProxy.getDelegateProxyStatus())) {
154+
// gauge already exists and value is correct
155+
continue;
156+
}
157+
if (existingGauge != null) {
158+
registry.remove(existingGauge);
159+
}
160+
161+
BackendContainerName backendContainerName = getBackendContainerName(proxy);
162+
if (backendContainerName == null) {
163+
// container not fully ready, will be registered later
164+
continue;
165+
}
166+
167+
registry.gauge("delegate_app_info",
168+
Tags.of(
169+
"spec.id", specId,
170+
"proxy.id", proxy.getId(),
171+
"proxy.created.timestamp", Long.toString(proxy.getCreatedTimestamp()),
172+
"resource.id", backendContainerName.getName(),
173+
"proxy.namespace", backendContainerName.getNamespace()),
174+
PROXY_STATUS_TO_INTEGER.get(delegateProxy.getDelegateProxyStatus())
175+
);
154176
}
155-
156-
BackendContainerName backendContainerName = getBackendContainerName(proxy);
157-
if (backendContainerName == null) {
158-
// container not fully ready, will be registered later
177+
}
178+
for (Gauge gauge : existingGauges.values()) {
179+
String proxyId = gauge.getId().getTag("proxy.id");
180+
if (proxyId != null && recentProxies.getIfPresent(proxyId) != null) {
181+
// this DelegateProxy has been removed, mark it as ToRemove
182+
// when the TTL of this proxy in recentProxies expires, the gauge will be removed
183+
// this waiting period allows the metric system to pick up that the proxy is being removed
184+
registry.remove(gauge);
185+
registry.gauge("delegate_app_info",
186+
Tags.of(
187+
"spec.id", gauge.getId().getTag("spec.id"),
188+
"proxy.id", gauge.getId().getTag("proxy.id"),
189+
"proxy.created.timestamp", gauge.getId().getTag("proxy.created.timestamp"),
190+
"resource.id", gauge.getId().getTag("resource.id"),
191+
"proxy.namespace", gauge.getId().getTag("proxy.namespace")),
192+
PROXY_STATUS_TO_INTEGER.get(DelegateProxyStatus.ToRemove)
193+
);
159194
continue;
160195
}
161196

162-
registry.gauge("delegate_app_info",
163-
Tags.of(
164-
"spec.id", specId,
165-
"proxy.id", proxy.getId(),
166-
"proxy.created.timestamp", Long.toString(proxy.getCreatedTimestamp()),
167-
"resource.id", backendContainerName.getName(),
168-
"proxy.namespace", backendContainerName.getNamespace()),
169-
PROXY_STATUS_TO_INTEGER.get(delegateProxy.getDelegateProxyStatus())
170-
);
171-
}
172-
}
173-
for (Gauge gauge : existingGauges.values()) {
174-
String proxyId = gauge.getId().getTag("proxy.id");
175-
if (proxyId != null && recentProxies.getIfPresent(proxyId) != null) {
176-
// this DelegateProxy has been removed, mark it as ToRemove
177-
// when the TTL of this proxy in recentProxies expires, the gauge will be removed
178-
// this waiting period allows the metric system to pick up that the proxy is being removed
197+
// the proxy of this gauge no longer exists -> remove the gauge
179198
registry.remove(gauge);
180-
registry.gauge("delegate_app_info",
181-
Tags.of(
182-
"spec.id", gauge.getId().getTag("spec.id"),
183-
"proxy.id", gauge.getId().getTag("proxy.id"),
184-
"proxy.created.timestamp", gauge.getId().getTag("proxy.created.timestamp"),
185-
"resource.id", gauge.getId().getTag("resource.id"),
186-
"proxy.namespace", gauge.getId().getTag("proxy.namespace")),
187-
PROXY_STATUS_TO_INTEGER.get(DelegateProxyStatus.ToRemove)
188-
);
189-
continue;
190199
}
191-
192-
// the proxy of this gauge no longer exists -> remove the gauge
193-
registry.remove(gauge);
200+
} catch (Exception e) {
201+
logger.warn("Error while updating delegateAppInfo", e);
194202
}
195203
}
196204

0 commit comments

Comments
 (0)