Skip to content

Commit 26c5c07

Browse files
committed
spotless apply
1 parent 6fd344e commit 26c5c07

File tree

4 files changed

+74
-80
lines changed

4 files changed

+74
-80
lines changed

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/NettyChannelPipelineInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static void addHandler(
142142
io.opentelemetry.javaagent.instrumentation.netty.v4_0.client
143143
.HttpClientRequestTracingHandler.class
144144
.getName(),
145-
new OtelHttpClientRequestTracingHandler());
145+
new OtelHttpClientRequestTracingHandler());
146146
} else if (handler instanceof HttpRequestEncoder) {
147147
pipeline.addLast(
148148
HttpClientRequestTracingHandler.class.getName(),

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/client/OtelHttpClientRequestTracingHandler.java

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,102 +19,99 @@
1919
import io.netty.channel.ChannelHandlerContext;
2020
import io.netty.channel.ChannelPromise;
2121
import io.netty.handler.codec.http.HttpRequest;
22-
import io.netty.handler.codec.http.HttpResponse;
2322
import io.opentelemetry.api.trace.Span;
2423
import io.opentelemetry.api.trace.SpanContext;
2524
import io.opentelemetry.context.Context;
26-
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
27-
import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
28-
import java.util.concurrent.ConcurrentHashMap;
2925
import io.opentelemetry.javaagent.instrumentation.netty.v4_0.AttributeKeys;
3026
import io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.HttpClientRequestTracingHandler;
27+
import java.util.concurrent.ConcurrentHashMap;
3128

3229
/**
3330
* Custom extension of OpenTelemetry's HttpClientRequestTracingHandler that ensures proper context
3431
* propagation by using Context.current() as the parent context.
3532
*/
3633
public class OtelHttpClientRequestTracingHandler extends HttpClientRequestTracingHandler {
3734

38-
// Store the server context for each thread
39-
private static final ThreadLocal<Context> SERVER_CONTEXT = new ThreadLocal<>();
35+
// Store the server context for each thread
36+
private static final ThreadLocal<Context> SERVER_CONTEXT = new ThreadLocal<>();
4037

41-
// Store the mapping from thread ID to server span context (for cross-thread scenarios)
42-
private static final ConcurrentHashMap<Long, SpanContext> THREAD_TO_SPAN_CONTEXT =
43-
new ConcurrentHashMap<>();
38+
// Store the mapping from thread ID to server span context (for cross-thread scenarios)
39+
private static final ConcurrentHashMap<Long, SpanContext> THREAD_TO_SPAN_CONTEXT =
40+
new ConcurrentHashMap<>();
4441

45-
// Maximum size for the thread map before triggering cleanup
46-
private static final int MAX_THREAD_MAP_SIZE = 1000;
42+
// Maximum size for the thread map before triggering cleanup
43+
private static final int MAX_THREAD_MAP_SIZE = 1000;
4744

48-
// Cleanup flag to avoid excessive synchronized blocks
49-
private static volatile boolean cleanupNeeded = false;
45+
// Cleanup flag to avoid excessive synchronized blocks
46+
private static volatile boolean cleanupNeeded = false;
5047

51-
public OtelHttpClientRequestTracingHandler() {
52-
super();
53-
}
48+
public OtelHttpClientRequestTracingHandler() {
49+
super();
50+
}
5451

55-
/**
56-
* Stores the current context as the server context for this thread. This should be called from
57-
* the server handler.
58-
*/
59-
public static void storeServerContext(Context context) {
60-
SERVER_CONTEXT.set(context);
61-
62-
// Also store the span context by thread ID for cross-thread lookup
63-
Span span = Span.fromContext(context);
64-
if (span != null && span.getSpanContext().isValid()) {
65-
THREAD_TO_SPAN_CONTEXT.put(Thread.currentThread().getId(), span.getSpanContext());
66-
67-
// Check if we need to clean up the map
68-
if (THREAD_TO_SPAN_CONTEXT.size() > MAX_THREAD_MAP_SIZE) {
69-
cleanupNeeded = true;
70-
}
71-
}
72-
}
52+
/**
53+
* Stores the current context as the server context for this thread. This should be called from
54+
* the server handler.
55+
*/
56+
public static void storeServerContext(Context context) {
57+
SERVER_CONTEXT.set(context);
58+
59+
// Also store the span context by thread ID for cross-thread lookup
60+
Span span = Span.fromContext(context);
61+
if (span != null && span.getSpanContext().isValid()) {
62+
THREAD_TO_SPAN_CONTEXT.put(Thread.currentThread().getId(), span.getSpanContext());
7363

74-
/**
75-
* Perform cleanup of the thread map if it has grown too large. This is done in a synchronized
76-
* block to prevent concurrent modification issues.
77-
*/
78-
private static void cleanupThreadMapIfNeeded() {
79-
if (cleanupNeeded) {
80-
synchronized (THREAD_TO_SPAN_CONTEXT) {
81-
if (THREAD_TO_SPAN_CONTEXT.size() > MAX_THREAD_MAP_SIZE) {
82-
THREAD_TO_SPAN_CONTEXT.clear();
83-
cleanupNeeded = false;
84-
}
85-
}
64+
// Check if we need to clean up the map
65+
if (THREAD_TO_SPAN_CONTEXT.size() > MAX_THREAD_MAP_SIZE) {
66+
cleanupNeeded = true;
67+
}
68+
}
69+
}
70+
71+
/**
72+
* Perform cleanup of the thread map if it has grown too large. This is done in a synchronized
73+
* block to prevent concurrent modification issues.
74+
*/
75+
private static void cleanupThreadMapIfNeeded() {
76+
if (cleanupNeeded) {
77+
synchronized (THREAD_TO_SPAN_CONTEXT) {
78+
if (THREAD_TO_SPAN_CONTEXT.size() > MAX_THREAD_MAP_SIZE) {
79+
THREAD_TO_SPAN_CONTEXT.clear();
80+
cleanupNeeded = false;
8681
}
82+
}
8783
}
84+
}
8885

89-
@Override
90-
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) {
91-
try {
92-
if (!(msg instanceof HttpRequest)) {
93-
super.write(ctx, msg, prm);
94-
return;
95-
}
86+
@Override
87+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) {
88+
try {
89+
if (!(msg instanceof HttpRequest)) {
90+
super.write(ctx, msg, prm);
91+
return;
92+
}
9693

97-
Context parentContext = SERVER_CONTEXT.get();
94+
Context parentContext = SERVER_CONTEXT.get();
9895

99-
// Fallback -> If no context in thread local, try Context.current()
100-
if (parentContext == null) {
101-
parentContext = Context.current();
102-
}
96+
// Fallback -> If no context in thread local, try Context.current()
97+
if (parentContext == null) {
98+
parentContext = Context.current();
99+
}
103100

104-
// Store the parent context in the channel attributes
105-
// This is used by the Opentelemetry's HttpClientRequestTracingHandler in propagating correct
106-
// context.
107-
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).set(parentContext);
101+
// Store the parent context in the channel attributes
102+
// This is used by the Opentelemetry's HttpClientRequestTracingHandler in propagating correct
103+
// context.
104+
ctx.channel().attr(AttributeKeys.CLIENT_PARENT_CONTEXT).set(parentContext);
108105

109-
// Call the parent implementation which will use our stored parent context
110-
super.write(ctx, msg, prm);
106+
// Call the parent implementation which will use our stored parent context
107+
super.write(ctx, msg, prm);
111108

112-
// Clean up after use to prevent memory leaks
113-
SERVER_CONTEXT.remove();
114-
THREAD_TO_SPAN_CONTEXT.remove(Thread.currentThread().getId());
115-
cleanupThreadMapIfNeeded();
109+
// Clean up after use to prevent memory leaks
110+
SERVER_CONTEXT.remove();
111+
THREAD_TO_SPAN_CONTEXT.remove(Thread.currentThread().getId());
112+
cleanupThreadMapIfNeeded();
116113

117-
} catch (Exception ignored) {
118-
}
114+
} catch (Exception ignored) {
119115
}
116+
}
120117
}

instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_0/server/HttpServerRequestTracingHandler.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
import io.opentelemetry.context.Scope;
3131
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.AttributeKeys;
3232
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.DataCaptureUtils;
33+
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.OtelHttpClientRequestTracingHandler;
3334
import java.nio.charset.Charset;
3435
import java.util.HashMap;
3536
import java.util.Map;
36-
37-
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.client.OtelHttpClientRequestTracingHandler;
3837
import org.hypertrace.agent.core.config.InstrumentationConfig;
3938
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
4039
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
@@ -61,7 +60,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
6160
return;
6261
}
6362

64-
6563
// Store the server context in our ThreadLocal for later use by client handlers
6664
// This is CRITICAL for proper context propagation to client spans
6765
OtelHttpClientRequestTracingHandler.storeServerContext(context);
@@ -81,8 +79,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
8179

8280
CharSequence contentType = DataCaptureUtils.getContentType(httpRequest);
8381
if (instrumentationConfig.httpBody().request()
84-
&& contentType != null
85-
&& ContentTypeUtils.shouldCapture(contentType.toString())) {
82+
&& contentType != null
83+
&& ContentTypeUtils.shouldCapture(contentType.toString())) {
8684

8785
CharSequence contentLengthHeader = DataCaptureUtils.getContentLength(httpRequest);
8886
int contentLength = ContentLengthUtils.parseLength(contentLengthHeader);
@@ -93,21 +91,21 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
9391
// set the buffer to capture response body
9492
// the buffer is used byt captureBody method
9593
Attribute<BoundedByteArrayOutputStream> bufferAttr =
96-
ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER);
94+
ctx.channel().attr(AttributeKeys.REQUEST_BODY_BUFFER);
9795
bufferAttr.set(BoundedBuffersFactory.createStream(contentLength, charset));
9896

9997
channel.attr(AttributeKeys.CHARSET).set(charset);
10098
}
10199
}
102100

103101
if ((msg instanceof HttpContent || msg instanceof ByteBuf)
104-
&& instrumentationConfig.httpBody().request()) {
102+
&& instrumentationConfig.httpBody().request()) {
105103
Charset charset = channel.attr(AttributeKeys.CHARSET).get();
106104
if (charset == null) {
107105
charset = ContentTypeCharsetUtils.getDefaultCharset();
108106
}
109107
DataCaptureUtils.captureBody(
110-
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset);
108+
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset);
111109
}
112110
}
113111

instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/netty/v4_1/server/HttpServerRequestTracingHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
110110
DataCaptureUtils.captureBody(
111111
span, channel, AttributeKeys.REQUEST_BODY_BUFFER, msg, null, charset);
112112
}
113-
114113
}
115114
ctx.fireChannelRead(msg);
116115
}

0 commit comments

Comments
 (0)