Commit 6faa527d authored by Ananas Orange's avatar Ananas Orange Committed by Jujube Orange
Browse files

Moved to new repo

parent dd85c9b0
Pipeline #266466 passed with stages
in 21 minutes and 50 seconds
......@@ -3,6 +3,7 @@ package fr.gouv.clea.consumer.service.impl;
import fr.gouv.clea.consumer.configuration.VenueConsumerProperties;
import fr.gouv.clea.consumer.repository.IExposedVisitRepository;
import fr.gouv.clea.consumer.service.IExposedVisitEntityService;
import fr.gouv.clea.consumer.utils.MetricsService;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -22,6 +23,8 @@ public class ExposedVisitEntityService implements IExposedVisitEntityService {
private final VenueConsumerProperties properties;
private final MetricsService metricsService;
@Override
@Transactional
@Scheduled(cron = "${clea.conf.scheduling.purge.cron}")
......@@ -34,8 +37,10 @@ public class ExposedVisitEntityService implements IExposedVisitEntityService {
);
long end = System.currentTimeMillis();
log.info("successfully purged {} entries from DB in {} seconds", count, (end - start) / 1000);
metricsService.getPurgedCounter().increment(count);
} catch (Exception e) {
log.error("error during purge");
metricsService.getFailedPurgeCounter().increment();
throw e;
}
}
......
......@@ -8,6 +8,7 @@ import fr.gouv.clea.consumer.model.Visit;
import fr.gouv.clea.consumer.repository.IReportStatRepository;
import fr.gouv.clea.consumer.repository.IStatLocationJpaRepository;
import fr.gouv.clea.consumer.service.IStatService;
import fr.gouv.clea.consumer.utils.MetricsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
......@@ -31,6 +32,8 @@ public class StatService implements IStatService {
private final VenueConsumerProperties properties;
private final MetricsService metricsService;
@Override
public void logStats(Visit visit) {
StatLocationKey statLocationKey = buildKey(visit);
......
package fr.gouv.clea.consumer.utils;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Data
public class MetricsService {
private static final String PURGE_LABEL = "consumer.purge";
private static final String STAT_LABEL = "consumer.stat";
//
private MeterRegistry meterRegistry;
private Counter purgedCounter;
private Counter failedPurgeCounter;
private Counter statCounter;
private Counter failedStatCounter;
@Autowired
public MetricsService(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
this.purgedCounter = Counter.builder(PURGE_LABEL)
.tag("type", "purged")
.description("The number of purged entries")
.register(meterRegistry);
this.failedPurgeCounter = Counter.builder(PURGE_LABEL)
.tag("type", "purgeFailed")
.description("The number of failed purge operations")
.register(meterRegistry);
this.statCounter = Counter.builder(STAT_LABEL)
.tag("type", "statSaved")
.description("The number of saved stats")
.register(meterRegistry);
this.failedStatCounter = Counter.builder(STAT_LABEL)
.tag("type", "statFailed")
.description("The number of failed stat saving")
.register(meterRegistry);
}
}
......@@ -31,7 +31,7 @@ management:
endpoints:
web:
exposure:
include: health,metrics
include: health,metrics,prometheus
server:
port: 8080
......
......@@ -6,6 +6,7 @@ import fr.gouv.clea.ws.exception.CleaBadRequestException;
import fr.gouv.clea.ws.model.DecodedVisit;
import fr.gouv.clea.ws.service.IReportService;
import fr.gouv.clea.ws.utils.BadArgumentsLoggerService;
import fr.gouv.clea.ws.utils.MetricsService;
import fr.gouv.clea.ws.utils.UriConstants;
import fr.gouv.clea.ws.vo.ReportRequest;
import fr.gouv.clea.ws.vo.Visit;
......@@ -42,6 +43,8 @@ public class CleaController implements CleaWsRestAPI {
private final Validator validator;
private final MetricsService metricsService;
@Override
@PostMapping(path = UriConstants.API_V1
+ UriConstants.REPORT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -51,10 +54,11 @@ public class CleaController implements CleaWsRestAPI {
if (!filtered.getVisits().isEmpty()) {
reported = reportService.report(filtered);
}
String message = String.format(
"%s reports processed, %s rejected", reported.size(),
reportRequestVo.getVisits().size() - reported.size()
);
var processedReports = reported.size();
var rejectedReports = reportRequestVo.getVisits().size() - reported.size();
var message = String.format("%s reports processed, %s rejected", processedReports, rejectedReports);
metricsService.getProcessedCounter().increment(processedReports);
metricsService.getRejectedCounter().increment(rejectedReports);
log.info(message);
return new ReportResponse(true, message);
}
......
......@@ -6,6 +6,7 @@ import fr.gouv.clea.ws.model.ReportStat;
import fr.gouv.clea.ws.service.IProducerService;
import fr.gouv.clea.ws.service.IReportService;
import fr.gouv.clea.ws.utils.MessageFormatter;
import fr.gouv.clea.ws.utils.MetricsService;
import fr.gouv.clea.ws.vo.ReportRequest;
import fr.gouv.clea.ws.vo.Visit;
import fr.inria.clea.lsp.EncryptedLocationSpecificPart;
......@@ -37,6 +38,8 @@ public class ReportService implements IReportService {
private final IProducerService producerService;
private final MetricsService metricsService;
@Override
public List<DecodedVisit> report(ReportRequest reportRequestVo) {
final Instant now = Instant.now();
......@@ -97,6 +100,7 @@ public class ReportService implements IReportService {
) > properties.getRetentionDurationInDays();
if (outdated) {
log.warn("report: {} rejected: Outdated", MessageFormatter.truncateQrCode(visit.getQrCode()));
metricsService.getOutdatedCounter().increment();
}
return outdated;
}
......@@ -105,6 +109,7 @@ public class ReportService implements IReportService {
boolean future = TimeUtils.instantFromTimestamp(visit.getQrCodeScanTimeAsNtpTimestamp()).isAfter(now);
if (future) {
log.warn("report: {} rejected: In future", MessageFormatter.truncateQrCode(visit.getQrCode()));
metricsService.getFutureCounter().increment();
}
return future;
}
......@@ -125,6 +130,7 @@ public class ReportService implements IReportService {
"report: {} {} rejected: Duplicate",
MessageFormatter.truncateUUID(one.getStringLocationTemporaryPublicId()), one.getQrCodeScanTime()
);
metricsService.getDuplicateCounter().increment();
return true;
}
return false;
......@@ -142,14 +148,15 @@ public class ReportService implements IReportService {
private long validatePivotDate(long pivotDate, Instant now) {
Instant pivotDateAsInstant = TimeUtils.instantFromTimestamp(pivotDate);
Instant nowWithoutMilis = now.truncatedTo(ChronoUnit.SECONDS);
Instant retentionDateLimit = nowWithoutMilis.minus(properties.getRetentionDurationInDays(), ChronoUnit.DAYS);
Instant nowWithoutMillis = now.truncatedTo(ChronoUnit.SECONDS);
Instant retentionDateLimit = nowWithoutMillis.minus(properties.getRetentionDurationInDays(), ChronoUnit.DAYS);
if (pivotDateAsInstant.isAfter(now) || pivotDateAsInstant.isBefore(retentionDateLimit)) {
long retentionDateLimitAsNtp = TimeUtils.ntpTimestampFromInstant(retentionDateLimit);
log.warn(
"pivotDate: {} not between retentionLimitDate: {} and now: {}", pivotDateAsInstant,
retentionDateLimit, now
);
metricsService.getNotCurrentCounter().increment();
return retentionDateLimitAsNtp;
} else {
return pivotDate;
......
package fr.gouv.clea.ws.utils;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Getter
public class MetricsService {
private static final String VISIT_LABEL = "report.visit";
private static final String TOKEN_LABEL = "report.token";
private static final String PIVOT_LABEL = "report.pivotDate";
//
private final MeterRegistry meterRegistry;
private final Counter processedCounter;
private final Counter rejectedCounter;
private final Counter duplicateCounter;
private final Counter outdatedCounter;
private final Counter notCurrentCounter;
private final Counter futureCounter;
private final Counter missingTokenCounter;
private final Counter invalidTokenCounter;
public MetricsService(
@Autowired MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
this.processedCounter = Counter.builder(VISIT_LABEL)
.tag("type", "processed")
.description("The number of processed reports")
.register(meterRegistry);
this.rejectedCounter = Counter.builder(VISIT_LABEL)
.tag("type", "rejected")
.description("The number of rejected reports")
.register(meterRegistry);
this.duplicateCounter = Counter.builder(VISIT_LABEL)
.tag("type", "duplicate")
.description("The number of duplicated reports")
.register(meterRegistry);
this.outdatedCounter = Counter.builder(VISIT_LABEL)
.tag("type", "outdated")
.description("The number of outdated reports")
.register(meterRegistry);
this.futureCounter = Counter.builder(VISIT_LABEL)
.tag("type", "future")
.description("The number of reports in future")
.register(meterRegistry);
this.notCurrentCounter = Counter.builder(PIVOT_LABEL)
.tag("type", "notCurrent")
.description("The number of non current pivotDates")
.register(meterRegistry);
this.missingTokenCounter = Counter.builder(TOKEN_LABEL)
.tag("type", "missing")
.description("The number of requests with a missing JWT token")
.register(meterRegistry);
this.invalidTokenCounter = Counter.builder(TOKEN_LABEL)
.tag("type", "invalid")
.description("The number of requests with an invalid JWT token")
.register(meterRegistry);
}
}
......@@ -17,7 +17,7 @@ management:
endpoints:
web:
exposure:
include: health,metrics
include: health,metrics,prometheus
server:
port: 8080
......
......@@ -21,7 +21,7 @@ management:
endpoints:
web:
exposure:
include: health,metrics
include: health,metrics,prometheus
server:
port: 8080
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment