Skip to content

Commit e99e45b

Browse files
committed
build takes function instead of direct callbacks.
1 parent c2beac7 commit e99e45b

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ static OpampClientBuilder builder() {
3535
*/
3636
void setRemoteConfigStatus(RemoteConfigStatus remoteConfigStatus);
3737

38+
Callbacks NOOP_CALLBACKS =
39+
new Callbacks() {
40+
@Override
41+
public void onConnect() {}
42+
43+
@Override
44+
public void onConnectFailed(@org.jetbrains.annotations.Nullable Throwable throwable) {}
45+
46+
@Override
47+
public void onErrorResponse(ServerErrorResponse errorResponse) {}
48+
49+
@Override
50+
public void onMessage(MessageData messageData) {}
51+
};
52+
3853
interface Callbacks {
3954
/**
4055
* Called when the connection is successfully established to the Server. For WebSocket clients

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.UUID;
23+
import java.util.function.Function;
2324
import javax.annotation.Nullable;
2425
import opamp.proto.AgentCapabilities;
2526
import opamp.proto.AgentDescription;
@@ -376,7 +377,7 @@ public OpampClientBuilder setEffectiveConfigState(State.EffectiveConfig effectiv
376377
return this;
377378
}
378379

379-
public OpampClient build(OpampClient.Callbacks callbacks) {
380+
public OpampClient build(Function<OpampClient, OpampClient.Callbacks> callbacks) {
380381
List<KeyValue> protoIdentifyingAttributes = new ArrayList<>();
381382
List<KeyValue> protoNonIdentifyingAttributes = new ArrayList<>();
382383
identifyingAttributes.forEach(

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Collections;
3030
import java.util.List;
3131
import java.util.concurrent.atomic.AtomicBoolean;
32+
import java.util.function.Function;
3233
import java.util.function.Supplier;
3334
import javax.annotation.Nonnull;
3435
import okio.ByteString;
@@ -50,7 +51,7 @@ public final class OpampClientImpl
5051
private final OpampClientState state;
5152
private final RecipeManager recipeManager;
5253
private final AtomicBoolean hasStopped = new AtomicBoolean(false);
53-
private final Callbacks callbacks;
54+
private volatile Callbacks callbacks = NOOP_CALLBACKS;
5455

5556
/** Fields that must always be sent. */
5657
private static final List<Field> REQUIRED_FIELDS;
@@ -82,7 +83,9 @@ public final class OpampClientImpl
8283
}
8384

8485
public static OpampClientImpl create(
85-
RequestService requestService, OpampClientState state, Callbacks callbacks) {
86+
RequestService requestService,
87+
OpampClientState state,
88+
Function<OpampClient, Callbacks> callbacksFunction) {
8689
AgentToServerAppenders appenders =
8790
new AgentToServerAppenders(
8891
AgentDescriptionAppender.create(state.agentDescription),
@@ -95,7 +98,8 @@ public static OpampClientImpl create(
9598
AgentDisconnectAppender.create());
9699
OpampClientImpl client =
97100
new OpampClientImpl(
98-
requestService, appenders, state, RecipeManager.create(REQUIRED_FIELDS), callbacks);
101+
requestService, appenders, state, RecipeManager.create(REQUIRED_FIELDS));
102+
client.callbacks = callbacksFunction.apply(client);
99103

100104
// Start
101105
requestService.start(client, client);
@@ -110,13 +114,11 @@ private OpampClientImpl(
110114
RequestService requestService,
111115
AgentToServerAppenders appenders,
112116
OpampClientState state,
113-
RecipeManager recipeManager,
114-
Callbacks callbacks) {
117+
RecipeManager recipeManager) {
115118
this.requestService = requestService;
116119
this.appenders = appenders;
117120
this.state = state;
118121
this.recipeManager = recipeManager;
119-
this.callbacks = callbacks;
120122
}
121123

122124
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private RecordedRequest initializeClient(ServerToAgent initialResponse) {
395395
enqueueServerToAgentResponse(initialResponse);
396396

397397
callbacks = spy(new TestCallbacks());
398-
client = OpampClientImpl.create(requestService, state, callbacks);
398+
client = OpampClientImpl.create(requestService, state, x -> callbacks);
399399

400400
return takeRequest();
401401
}

0 commit comments

Comments
 (0)