Skip to content

Commit 6cf9063

Browse files
authored
OpAMP callback changes (#2336)
1 parent 7a62ab8 commit 6cf9063

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

opamp-client/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ OpampClient client =
2222
.build(
2323
new OpampClient.Callbacks() {
2424
@Override
25-
public void onConnect() {}
25+
public void onConnect(OpampClient client) {}
2626

2727
@Override
28-
public void onConnectFailed(@Nullable Throwable throwable) {}
28+
public void onConnectFailed(OpampClient client, @Nullable Throwable throwable) {}
2929

3030
@Override
31-
public void onErrorResponse(ServerErrorResponse errorResponse) {}
31+
public void onErrorResponse(OpampClient client, ServerErrorResponse errorResponse) {}
3232

3333
@Override
34-
public void onMessage(MessageData messageData) {
34+
public void onMessage(OpampClient client, MessageData messageData) {
3535
AgentRemoteConfig remoteConfig = messageData.getRemoteConfig();
3636
if (remoteConfig != null) {
3737
// A remote config was received

opamp-client/src/main/java/io/opentelemetry/opamp/client/OpampClient.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static OpampClientBuilder builder() {
2222
* Sets attributes of the Agent. The attributes will be included in the next outgoing status
2323
* report. This is typically used by Agents which allow their AgentDescription to change
2424
* dynamically while the OpAMPClient is started. May be also called from {@link
25-
* Callbacks#onMessage(MessageData)}.
25+
* Callbacks#onMessage(OpampClient, MessageData)}.
2626
*
2727
* @param agentDescription The new agent description.
2828
*/
@@ -40,26 +40,30 @@ interface Callbacks {
4040
* Called when the connection is successfully established to the Server. For WebSocket clients
4141
* this is called after the handshake is completed without any error. For HTTP clients this is
4242
* called for any request if the response status is OK.
43+
*
44+
* @param client The client that's connected.
4345
*/
44-
void onConnect();
46+
void onConnect(OpampClient client);
4547

4648
/**
4749
* Called when the connection to the Server cannot be established. May also be called if the
4850
* connection is lost and reconnection attempt fails.
4951
*
52+
* @param client The client that failed to connect.
5053
* @param throwable The exception.
5154
*/
52-
void onConnectFailed(@Nullable Throwable throwable);
55+
void onConnectFailed(OpampClient client, @Nullable Throwable throwable);
5356

5457
/**
5558
* Called when the Server reports an error in response to some previously sent request. Useful
5659
* for logging purposes. The Agent should not attempt to process the error by reconnecting or
5760
* retrying previous operations. The client handles the ErrorResponse_UNAVAILABLE case
5861
* internally by performing retries as necessary.
5962
*
63+
* @param client The client that received an error response.
6064
* @param errorResponse The error returned by the Server.
6165
*/
62-
void onErrorResponse(ServerErrorResponse errorResponse);
66+
void onErrorResponse(OpampClient client, ServerErrorResponse errorResponse);
6367

6468
/**
6569
* Called when the Agent receives a message that needs processing. See {@link MessageData}
@@ -70,8 +74,9 @@ interface Callbacks {
7074
* onMessage returns. This is advisable if processing can take a long time. In that case
7175
* returning quickly is preferable to avoid blocking the {@link OpampClient}.
7276
*
77+
* @param client The client that received a message.
7378
* @param messageData The server response data that needs processing.
7479
*/
75-
void onMessage(MessageData messageData);
80+
void onMessage(OpampClient client, MessageData messageData);
7681
}
7782
}

opamp-client/src/main/java/io/opentelemetry/opamp/client/internal/impl/OpampClientImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ public void setRemoteConfigStatus(@Nonnull RemoteConfigStatus remoteConfigStatus
146146

147147
@Override
148148
public void onConnectionSuccess() {
149-
callbacks.onConnect();
149+
callbacks.onConnect(this);
150150
}
151151

152152
@Override
153153
public void onConnectionFailed(Throwable throwable) {
154-
callbacks.onConnectFailed(throwable);
154+
callbacks.onConnectFailed(this, throwable);
155155
}
156156

157157
@Override
@@ -168,7 +168,7 @@ public void onRequestFailed(Throwable throwable) {
168168
preserveFailedRequestRecipe();
169169
if (throwable instanceof OpampServerResponseException) {
170170
ServerErrorResponse errorResponse = ((OpampServerResponseException) throwable).errorResponse;
171-
callbacks.onErrorResponse(errorResponse);
171+
callbacks.onErrorResponse(this, errorResponse);
172172
}
173173
}
174174

@@ -195,7 +195,7 @@ private void handleResponsePayload(ServerToAgent response) {
195195
}
196196

197197
if (notifyOnMessage) {
198-
callbacks.onMessage(messageBuilder.build());
198+
callbacks.onMessage(this, messageBuilder.build());
199199
}
200200
}
201201

opamp-client/src/test/java/io/opentelemetry/opamp/client/internal/impl/OpampClientImplTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99
import static org.awaitility.Awaitility.await;
1010
import static org.mockito.ArgumentMatchers.any;
11+
import static org.mockito.ArgumentMatchers.eq;
1112
import static org.mockito.Mockito.never;
1213
import static org.mockito.Mockito.spy;
1314
import static org.mockito.Mockito.verify;
@@ -199,7 +200,8 @@ void onSuccess_withChangesToReport_notifyCallbackOnMessage() {
199200
// Await for onMessage call
200201
await().atMost(Duration.ofSeconds(5)).until(() -> callbacks.onMessageCalls.get() == 1);
201202

202-
verify(callbacks).onMessage(MessageData.builder().setRemoteConfig(remoteConfig).build());
203+
verify(callbacks)
204+
.onMessage(client, MessageData.builder().setRemoteConfig(remoteConfig).build());
203205
}
204206

205207
@Test
@@ -214,7 +216,7 @@ void onSuccess_withNoChangesToReport_doNotNotifyCallbackOnMessage() {
214216
// Giving some time for the callback to get called
215217
await().during(Duration.ofSeconds(1));
216218

217-
verify(callbacks, never()).onMessage(any());
219+
verify(callbacks, never()).onMessage(eq(client), any());
218220
}
219221

220222
@Test
@@ -257,8 +259,8 @@ void onConnectionSuccessful_notifyCallback() {
257259

258260
await().atMost(Duration.ofSeconds(5)).until(() -> callbacks.onConnectCalls.get() == 1);
259261

260-
verify(callbacks).onConnect();
261-
verify(callbacks, never()).onConnectFailed(any());
262+
verify(callbacks).onConnect(client);
263+
verify(callbacks, never()).onConnectFailed(eq(client), any());
262264
}
263265

264266
@Test
@@ -301,8 +303,8 @@ void onFailedResponse_withServerErrorData_notifyCallback() {
301303

302304
await().atMost(Duration.ofSeconds(5)).until(() -> callbacks.onErrorResponseCalls.get() == 1);
303305

304-
verify(callbacks).onErrorResponse(errorResponse);
305-
verify(callbacks, never()).onMessage(any());
306+
verify(callbacks).onErrorResponse(client, errorResponse);
307+
verify(callbacks, never()).onMessage(eq(client), any());
306308
}
307309

308310
@Test
@@ -312,7 +314,7 @@ void onConnectionFailed_notifyCallback() {
312314

313315
client.onConnectionFailed(throwable);
314316

315-
verify(callbacks).onConnectFailed(throwable);
317+
verify(callbacks).onConnectFailed(client, throwable);
316318
}
317319

318320
@Test
@@ -450,22 +452,22 @@ private static class TestCallbacks implements OpampClient.Callbacks {
450452
private final AtomicInteger onMessageCalls = new AtomicInteger();
451453

452454
@Override
453-
public void onConnect() {
455+
public void onConnect(OpampClient client) {
454456
onConnectCalls.incrementAndGet();
455457
}
456458

457459
@Override
458-
public void onConnectFailed(@Nullable Throwable throwable) {
460+
public void onConnectFailed(OpampClient client, @Nullable Throwable throwable) {
459461
onConnectFailedCalls.incrementAndGet();
460462
}
461463

462464
@Override
463-
public void onErrorResponse(ServerErrorResponse errorResponse) {
465+
public void onErrorResponse(OpampClient client, ServerErrorResponse errorResponse) {
464466
onErrorResponseCalls.incrementAndGet();
465467
}
466468

467469
@Override
468-
public void onMessage(MessageData messageData) {
470+
public void onMessage(OpampClient client, MessageData messageData) {
469471
onMessageCalls.incrementAndGet();
470472
}
471473
}

0 commit comments

Comments
 (0)