Elffuss BraidHub

kikocis/java-driver public

JAVA-1053: Add a metric for authentication errors (#752)

Changeset9945f5df57d4cb4dca34759d6d9af4b484de063a4f97ee62f3d754a55b1bb3df
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-09-20 14:36 -07:00
CommitterGitHub <noreply@github.com> · 2016-09-20 14:36 -07:00
Parents205c5dd93f7f

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -14,6 +14,7 @@
1414 - [bug] JAVA-1272: BuiltStatement not able to print its query string if it contains mapped UDTs.
1515 - [bug] JAVA-1292: 'Adjusted frame length' error breaks driver's ability to read data.
1616 - [improvement] JAVA-1293: Make DecoderForStreamIdSize.MAX_FRAME_LENGTH configurable.
17+- [improvement] JAVA-1053: Add a metric for authentication errors
1718
1819
1920 ### 3.0.3
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +15 −1
@@ -249,7 +249,13 @@
249249 throw new TransportException(address, String.format("Error initializing connection: %s", error.message));
250250 case AUTHENTICATE:
251251 Responses.Authenticate authenticate = (Responses.Authenticate) response;
252- Authenticator authenticator = factory.authProvider.newAuthenticator(address, authenticate.authenticator);
252+ Authenticator authenticator;
253+ try {
254+ authenticator = factory.authProvider.newAuthenticator(address, authenticate.authenticator);
255+ } catch (AuthenticationException e) {
256+ incrementAuthErrorMetric();
257+ throw e;
258+ }
253259 switch (protocolVersion) {
254260 case V1:
255261 if (authenticator instanceof ProtocolV1Authenticator)
@@ -320,6 +326,7 @@
320326 case READY:
321327 return checkClusterName(protocolVersion, executor);
322328 case ERROR:
329+ incrementAuthErrorMetric();
323330 throw new AuthenticationException(address, ((Responses.Error) authResponse).message);
324331 default:
325332 throw new TransportException(address, String.format("Unexpected %s response message from server to a CREDENTIALS message", authResponse.type));
@@ -375,6 +382,7 @@
375382 if (message.startsWith("java.lang.ArrayIndexOutOfBoundsException: 15"))
376383 message = String.format("Cannot use authenticator %s with protocol version 1, "
377384 + "only plain text authentication is supported with this protocol version", authenticator);
385+ incrementAuthErrorMetric();
378386 throw new AuthenticationException(address, message);
379387 default:
380388 throw new TransportException(address, String.format("Unexpected %s response message from server to authentication message", authResponse.type));
@@ -383,6 +391,12 @@
383391 };
384392 }
385393
394+ private void incrementAuthErrorMetric() {
395+ if (factory.manager.configuration.getMetricsOptions().isEnabled()) {
396+ factory.manager.metrics.getErrorMetrics().getAuthenticationErrors().inc();
397+ }
398+ }
399+
386400 private UnsupportedProtocolVersionException unsupportedProtocolVersionException(ProtocolVersion triedVersion, ProtocolVersion serverProtocolVersion) {
387401 logger.debug("Got unsupported protocol version error from {} for version {} server supports version {}", address, triedVersion, serverProtocolVersion);
388402 return new UnsupportedProtocolVersionException(address, triedVersion, serverProtocolVersion);
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +10 −0
@@ -268,6 +268,7 @@
268268 public class Errors {
269269
270270 private final Counter connectionErrors = registry.counter("connection-errors");
271+ private final Counter authenticationErrors = registry.counter("authentication-errors");
271272
272273 private final Counter writeTimeouts = registry.counter("write-timeouts");
273274 private final Counter readTimeouts = registry.counter("read-timeouts");
@@ -311,6 +312,15 @@
311312 return connectionErrors;
312313 }
313314
315+ /**
316+ * Returns the number of authentication errors while connecting to Cassandra nodes.
317+ *
318+ * @return the number of errors.
319+ */
320+ public Counter getAuthenticationErrors() {
321+ return authenticationErrors;
322+ }
323+
314324 /**
315325 * Returns the number of write requests that returned a timeout (independently
316326 * of the final decision taken by the {@link com.datastax.driver.core.policies.RetryPolicy}).
modified driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java +12 −2
@@ -24,6 +24,7 @@
2424
2525 import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
2626 import static com.datastax.driver.core.TestUtils.findHost;
27+import static org.assertj.core.api.Assertions.assertThat;
2728 import static org.mockito.Mockito.*;
2829
2930 /**
@@ -55,6 +56,7 @@
5556 .build();
5657 cluster.connect();
5758 verify(authProvider, atLeastOnce()).newAuthenticator(findHost(cluster, 1).getSocketAddress(), "org.apache.cassandra.auth.PasswordAuthenticator");
59+ assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(0);
5860 }
5961
6062 @Test(groups = "short", expectedExceptions = AuthenticationException.class)
@@ -64,7 +66,11 @@
6466 .withPort(ccm().getBinaryPort())
6567 .withCredentials("bogus", "bogus")
6668 .build());
67- cluster.connect();
69+ try {
70+ cluster.connect();
71+ } finally {
72+ assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(1);
73+ }
6874 }
6975
7076 @Test(groups = "short", expectedExceptions = AuthenticationException.class)
@@ -73,7 +79,11 @@
7379 .addContactPoints(getContactPoints())
7480 .withPort(ccm().getBinaryPort())
7581 .build());
76- cluster.connect();
82+ try {
83+ cluster.connect();
84+ } finally {
85+ assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(1);
86+ }
7787 }
7888
7989 }
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +2 −0
@@ -115,6 +115,8 @@
115115 TimeUnit.SECONDS.sleep(5);
116116 } while (iterations < maxIterations && authProvider.count.get() <= initialCount);
117117 assertThat(iterations).isLessThan(maxIterations);
118+ assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount())
119+ .isEqualTo(authProvider.count.get() - initialCount);
118120
119121 // Fix the credentials
120122 authProvider.setPassword("cassandra");