maybe fix metrics randomly not writing to influx
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m51s

This commit is contained in:
Lee 2024-04-18 17:38:14 +01:00
parent 547fa075f3
commit 7a5b42e9d7
3 changed files with 25 additions and 13 deletions

@ -125,24 +125,32 @@ public class MetricService {
* @param metric the metric to save * @param metric the metric to save
*/ */
private void saveMetric(Metric<?> metric) { private void saveMetric(Metric<?> metric) {
metricsRepository.save(metric); // Save the metric to the repository try {
metricsRepository.save(metric); // Save the metric to the repository
} catch (Exception e) {
log.error("Failed to save metric to MongoDB", e);
}
} }
/** /**
* Push all metrics to InfluxDB. * Push all metrics to InfluxDB.
*/ */
private void writeToInflux() { private void writeToInflux() {
List<Point> points = new ArrayList<>(); try {
for (Metric<?> metric : metrics.values()) { List<Point> points = new ArrayList<>();
if (metric.isCollector()) { for (Metric<?> metric : metrics.values()) {
metric.collect(); if (metric.isCollector()) {
} metric.collect();
Point point = metric.toPoint(); }
if (point != null) { Point point = metric.toPoint();
points.add(point); if (point != null) {
points.add(point);
}
} }
influxWriteApi.writePoints(points);
log.info("Wrote {} metrics to Influx", metrics.size());
} catch (Exception e) {
log.error("Failed to write metrics to Influx", e);
} }
influxWriteApi.writePoints(points);
log.info("Wrote {} metrics to Influx", metrics.size());
} }
} }

@ -83,7 +83,10 @@ public class PlayerService {
mojangProfile.getProperties() // Raw properties mojangProfile.getProperties() // Raw properties
) )
); );
((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class)).addLookup(uuid); // Add the lookup to the unique player lookups
// Add the lookup to the unique player lookups metric
((UniquePlayerLookupsMetric) metricService.getMetric(UniquePlayerLookupsMetric.class))
.addLookup(uuid);
playerCacheRepository.save(player); playerCacheRepository.save(player);
player.getCache().setCached(false); player.getCache().setCached(false);

@ -100,8 +100,9 @@ public class ServerService {
((JavaMinecraftServer) server.getServer()).setMojangBlocked(mojangService.isServerBlocked(hostname)); ((JavaMinecraftServer) server.getServer()).setMojangBlocked(mojangService.isServerBlocked(hostname));
} }
// Add the server lookup to the unique server lookups metric
((UniqueServerLookupsMetric) metricService.getMetric(UniqueServerLookupsMetric.class)) ((UniqueServerLookupsMetric) metricService.getMetric(UniqueServerLookupsMetric.class))
.addLookup("%s-%s:%s".formatted(platformName, hostname, port)); // Add the server lookup to the unique server lookups .addLookup("%s-%s:%s".formatted(platformName, hostname, port));
log.info("Found server: {}:{}", hostname, port); log.info("Found server: {}:{}", hostname, port);
serverCacheRepository.save(server); serverCacheRepository.save(server);