-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
106 additions
and
59 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
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
79 changes: 79 additions & 0 deletions
79
...ry/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/ServerContexts.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,79 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.netty.v4_1.internal; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.util.Attribute; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
/** | ||
* A helper class for keeping track of incoming requests and spans associated with them. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public final class ServerContexts { | ||
private static final int PIPELINING_LIMIT = 1000; | ||
// With http pipelining multiple requests can be sent on the same connection. Responses should be | ||
// sent in the same order the requests came in. We use this deque to store the request context | ||
// and pop elements as responses are sent. | ||
private final Deque<ServerContext> serverContexts = new ArrayDeque<>(); | ||
private volatile boolean broken = false; | ||
|
||
private ServerContexts() {} | ||
|
||
public static ServerContexts get(Channel channel) { | ||
return channel.attr(AttributeKeys.SERVER_CONTEXTS).get(); | ||
} | ||
|
||
public static ServerContexts getOrCreate(Channel channel) { | ||
Attribute<ServerContexts> attribute = channel.attr(AttributeKeys.SERVER_CONTEXTS); | ||
ServerContexts result = attribute.get(); | ||
if (result == null) { | ||
result = new ServerContexts(); | ||
attribute.set(result); | ||
} | ||
return result; | ||
} | ||
|
||
public static ServerContext peekFirst(Channel channel) { | ||
ServerContexts serverContexts = get(channel); | ||
return serverContexts != null ? serverContexts.peekFirst() : null; | ||
} | ||
|
||
public ServerContext peekFirst() { | ||
return serverContexts.peekFirst(); | ||
} | ||
|
||
public ServerContext peekLast() { | ||
return serverContexts.peekFirst(); | ||
} | ||
|
||
public ServerContext pollFirst() { | ||
return serverContexts.pollFirst(); | ||
} | ||
|
||
public ServerContext pollLast() { | ||
return serverContexts.pollLast(); | ||
} | ||
|
||
public void addLast(ServerContext context) { | ||
if (broken) { | ||
return; | ||
} | ||
// If the pipelining limit is exceeded we'll stop tracing and mark the channel as broken. | ||
// Exceeding the limit indicates that there is good chance that server context are not removed | ||
// from the deque and there could be a memory leak. This could happen when http server decides | ||
// not to send response to some requests, for example see | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/11942 | ||
if (serverContexts.size() > PIPELINING_LIMIT) { | ||
broken = true; | ||
serverContexts.clear(); | ||
} | ||
serverContexts.addLast(context); | ||
} | ||
} |
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
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