Skip to content

Commit b5ceb7d

Browse files
authored
OpAMP client: Add additional identifying and recommended non-identifying res attrs (#2614)
* add additional identifying and recommended non-identifying resource attributes. * spotless * test resource attributes in agent description info * let's add deployment environment too, not because anybody asked for it yet, but because I think they will....
1 parent 085ecf7 commit b5ceb7d

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

custom/src/main/java/com/splunk/opentelemetry/opamp/OpampActivator.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818

1919
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
2020
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;
21+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
22+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION;
23+
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
24+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_NAME;
25+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE;
26+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION;
27+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_INSTANCE_ID;
28+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
2129
import static java.util.logging.Level.WARNING;
2230

2331
import com.google.auto.service.AutoService;
32+
import io.opentelemetry.api.common.AttributeKey;
2433
import io.opentelemetry.javaagent.extension.AgentListener;
2534
import io.opentelemetry.opamp.client.OpampClient;
2635
import io.opentelemetry.opamp.client.OpampClientBuilder;
@@ -30,7 +39,6 @@
3039
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
3140
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
3241
import io.opentelemetry.sdk.resources.Resource;
33-
import io.opentelemetry.semconv.ServiceAttributes;
3442
import java.util.logging.Logger;
3543
import opamp.proto.ServerErrorResponse;
3644
import org.jetbrains.annotations.Nullable;
@@ -50,12 +58,11 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetr
5058
}
5159

5260
Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
53-
String serviceName = resource.getAttribute(ServiceAttributes.SERVICE_NAME);
5461

5562
String endpoint = config.getString(OP_AMP_ENDPOINT);
5663
startOpampClient(
5764
endpoint,
58-
serviceName,
65+
resource,
5966
new OpampClient.Callbacks() {
6067
@Override
6168
public void onConnect(OpampClient opampClient) {}
@@ -77,16 +84,39 @@ public void onMessage(OpampClient opampClient, MessageData messageData) {}
7784
}
7885

7986
static OpampClient startOpampClient(
80-
String endpoint, String serviceName, OpampClient.Callbacks callbacks) {
87+
String endpoint, Resource resource, OpampClient.Callbacks callbacks) {
88+
8189
OpampClientBuilder builder = OpampClient.builder();
8290
builder.enableRemoteConfig();
8391
if (endpoint != null) {
8492
builder.setRequestService(HttpRequestService.create(OkHttpSender.create(endpoint)));
8593
}
86-
if (serviceName != null) {
87-
builder.putIdentifyingAttribute("service.name", serviceName);
88-
}
94+
addIdentifying(builder, resource, DEPLOYMENT_ENVIRONMENT_NAME);
95+
addIdentifying(builder, resource, SERVICE_NAME);
96+
addIdentifying(builder, resource, SERVICE_VERSION);
97+
addIdentifying(builder, resource, SERVICE_NAMESPACE);
98+
addIdentifying(builder, resource, SERVICE_INSTANCE_ID);
99+
100+
addNonIdentifying(builder, resource, OS_NAME);
101+
addNonIdentifying(builder, resource, OS_TYPE);
102+
addNonIdentifying(builder, resource, OS_VERSION);
89103

90104
return builder.build(callbacks);
91105
}
106+
107+
static void addIdentifying(
108+
OpampClientBuilder builder, Resource res, AttributeKey<String> resourceAttr) {
109+
String attr = res.getAttribute(resourceAttr);
110+
if (attr != null) {
111+
builder.putIdentifyingAttribute(resourceAttr.getKey(), attr);
112+
}
113+
}
114+
115+
static void addNonIdentifying(
116+
OpampClientBuilder builder, Resource res, AttributeKey<String> resourceAttr) {
117+
String attr = res.getAttribute(resourceAttr);
118+
if (attr != null) {
119+
builder.putNonIdentifyingAttribute(resourceAttr.getKey(), attr);
120+
}
121+
}
92122
}

custom/src/test/java/com/splunk/opentelemetry/opamp/OpampActivatorTest.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@
1616

1717
package com.splunk.opentelemetry.opamp;
1818

19+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
20+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION;
21+
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
22+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_NAME;
23+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE;
24+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION;
25+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
1926
import static org.assertj.core.api.Assertions.assertThat;
2027

