-
Notifications
You must be signed in to change notification settings - Fork 45
Add opamp client #2513
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
Merged
Add opamp client #2513
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
91 changes: 91 additions & 0 deletions
91
custom/src/main/java/com/splunk/opentelemetry/opamp/OpampActivator.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,91 @@ | ||
| /* | ||
| * Copyright Splunk Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.splunk.opentelemetry.opamp; | ||
|
|
||
| import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig; | ||
| import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource; | ||
| import static java.util.logging.Level.WARNING; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.extension.AgentListener; | ||
| import io.opentelemetry.opamp.client.internal.OpampClient; | ||
| import io.opentelemetry.opamp.client.internal.OpampClientBuilder; | ||
| import io.opentelemetry.opamp.client.internal.connectivity.http.OkHttpSender; | ||
| import io.opentelemetry.opamp.client.internal.request.service.HttpRequestService; | ||
| import io.opentelemetry.opamp.client.internal.response.MessageData; | ||
| import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import io.opentelemetry.semconv.ServiceAttributes; | ||
| import java.util.logging.Logger; | ||
| import opamp.proto.ServerErrorResponse; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @AutoService(AgentListener.class) | ||
| public class OpampActivator implements AgentListener { | ||
| private static final Logger logger = Logger.getLogger(OpampActivator.class.getName()); | ||
|
|
||
| private static final String OP_AMP_ENABLED_PROPERTY = "splunk.opamp.enabled"; | ||
| private static final String OP_AMP_ENDPOINT = "splunk.opamp.endpoint"; | ||
|
|
||
| @Override | ||
| public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) { | ||
| ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk); | ||
| if (!config.getBoolean(OP_AMP_ENABLED_PROPERTY, false)) { | ||
| return; | ||
| } | ||
|
|
||
| Resource resource = getResource(autoConfiguredOpenTelemetrySdk); | ||
| String serviceName = resource.getAttribute(ServiceAttributes.SERVICE_NAME); | ||
|
|
||
| String endpoint = config.getString(OP_AMP_ENDPOINT); | ||
| startOpampClient( | ||
| endpoint, | ||
| serviceName, | ||
| new OpampClient.Callbacks() { | ||
| @Override | ||
| public void onConnect() {} | ||
|
|
||
| @Override | ||
| public void onConnectFailed(@Nullable Throwable throwable) { | ||
| logger.log(WARNING, "Connection to OpAMP server failed", throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onErrorResponse(ServerErrorResponse errorResponse) { | ||
| logger.log(WARNING, "OpAMP server returned error " + errorResponse); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(MessageData messageData) {} | ||
| }); | ||
| } | ||
|
|
||
| static OpampClient startOpampClient( | ||
| String endpoint, String serviceName, OpampClient.Callbacks callbacks) { | ||
| OpampClientBuilder builder = OpampClient.builder(); | ||
| builder.enableRemoteConfig(); | ||
| if (endpoint != null) { | ||
| builder.setRequestService(HttpRequestService.create(OkHttpSender.create(endpoint))); | ||
| } | ||
| if (serviceName != null) { | ||
| builder.putIdentifyingAttribute("service.name", serviceName); | ||
| } | ||
|
|
||
| return builder.build(callbacks); | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
custom/src/test/java/com/splunk/opentelemetry/opamp/OpampActivatorTest.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,116 @@ | ||
| /* | ||
| * Copyright Splunk Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.splunk.opentelemetry.opamp; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; | ||
| import io.opentelemetry.opamp.client.internal.OpampClient; | ||
| import io.opentelemetry.opamp.client.internal.response.MessageData; | ||
| import io.opentelemetry.testing.internal.armeria.common.HttpResponse; | ||
| import io.opentelemetry.testing.internal.armeria.common.HttpStatus; | ||
| import io.opentelemetry.testing.internal.armeria.common.MediaType; | ||
| import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
| import okio.ByteString; | ||
| import opamp.proto.AgentConfigFile; | ||
| import opamp.proto.AgentConfigMap; | ||
| import opamp.proto.AgentRemoteConfig; | ||
| import opamp.proto.ServerErrorResponse; | ||
| import opamp.proto.ServerToAgent; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class OpampActivatorTest { | ||
| private static final MockWebServerExtension server = new MockWebServerExtension(); | ||
|
|
||
| @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); | ||
|
|
||
| @BeforeAll | ||
| static void setUp() { | ||
| server.start(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void reset() { | ||
| server.beforeTestExecution(null); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void cleanUp() { | ||
| server.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void testOpamp() throws Exception { | ||
| // given | ||
| Map<String, AgentConfigFile> configMap = | ||
| Collections.singletonMap( | ||
| "test-key", | ||
| new AgentConfigFile.Builder().body(ByteString.encodeUtf8("test-value")).build()); | ||
| ServerToAgent response = | ||
| new ServerToAgent.Builder() | ||
| .remote_config( | ||
| new AgentRemoteConfig.Builder() | ||
| .config(new AgentConfigMap.Builder().config_map(configMap).build()) | ||
| .build()) | ||
| .build(); | ||
| server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.X_PROTOBUF, response.encode())); | ||
|
|
||
| CompletableFuture<MessageData> result = new CompletableFuture<>(); | ||
| OpampClient client = | ||
| OpampActivator.startOpampClient( | ||
| server.httpUri().toString(), | ||
| "test", | ||
| new OpampClient.Callbacks() { | ||
| @Override | ||
| public void onConnect() {} | ||
|
|
||
| @Override | ||
| public void onConnectFailed(@Nullable Throwable throwable) { | ||
| result.completeExceptionally(throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onErrorResponse(ServerErrorResponse serverErrorResponse) { | ||
| result.completeExceptionally( | ||
| new IllegalStateException(serverErrorResponse.toString())); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(MessageData messageData) { | ||
| result.complete(messageData); | ||
| } | ||
| }); | ||
| cleanup.deferCleanup(client); | ||
|
|
||
| // when | ||
| MessageData message = result.get(5, TimeUnit.SECONDS); | ||
| AgentRemoteConfig remoteConfig = message.getRemoteConfig(); | ||
|
|
||
| // then | ||
| assertThat(remoteConfig).isNotNull(); | ||
| assertThat(remoteConfig.config.config_map.get("test-key").body.utf8()).isEqualTo("test-value"); | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.