8
8
import de .muenchen .captchaservice .repository .InvalidatedPayloadRepository ;
9
9
import de .muenchen .captchaservice .service .difficulty .DifficultyService ;
10
10
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
11
- import io .micrometer .core .instrument .MeterRegistry ;
12
- import io .micrometer .core .instrument .Counter ;
13
- import io .micrometer .core .instrument .DistributionSummary ;
14
- import io .micrometer .core .instrument .Gauge ;
15
- import java .util .concurrent .atomic .AtomicLong ;
16
11
import lombok .extern .slf4j .Slf4j ;
17
12
import org .altcha .altcha .Altcha ;
18
13
import org .apache .commons .codec .digest .DigestUtils ;
@@ -27,39 +22,24 @@ public class CaptchaService {
27
22
private final CaptchaProperties captchaProperties ;
28
23
private final InvalidatedPayloadRepository invalidatedPayloadRepository ;
29
24
private final DifficultyService difficultyService ;
30
- private final Counter challengeCounter ;
31
- private final Counter verifySuccessCounter ;
32
- private final DistributionSummary tookTimeSummary ;
33
- private final AtomicLong invalidatedPayloadCount = new AtomicLong (0 );
25
+ private final MetricsService metricsService ;
34
26
35
27
@ SuppressFBWarnings (value = { "EI_EXPOSE_REP2" }, justification = "Dependency Injection" )
36
28
public CaptchaService (final CaptchaProperties captchaProperties , final DifficultyService difficultyService ,
37
- final InvalidatedPayloadRepository invalidatedPayloadRepository , MeterRegistry registry ) {
29
+ final InvalidatedPayloadRepository invalidatedPayloadRepository , MetricsService metricsService ) {
38
30
this .captchaProperties = captchaProperties ;
39
31
this .invalidatedPayloadRepository = invalidatedPayloadRepository ;
40
32
this .difficultyService = difficultyService ;
33
+ this .metricsService = metricsService ;
41
34
42
- // Initialize counter with current count from database
43
- this .invalidatedPayloadCount .set (invalidatedPayloadRepository .countByExpiresAtGreaterThan (Instant .now ()));
44
-
45
- // Initialize metrics
46
- this .challengeCounter = Counter .builder ("captcha.challenge.requests" )
47
- .description ("Counter for captcha challenge requests" )
48
- .register (registry );
49
- this .verifySuccessCounter = Counter .builder ("captcha.verify.success" )
50
- .description ("Counter for captcha verify success requests" )
51
- .register (registry );
52
- this .tookTimeSummary = DistributionSummary .builder ("captcha.verify.took.time" )
53
- .description ("Summary of the time taken to verify captcha payloads" )
54
- .register (registry );
55
- Gauge .builder ("captcha.invalidated.payloads" , invalidatedPayloadCount , AtomicLong ::get )
56
- .description ("Gauge for the number of currently invalidated payloads" )
57
- .register (registry );
35
+ metricsService .initializeInvalidatedPayloadsGauge ();
58
36
}
59
37
60
38
public Altcha .Challenge createChallenge (final String siteKey , final SourceAddress sourceAddress ) {
61
- challengeCounter .increment ();
62
39
final long difficulty = difficultyService .getDifficultyForSourceAddress (siteKey , sourceAddress );
40
+
41
+ metricsService .recordChallengeRequest (siteKey , sourceAddress );
42
+
63
43
difficultyService .registerRequest (siteKey , sourceAddress );
64
44
final Altcha .ChallengeOptions options = new Altcha .ChallengeOptions ();
65
45
options .algorithm = Altcha .Algorithm .SHA256 ;
@@ -74,17 +54,19 @@ public Altcha.Challenge createChallenge(final String siteKey, final SourceAddres
74
54
return null ;
75
55
}
76
56
77
- public boolean verify (final String siteKey , final ExtendedPayload payload ) {
57
+ public boolean verify (final String siteKey , final ExtendedPayload payload , final SourceAddress sourceAddress ) {
78
58
if (isPayloadInvalidated (siteKey , payload )) {
79
59
return false ;
80
60
}
81
61
try {
82
62
final boolean isValid = Altcha .verifySolution (payload , captchaProperties .hmacKey (), true );
83
63
if (isValid ) {
64
+ metricsService .recordVerifySuccess (siteKey , sourceAddress );
65
+
84
66
if (payload .getTook () != null ) {
85
- tookTimeSummary . record ( payload .getTook ());
67
+ metricsService . recordClientSolveTime ( siteKey , sourceAddress , payload .getTook ());
86
68
}
87
- verifySuccessCounter . increment ();
69
+
88
70
invalidatePayload (payload );
89
71
}
90
72
return isValid ;
@@ -97,7 +79,6 @@ public boolean verify(final String siteKey, final ExtendedPayload payload) {
97
79
public void invalidatePayload (final Altcha .Payload payload ) {
98
80
final String payloadHash = getPayloadHash (payload );
99
81
final InvalidatedPayload invalidatedPayload = new InvalidatedPayload (payloadHash , Instant .now ().plusSeconds (captchaProperties .captchaTimeoutSeconds ()));
100
- invalidatedPayloadCount .incrementAndGet ();
101
82
invalidatedPayloadRepository .save (invalidatedPayload );
102
83
log .debug ("Invalidated payloadHash: {}" , payloadHash );
103
84
}
@@ -118,13 +99,4 @@ private static String getPayloadHash(final Altcha.Payload payload) {
118
99
payload .salt ,
119
100
payload .signature ));
120
101
}
121
-
122
- public void decrementInvalidatedPayloadCount (long count ) {
123
- invalidatedPayloadCount .addAndGet (-count );
124
- }
125
-
126
- public void resetInvalidatedPayloadCount () {
127
- this .invalidatedPayloadCount .set (
128
- invalidatedPayloadRepository .countByExpiresAtGreaterThan (java .time .Instant .now ()));
129
- }
130
102
}
0 commit comments