Skip to content

Commit

Permalink
Add metrics for tracking blacklist rules usage
Browse files Browse the repository at this point in the history
  • Loading branch information
diwanshu-crm committed Aug 8, 2023
1 parent 6a4b5b7 commit e8d6fc0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions carbonj.service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN yum update -y && \
RUN yum install -y gcc-c++ gcc make libtool automake autoconf make python3-devel

RUN rpm --import http://repos.azulsystems.com/RPM-GPG-KEY-azulsystems && \
curl -o /etc/yum.repos.d/zulu.repo http://repos.azulsystems.com/rhel/zulu.repo && \
curl -L -o /etc/yum.repos.d/zulu.repo http://repos.azulsystems.com/rhel/zulu.repo && \
yum update -y && \
yum install -y zulu-11 \
python3 \
Expand All @@ -35,7 +35,7 @@ RUN rpm --import http://repos.azulsystems.com/RPM-GPG-KEY-azulsystems && \
yum clean all

# Install aiohttp required by the script
RUN pip3 install aiohttp
RUN pip3 install multidict && pip3 install typing-extensions && pip3 install aiohttp

# Ensure crontab starts
RUN /sbin/chkconfig crond on && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
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;
Expand Down Expand Up @@ -36,16 +37,16 @@ public class MetricList implements StatsAware

volatile private List<String> configLines = new ArrayList<>( );

volatile private List<Pattern> patterns = new ArrayList<>( );
volatile private List<Pair<Pattern, Counter>> patternsAndCounters = new ArrayList<>( );

volatile private boolean empty = patterns.isEmpty();
volatile private boolean empty = patternsAndCounters.isEmpty();

private final String confSrc;

private final ConfigServerUtil configServerUtil;

public MetricList(MetricRegistry metricRegistry, String name, File confFile, String confSrc,
ConfigServerUtil configServerUtil )
ConfigServerUtil configServerUtil )
{
this.name = Preconditions.checkNotNull(name);
this.confFile = Preconditions.checkNotNull( confFile );
Expand All @@ -59,22 +60,26 @@ public MetricList(MetricRegistry metricRegistry, String name, File confFile, St

public boolean match(String name)
{
if ( patterns.isEmpty() )
if ( patternsAndCounters.isEmpty() )
{
return false;
}

List<Pattern> currentPatterns = patterns; // copy so we don't keep hitting the volatile barrier
for ( Pattern p : currentPatterns )
List<Pair<Pattern, Counter>> 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<Pattern, Counter> p = currentPatternsAndCounters.get(i);

if( ".*".equals( p.getLeft().pattern() ) )
{
patternsAndCounters.get(i).getRight().inc();
droppedMetrics.inc();
return true;
}

if ( p.matcher( name ).find() )
if ( p.getLeft().matcher( name ).find() )
{
patternsAndCounters.get(i).getRight().inc();
droppedMetrics.inc();
return true;
}
Expand Down Expand Up @@ -122,9 +127,8 @@ public void reload()
log.info(String.format("Metric list [%s] configuration file has changed. File: [%s]", name, confFile));

List<String> oldLines = this.configLines;
List<Pattern> newPatterns = parseConfig( lines );

this.patterns = newPatterns;
List<Pair<Pattern, Counter>> newPatternsAndCounters = parseConfig( lines );
this.patternsAndCounters = newPatternsAndCounters;
this.configLines = lines;
log.info(String.format("Metric list [%s] updated.", name));
if( log.isDebugEnabled() )
Expand All @@ -138,12 +142,24 @@ public void reload()
}
}

private List<Pattern> parseConfig(List<String> lines)
private List<Pair<Pattern, Counter>> parseConfig(List<String> 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<Pair<Pattern, Counter>> patternCounterPairs = lines.stream()
.map(String::trim)
.filter(line -> line.length() > 0 && !line.startsWith("#"))
.map(line -> {
Pattern pattern = Pattern.compile(line);
Counter counter = new Counter();
return Pair.of(pattern, counter); // Create and return the Pair
})
.collect(Collectors.toList());

// Reset the counters here if needed
for (Pair<Pattern, Counter> pair : patternCounterPairs) {
pair.getValue().dec(pair.getValue().getCount()); // Reset the counter to zero
}
return patternCounterPairs;
}

@Override
Expand All @@ -154,6 +170,6 @@ public void dumpStats()

public boolean isEmpty()
{
return patterns.isEmpty();
return patternsAndCounters.isEmpty();
}
}

0 comments on commit e8d6fc0

Please sign in to comment.