Skip to content

Commit

Permalink
Don't capture http.target as metrics attribute (#5081)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Jan 12, 2022
1 parent 5577afb commit b8ea362
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -18,6 +19,14 @@
@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();
Expand Down Expand Up @@ -96,7 +105,8 @@ private static <T> boolean containsAttribute(
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ void collectsMetrics() {
.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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ public void shouldApplyServerDurationView_withTarget() {
.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));
}
Expand Down

0 comments on commit b8ea362

Please sign in to comment.