Skip to content

Commit 84ba230

Browse files
committed
assert on resource attributes
1 parent ca2f395 commit 84ba230

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

smoke-tests/src/test/java/com/example/javaagent/smoketest/DeclarativeConfigSmokeTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
*/
1919
package com.example.javaagent.smoketest;
2020

21+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_INSTANCE_ID;
22+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
23+
import static io.opentelemetry.semconv.incubating.ContainerIncubatingAttributes.CONTAINER_ID;
24+
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_ARCH;
25+
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_NAME;
26+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE;
27+
import static io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION;
28+
import static io.opentelemetry.semconv.incubating.ProcessIncubatingAttributes.PROCESS_COMMAND_ARGS;
29+
import static io.opentelemetry.semconv.incubating.ProcessIncubatingAttributes.PROCESS_PID;
30+
import static io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes.TELEMETRY_DISTRO_NAME;
31+
import static io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes.TELEMETRY_DISTRO_VERSION;
2132
import static org.assertj.core.api.Assertions.assertThat;
2233
import static org.assertj.core.api.Assertions.tuple;
2334

@@ -82,20 +93,41 @@ static void start() throws IOException {
8293
container
8394
.withCopyFileToContainer(MountableFile.forHostPath(configFile), "/config.yaml")
8495
.withEnv("OTEL_CONFIG_FILE", "/config.yaml")
85-
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", BACKEND_ENDPOINT),
96+
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", BACKEND_ENDPOINT)
97+
.withEnv("OTEL_SERVICE_NAME", "my-service-name"),
8698
false);
8799
}
88100

89101
@Test
90-
void healthcheck() {
102+
void resourceAttributes() {
91103
doRequest(getUrl("/health"), okResponseBody("Alive!"));
92104

105+
// This does not test that cloud resource providers are included in the config
106+
// but at least we know that if they are added in the config their names are valid as otherwise
107+
// the agent does not start with any missing component in config.
108+
93109
List<ExportTraceServiceRequest> traces = waitForTraces();
94110
List<Span> spans = getSpans(traces).toList();
95111
assertThat(spans)
96112
.hasSize(1)
97113
.extracting("name", "kind")
98114
.containsOnly(tuple("GET /health", Span.SpanKind.SPAN_KIND_SERVER));
115+
116+
assertThat(getResourceAttributes(traces))
117+
.containsKey(HOST_NAME.getKey())
118+
.containsKey(HOST_ARCH.getKey())
119+
.containsKey(OS_TYPE.getKey())
120+
.containsKey(OS_VERSION.getKey())
121+
.containsKey(PROCESS_PID.getKey())
122+
.containsKey(PROCESS_COMMAND_ARGS.getKey())
123+
.containsKey(CONTAINER_ID.getKey())
124+
// service name should be provided by the 'service' resource attribute detector which
125+
// reads environment variables (here it's not env variable injected in declarative config).
126+
.containsEntry(SERVICE_NAME.getKey(), attributeValue("my-service-name"))
127+
.containsKey(SERVICE_INSTANCE_ID.getKey())
128+
// ensures that we override the default distro resource attributes
129+
.containsEntry(TELEMETRY_DISTRO_NAME.getKey(), attributeValue("elastic"))
130+
.containsKey(TELEMETRY_DISTRO_VERSION.getKey());
99131
}
100132

101133
@AfterAll

smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ protected Stream<Span> getSpans(List<ExportTraceServiceRequest> traces) {
384384
.flatMap(it -> it.getSpansList().stream());
385385
}
386386

387+
protected Map<String, AnyValue> getResourceAttributes(List<ExportTraceServiceRequest> traces) {
388+
return traces.stream()
389+
.flatMap(it -> it.getResourceSpansList().stream())
390+
.flatMap(it -> it.getResource().getAttributesList().stream())
391+
.collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
392+
}
393+
387394
protected static String bytesToHex(byte[] bytes) {
388395
StringBuilder sb = new StringBuilder();
389396
for (int i = 0; i < bytes.length; i++) {

0 commit comments

Comments
 (0)