You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by eo...@apache.org on 2017/06/01 11:16:53 UTC

bookkeeper git commit: BOOKKEEPER-1085: Introduce the AlertStatsLogger

Repository: bookkeeper
Updated Branches:
  refs/heads/master e33ec10aa -> d1f37dafb


BOOKKEEPER-1085: Introduce the AlertStatsLogger

Introduce the AlertStatsLogger used to increment a metric whenever an event that should never happen is detected. Allow specifying an optional scope to better classify the error conditions

RB_ID=598662

Author: Sijie Guo <si...@apache.org>
Author: Robin Dhamankar <rd...@twitter.com>

Reviewers: Enrico Olivelli, Jia Zhai

Closes #173 from sijie/add_alert_state_logger


Project: http://git-wip-us.apache.org/repos/asf/bookkeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/bookkeeper/commit/d1f37daf
Tree: http://git-wip-us.apache.org/repos/asf/bookkeeper/tree/d1f37daf
Diff: http://git-wip-us.apache.org/repos/asf/bookkeeper/diff/d1f37daf

Branch: refs/heads/master
Commit: d1f37dafbb475d4d6aab4769335550428c680269
Parents: e33ec10
Author: Sijie Guo <si...@apache.org>
Authored: Thu Jun 1 13:16:48 2017 +0200
Committer: Enrico Olivelli <eo...@apache.org>
Committed: Thu Jun 1 13:16:48 2017 +0200

----------------------------------------------------------------------
 .../bookkeeper/stats/AlertStatsLogger.java      | 79 ++++++++++++++++++++
 1 file changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/d1f37daf/bookkeeper-server/src/main/java/org/apache/bookkeeper/stats/AlertStatsLogger.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/stats/AlertStatsLogger.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/stats/AlertStatsLogger.java
new file mode 100644
index 0000000..63ba3b5
--- /dev/null
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/stats/AlertStatsLogger.java
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.bookkeeper.stats;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class is used to raise alert when we detect an event that should never happen in production
+ */
+public class AlertStatsLogger {
+    private static final Logger logger = LoggerFactory.getLogger(AlertStatsLogger.class);
+
+    public final String alertStatName;
+
+    private final StatsLogger globalStatsLogger;
+    private final StatsLogger scopedStatsLogger;
+    private final String scope;
+    private Counter globalCounter = null;
+    private Counter scopedCounter = null;
+
+    public AlertStatsLogger(StatsLogger globalStatsLogger, String scope, String alertStatName) {
+        this.globalStatsLogger = globalStatsLogger;
+        this.scope = scope;
+        this.scopedStatsLogger = globalStatsLogger.scope(scope);
+        this.alertStatName = alertStatName;
+    }
+
+    public AlertStatsLogger(StatsLogger globalStatsLogger, String alertStatName) {
+        this.globalStatsLogger = globalStatsLogger;
+        this.scope = null;
+        this.scopedStatsLogger = null;
+        this.alertStatName = alertStatName;
+    }
+
+    private String format(String msg) {
+        return msg.startsWith("ALERT!: ") ? msg :
+                ("ALERT!: " + (scope != null ? "(" + scope + "):" : "" ) + msg);
+    }
+
+    private void initializeCountersIfNeeded() {
+        if (null != globalCounter) {
+            return;
+        }
+
+        globalCounter = globalStatsLogger.getCounter(alertStatName);
+
+        if (null != scopedStatsLogger) {
+            scopedCounter = scopedStatsLogger.getCounter(alertStatName);
+        }
+    }
+
+    /**
+     * Report an alertable condition". Prefixes "ALERT!: " if not already prefixed.
+     */
+    public void raise(String msg, Object... args) {
+        initializeCountersIfNeeded();
+        globalCounter.inc();
+        if (null != scopedCounter) {
+            scopedCounter.inc();
+        }
+        logger.error(format(msg), args);
+        logger.error("fake exception to generate stack trace", new Exception());
+    }
+}