Skip to content
Merged
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 @@ -19,24 +19,42 @@

import static org.apache.hadoop.hdds.utils.RocksDBStoreMetrics.ROCKSDB_CONTEXT_PREFIX;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.metrics2.MetricsTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Util class for
* {@link org.apache.hadoop.hdds.server.http.PrometheusMetricsSink}.
*/
public final class PrometheusMetricsSinkUtil {
private static final Logger LOG = LoggerFactory.getLogger(PrometheusMetricsSinkUtil.class);

private static final Pattern SPLIT_PATTERN =
Pattern.compile("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
private static final Pattern REPLACE_PATTERN =
Pattern.compile("[^a-zA-Z0-9]+");

private static final int NORMALIZED_NAME_CACHE_MAX_SIZE = 100_000;

// Original metric name -> Normalized Prometheus metric name
private static final CacheLoader<String, String> NORMALIZED_NAME_CACHE_LOADER =
CacheLoader.from(PrometheusMetricsSinkUtil::normalizeImpl);
private static final com.google.common.cache.LoadingCache<String, String> NORMALIZED_NAME_CACHE =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (change only if you need to update the patch for any other reason): add import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, previously I was using Guava Cache interface that conflicts with our internal Cache class.

CacheBuilder.newBuilder()
.maximumSize(NORMALIZED_NAME_CACHE_MAX_SIZE)
.build(NORMALIZED_NAME_CACHE_LOADER);

/**
* Never constructed.
*/
Expand Down Expand Up @@ -94,11 +112,41 @@ public static String prometheusName(String recordName,
}

/**
* Normalizes metrics tag key name.
* @param baseName
* @return normalized name.
* Normalizes the original metric name to follow the Prometheus naming convention. This method
* utilizes cache to improve performance.
* <p>
* Reference:
* <ul>
* <li>
* <a href="https://prometheus.io/docs/practices/naming/">
* Metrics and Label Naming</a>
* </li>
* <li>
* <a href="https://prometheus.io/docs/instrumenting/exposition_formats/">
* Exposition formats</a>
* </li>
* </ul>
* </p>
* @param baseName The original metric name.
* @return Prometheus normalized name.
*/
private static String normalizeName(String baseName) {
try {
return NORMALIZED_NAME_CACHE.get(baseName);
} catch (ExecutionException | UncheckedExecutionException e) {
// This should not happen since normalization function do not throw any exception
// Nevertheless, we can fall back to uncached implementation if it somehow happens.
LOG.warn("Exception encountered when loading metric with base name {} from cache, " +
"fall back to uncached normalization implementation", baseName, e);
return normalizeImpl(baseName);
}
}

/**
* Underlying Prometheus normalization implementation.
* See {@link PrometheusMetricsSinkUtil#normalizeName(String)} for more information.
*/
private static String normalizeImpl(String baseName) {
String[] parts = SPLIT_PATTERN.split(baseName);
String result = String.join("_", parts).toLowerCase();
return REPLACE_PATTERN.matcher(result).replaceAll("_");
Expand Down