-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reactor Netty: emit actual HTTP client spans spans on connection errors
- Loading branch information
Mateusz Rzeszutek
committed
Jul 27, 2023
1 parent
dc5d76a
commit 3de3908
Showing
14 changed files
with
233 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...entelemetry/instrumentation/netty/v4/common/internal/client/NettyInstrumentationFlag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.netty.v4.common.internal.client; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public enum NettyInstrumentationFlag { | ||
ENABLED, | ||
ERROR_ONLY, | ||
DISABLED; | ||
|
||
public static NettyInstrumentationFlag enabledOrErrorOnly(boolean b) { | ||
return b ? ENABLED : ERROR_ONLY; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...telemetry/instrumentation/netty/v4/common/internal/client/NoopConnectionInstrumenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.netty.v4.common.internal.client; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.netty.channel.Channel; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.netty.common.internal.NettyConnectionRequest; | ||
import javax.annotation.Nullable; | ||
|
||
enum NoopConnectionInstrumenter implements NettyConnectionInstrumenter { | ||
INSTANCE; | ||
|
||
@Override | ||
public boolean shouldStart(Context parentContext, NettyConnectionRequest request) { | ||
return false; | ||
} | ||
|
||
@CanIgnoreReturnValue | ||
@Override | ||
public Context start(Context parentContext, NettyConnectionRequest request) { | ||
return parentContext; | ||
} | ||
|
||
@Override | ||
public void end( | ||
Context context, | ||
NettyConnectionRequest request, | ||
Channel channel, | ||
@Nullable Throwable error) {} | ||
} |
28 changes: 28 additions & 0 deletions
28
...io/opentelemetry/instrumentation/netty/v4/common/internal/client/NoopSslInstrumenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.netty.v4.common.internal.client; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.context.Context; | ||
import javax.annotation.Nullable; | ||
|
||
enum NoopSslInstrumenter implements NettySslInstrumenter { | ||
INSTANCE; | ||
|
||
@Override | ||
public boolean shouldStart(Context parentContext, NettySslRequest request) { | ||
return false; | ||
} | ||
|
||
@CanIgnoreReturnValue | ||
@Override | ||
public Context start(Context parentContext, NettySslRequest request) { | ||
return parentContext; | ||
} | ||
|
||
@Override | ||
public void end(Context context, NettySslRequest request, @Nullable Throwable error) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
.../opentelemetry/javaagent/instrumentation/reactornetty/v1_0/FailedRequestWithUrlMaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
import reactor.netty.http.client.HttpClientConfig; | ||
import reactor.netty.http.client.HttpClientRequest; | ||
|
||
final class FailedRequestWithUrlMaker { | ||
|
||
static HttpClientRequest create(HttpClientConfig config, HttpClientRequest failedRequest) { | ||
return (HttpClientRequest) | ||
Proxy.newProxyInstance( | ||
FailedRequestWithUrlMaker.class.getClassLoader(), | ||
new Class<?>[] {HttpClientRequest.class}, | ||
new HttpRequestInvocationHandler(config, failedRequest)); | ||
} | ||
|
||
private static final class HttpRequestInvocationHandler implements InvocationHandler { | ||
|
||
private final HttpClientConfig config; | ||
private final HttpClientRequest failedRequest; | ||
|
||
private HttpRequestInvocationHandler(HttpClientConfig config, HttpClientRequest failedRequest) { | ||
this.config = config; | ||
this.failedRequest = failedRequest; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if ("resourceUrl".equals(method.getName())) { | ||
return computeUrlFromConfig(); | ||
} | ||
try { | ||
return method.invoke(failedRequest, args); | ||
} catch (InvocationTargetException exception) { | ||
throw exception.getCause(); | ||
} | ||
} | ||
|
||
private String computeUrlFromConfig() { | ||
String uri = config.uri(); | ||
if (isAbsolute(uri)) { | ||
return uri; | ||
} | ||
|
||
// use the baseUrl if it was configured | ||
String baseUrl = config.baseUrl(); | ||
if (baseUrl != null) { | ||
if (baseUrl.endsWith("/") && uri.startsWith("/")) { | ||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1); | ||
} | ||
return baseUrl + uri; | ||
} | ||
|
||
// otherwise, use the host+port config to construct the full url | ||
SocketAddress hostAddress = config.remoteAddress().get(); | ||
if (hostAddress instanceof InetSocketAddress) { | ||
InetSocketAddress inetHostAddress = (InetSocketAddress) hostAddress; | ||
return (config.isSecure() ? "https://" : "http://") | ||
+ inetHostAddress.getHostString() | ||
+ ":" | ||
+ inetHostAddress.getPort() | ||
+ (uri.startsWith("/") ? "" : "/") | ||
+ uri; | ||
} | ||
|
||
return uri; | ||
} | ||
|
||
private static boolean isAbsolute(String uri) { | ||
return uri != null && !uri.isEmpty() && !uri.startsWith("/"); | ||
} | ||
} | ||
|
||
private FailedRequestWithUrlMaker() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.