From 76e2b6025015b889a55cbfd9b80fbddce6a04455 Mon Sep 17 00:00:00 2001 From: Colin McDonald Date: Mon, 16 May 2016 16:48:35 -0400 Subject: [PATCH] Add more metrics --- src/main/java/net/frozenorb/apiv3/auditLog/AuditLog.java | 1 + .../net/frozenorb/apiv3/filters/AuthorizationFilter.java | 2 +- .../java/net/frozenorb/apiv3/filters/LoggingFilter.java | 8 +++----- src/main/java/net/frozenorb/apiv3/routes/GETDump.java | 1 + .../java/net/frozenorb/apiv3/unsorted/Notification.java | 5 +++++ src/main/java/net/frozenorb/apiv3/utils/MojangUtils.java | 3 +++ src/main/java/net/frozenorb/apiv3/utils/TOTPUtils.java | 8 +++++++- src/main/java/net/frozenorb/apiv3/utils/UUIDUtils.java | 9 ++++++++- 8 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/frozenorb/apiv3/auditLog/AuditLog.java b/src/main/java/net/frozenorb/apiv3/auditLog/AuditLog.java index e668c9f..a8eef3c 100644 --- a/src/main/java/net/frozenorb/apiv3/auditLog/AuditLog.java +++ b/src/main/java/net/frozenorb/apiv3/auditLog/AuditLog.java @@ -17,6 +17,7 @@ public class AuditLog { } public static void log(User performedBy, String performedByIp, Actor actor, AuditLogActionType actionType, Map actionData) { + APIv3.getStatsD().incrementCounter("apiv3.auditLog.insertions"); APIv3.getDatastore().save(new AuditLogEntry(performedBy, performedByIp, actor, actionType, actionData)); } diff --git a/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java b/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java index 611356f..f9070cd 100644 --- a/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java +++ b/src/main/java/net/frozenorb/apiv3/filters/AuthorizationFilter.java @@ -16,7 +16,7 @@ public final class AuthorizationFilter implements Filter { if (!actor.isAuthorized()) { APIv3.getStatsD().incrementCounter("apiv3.http.unauthorized"); res.header("WWW-Authenticate", "Basic realm=\"MineHQ\""); - Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Unauthorized access: Please authenticate as either a server, the website, or an authorized user. You're currently authorized as " + actor.getName()))); + Spark.halt(401, APIv3.getGson().toJson(ErrorUtils.error("Unauthorized access: Please authorize as an approved actor. You're currently authorized as " + actor.getName()))); } } diff --git a/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java b/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java index e09a2e5..0eb5dc6 100644 --- a/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java +++ b/src/main/java/net/frozenorb/apiv3/filters/LoggingFilter.java @@ -3,6 +3,7 @@ package net.frozenorb.apiv3.filters; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import net.frozenorb.apiv3.actors.Actor; import spark.Filter; import spark.Request; import spark.Response; @@ -13,11 +14,8 @@ public final class LoggingFilter implements Filter { @Getter @Setter private static boolean debug = false; public void handle(Request req, Response res) { - if (debug) { - log.info(req.requestMethod().toUpperCase() + " " + req.url() + "\n" + res.body()); - } else { - log.info(req.requestMethod().toUpperCase() + " " + req.url()); - } + Actor actor = req.attribute("actor"); + log.info("(" + actor.getName() + " - " + actor.getType() + ") " + req.requestMethod().toUpperCase() + " " + req.pathInfo() + (debug ? "\n" + res.body() : "")); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/GETDump.java b/src/main/java/net/frozenorb/apiv3/routes/GETDump.java index 170bbb2..6bacbfb 100644 --- a/src/main/java/net/frozenorb/apiv3/routes/GETDump.java +++ b/src/main/java/net/frozenorb/apiv3/routes/GETDump.java @@ -1,6 +1,7 @@ package net.frozenorb.apiv3.routes; import com.google.common.collect.ImmutableSet; +import com.timgroup.statsd.Event; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.Grant; import net.frozenorb.apiv3.models.Punishment; diff --git a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java index 5a74903..1813cba 100644 --- a/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java +++ b/src/main/java/net/frozenorb/apiv3/unsorted/Notification.java @@ -7,6 +7,7 @@ import com.cribbstechnologies.clients.mandrill.model.MandrillRecipient; import com.cribbstechnologies.clients.mandrill.request.MandrillMessagesRequest; import com.twilio.sdk.TwilioRestException; import com.twilio.sdk.resource.factory.MessageFactory; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.models.NotificationTemplate; import net.frozenorb.apiv3.utils.MandrillUtils; import net.frozenorb.apiv3.utils.TwillioUtils; @@ -46,7 +47,9 @@ public final class Notification { MandrillMessageRequest request = new MandrillMessageRequest(); request.setMessage(message); mandrillMessagesRequest.sendMessage(request); + APIv3.getStatsD().incrementCounter("apiv3.notification.email.success"); } catch (RequestFailedException ex) { + APIv3.getStatsD().incrementCounter("apiv3.notification.email.failure"); throw new IOException("Failed to send notification to user", ex); } } @@ -60,7 +63,9 @@ public final class Notification { try { twillioMessageFactory.create(params); + APIv3.getStatsD().incrementCounter("apiv3.notification.text.success"); } catch (TwilioRestException ex) { + APIv3.getStatsD().incrementCounter("apiv3.notification.text.failure"); throw new IOException("Failed to send notification to user", ex); } } diff --git a/src/main/java/net/frozenorb/apiv3/utils/MojangUtils.java b/src/main/java/net/frozenorb/apiv3/utils/MojangUtils.java index 1fb5600..13bf39b 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/MojangUtils.java +++ b/src/main/java/net/frozenorb/apiv3/utils/MojangUtils.java @@ -1,6 +1,7 @@ package net.frozenorb.apiv3.utils; import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -29,8 +30,10 @@ public class MojangUtils { throw new RuntimeException("Hit Mojang API rate limit"); } + APIv3.getStatsD().incrementCounter("apiv3.mojang.sessionServer.success"); return name; } catch (Exception ex) { + APIv3.getStatsD().incrementCounter("apiv3.mojang.sessionServer.failure"); throw new RuntimeException(ex); } } diff --git a/src/main/java/net/frozenorb/apiv3/utils/TOTPUtils.java b/src/main/java/net/frozenorb/apiv3/utils/TOTPUtils.java index f094c50..5af89ef 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/TOTPUtils.java +++ b/src/main/java/net/frozenorb/apiv3/utils/TOTPUtils.java @@ -21,7 +21,13 @@ public class TOTPUtils { } public static boolean authorizeUser(User user, int code) { - return googleAuthenticator.authorize(user.getTotpSecret(), code); + boolean authorized = googleAuthenticator.authorize(user.getTotpSecret(), code); + + if (!authorized) { + APIv3.getStatsD().incrementCounter("apiv3.totp.failure"); + } + + return authorized; } public static String getQRCodeURL(User user, GoogleAuthenticatorKey key) { diff --git a/src/main/java/net/frozenorb/apiv3/utils/UUIDUtils.java b/src/main/java/net/frozenorb/apiv3/utils/UUIDUtils.java index 341dc57..9a50a64 100644 --- a/src/main/java/net/frozenorb/apiv3/utils/UUIDUtils.java +++ b/src/main/java/net/frozenorb/apiv3/utils/UUIDUtils.java @@ -1,6 +1,7 @@ package net.frozenorb.apiv3.utils; import lombok.experimental.UtilityClass; +import net.frozenorb.apiv3.APIv3; import java.util.UUID; @@ -8,7 +9,13 @@ import java.util.UUID; public class UUIDUtils { public static boolean isAcceptableUUID(UUID uuid) { - return uuid.version() == 4; + boolean acceptable = uuid.version() == 4; + + if (!acceptable) { + APIv3.getStatsD().incrementCounter("apiv3.uuid.invalid"); + } + + return acceptable; } } \ No newline at end of file