Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package io.grpc.gcp.observability.interceptors;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.Internal;
import io.grpc.gcp.observability.ObservabilityConfig;
import io.grpc.gcp.observability.ObservabilityConfig.LogFilter;
import java.util.ArrayDeque;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import javax.annotation.concurrent.GuardedBy;

/**
* Parses gRPC GcpObservability configuration filters for interceptors usage.
Expand All @@ -33,10 +39,16 @@ public class ConfigFilterHelper {
public static final FilterParams NO_FILTER_PARAMS
= FilterParams.create(false, 0, 0);

private final ObservabilityConfig config;
private static final int MAX_CACHED_METHODS = 1024;

private final FifoCache<String, FilterParams> clientFilterParamsCache;
private final FifoCache<String, FilterParams> serverFilterParamsCache;

private ConfigFilterHelper(ObservabilityConfig config) {
this.config = config;
clientFilterParamsCache = new FifoCache<>(MAX_CACHED_METHODS,
method -> matchFilters(method, config.getClientLogFilters()));
serverFilterParamsCache = new FifoCache<>(MAX_CACHED_METHODS,
method -> matchFilters(method, config.getServerLogFilters()));
}

/**
Expand All @@ -61,28 +73,61 @@ public static ConfigFilterHelper getInstance(ObservabilityConfig config) {
* (log field will be set to true) 2. values of payload limits retrieved from configuration
*/
public FilterParams logRpcMethod(String fullMethodName, boolean client) {
FilterParams params = NO_FILTER_PARAMS;

int index = checkNotNull(fullMethodName, "fullMethodName").lastIndexOf('/');
String serviceName = fullMethodName.substring(0, index);

List<LogFilter> logFilters =
client ? config.getClientLogFilters() : config.getServerLogFilters();
checkNotNull(fullMethodName, "fullMethodName");
FifoCache<String, FilterParams> cache =
client ? clientFilterParamsCache : serverFilterParamsCache;
return cache.get(fullMethodName);
}

// TODO (dnvindhya): Optimize by caching results for fullMethodName.
private static FilterParams matchFilters(String fullMethodName, List<LogFilter> logFilters) {
String serviceName = fullMethodName.substring(0, fullMethodName.lastIndexOf('/'));
for (LogFilter logFilter : logFilters) {
if (logFilter.matchAll
|| logFilter.services.contains(serviceName)
|| logFilter.methods.contains(fullMethodName)) {
if (logFilter.excludePattern) {
return params;
return NO_FILTER_PARAMS;
}
int currentHeaderBytes = logFilter.headerBytes;
int currentMessageBytes = logFilter.messageBytes;
return FilterParams.create(true, currentHeaderBytes, currentMessageBytes);
}
}
return params;
return NO_FILTER_PARAMS;
}

private static final class FifoCache<K, V> {
private final int maxSize;
private final Function<? super K, ? extends V> loader;
private final ConcurrentMap<K, V> entries = new ConcurrentHashMap<>();
@GuardedBy("this")
private final ArrayDeque<K> insertionOrder = new ArrayDeque<>();

FifoCache(int maxSize, Function<? super K, ? extends V> loader) {
checkArgument(maxSize > 0, "maxSize must be positive");
this.maxSize = maxSize;
this.loader = checkNotNull(loader, "loader");
}

V get(K key) {
V value = entries.get(key);
return value != null ? value : load(key);
}

// Keep eviction and insertion together while allowing cache hits to proceed concurrently.
private synchronized V load(K key) {
V value = entries.get(key);
if (value != null) {
return value;
}
value = checkNotNull(loader.apply(key), "loader returned null");
if (insertionOrder.size() == maxSize) {
entries.remove(insertionOrder.removeFirst());
}
entries.put(key, value);
insertionOrder.addLast(key);
return value;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,13 @@ public void checkMethodToBeLoggedConditional() {
= configFilterHelper.logRpcMethod("service4/method3", false);
assertThat(serverResultParams).isEqualTo(excludeParams);
}

@Test
public void reusesFilterParamsForSameMethod() {
when(mockConfig.getClientLogFilters()).thenReturn(configLogFilters);

String method = "service1/Method2";
FilterParams filterParams = configFilterHelper.logRpcMethod(method, true);
assertThat(configFilterHelper.logRpcMethod(method, true)).isSameInstanceAs(filterParams);
}
}
Loading