From f843bef21dda6442cf20b7af9ad58add6043b126 Mon Sep 17 00:00:00 2001 From: Yashwant Date: Tue, 3 Jun 2025 14:41:55 +0530 Subject: [PATCH 1/2] adding allow list metric view configration and adding cardinality support --- .../extensions/MetricViewConfiguration.java | 132 +++++++++++++----- 1 file changed, 100 insertions(+), 32 deletions(-) diff --git a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java index 32774ead..eb79dea1 100644 --- a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java +++ b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java @@ -17,45 +17,113 @@ package org.hypertrace.agent.otel.extensions; import io.opentelemetry.sdk.metrics.View; +import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; public class MetricViewConfiguration { + private static final Logger logger = Logger.getLogger(MetricViewConfiguration.class.getName()); + // OpenTelemetry's cardinality limit property + private static final String OTEL_CARDINALITY_LIMIT = + "otel.experimental.metrics.cardinality.limit"; + + // Default HTTP attributes to include + private static final Set KEYS_TO_RETAIN = + new HashSet<>( + Arrays.asList( + "http.method", + "http.status_code", + "http.scheme", + "http.route", + "rpc.method", + "rpc.grpc.status_code", + "rpc.service")); + + /** + * Creates a View with HTTP attribute filtering and cardinality limit based on OpenTelemetry + * configuration. + * + *

The cardinality limit is set from the OpenTelemetry system property or environment variable: + * + *

+ * + * @return a configured View with HTTP attribute filtering and cardinality limit + */ public static View createView() { - // Attributes to exclude - Set excludedAttributes = - new HashSet<>( - Arrays.asList( - "net.sock.peer.addr", - "net.sock.host.addr", - "net.sock.peer.port", - "net.sock.host.port", - "net.host.name", - "net.host.port", - "net.protocol.name", - "net.protocol.version", - "http.user_agent", - "enduser.id", - "http.client_ip", - "http.route", - "http.target", - "http.request_content_length", - "http.response_content_length", - "user_agent.original")); - - // Build the view - return View.builder() - .setAttributeFilter( - attributes -> { - for (String attribute : excludedAttributes) { - if (attributes.contains(attribute)) { + // Build the view with our attribute filter + View view = + View.builder() + .setAttributeFilter( + attributes -> { + for (String attribute : KEYS_TO_RETAIN) { + if (attributes.contains(attribute)) { + return true; + } + } return false; - } - } - return true; - }) - .build(); + }) + .build(); + + Integer cardinalityLimit = getCardinalityLimit(); + + /* Only apply cardinality limit if it's explicitly configured + The global cardinality configuration field 'otel.experimental.metrics.cardinality.limit' does not apply for custom views + Also the view builder, does not have a setter for cardinality limit in 1.33.0 SDK we use. + So using reflection to set the cardinality limit + */ + if (cardinalityLimit != null) { + try { + // Get the View class + Class viewClass = view.getClass(); + + // Get the cardinalityLimit field + Field cardinalityLimitField = viewClass.getDeclaredField("cardinalityLimit"); + cardinalityLimitField.setAccessible(true); + + // Set the cardinality limit + cardinalityLimitField.set(view, cardinalityLimit); + + } catch (Exception e) { + logger.log(Level.WARNING, "Failed to set cardinality limit using reflection", e); + } + } + + return view; + } + + /** + * Gets the cardinality limit from OpenTelemetry's system property or environment variable. + * + * @return the configured cardinality limit, or null if not configured + */ + private static Integer getCardinalityLimit() { + String limitValue = getProperty(OTEL_CARDINALITY_LIMIT); + + if (limitValue != null && !limitValue.isEmpty()) { + try { + return Integer.parseInt(limitValue); + } catch (NumberFormatException e) { + logger.log(Level.WARNING, "Invalid cardinality limit value: " + limitValue, e); + } + } + + return null; // No explicit configuration + } + + /** + * Gets a property from system properties or environment variables. + * + * @param name the property name + * @return the property value, or null if not set + */ + private static String getProperty(String name) { + return System.getProperty(name, System.getenv(name.replaceAll("\\.", "_").toUpperCase())); } } From 17c40d6374e81768b817f211b1c03a69a50877dc Mon Sep 17 00:00:00 2001 From: Yashwant Date: Tue, 3 Jun 2025 15:44:34 +0530 Subject: [PATCH 2/2] removing route from allow list --- .../agent/otel/extensions/MetricViewConfiguration.java | 1 - 1 file changed, 1 deletion(-) diff --git a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java index eb79dea1..af1770df 100644 --- a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java +++ b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/MetricViewConfiguration.java @@ -38,7 +38,6 @@ public class MetricViewConfiguration { "http.method", "http.status_code", "http.scheme", - "http.route", "rpc.method", "rpc.grpc.status_code", "rpc.service"));