Consistently set http.scheme in all netty instrumentations
MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientAttributesExtractor.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/NettyHttpClientAttributesExtractor.java
package io.opentelemetry.javaagent.instrumentation.netty.v3_8.client;
+import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.util.HttpSchemeUtil.getScheme;
+
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.netty.v3_8.HttpRequestAndChannel;
import java.net.URI;
@@ -24,8 +26,7 @@ final class NettyHttpClientAttributesExtractor
String target = requestAndChannel.request().getUri();
URI uri = new URI(target);
if ((uri.getHost() == null || uri.getHost().equals("")) && hostHeader != null) {
- String scheme = "http://";
- return scheme + hostHeader + target;
+ return getScheme(requestAndChannel) + "://" + hostHeader + target;
}
return uri.toString();
} catch (URISyntaxException e) {

MODIFY
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyHttpServerAttributesExtractor.java
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/NettyHttpServerAttributesExtractor.java
package io.opentelemetry.javaagent.instrumentation.netty.v3_8.server;
+import static io.opentelemetry.javaagent.instrumentation.netty.v3_8.util.HttpSchemeUtil.getScheme;
+
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.netty.v3_8.HttpRequestAndChannel;
import java.util.List;
@@ -84,9 +86,8 @@ final class NettyHttpServerAttributesExtractor
}
@Override
- @Nullable
protected String scheme(HttpRequestAndChannel requestAndChannel) {
- return null;
+ return getScheme(requestAndChannel);
}
@Override

ADD
instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/util/HttpSchemeUtil.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.netty.v3_8.util;
+
+import io.opentelemetry.javaagent.instrumentation.netty.v3_8.HttpRequestAndChannel;
+import org.jboss.netty.channel.ChannelHandler;
+
+public final class HttpSchemeUtil {
+
+ private static final Class<? extends ChannelHandler> sslHandlerClass = getSslHandlerClass();
+
+ @SuppressWarnings("unchecked")
+ private static Class<? extends ChannelHandler> getSslHandlerClass() {
+ try {
+ return (Class<? extends ChannelHandler>)
+ Class.forName(
+ "org.jboss.netty.handler.ssl.SslHandler",
+ false,
+ HttpSchemeUtil.class.getClassLoader());
+ } catch (ClassNotFoundException exception) {
+ return null;
+ }
+ }
+
+ public static String getScheme(HttpRequestAndChannel requestAndChannel) {
+ return isHttps(requestAndChannel) ? "https" : "http";
+ }
+
+ private static boolean isHttps(HttpRequestAndChannel requestAndChannel) {
+ return sslHandlerClass != null
+ && requestAndChannel.channel().getPipeline().get(sslHandlerClass) != null;
+ }
+
+ private HttpSchemeUtil() {}
+}

ADD
instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/common/HttpSchemeUtil.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.netty.common;
+
+import io.netty.channel.ChannelHandler;
+
+public final class HttpSchemeUtil {
+
+ private static final Class<? extends ChannelHandler> sslHandlerClass = getSslHandlerClass();
+
+ @SuppressWarnings("unchecked")
+ private static Class<? extends ChannelHandler> getSslHandlerClass() {
+ try {
+ return (Class<? extends ChannelHandler>)
+ Class.forName(
+ "io.netty.handler.ssl.SslHandler", false, HttpSchemeUtil.class.getClassLoader());
+ } catch (ClassNotFoundException exception) {
+ return null;
+ }
+ }
+
+ public static String getScheme(HttpRequestAndChannel requestAndChannel) {
+ return isHttps(requestAndChannel) ? "https" : "http";
+ }
+
+ private static boolean isHttps(HttpRequestAndChannel requestAndChannel) {
+ return sslHandlerClass != null
+ && requestAndChannel.channel().pipeline().get(sslHandlerClass) != null;
+ }
+
+ private HttpSchemeUtil() {}
+}

