Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION;
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_NAME;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION;
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_INSTANCE_ID;
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
import static java.util.logging.Level.WARNING;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.opamp.client.OpampClient;
import io.opentelemetry.opamp.client.OpampClientBuilder;
Expand All @@ -30,7 +39,6 @@
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;
Expand All @@ -50,12 +58,11 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetr
}

Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
String serviceName = resource.getAttribute(ServiceAttributes.SERVICE_NAME);

String endpoint = config.getString(OP_AMP_ENDPOINT);
startOpampClient(
endpoint,
serviceName,
resource,
new OpampClient.Callbacks() {
@Override
public void onConnect(OpampClient opampClient) {}
Expand All @@ -77,16 +84,39 @@ public void onMessage(OpampClient opampClient, MessageData messageData) {}
}

static OpampClient startOpampClient(
String endpoint, String serviceName, OpampClient.Callbacks callbacks) {
String endpoint, Resource resource, 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);
}
addIdentifying(builder, resource, DEPLOYMENT_ENVIRONMENT_NAME);
addIdentifying(builder, resource, SERVICE_NAME);
addIdentifying(builder, resource, SERVICE_VERSION);
addIdentifying(builder, resource, SERVICE_NAMESPACE);
addIdentifying(builder, resource, SERVICE_INSTANCE_ID);

addNonIdentifying(builder, resource, OS_NAME);
addNonIdentifying(builder, resource, OS_TYPE);
addNonIdentifying(builder, resource, OS_VERSION);

return builder.build(callbacks);
}

static void addIdentifying(
OpampClientBuilder builder, Resource res, AttributeKey<String> resourceAttr) {
String attr = res.getAttribute(resourceAttr);
if (attr != null) {
builder.putIdentifyingAttribute(resourceAttr.getKey(), attr);
}
}

static void addNonIdentifying(
OpampClientBuilder builder, Resource res, AttributeKey<String> resourceAttr) {
String attr = res.getAttribute(resourceAttr);
if (attr != null) {
builder.putNonIdentifyingAttribute(resourceAttr.getKey(), attr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@

package com.splunk.opentelemetry.opamp;

import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION;
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_NAME;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE;
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION;
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.opamp.client.OpampClient;
import io.opentelemetry.opamp.client.internal.response.MessageData;
import io.opentelemetry.sdk.resources.Resource;
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 io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.RecordedRequest;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -33,6 +44,9 @@
import opamp.proto.AgentConfigFile;
import opamp.proto.AgentConfigMap;
import opamp.proto.AgentRemoteConfig;
import opamp.proto.AgentToServer;
import opamp.proto.AnyValue;
import opamp.proto.KeyValue;
import opamp.proto.ServerErrorResponse;
import opamp.proto.ServerToAgent;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -65,6 +79,22 @@ static void cleanUp() {
@Test
void testOpamp() throws Exception {
// given
Attributes attributes =
Attributes.of(
DEPLOYMENT_ENVIRONMENT_NAME,
"test-deployment-env",
SERVICE_NAME,
"test-service",
SERVICE_VERSION,
"test-ver",
SERVICE_NAMESPACE,
"test-ns")
.toBuilder()
.put(OS_NAME, "test-os-name")
.put(OS_TYPE, "test-os-type")
.put(OS_VERSION, "test-os-ver")
.build();
Resource resource = Resource.create(attributes);
Map<String, AgentConfigFile> configMap =
Collections.singletonMap(
"test-key",
Expand All @@ -82,7 +112,7 @@ void testOpamp() throws Exception {
OpampClient client =
OpampActivator.startOpampClient(
server.httpUri().toString(),
"test",
resource,
new OpampClient.Callbacks() {
@Override
public void onConnect(OpampClient opampClient) {}
Expand Down Expand Up @@ -113,5 +143,30 @@ public void onMessage(OpampClient opampClient, MessageData messageData) {
// then
assertThat(remoteConfig).isNotNull();
assertThat(remoteConfig.config.config_map.get("test-key").body.utf8()).isEqualTo("test-value");

RecordedRequest recordedRequest = server.takeRequest();
byte[] body = recordedRequest.request().content().array();
AgentToServer agentToServer = AgentToServer.ADAPTER.decode(body);
assertIdentifying(agentToServer, DEPLOYMENT_ENVIRONMENT_NAME, "test-deployment-env");
assertIdentifying(agentToServer, SERVICE_NAME, "test-service");
assertIdentifying(agentToServer, SERVICE_VERSION, "test-ver");
assertIdentifying(agentToServer, SERVICE_NAMESPACE, "test-ns");
assertNonIdentifying(agentToServer, OS_NAME, "test-os-name");
assertNonIdentifying(agentToServer, OS_TYPE, "test-os-type");
assertNonIdentifying(agentToServer, OS_VERSION, "test-os-ver");
}

private void assertIdentifying(
AgentToServer agentToServer, AttributeKey<String> attr, String expected) {
KeyValue kv =
new KeyValue(attr.getKey(), new AnyValue.Builder().string_value(expected).build());
assertThat(agentToServer.agent_description.identifying_attributes).contains(kv);
}

private void assertNonIdentifying(
AgentToServer agentToServer, AttributeKey<String> attr, String expected) {
KeyValue kv =
new KeyValue(attr.getKey(), new AnyValue.Builder().string_value(expected).build());
assertThat(agentToServer.agent_description.non_identifying_attributes).contains(kv);
}
}