-
Notifications
You must be signed in to change notification settings - Fork 169
OpAMP WebSocket service #1969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
laurit
merged 28 commits into
open-telemetry:main
from
LikeTheSalad:opamp-websocket-service
Jul 10, 2025
Merged
OpAMP WebSocket service #1969
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1fb5d4f
Creating WebSocket contract
LikeTheSalad 52476f7
Updating tests to reuse tools and adding javadocs
LikeTheSalad 2a0064a
Adding tests for OkHttpWebSocket
LikeTheSalad a8a0cfd
Updating tests
LikeTheSalad 4a9332a
Adding tests to WebSocketRequestService
LikeTheSalad 0e0b982
Adding test dependency
LikeTheSalad 65156f8
Clean up
LikeTheSalad a6bf6e0
Spotless
LikeTheSalad 228a010
Using enum for OkHttpWebSocket statuses
LikeTheSalad c7c74ca
Using protobuf bom
LikeTheSalad cd80847
Validating illegal argument exception when closing
LikeTheSalad ac84012
Updating javadoc wording
LikeTheSalad ba37adc
Synchronizing access to hasPendingRequest
LikeTheSalad b392923
Update opamp-client/src/main/java/io/opentelemetry/opamp/client/inter…
LikeTheSalad c9fba42
Update opamp-client/src/main/java/io/opentelemetry/opamp/client/inter…
LikeTheSalad 522587d
Update opamp-client/src/main/java/io/opentelemetry/opamp/client/inter…
LikeTheSalad 4624809
Merge remote-tracking branch 'origin/opamp-websocket-service' into op…
LikeTheSalad b371629
Adding onClosing method
LikeTheSalad dc7f2a9
Using onClosing
LikeTheSalad f08736a
Merge branch 'main' into opamp-websocket-service
LikeTheSalad defd60b
Merge branch 'main' into opamp-websocket-service
LikeTheSalad 061e743
Adding comment explaining error rethrowing
LikeTheSalad 560c92a
Using daemon thread factory
LikeTheSalad 950d7c3
./gradlew spotlessApply
otelbot[bot] cf26a64
Merge branch 'main' into opamp-websocket-service
LikeTheSalad 83b4c54
Describing websocket close code
LikeTheSalad 3f7f483
Adding comment to explain last message when closing the websocket ser…
LikeTheSalad 3728857
Applying changes suggested from reviews
LikeTheSalad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
...n/java/io/opentelemetry/opamp/client/internal/connectivity/websocket/OkHttpWebSocket.java
This file contains hidden or 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,115 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.opamp.client.internal.connectivity.websocket; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Response; | ||
| import okhttp3.WebSocketListener; | ||
| import okio.ByteString; | ||
|
|
||
| public class OkHttpWebSocket implements WebSocket { | ||
| private final String url; | ||
| private final OkHttpClient client; | ||
| private final AtomicReference<Status> status = new AtomicReference<>(Status.NOT_RUNNING); | ||
| private final AtomicReference<okhttp3.WebSocket> webSocket = new AtomicReference<>(); | ||
|
|
||
| public static OkHttpWebSocket create(String url) { | ||
| return create(url, new OkHttpClient()); | ||
| } | ||
|
|
||
| public static OkHttpWebSocket create(String url, OkHttpClient client) { | ||
| return new OkHttpWebSocket(url, client); | ||
| } | ||
|
|
||
| private OkHttpWebSocket(String url, OkHttpClient client) { | ||
| this.url = url; | ||
| this.client = client; | ||
| } | ||
|
|
||
| @Override | ||
| public void open(Listener listener) { | ||
| if (status.compareAndSet(Status.NOT_RUNNING, Status.STARTING)) { | ||
| okhttp3.Request request = new okhttp3.Request.Builder().url(url).build(); | ||
| webSocket.set(client.newWebSocket(request, new ListenerAdapter(listener))); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean send(byte[] request) { | ||
| if (status.get() != Status.RUNNING) { | ||
| return false; | ||
| } | ||
| return getWebSocket().send(ByteString.of(request)); | ||
| } | ||
|
|
||
| @Override | ||
| public void close(int code, @Nullable String reason) { | ||
| if (status.compareAndSet(Status.RUNNING, Status.CLOSING)) { | ||
| try { | ||
| if (!getWebSocket().close(code, reason)) { | ||
| status.set(Status.NOT_RUNNING); | ||
| } | ||
| } catch (IllegalArgumentException e) { | ||
| status.set(Status.RUNNING); | ||
| // Re-throwing as this error happens due to a caller error. | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private okhttp3.WebSocket getWebSocket() { | ||
| return Objects.requireNonNull(webSocket.get()); | ||
| } | ||
|
|
||
| private class ListenerAdapter extends WebSocketListener { | ||
| private final Listener delegate; | ||
|
|
||
| private ListenerAdapter(Listener delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void onOpen(@Nonnull okhttp3.WebSocket webSocket, @Nonnull Response response) { | ||
| status.set(Status.RUNNING); | ||
| delegate.onOpen(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClosing(@Nonnull okhttp3.WebSocket webSocket, int code, @Nonnull String reason) { | ||
| status.set(Status.CLOSING); | ||
| delegate.onClosing(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClosed(@Nonnull okhttp3.WebSocket webSocket, int code, @Nonnull String reason) { | ||
| status.set(Status.NOT_RUNNING); | ||
| delegate.onClosed(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure( | ||
| @Nonnull okhttp3.WebSocket webSocket, @Nonnull Throwable t, @Nullable Response response) { | ||
| status.set(Status.NOT_RUNNING); | ||
| delegate.onFailure(t); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(@Nonnull okhttp3.WebSocket webSocket, @Nonnull ByteString bytes) { | ||
| delegate.onMessage(bytes.toByteArray()); | ||
| } | ||
| } | ||
|
|
||
| enum Status { | ||
| NOT_RUNNING, | ||
| STARTING, | ||
| CLOSING, | ||
| RUNNING | ||
| } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
...rc/main/java/io/opentelemetry/opamp/client/internal/connectivity/websocket/WebSocket.java
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.opamp.client.internal.connectivity.websocket; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| public interface WebSocket { | ||
| /** | ||
| * Starts the websocket connection if it's not yet started or if it has been closed. | ||
| * | ||
| * @param listener Will receive events from the websocket connection. | ||
| */ | ||
| void open(Listener listener); | ||
|
|
||
| /** | ||
| * Stops the websocket connection if running. Nothing will happen if it's already stopped. | ||
| * | ||
| * @param code Status code as defined by <a | ||
| * href="http://tools.ietf.org/html/rfc6455#section-7.4">Section 7.4 of RFC 6455</a> | ||
| * @param reason Reason for shutting down, as explained in <a | ||
| * href="https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1">Section 5.5.1 of RFC | ||
| * 6455</a> | ||
| */ | ||
| void close(int code, @Nullable String reason); | ||
|
|
||
| /** | ||
| * Sends a message via the websocket connection. | ||
| * | ||
| * @param request The message payload. | ||
| * @return {@code false} If the message can't be dispatched for any reason, whether the websocket | ||
| * isn't running, or the connection isn't established, or it's terminated. {@code true} if the | ||
| * message can get sent. Returning {@code true} doesn't guarantee that the message will arrive | ||
| * at the remote peer. | ||
| */ | ||
| boolean send(byte[] request); | ||
|
|
||
| interface Listener { | ||
|
|
||
| /** | ||
| * Called when the websocket connection is successfully established with the remote peer. The | ||
| * client may start sending messages after this method is called. | ||
| */ | ||
| void onOpen(); | ||
|
|
||
| /** | ||
| * Called when the closing handshake has started. No further messages will be sent after this | ||
| * method call. | ||
| */ | ||
| void onClosing(); | ||
|
|
||
| /** Called when the connection is terminated and no further messages can be transmitted. */ | ||
| void onClosed(); | ||
|
|
||
| /** | ||
| * Called when receiving a message from the remote peer. | ||
| * | ||
| * @param data The payload sent by the remote peer. | ||
| */ | ||
| void onMessage(byte[] data); | ||
|
|
||
| /** | ||
| * Called when the connection is closed or cannot be established due to an error. No messages | ||
| * can be transmitted after this method is called. | ||
| * | ||
| * @param t The error. | ||
| */ | ||
| void onFailure(Throwable t); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...main/java/io/opentelemetry/opamp/client/internal/request/service/DaemonThreadFactory.java
This file contains hidden or 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,25 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.opamp.client.internal.request.service; | ||
|
|
||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ThreadFactory; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| final class DaemonThreadFactory implements ThreadFactory { | ||
| private final ThreadFactory delegate = Executors.defaultThreadFactory(); | ||
|
|
||
| @Override | ||
| public Thread newThread(@Nonnull Runnable r) { | ||
| Thread t = delegate.newThread(r); | ||
| try { | ||
| t.setDaemon(true); | ||
| } catch (SecurityException e) { | ||
| // Well, we tried. | ||
| } | ||
| return t; | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is also
fun onMessage(webSocket: WebSocket, text: String)is it fine to leave that unhandled? I guess it is since it isn't used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary format should be the only one we need, so yeah I think it's fine to leave the utf-8 message unhandled.