Browse Source

Don't capture http.target as metrics attribute (#5081)

Trask Stalnaker 3 years ago
parent
commit
b8ea362112

+ 11 - 1
instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsView.java

@@ -8,6 +8,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.http;
 import io.opentelemetry.api.common.AttributeKey;
 import io.opentelemetry.api.common.Attributes;
 import io.opentelemetry.api.common.AttributesBuilder;
+import io.opentelemetry.instrumentation.api.config.Config;
 import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
 import java.util.HashSet;
 import java.util.Set;
@@ -18,6 +19,14 @@ import java.util.function.BiConsumer;
 @SuppressWarnings("rawtypes")
 final class TemporaryMetricsView {
 
+  // TODO (trask) remove this once http.route is captured consistently
+  //
+  // this is not enabled by default because it falls back to http.target (which can be high
+  // cardinality) when http.route is not available
+  private static final boolean USE_HTTP_TARGET_FALLBACK =
+      Config.get()
+          .getBoolean("otel.instrumentation.metrics.experimental.use-http-target-fallback", false);
+
   private static final Set<AttributeKey> durationAlwaysInclude = buildDurationAlwaysInclude();
   private static final Set<AttributeKey> durationClientView = buildDurationClientView();
   private static final Set<AttributeKey> durationServerView = buildDurationServerView();
@@ -96,7 +105,8 @@ final class TemporaryMetricsView {
   static Attributes applyServerDurationView(Attributes startAttributes, Attributes endAttributes) {
     Set<AttributeKey> fullSet = durationServerView;
     // Use http.target when http.route is not available.
-    if (!containsAttribute(SemanticAttributes.HTTP_ROUTE, startAttributes, endAttributes)) {
+    if (USE_HTTP_TARGET_FALLBACK
+        && !containsAttribute(SemanticAttributes.HTTP_ROUTE, startAttributes, endAttributes)) {
       fullSet = durationServerFallbackView;
     }
     AttributesBuilder filtered = Attributes.builder();

+ 0 - 1
instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsTest.java

@@ -143,7 +143,6 @@ class HttpServerMetricsTest {
                                         .containsOnly(
                                             attributeEntry("http.scheme", "https"),
                                             attributeEntry("http.host", "host"),
-                                            attributeEntry("http.target", "/"),
                                             attributeEntry("http.method", "GET"),
                                             attributeEntry("http.status_code", 200),
                                             attributeEntry("http.flavor", "2.0"));

+ 0 - 2
instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsViewTest.java

@@ -136,8 +136,6 @@ public class TemporaryMetricsViewTest {
         .containsOnly(
             attributeEntry(SemanticAttributes.HTTP_SCHEME.getKey(), "https"),
             attributeEntry(SemanticAttributes.HTTP_HOST.getKey(), "somehost"),
-            attributeEntry(
-                SemanticAttributes.HTTP_TARGET.getKey(), "/somehost/high/cardinality/12345"),
             attributeEntry(SemanticAttributes.HTTP_METHOD.getKey(), "GET"),
             attributeEntry(SemanticAttributes.HTTP_STATUS_CODE.getKey(), 500));
   }