From b45ffd7cbf62298c1487d99e2f7384de33533a41 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Mon, 19 Jun 2023 11:06:46 +0200 Subject: [PATCH] Make server.socket.* attributes on the HTTP server side opt-in --- .../http/HttpClientAttributesExtractor.java | 3 ++- .../http/HttpServerAttributesExtractor.java | 10 ++++++++-- .../HttpServerAttributesExtractorBuilder.java | 18 +++++++++++++++++- .../net/NetClientAttributesExtractor.java | 3 ++- .../net/NetServerAttributesExtractor.java | 3 ++- .../network/ServerAttributesExtractor.java | 3 ++- .../InternalServerAttributesExtractor.java | 11 +++++++---- .../HttpServerAttributesExtractorTest.java | 1 + ...rverAttributesExtractorBothSemconvTest.java | 1 + ...erAttributesExtractorStableSemconvTest.java | 18 ++++++++++++++++++ 10 files changed, 60 insertions(+), 11 deletions(-) diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java index be79eb996dbd..a0d35c94af38 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java @@ -102,7 +102,8 @@ public static HttpClientAttributesExtractorBuilder HttpServerAttributesExtractorBuilder netAttributesGetter, List capturedRequestHeaders, List capturedResponseHeaders, - Set knownMethods) { + Set knownMethods, + boolean captureServerSocketAttributes) { this( httpAttributesGetter, netAttributesGetter, capturedRequestHeaders, capturedResponseHeaders, knownMethods, + captureServerSocketAttributes, HttpRouteHolder::getRoute); } @@ -97,6 +99,7 @@ public static HttpServerAttributesExtractorBuilder capturedRequestHeaders, List capturedResponseHeaders, Set knownMethods, + boolean captureServerSocketAttributes, Function httpRouteHolderGetter) { super(httpAttributesGetter, capturedRequestHeaders, capturedResponseHeaders, knownMethods); HttpNetAddressPortExtractor addressPortExtractor = @@ -123,7 +126,10 @@ public static HttpServerAttributesExtractorBuilder( netAttributesGetter, diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java index 5ef74e342ae9..75a1d1b148ac 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java @@ -22,6 +22,7 @@ public final class HttpServerAttributesExtractorBuilder { List capturedRequestHeaders = emptyList(); List capturedResponseHeaders = emptyList(); Set knownMethods = HttpConstants.KNOWN_METHODS; + boolean captureServerSocketAttributes = false; HttpServerAttributesExtractorBuilder( HttpServerAttributesGetter httpAttributesGetter, @@ -89,6 +90,20 @@ public HttpServerAttributesExtractorBuilder setKnownMethods( return this; } + /** + * Configures the extractor to capture the optional {@code server.socket.address} and {@code + * server.socket.port} attributes, which are not collected by default. + * + * @param captureServerSocketAttributes {@code true} if the extractor should collect the optional + * {@code server.socket.address} and {@code server.socket.port} attributes. + */ + @CanIgnoreReturnValue + public HttpServerAttributesExtractorBuilder setCaptureServerSocketAttributes( + boolean captureServerSocketAttributes) { + this.captureServerSocketAttributes = captureServerSocketAttributes; + return this; + } + /** * Returns a new {@link HttpServerAttributesExtractor} with the settings of this {@link * HttpServerAttributesExtractorBuilder}. @@ -99,6 +114,7 @@ public AttributesExtractor build() { netAttributesGetter, capturedRequestHeaders, capturedResponseHeaders, - knownMethods); + knownMethods, + captureServerSocketAttributes); } } diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java index d242794ebff4..5eb402986d59 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/NetClientAttributesExtractor.java @@ -53,7 +53,8 @@ private NetClientAttributesExtractor(NetClientAttributesGetter( getter, diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java index 956873d728aa..0065121a012c 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/ServerAttributesExtractor.java @@ -41,7 +41,8 @@ public static ServerAttributesExtractor c /* emitStableUrlAttributes= */ true, /* emitOldHttpAttributes= */ false, // this param does not matter when old semconv is off - InternalServerAttributesExtractor.Mode.HOST); + InternalServerAttributesExtractor.Mode.HOST, + /* captureServerSocketAttributes= */ true); } @Override diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java index 6a3299c82b97..29fdd2577975 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/network/internal/InternalServerAttributesExtractor.java @@ -26,6 +26,7 @@ public final class InternalServerAttributesExtractor { private final boolean emitStableUrlAttributes; private final boolean emitOldHttpAttributes; private final Mode oldSemconvMode; + private final boolean captureServerSocketAttributes; public InternalServerAttributesExtractor( ServerAttributesGetter getter, @@ -33,13 +34,15 @@ public InternalServerAttributesExtractor( FallbackAddressPortExtractor fallbackAddressPortExtractor, boolean emitStableUrlAttributes, boolean emitOldHttpAttributes, - Mode oldSemconvMode) { + Mode oldSemconvMode, + boolean captureServerSocketAttributes) { this.getter = getter; this.captureServerPortCondition = captureServerPortCondition; this.fallbackAddressPortExtractor = fallbackAddressPortExtractor; this.emitStableUrlAttributes = emitStableUrlAttributes; this.emitOldHttpAttributes = emitOldHttpAttributes; this.oldSemconvMode = oldSemconvMode; + this.captureServerSocketAttributes = captureServerSocketAttributes; } public void onStart(AttributesBuilder attributes, REQUEST request) { @@ -69,7 +72,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO String serverSocketAddress = getter.getServerSocketAddress(request, response); if (serverSocketAddress != null && !serverSocketAddress.equals(serverAddressAndPort.address)) { - if (emitStableUrlAttributes) { + if (emitStableUrlAttributes && captureServerSocketAttributes) { internalSet(attributes, NetworkAttributes.SERVER_SOCKET_ADDRESS, serverSocketAddress); } if (emitOldHttpAttributes) { @@ -81,7 +84,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO if (serverSocketPort != null && serverSocketPort > 0 && !serverSocketPort.equals(serverAddressAndPort.port)) { - if (emitStableUrlAttributes) { + if (emitStableUrlAttributes && captureServerSocketAttributes) { internalSet(attributes, NetworkAttributes.SERVER_SOCKET_PORT, (long) serverSocketPort); } if (emitOldHttpAttributes) { @@ -91,7 +94,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO String serverSocketDomain = getter.getServerSocketDomain(request, response); if (serverSocketDomain != null && !serverSocketDomain.equals(serverAddressAndPort.address)) { - if (emitStableUrlAttributes) { + if (emitStableUrlAttributes && captureServerSocketAttributes) { internalSet(attributes, NetworkAttributes.SERVER_SOCKET_DOMAIN, serverSocketDomain); } if (emitOldHttpAttributes && oldSemconvMode.socketDomain != null) { diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java index 3e13d60b6d47..427ccb8c65f2 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorTest.java @@ -164,6 +164,7 @@ void normal() { singletonList("Custom-Request-Header"), singletonList("Custom-Response-Header"), HttpConstants.KNOWN_METHODS, + false, routeFromContext); AttributesBuilder startAttributes = Attributes.builder(); diff --git a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBothSemconvTest.java b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBothSemconvTest.java index c42a83790055..d37f39216fde 100644 --- a/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBothSemconvTest.java +++ b/instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBothSemconvTest.java @@ -158,6 +158,7 @@ void normal() { singletonList("Custom-Request-Header"), singletonList("Custom-Response-Header"), HttpConstants.KNOWN_METHODS, + false, routeFromContext); AttributesBuilder startAttributes = Attributes.builder(); diff --git a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java index c1e74a725b82..d037b62584b0 100644 --- a/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java +++ b/instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorStableSemconvTest.java @@ -134,6 +134,21 @@ public Integer getServerPort(Map request) { String value = request.get("hostPort"); return value == null ? null : Integer.parseInt(value); } + + @Nullable + @Override + public String getServerSocketAddress( + Map request, @Nullable Map response) { + return request.get("serverSocketAddress"); + } + + @Nullable + @Override + public Integer getServerSocketPort( + Map request, @Nullable Map response) { + String value = request.get("serverSocketPort"); + return value == null ? null : Integer.parseInt(value); + } } @Test @@ -154,6 +169,8 @@ void normal() { request.put("type", "ipv4"); request.put("protocolName", "http"); request.put("protocolVersion", "2.0"); + request.put("serverSocketAddress", "1.2.3.4"); + request.put("serverSocketPort", "42"); Map response = new HashMap<>(); response.put("statusCode", "202"); @@ -169,6 +186,7 @@ void normal() { singletonList("Custom-Request-Header"), singletonList("Custom-Response-Header"), HttpConstants.KNOWN_METHODS, + false, routeFromContext); AttributesBuilder startAttributes = Attributes.builder();