MODIFY
instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/common/client/NettyHttpClientAttributesExtractor.java
instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/common/client/NettyHttpClientAttributesExtractor.java
package io.opentelemetry.javaagent.instrumentation.netty.common.client;
-import io.netty.channel.ChannelHandler;
+import static io.opentelemetry.javaagent.instrumentation.netty.common.HttpSchemeUtil.getScheme;
+
import io.netty.handler.codec.http.HttpResponse;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.netty.common.HttpRequestAndChannel;
@@ -17,21 +18,6 @@ import javax.annotation.Nullable;
final class NettyHttpClientAttributesExtractor
extends HttpClientAttributesExtractor<HttpRequestAndChannel, HttpResponse> {
- private static final Class<? extends ChannelHandler> sslHandlerClass = getSslHandlerClass();
-
- @SuppressWarnings("unchecked")
- private static Class<? extends ChannelHandler> getSslHandlerClass() {
- try {
- return (Class<? extends ChannelHandler>)
- Class.forName(
- "io.netty.handler.ssl.SslHandler",
- false,
- NettyHttpClientAttributesExtractor.class.getClassLoader());
- } catch (ClassNotFoundException exception) {
- return null;
- }
- }
-
@Override
@Nullable
protected String url(HttpRequestAndChannel requestAndChannel) {
@@ -40,9 +26,7 @@ final class NettyHttpClientAttributesExtractor
String target = requestAndChannel.request().getUri();
URI uri = new URI(target);
if ((uri.getHost() == null || uri.getHost().equals("")) && hostHeader != null) {
- boolean isHttps = requestAndChannel.channel().pipeline().get(sslHandlerClass) != null;
- String scheme = isHttps ? "https://" : "http://";
- return scheme + hostHeader + target;
+ return getScheme(requestAndChannel) + "://" + hostHeader + target;
}
return uri.toString();
} catch (URISyntaxException e) {

MODIFY
instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/common/server/NettyHttpServerAttributesExtractor.java
instrumentation/netty/netty-4-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/common/server/NettyHttpServerAttributesExtractor.java
package io.opentelemetry.javaagent.instrumentation.netty.common.server;
-import io.netty.channel.ChannelHandler;
+import static io.opentelemetry.javaagent.instrumentation.netty.common.HttpSchemeUtil.getScheme;
+
import io.netty.handler.codec.http.HttpResponse;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.netty.common.HttpRequestAndChannel;
@@ -15,21 +16,6 @@ import javax.annotation.Nullable;
final class NettyHttpServerAttributesExtractor
extends HttpServerAttributesExtractor<HttpRequestAndChannel, HttpResponse> {
- private static final Class<? extends ChannelHandler> sslHandlerClass = getSslHandlerClass();
-
- @SuppressWarnings("unchecked")
- private static Class<? extends ChannelHandler> getSslHandlerClass() {
- try {
- return (Class<? extends ChannelHandler>)
- Class.forName(
- "io.netty.handler.ssl.SslHandler",
- false,
- NettyHttpServerAttributesExtractor.class.getClassLoader());
- } catch (ClassNotFoundException exception) {
- return null;
- }
- }
-
@Override
protected String method(HttpRequestAndChannel requestAndChannel) {
return requestAndChannel.request().getMethod().name();
@@ -100,10 +86,8 @@ final class NettyHttpServerAttributesExtractor
}
@Override
- @Nullable
protected String scheme(HttpRequestAndChannel requestAndChannel) {
- boolean isHttps = requestAndChannel.channel().pipeline().get(sslHandlerClass) != null;
- return isHttps ? "https" : "http";
+ return getScheme(requestAndChannel);
}
@Override

MODIFY
testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy
testing-common/src/main/groovy/io/opentelemetry/instrumentation/test/base/HttpServerTest.groovy
@@ -685,8 +685,7 @@ abstract class HttpServerTest<SERVER> extends InstrumentationSpecification imple
"${SemanticAttributes.HTTP_USER_AGENT.key}" TEST_USER_AGENT
"${SemanticAttributes.HTTP_HOST}" { it == "localhost" || it == "localhost:${port}" }
- // TODO netty does not set http.scheme - refactor HTTP server tests so that it's possible to specify extracted attributes, like in HTTP client tests
- "${SemanticAttributes.HTTP_SCHEME}" { it == "http" || it == null }
+ "${SemanticAttributes.HTTP_SCHEME}" "http"
"${SemanticAttributes.HTTP_TARGET}" endpoint.resolvePath(address).getPath() + "?id=$requestId"
if (extraAttributes.contains(SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH)) {

Consistently set http.scheme in all netty instrumentations (#4576)