Skip to content

Commit 7c11c3d

Browse files
committed
Fix #34609: catch errors in stat collectors
1 parent 018244c commit 7c11c3d

6 files changed

Lines changed: 94 additions & 71 deletions

File tree

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,24 @@ public void registerSeatWaitTime(String specId, Duration time) {
123123

124124
@EventListener
125125
public void onNewProxyEvent(NewProxyEvent event) {
126-
if (event.getUserId() != null || event.getBackendContainerName() == null) {
127-
return;
128-
}
129-
if (specIds.contains(event.getSpecId())) {
130-
recentProxies.put(event.getProxyId(), event.getProxyId());
131-
registry.gauge("delegate.app.status",
132-
Tags.of(
133-
"spec.id", event.getSpecId(),
134-
"proxy.id", event.getProxyId(),
135-
"proxy.created.timestamp", Long.toString(event.getCreatedTimestamp()),
136-
"resource.id", event.getBackendContainerName().getName(),
137-
"proxy.namespace", event.getBackendContainerName().getNamespace()),
138-
PROXY_STATUS_TO_INTEGER.get(DelegateProxyStatus.Pending)
139-
);
126+
try {
127+
if (event.getUserId() != null || event.getBackendContainerName() == null) {
128+
return;
129+
}
130+
if (specIds.contains(event.getSpecId())) {
131+
recentProxies.put(event.getProxyId(), event.getProxyId());
132+
registry.gauge("delegate.app.status",
133+
Tags.of(
134+
"spec.id", event.getSpecId(),
135+
"proxy.id", event.getProxyId(),
136+
"proxy.created.timestamp", Long.toString(event.getCreatedTimestamp()),
137+
"resource.id", event.getBackendContainerName().getName(),
138+
"proxy.namespace", event.getBackendContainerName().getNamespace()),
139+
PROXY_STATUS_TO_INTEGER.get(DelegateProxyStatus.Pending)
140+
);
141+
}
142+
} catch (Exception e) {
143+
logger.warn("Collecting event failed", e);
140144
}
141145
}
142146

src/main/java/eu/openanalytics/containerproxy/stat/impl/AbstractDbCollector.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,45 @@ public abstract class AbstractDbCollector implements IStatCollector {
5151

5252
@Async
5353
@EventListener
54-
public void onUserLogoutEvent(UserLogoutEvent event) throws IOException {
55-
writeToDb(event, event.getTimestamp(), event.getUserId(), "Logout", null, event.getAuthentication());
54+
public void onUserLogoutEvent(UserLogoutEvent event) {
55+
try {
56+
writeToDb(event, event.getTimestamp(), event.getUserId(), "Logout", null, event.getAuthentication());
57+
} catch (Exception e) {
58+
logger.warn("Collecting event failed", e);
59+
}
5660
}
5761

5862
@Async
5963
@EventListener
6064
public void onUserLoginEvent(UserLoginEvent event) throws IOException {
61-
writeToDb(event, event.getTimestamp(), event.getUserId(), "Login", null, event.getAuthentication());
65+
try {
66+
writeToDb(event, event.getTimestamp(), event.getUserId(), "Login", null, event.getAuthentication());
67+
} catch (Exception e) {
68+
logger.warn("Collecting event failed", e);
69+
}
6270
}
6371

6472
@Async
6573
@EventListener
6674
public void onProxyStartEvent(ProxyStartEvent event) throws IOException {
67-
if (event.isLocalEvent()) {
68-
writeToDb(event,event.getTimestamp(), event.getUserId(), "ProxyStart", event.getSpecId(), event.getAuthentication());
75+
try {
76+
if (event.isLocalEvent()) {
77+
writeToDb(event, event.getTimestamp(), event.getUserId(), "ProxyStart", event.getSpecId(), event.getAuthentication());
78+
}
79+
} catch (Exception e) {
80+
logger.warn("Collecting event failed", e);
6981
}
7082
}
7183

7284
@Async
7385
@EventListener
7486
public void onProxyStopEvent(ProxyStopEvent event) throws IOException {
75-
if (event.isLocalEvent()) {
76-
writeToDb(event, event.getTimestamp(), event.getUserId(), "ProxyStop", event.getSpecId(), event.getAuthentication());
87+
try {
88+
if (event.isLocalEvent()) {
89+
writeToDb(event, event.getTimestamp(), event.getUserId(), "ProxyStop", event.getSpecId(), event.getAuthentication());
90+
}
91+
} catch (Exception e) {
92+
logger.warn("Collecting event failed", e);
7793
}
7894
}
7995

@@ -87,7 +103,7 @@ public void onAuthFailedEvent(AuthFailedEvent event) {
87103
// TODO
88104
}
89105

90-
protected abstract void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws IOException;
106+
protected abstract void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws Exception;
91107

92108
protected Map<String, String> resolveAttributes(Authentication authentication, ApplicationEvent event, List<StatCollectorFactory.UsageStatsAttribute> usageStatsAttributes) {
93109
if (usageStatsAttributes == null) {

src/main/java/eu/openanalytics/containerproxy/stat/impl/CSVCollector.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,21 @@ public void init() throws IOException {
111111
}
112112

113113
@Override
114-
protected synchronized void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) {
114+
protected synchronized void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws Exception {
115+
Map<String, String> row = new HashMap<>();
116+
for (String column : schema.getColumnNames()) {
117+
row.put(column, "");
118+
}
119+
row.put("event_time", Long.toString(timestamp));
120+
row.put("username", Objects.requireNonNullElse(userId, ""));
121+
row.put("type", Objects.requireNonNullElse(type, ""));
122+
row.put("data", Objects.requireNonNullElse(data, ""));
123+
row.putAll(resolveAttributes(authentication, event, usageStatsAttributes));
115124
try {
116-
Map<String, String> row = new HashMap<>();
117-
for (String column : schema.getColumnNames()) {
118-
row.put(column, "");
119-
}
120-
row.put("event_time", Long.toString(timestamp));
121-
row.put("username", Objects.requireNonNullElse(userId, ""));
122-
row.put("type", Objects.requireNonNullElse(type, ""));
123-
row.put("data", Objects.requireNonNullElse(data, ""));
124-
row.putAll(resolveAttributes(authentication, event, usageStatsAttributes));
125-
try {
126-
writer.write(row);
127-
} catch (Exception e) {
128-
logger.warn("Error while writing to CSV file, data: {}", row, e);
129-
writer = csvMapper.writer(schema).writeValues(fileWriter);
130-
}
125+
writer.write(row);
131126
} catch (Exception e) {
132-
logger.warn("Error while collecting statistic", e);
127+
logger.warn("Error while writing to CSV file, data: {}", row, e);
128+
writer = csvMapper.writer(schema).writeValues(fileWriter);
133129
}
134130
}
135131

src/main/java/eu/openanalytics/containerproxy/stat/impl/InfluxDBCollector.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,27 @@ public InfluxDBCollector(String url) {
4747
}
4848

4949
@Override
50-
protected void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws IOException {
50+
protected void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws Exception {
5151
String body = String.format("event,username=%s,type=%s data=\"%s\"",
5252
userId.replace(" ", "\\ "),
5353
type.replace(" ", "\\ "),
5454
Optional.ofNullable(data).orElse(""));
5555

56-
try {
57-
HttpURLConnection conn = (HttpURLConnection) new URI(destination).toURL().openConnection();
58-
conn.setRequestMethod("POST");
59-
conn.setDoOutput(true);
60-
try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) {
61-
dos.write(body.getBytes(StandardCharsets.UTF_8));
62-
dos.flush();
63-
}
64-
int responseCode = conn.getResponseCode();
65-
if (responseCode == 204) {
66-
// All is well.
67-
} else {
68-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
69-
IOUtils.copy(conn.getErrorStream(), bos);
70-
throw new IOException(bos.toString());
71-
}
72-
} catch (URISyntaxException e) {
73-
throw new RuntimeException(e);
56+
HttpURLConnection conn = (HttpURLConnection) new URI(destination).toURL().openConnection();
57+
conn.setRequestMethod("POST");
58+
conn.setDoOutput(true);
59+
try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) {
60+
dos.write(body.getBytes(StandardCharsets.UTF_8));
61+
dos.flush();
62+
}
63+
int responseCode = conn.getResponseCode();
64+
if (responseCode == 204) {
65+
// All is well.
66+
} else {
67+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
68+
IOUtils.copy(conn.getErrorStream(), bos);
69+
throw new IOException(bos.toString());
7470
}
7571
}
72+
7673
}

src/main/java/eu/openanalytics/containerproxy/stat/impl/JDBCCollector.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void init() throws IOException {
147147
}
148148

149149
@Override
150-
protected void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws IOException {
150+
protected void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws Exception {
151151
try (Connection con = ds.getConnection()) {
152152
Map<String, String> attributes = resolveAttributes(authentication, event, usageStatsAttributes);
153153
String sql = buildQuery(attributes.keySet());
@@ -165,8 +165,6 @@ protected void writeToDb(ApplicationEvent event, long timestamp, String userId,
165165

166166
stmt.executeUpdate();
167167
}
168-
} catch (SQLException e) {
169-
throw new IOException("Exception while logging stats", e);
170168
}
171169
}
172170

src/main/java/eu/openanalytics/containerproxy/stat/impl/Micrometer.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,22 @@ public void run() {
160160

161161
@EventListener
162162
public void onUserLogoutEvent(UserLogoutEvent event) {
163-
logger.debug("UserLogoutEvent [user: {}, expired: {}]", event.getUserId(), event.getWasExpired());
164-
userLogouts.increment();
163+
try {
164+
logger.debug("UserLogoutEvent [user: {}, expired: {}]", event.getUserId(), event.getWasExpired());
165+
userLogouts.increment();
166+
} catch (Exception e) {
167+
logger.warn("Collecting event failed", e);
168+
}
165169
}
166170

167171
@EventListener
168172
public void onUserLoginEvent(UserLoginEvent event) {
169-
logger.debug("UserLoginEvent [user: {}]", event.getUserId());
170-
userLogins.increment();
173+
try {
174+
logger.debug("UserLoginEvent [user: {}]", event.getUserId());
175+
userLogins.increment();
176+
} catch (Exception e) {
177+
logger.warn("Collecting event failed", e);
178+
}
171179
}
172180

173181
@EventListener
@@ -190,7 +198,7 @@ public void onNewProxyEvent(NewProxyEvent event) {
190198
}
191199
logger.debug("NewProxyEvent [user: {}]", event.getUserId());
192200
} catch (Exception e) {
193-
logger.error("Error in onNewProxyEvent", e);
201+
logger.warn("Collecting event failed", e);
194202
}
195203
}
196204

@@ -247,7 +255,7 @@ public void onProxyStartEvent(ProxyStartEvent event) {
247255
registry.timer("applicationStartupTime", "spec.id", event.getSpecId()).record(d);
248256
});
249257
} catch (Exception e) {
250-
logger.error("Error in onProxyStartEvent", e);
258+
logger.warn("Collecting event failed", e);
251259
}
252260
}
253261

@@ -296,7 +304,7 @@ public void onProxyStopEvent(ProxyStopEvent event) {
296304
registry.counter("appCrashes", "spec.id", event.getSpecId()).increment();
297305
}
298306
} catch (Exception e) {
299-
logger.error("Error in onProxyStopEvent", e);
307+
logger.warn("Collecting event failed", e);
300308
}
301309
}
302310

@@ -336,14 +344,18 @@ public void onProxyStartFailedEvent(ProxyStartFailedEvent event) {
336344
logger.debug("ProxyStartFailedEvent [user: {}, specId: {}]", event.getUserId(), event.getSpecId());
337345
registry.counter("startFailed", "spec.id", event.getSpecId()).increment();
338346
} catch (Exception e) {
339-
logger.error("Error in onProxyStartFailedEvent", e);
347+
logger.warn("Collecting event failed", e);
340348
}
341349
}
342350

343351
@EventListener
344352
public void onAuthFailedEvent(AuthFailedEvent event) {
345-
logger.debug("AuthFailedEvent [user: {}]", event.getUserId());
346-
authFailedCounter.increment();
353+
try {
354+
logger.debug("AuthFailedEvent [user: {}]", event.getUserId());
355+
authFailedCounter.increment();
356+
} catch (Exception e) {
357+
logger.warn("Collecting event failed", e);
358+
}
347359
}
348360

349361
/**

0 commit comments

Comments
 (0)