Skip to content

Commit

Permalink
Add metric of log
Browse files Browse the repository at this point in the history
### What changes are proposed in this pull request?

Add metric of log.

pr-link: #13839
change-id: cid-4275e40e7b1e126d3375c74abd0294740cf1bbfd
  • Loading branch information
codings-dan committed Aug 6, 2021
1 parent d6c6733 commit 0fba8bb
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
5 changes: 4 additions & 1 deletion conf/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# May get overridden by System Property

log4j.rootLogger=INFO, ${alluxio.logger.type}, ${alluxio.remote.logger.type}
log4j.rootLogger=INFO, ${alluxio.logger.type}, ${alluxio.remote.logger.type}, EventCounter

log4j.category.alluxio.logserver=INFO, ${alluxio.logserver.logger.type}
log4j.additivity.alluxio.logserver=false
Expand All @@ -34,6 +34,9 @@ log4j.appender.Console.layout.ConversionPattern=%d{ISO8601} %-5p %c{1} - %m%n
log4j.logger.org.apache.parquet.hadoop.InternalParquetRecordWriter=WARN
log4j.logger.org.apache.parquet.hadoop.InternalParquetRecordReader=WARN

# Appender for EventCounter
log4j.appender.EventCounter=alluxio.metrics.EventCounter

# Appender for Job Master
log4j.appender.JOB_MASTER_LOGGER=org.apache.log4j.RollingFileAppender
log4j.appender.JOB_MASTER_LOGGER.File=${alluxio.logs.dir}/job_master.log
Expand Down
121 changes: 121 additions & 0 deletions core/common/src/main/java/alluxio/metrics/EventCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package alluxio.metrics;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

/**
* A log4J Appender that simply counts logging events in four levels:
* FATAL, ERROR WARN and INFO. The class name is used in log4j.properties
*/
public class EventCounter extends AppenderSkeleton {
private static final int FATAL = 0;
private static final int ERROR = 1;
private static final int WARN = 2;
private static final int INFO = 3;
private static EventCounter.EventCounts sCount = new EventCounter.EventCounts();

/**
* The constructor of EventCounter.
*/
public EventCounter() {
}

/**
* Gets the number of fatal log.
*
* @return the number of fatal log
*/
public static long getFatal() {
return sCount.get(FATAL);
}

/**
* Gets the number of error log.
*
* @return the number of error log
*/
public static long getError() {
return sCount.get(ERROR);
}

/**
* Gets the number of warn log.
*
* @return the number of warn log
*/
public static long getWarn() {
return sCount.get(WARN);
}

/**
* Gets the number of info log.
*
* @return the number of info log
*/
public static long getInfo() {
return sCount.get(INFO);
}

/**
* Add the number of corresponding level log.
*
* @param event event of generating log
*/
public void append(LoggingEvent event) {
Level level = event.getLevel();
if (level.equals(Level.INFO)) {
sCount.incr(INFO);
} else if (level.equals(Level.WARN)) {
sCount.incr(WARN);
} else if (level.equals(Level.ERROR)) {
sCount.incr(ERROR);
} else if (level.equals(Level.FATAL)) {
sCount.incr(FATAL);
}
}

/**
* Release any resources allocated within the appender such as file
* handles, network connections, etc.
*/
public void close() {
}

/**
* Configurators call this method to determine if the appender
* requires a layout.
*
* @return if the appender requires a layout
*/
public boolean requiresLayout() {
return false;
}

private static class EventCounts {
private final long[] mCounts;

private EventCounts() {
mCounts = new long[]{0L, 0L, 0L, 0L};
}

private synchronized void incr(int i) {
mCounts[i]++;
}

private synchronized long get(int i) {
return mCounts[i];
}
}
}
34 changes: 34 additions & 0 deletions core/common/src/main/java/alluxio/metrics/LogStateCounterSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package alluxio.metrics;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;

import java.util.HashMap;
import java.util.Map;

/**
* A set of counters for the log metric.
*/
public class LogStateCounterSet implements MetricSet {
@Override
public Map<String, Metric> getMetrics() {
final Map<String, Metric> gauges = new HashMap<>();
gauges.put("log.info.count", (Gauge<Long>) EventCounter::getInfo);
gauges.put("log.warn.count", (Gauge<Long>) EventCounter::getWarn);
gauges.put("log.error.count", (Gauge<Long>) EventCounter::getError);
gauges.put("log.fatal.count", (Gauge<Long>) EventCounter::getFatal);
return gauges;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public static InstanceType fromString(String text) {
METRIC_REGISTRY.registerAll(new MemoryUsageGaugeSet());
METRIC_REGISTRY.registerAll(new ClassLoadingGaugeSet());
METRIC_REGISTRY.registerAll(new CachedThreadStatesGaugeSet(5, TimeUnit.SECONDS));
METRIC_REGISTRY.registerAll(new LogStateCounterSet());
}

@GuardedBy("MetricsSystem")
Expand Down

0 comments on commit 0fba8bb

Please sign in to comment.