Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to disable request/error counters and cache stats #576

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions java/org/apache/catalina/core/StandardWrapperValve.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


import java.io.IOException;
import java.util.concurrent.atomic.LongAdder;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.RequestDispatcher;
Expand All @@ -34,6 +33,7 @@
import org.apache.catalina.connector.ClientAbortException;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.util.Counter;
import org.apache.catalina.valves.ValveBase;
import org.apache.coyote.CloseNowException;
import org.apache.tomcat.util.ExceptionUtils;
Expand Down Expand Up @@ -66,8 +66,8 @@ public StandardWrapperValve() {
private volatile long processingTime;
private volatile long maxTime;
private volatile long minTime = Long.MAX_VALUE;
private final LongAdder requestCount = new LongAdder();
private final LongAdder errorCount = new LongAdder();
private Counter requestCount;
private Counter errorCount;


// --------------------------------------------------------- Public Methods
Expand Down Expand Up @@ -294,7 +294,7 @@ public long getMinTime() {
* @return the number of requests processed by the associated wrapper.
*/
public long getRequestCount() {
return requestCount.sum();
return requestCount.get();
}

/**
Expand All @@ -303,15 +303,32 @@ public long getRequestCount() {
* @return the number of requests processed by the associated wrapper that resulted in an error.
*/
public long getErrorCount() {
return errorCount.sum();
return errorCount.get();
}

public void incrementErrorCount() {
errorCount.increment();
}

/**
* Enable/disable the request count and error count.
*
* @param enabled {@code true} if request and error counts are tracked, {@code false} otherwise.
*/
public void setEnableCounters(boolean enabled) {
requestCount = Counter.of(enabled);
errorCount = Counter.of(enabled);
}

@Override
protected void initInternal() throws LifecycleException {
// NOOP - Don't register this Valve in JMX
// Don't register this Valve in JMX

if (requestCount == null) {
requestCount = Counter.of(true);
}
if (errorCount == null) {
errorCount = Counter.of(true);
}
}
}
57 changes: 57 additions & 0 deletions java/org/apache/catalina/util/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.catalina.util;

import java.util.concurrent.atomic.LongAdder;

public interface Counter {
void increment();
long get();

static Counter of(boolean enabled) {
return enabled ? new Simple() : Noop.INSTANCE;
}

class Noop implements Counter {
private static final Counter INSTANCE = new Noop();

@Override
public void increment() {
// no-op
}

@Override
public long get() {
return 0;
}
}

class Simple implements Counter {
private final LongAdder adder = new LongAdder();

@Override
public void increment() {
adder.increment();
}

@Override
public long get() {
return adder.sum();
}
}
}
15 changes: 10 additions & 5 deletions java/org/apache/catalina/webresources/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

import org.apache.catalina.WebResource;
import org.apache.catalina.WebResourceRoot.CacheStrategy;
import org.apache.catalina.util.Counter;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
Expand All @@ -49,8 +49,8 @@ public class Cache {
private int objectMaxSize = (int) maxSize / OBJECT_MAX_SIZE_FACTOR;
private CacheStrategy cacheStrategy;

private LongAdder lookupCount = new LongAdder();
private LongAdder hitCount = new LongAdder();
private Counter lookupCount = Counter.of(true);
private Counter hitCount = Counter.of(true);

private final ConcurrentMap<String, CachedResource> resourceCache = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -265,6 +265,11 @@ void removeCacheEntry(String path) {
}
}

public void setEnableCounters(boolean enabled) {
lookupCount = Counter.of(enabled);
hitCount = Counter.of(enabled);
}

public CacheStrategy getCacheStrategy() {
return cacheStrategy;
}
Expand Down Expand Up @@ -292,11 +297,11 @@ public void setMaxSize(long maxSize) {
}

public long getLookupCount() {
return lookupCount.sum();
return lookupCount.get();
}

public long getHitCount() {
return hitCount.sum();
return hitCount.get();
}

public void setObjectMaxSize(int objectMaxSize) {
Expand Down