-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trace SSL handshakes in netty 4.1 (open-telemetry#4604)
* Trace SSL handshakes in netty 4.1 * Update testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/HttpClientTestServer.java Co-authored-by: Trask Stalnaker <[email protected]> * remove unneeded bit of code Co-authored-by: Trask Stalnaker <[email protected]>
- Loading branch information
Showing
15 changed files
with
515 additions
and
6 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
38 changes: 38 additions & 0 deletions
38
...elemetry/javaagent/instrumentation/netty/common/client/NettySslErrorOnlyInstrumenter.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,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.netty.common.client; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import javax.annotation.Nullable; | ||
|
||
final class NettySslErrorOnlyInstrumenter implements NettySslInstrumenter { | ||
|
||
private final Instrumenter<NettySslRequest, Void> instrumenter; | ||
|
||
NettySslErrorOnlyInstrumenter(Instrumenter<NettySslRequest, Void> instrumenter) { | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
public boolean shouldStart(Context parentContext, NettySslRequest request) { | ||
// the "real" check is done on end() anyway | ||
return true; | ||
} | ||
|
||
@Override | ||
public Context start(Context parentContext, NettySslRequest request) { | ||
return parentContext; | ||
} | ||
|
||
@Override | ||
public void end(Context context, NettySslRequest request, @Nullable Throwable error) { | ||
if (error != null && instrumenter.shouldStart(context, request)) { | ||
Context connectContext = instrumenter.start(context, request); | ||
instrumenter.end(connectContext, request, null, error); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...lemetry/javaagent/instrumentation/netty/common/client/NettySslInstrumentationHandler.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,74 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.netty.common.client; | ||
|
||
import io.netty.channel.ChannelDuplexHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.handler.ssl.SslHandshakeCompletionEvent; | ||
import io.opentelemetry.context.Context; | ||
import java.net.SocketAddress; | ||
|
||
// inspired by reactor-netty SslProvider.SslReadHandler | ||
public final class NettySslInstrumentationHandler extends ChannelDuplexHandler { | ||
|
||
private final NettySslInstrumenter instrumenter; | ||
private Context parentContext; | ||
private NettySslRequest request; | ||
private Context context; | ||
|
||
public NettySslInstrumentationHandler(NettySslInstrumenter instrumenter) { | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
public void channelRegistered(ChannelHandlerContext ctx) { | ||
// remember the parent context from the time of channel registration; | ||
// this happens inside Bootstrap#connect() | ||
parentContext = Context.current(); | ||
ctx.fireChannelRegistered(); | ||
} | ||
|
||
@Override | ||
public void connect( | ||
ChannelHandlerContext ctx, | ||
SocketAddress remoteAddress, | ||
SocketAddress localAddress, | ||
ChannelPromise promise) { | ||
|
||
// netty SslHandler starts the handshake after it receives the channelActive() signal; this | ||
// happens just after the connection is established | ||
// this makes connect() promise a good place to start the SSL handshake span | ||
promise.addListener( | ||
future -> { | ||
// there won't be any SSL handshake if the channel fails to connect | ||
if (!future.isSuccess()) { | ||
return; | ||
} | ||
request = NettySslRequest.create(ctx.channel()); | ||
if (instrumenter.shouldStart(parentContext, request)) { | ||
context = instrumenter.start(parentContext, request); | ||
} | ||
}); | ||
ctx.connect(remoteAddress, localAddress, promise); | ||
} | ||
|
||
@Override | ||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { | ||
if (evt instanceof SslHandshakeCompletionEvent) { | ||
if (ctx.pipeline().context(this) != null) { | ||
ctx.pipeline().remove(this); | ||
} | ||
|
||
SslHandshakeCompletionEvent handshake = (SslHandshakeCompletionEvent) evt; | ||
if (context != null) { | ||
instrumenter.end(context, request, handshake.cause()); | ||
} | ||
} | ||
|
||
ctx.fireUserEventTriggered(evt); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../io/opentelemetry/javaagent/instrumentation/netty/common/client/NettySslInstrumenter.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,18 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.netty.common.client; | ||
|
||
import io.opentelemetry.context.Context; | ||
import javax.annotation.Nullable; | ||
|
||
public interface NettySslInstrumenter { | ||
|
||
boolean shouldStart(Context parentContext, NettySslRequest request); | ||
|
||
Context start(Context parentContext, NettySslRequest request); | ||
|
||
void end(Context context, NettySslRequest request, @Nullable Throwable error); | ||
} |
34 changes: 34 additions & 0 deletions
34
...opentelemetry/javaagent/instrumentation/netty/common/client/NettySslInstrumenterImpl.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.javaagent.instrumentation.netty.common.client; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import javax.annotation.Nullable; | ||
|
||
final class NettySslInstrumenterImpl implements NettySslInstrumenter { | ||
|
||
private final Instrumenter<NettySslRequest, Void> instrumenter; | ||
|
||
NettySslInstrumenterImpl(Instrumenter<NettySslRequest, Void> instrumenter) { | ||
this.instrumenter = instrumenter; | ||
} | ||
|
||
@Override | ||
public boolean shouldStart(Context parentContext, NettySslRequest request) { | ||
return instrumenter.shouldStart(parentContext, request); | ||
} | ||
|
||
@Override | ||
public Context start(Context parentContext, NettySslRequest request) { | ||
return instrumenter.start(parentContext, request); | ||
} | ||
|
||
@Override | ||
public void end(Context context, NettySslRequest request, @Nullable Throwable error) { | ||
instrumenter.end(context, request, null, error); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...lemetry/javaagent/instrumentation/netty/common/client/NettySslNetAttributesExtractor.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,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.netty.common.client; | ||
|
||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP; | ||
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_UDP; | ||
|
||
import io.netty.channel.socket.DatagramChannel; | ||
import io.opentelemetry.instrumentation.api.instrumenter.net.InetSocketAddressNetClientAttributesExtractor; | ||
import java.net.InetSocketAddress; | ||
import javax.annotation.Nullable; | ||
|
||
final class NettySslNetAttributesExtractor | ||
extends InetSocketAddressNetClientAttributesExtractor<NettySslRequest, Void> { | ||
|
||
@Nullable | ||
@Override | ||
public InetSocketAddress getAddress(NettySslRequest request, @Nullable Void unused) { | ||
if (request.remoteAddress() instanceof InetSocketAddress) { | ||
return (InetSocketAddress) request.remoteAddress(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String transport(NettySslRequest request, @Nullable Void unused) { | ||
return request.channel() instanceof DatagramChannel ? IP_UDP : IP_TCP; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../java/io/opentelemetry/javaagent/instrumentation/netty/common/client/NettySslRequest.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,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.netty.common.client; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import io.netty.channel.Channel; | ||
import io.opentelemetry.javaagent.instrumentation.netty.common.Timer; | ||
import java.net.SocketAddress; | ||
import javax.annotation.Nullable; | ||
|
||
@AutoValue | ||
public abstract class NettySslRequest { | ||
|
||
static NettySslRequest create(Channel channel) { | ||
return new AutoValue_NettySslRequest(Timer.start(), channel, channel.remoteAddress()); | ||
} | ||
|
||
String spanName() { | ||
return "SSL handshake"; | ||
} | ||
|
||
abstract Timer timer(); | ||
|
||
abstract Channel channel(); | ||
|
||
@Nullable | ||
abstract SocketAddress remoteAddress(); | ||
} |
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
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.