From b855b51c354468c9f570772ef402c2bfbe10ceaf Mon Sep 17 00:00:00 2001 From: diwanshu Date: Wed, 23 Aug 2023 07:40:02 -0700 Subject: [PATCH] Add metrics for blacklist rules usage --- .../carbonj/service/engine/MetricList.java | 89 +++++++++++-------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/carbonj.service/src/main/java/com/demandware/carbonj/service/engine/MetricList.java b/carbonj.service/src/main/java/com/demandware/carbonj/service/engine/MetricList.java index b45eed08..8eb6b6be 100644 --- a/carbonj.service/src/main/java/com/demandware/carbonj/service/engine/MetricList.java +++ b/carbonj.service/src/main/java/com/demandware/carbonj/service/engine/MetricList.java @@ -6,10 +6,10 @@ */ package com.demandware.carbonj.service.engine; +import org.apache.commons.lang3.tuple.Pair; import com.codahale.metrics.Counter; import com.codahale.metrics.MetricRegistry; import com.demandware.carbonj.service.db.util.StatsAware; -import com.demandware.carbonj.service.strings.StringsCache; import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import com.google.re2j.Pattern; @@ -37,18 +37,23 @@ public class MetricList implements StatsAware volatile private List configLines = new ArrayList<>( ); - volatile private List patterns = new ArrayList<>( ); + volatile private List> patternsAndCounters = new ArrayList<>( ); + + volatile private boolean empty = patternsAndCounters.isEmpty(); private final String confSrc; private final ConfigServerUtil configServerUtil; + private final MetricRegistry metricRegistry; + public MetricList(MetricRegistry metricRegistry, String name, File confFile, String confSrc, - ConfigServerUtil configServerUtil ) + ConfigServerUtil configServerUtil ) { this.name = Preconditions.checkNotNull(name); this.confFile = Preconditions.checkNotNull( confFile ); log.info( String.format("Creating metric list [%s] with config file [%s]", name, confFile) ); + this.metricRegistry = metricRegistry; this.droppedMetrics = metricRegistry.counter( MetricRegistry.name( name, "drop" ) ); this.confSrc = confSrc; this.configServerUtil = configServerUtil; @@ -58,42 +63,41 @@ public MetricList(MetricRegistry metricRegistry, String name, File confFile, St public boolean match(String name) { - if ( patterns.isEmpty() ) + if ( patternsAndCounters.isEmpty() ) { return false; } - StringsCache.State state = StringsCache.getState(name); - boolean isBlackListed = false; - if (state != null && state.getBlackListed() != null) { - isBlackListed = state.getBlackListed(); - if (isBlackListed) { - droppedMetrics.inc(); - } - return isBlackListed; - } - - List currentPatterns = patterns; // copy so we don't keep hitting the volatile barrier - for ( Pattern p : currentPatterns ) + List> currentPatternsAndCounters = patternsAndCounters; // copy so we don't keep hitting the volatile barrier + for ( int i = 0; i < currentPatternsAndCounters.size(); i++ ) { - if( ".*".equals( p.pattern() ) ) + Pair p = currentPatternsAndCounters.get(i); + + if( ".*".equals( p.getLeft().pattern() ) ) { + patternsAndCounters.get(i).getRight().inc(); droppedMetrics.inc(); - isBlackListed = true; - break; + return true; } - if ( p.matcher( name ).find() ) + long startTime = 0; + if (log.isDebugEnabled()) { + startTime = System.nanoTime(); // Record start time + } + if ( p.getLeft().matcher( name ).find() ) { + patternsAndCounters.get(i).getRight().inc(); droppedMetrics.inc(); - isBlackListed = true; - break; + if (log.isDebugEnabled()) { + long endTime = System.nanoTime(); // Record end time + long duration = endTime - startTime; // Calculate duration in nanoseconds + log.debug("Pattern match runtime for {}: {} nanoseconds", p.getLeft().pattern(), duration); + } + return true; } + } - if (state != null) { - state.setBlackListed(isBlackListed); - } - return isBlackListed; + return false; } public void reload() @@ -113,7 +117,7 @@ public void reload() } lines = FileUtils.readLines(confFile, Charsets.UTF_8); } else if (confSrc.equalsIgnoreCase("server")) { - if (configServerUtil == null || configServerUtil.getConfigLines(name).isEmpty()) { + if (configServerUtil == null || !configServerUtil.getConfigLines(name).isPresent()) { log.warn("Unable to read metric list configuration from config server. Falling back to file."); if (!confFile.exists()) { log.warn(String.format("Metric list [%s] configuration file doesn't exist. File: [%s]", name, confFile)); @@ -136,10 +140,9 @@ public void reload() log.info(String.format("Metric list [%s] configuration file has changed. File: [%s]", name, confFile)); List oldLines = this.configLines; - - this.patterns = parseConfig( lines ); + List> newPatternsAndCounters = parseConfig( lines ); + this.patternsAndCounters = newPatternsAndCounters; this.configLines = lines; - StringsCache.invalidateCache(); log.info(String.format("Metric list [%s] updated.", name)); if( log.isDebugEnabled() ) { @@ -152,12 +155,24 @@ public void reload() } } - private List parseConfig(List lines) + private List> parseConfig(List lines) { - return lines.stream() - .map( String::trim ) - .filter( line -> line.length() > 0 && !line.startsWith( "#" ) ) - .map( Pattern::compile ).collect( Collectors.toList() ); + // Create an empty list to hold pairs of Pattern and Counter + List> patternCounterPairs = lines.stream() + .map(String::trim) + .filter(line -> line.length() > 0 && !line.startsWith("#")) + .map(line -> { + Pattern pattern = Pattern.compile(line); + Counter counter = metricRegistry.counter( MetricRegistry.name( name, "blacklist" ) ); + return Pair.of(pattern, counter); // Create and return the Pair + }) + .collect(Collectors.toList()); + + // Reset the counters here if needed + for (Pair pair : patternCounterPairs) { + pair.getValue().dec(pair.getValue().getCount()); // Reset the counter to zero + } + return patternCounterPairs; } @Override @@ -168,6 +183,6 @@ public void dumpStats() public boolean isEmpty() { - return patterns.isEmpty(); + return patternsAndCounters.isEmpty(); } -} +} \ No newline at end of file