Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5F763200CA3 for ; Thu, 1 Jun 2017 13:16:55 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5DDB3160BC4; Thu, 1 Jun 2017 11:16:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 9B0E4160BB5 for ; Thu, 1 Jun 2017 13:16:54 +0200 (CEST) Received: (qmail 32034 invoked by uid 500); 1 Jun 2017 11:16:53 -0000 Mailing-List: contact commits-help@bookkeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: bookkeeper-dev@bookkeeper.apache.org Delivered-To: mailing list commits@bookkeeper.apache.org Received: (qmail 32025 invoked by uid 99); 1 Jun 2017 11:16:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Jun 2017 11:16:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9D230DFDD5; Thu, 1 Jun 2017 11:16:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: eolivelli@apache.org To: commits@bookkeeper.apache.org Message-Id: <70af9554bc3e446793bbf5dd9a813850@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: bookkeeper git commit: BOOKKEEPER-1085: Introduce the AlertStatsLogger Date: Thu, 1 Jun 2017 11:16:53 +0000 (UTC) archived-at: Thu, 01 Jun 2017 11:16:55 -0000 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 Author: Robin Dhamankar 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 Authored: Thu Jun 1 13:16:48 2017 +0200 Committer: Enrico Olivelli 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()); + } +}