Skip to content

Commit e25ef9b

Browse files
committed
Ref #34306: improve error handling of CSV collector
1 parent 2ed0fc2 commit e25ef9b

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class CSVCollector extends AbstractDbCollector implements AutoCloseable {
5050
private FileWriter fileWriter;
5151
private SequenceWriter writer;
5252
private CsvSchema schema;
53+
private CsvMapper csvMapper;
5354

5455
public CSVCollector(String url, List<StatCollectorFactory.UsageStatsAttribute> usageStatsAttributes) {
5556
this.url = Path.of(url);
@@ -58,7 +59,7 @@ public CSVCollector(String url, List<StatCollectorFactory.UsageStatsAttribute> u
5859

5960
@PostConstruct
6061
public void init() throws IOException {
61-
CsvMapper csvMapper = new CsvMapper();
62+
csvMapper = new CsvMapper();
6263
csvMapper.enable(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS);
6364
csvMapper.enable(CsvGenerator.Feature.ALWAYS_QUOTE_EMPTY_STRINGS);
6465
CsvSchema.Builder schemaBuilder = CsvSchema.builder();
@@ -110,16 +111,25 @@ public void init() throws IOException {
110111

111112
@Override
112113
protected void writeToDb(ApplicationEvent event, long timestamp, String userId, String type, String data, Authentication authentication) throws IOException {
113-
Map<String, String> row = new HashMap<>();
114-
for (String column : schema.getColumnNames()) {
115-
row.put(column, "");
114+
try {
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));
124+
try {
125+
writer.write(row);
126+
} catch (Exception e) {
127+
logger.warn("Error while writing to CSV file, data: {}", row, e);
128+
writer = csvMapper.writer(schema).writeValues(fileWriter);
129+
}
130+
} catch (Exception e) {
131+
logger.warn("Error while collecting statistic", e);
116132
}
117-
row.put("event_time", Long.toString(timestamp));
118-
row.put("username", Objects.requireNonNullElse(userId, ""));
119-
row.put("type", Objects.requireNonNullElse(type, ""));
120-
row.put("data", Objects.requireNonNullElse(data, ""));
121-
row.putAll(resolveAttributes(authentication, event, usageStatsAttributes));
122-
writer.write(row);
123133
}
124134

125135
@Override

0 commit comments

Comments
 (0)