28+
import io.opentelemetry.api.common.AttributeKey;
29+
import io.opentelemetry.api.common.Attributes;
2130
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
2231
import io.opentelemetry.opamp.client.OpampClient;
2332
import io.opentelemetry.opamp.client.internal.response.MessageData;
33+
import io.opentelemetry.sdk.resources.Resource;
2434
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
2535
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
2636
import io.opentelemetry.testing.internal.armeria.common.MediaType;
2737
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension;
38+
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.RecordedRequest;
2839
import java.util.Collections;
2940
import java.util.Map;
3041
import java.util.concurrent.CompletableFuture;
@@ -33,6 +44,9 @@
3344
import opamp.proto.AgentConfigFile;
3445
import opamp.proto.AgentConfigMap;
3546
import opamp.proto.AgentRemoteConfig;
47+
import opamp.proto.AgentToServer;
48+
import opamp.proto.AnyValue;
49+
import opamp.proto.KeyValue;
3650
import opamp.proto.ServerErrorResponse;
3751
import opamp.proto.ServerToAgent;
3852
import org.jetbrains.annotations.Nullable;
@@ -65,6 +79,22 @@ static void cleanUp() {
6579
@Test
6680
void testOpamp() throws Exception {
6781
// given
82+
Attributes attributes =
83+
Attributes.of(
84+
DEPLOYMENT_ENVIRONMENT_NAME,
85+
"test-deployment-env",
86+
SERVICE_NAME,
87+
"test-service",
88+
SERVICE_VERSION,
89+
"test-ver",
90+
SERVICE_NAMESPACE,
91+
"test-ns")
92+
.toBuilder()
93+
.put(OS_NAME, "test-os-name")
94+
.put(OS_TYPE, "test-os-type")
95+
.put(OS_VERSION, "test-os-ver")
96+
.build();
97+
Resource resource = Resource.create(attributes);
6898
Map<String, AgentConfigFile> configMap =
6999
Collections.singletonMap(
70100
"test-key",
@@ -82,7 +112,7 @@ void testOpamp() throws Exception {
82112
OpampClient client =
83113
OpampActivator.startOpampClient(
84114
server.httpUri().toString(),
85-
"test",
115+
resource,
86116
new OpampClient.Callbacks() {
87117
@Override
88118
public void onConnect(OpampClient opampClient) {}
@@ -113,5 +143,30 @@ public void onMessage(OpampClient opampClient, MessageData messageData) {
113143
// then
114144
assertThat(remoteConfig).isNotNull();
115145
assertThat(remoteConfig.config.config_map.get("test-key").body.utf8()).isEqualTo("test-value");
146+
147+
RecordedRequest recordedRequest = server.takeRequest();
148+
byte[] body = recordedRequest.request().content().array();
149+
AgentToServer agentToServer = AgentToServer.ADAPTER.decode(body);
150+
assertIdentifying(agentToServer, DEPLOYMENT_ENVIRONMENT_NAME, "test-deployment-env");
151+
assertIdentifying(agentToServer, SERVICE_NAME, "test-service");
152+
assertIdentifying(agentToServer, SERVICE_VERSION, "test-ver");
153+
assertIdentifying(agentToServer, SERVICE_NAMESPACE, "test-ns");
154+
assertNonIdentifying(agentToServer, OS_NAME, "test-os-name");
155+
assertNonIdentifying(agentToServer, OS_TYPE, "test-os-type");
156+
assertNonIdentifying(agentToServer, OS_VERSION, "test-os-ver");
157+
}
158+
159+
private void assertIdentifying(
160+
AgentToServer agentToServer, AttributeKey<String> attr, String expected) {
161+
KeyValue kv =
162+
new KeyValue(attr.getKey(), new AnyValue.Builder().string_value(expected).build());
163+
assertThat(agentToServer.agent_description.identifying_attributes).contains(kv);
164+
}
165+
166+
private void assertNonIdentifying(
167+
AgentToServer agentToServer, AttributeKey<String> attr, String expected) {
168+
KeyValue kv =
169+
new KeyValue(attr.getKey(), new AnyValue.Builder().string_value(expected).build());
170+
assertThat(agentToServer.agent_description.non_identifying_attributes).contains(kv);
116171
}
117172
}

0 commit comments

Comments
 (0)