Skip to content

Commit 78c295b

Browse files
authored
semconv 1.27.0 update for deployment (#584)
* semconv 1.27.0 update for deployment * fix missing occurences * implement automatic fallback + test * provide new resource attribute when old is set * spotless
1 parent 3f9d1a7 commit 78c295b

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

custom/src/main/java/co/elastic/otel/ElasticAutoConfigurationCustomizerProvider.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
import co.elastic.otel.dynamicconfig.DynamicConfiguration;
2525
import co.elastic.otel.dynamicconfig.DynamicInstrumentation;
2626
import com.google.auto.service.AutoService;
27+
import io.opentelemetry.api.common.AttributeKey;
2728
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
2829
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
2930
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
31+
import io.opentelemetry.sdk.resources.Resource;
3032
import java.util.HashMap;
3133
import java.util.HashSet;
3234
import java.util.Map;
3335
import java.util.Set;
36+
import java.util.function.BiFunction;
3437

3538
@AutoService(AutoConfigurationCustomizerProvider.class)
3639
public class ElasticAutoConfigurationCustomizerProvider
@@ -52,6 +55,11 @@ public class ElasticAutoConfigurationCustomizerProvider
5255
static final String STACKTRACE_LEGACY2_DURATION =
5356
"elastic.otel.java.span.stacktrace.min.duration";
5457

58+
private static final AttributeKey<String> DEPLOYMENT_LEGACY =
59+
AttributeKey.stringKey("deployment.environment");
60+
private static final AttributeKey<String> DEPLOYMENT =
61+
AttributeKey.stringKey("deployment.environment.name");
62+
5563
@Override
5664
public void customize(AutoConfigurationCustomizer autoConfiguration) {
5765
autoConfiguration.addMetricExporterCustomizer(
@@ -72,6 +80,7 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7280
providerBuilder, DynamicConfiguration.UpdatableConfigurator.INSTANCE);
7381
return providerBuilder;
7482
});
83+
autoConfiguration.addResourceCustomizer(resourceProviders());
7584
}
7685

7786
static Map<String, String> propertiesCustomizer(ConfigProperties configProperties) {
@@ -85,6 +94,19 @@ static Map<String, String> propertiesCustomizer(ConfigProperties configPropertie
8594
return config;
8695
}
8796

97+
static BiFunction<Resource, ConfigProperties, Resource> resourceProviders() {
98+
return (resource, configProperties) -> {
99+
100+
// duplicate deprecated deployment.environment to deployment.environment.name as a convenience
101+
String deploymentLegacy = resource.getAttribute(DEPLOYMENT_LEGACY);
102+
if (deploymentLegacy != null && resource.getAttribute(DEPLOYMENT) == null) {
103+
resource = resource.toBuilder().put(DEPLOYMENT, deploymentLegacy).build();
104+
}
105+
106+
return resource;
107+
};
108+
}
109+
88110
private static void experimentalTelemetry(
89111
Map<String, String> config, ConfigProperties configProperties) {
90112
// enable experimental telemetry metrics by default if not explicitly disabled

custom/src/main/java/co/elastic/otel/config/LegacyConfigurations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private void addAllOptions() {
7070
"The Elastic [`service_version`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-service-version) option corresponds to setting the `service.version` key in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=service.version=1.2.3`.");
7171
addDocumentationOption(
7272
"environment",
73-
"The Elastic [`environment`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-environment) option corresponds to setting the `deployment.environment` key in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=deployment.environment=testing`.");
73+
"The Elastic [`environment`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-environment) option corresponds to setting the `deployment.environment.name` key in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=testing`.");
7474
addDocumentationOption(
7575
"global_labels",
7676
"The Elastic [`global_labels`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-global-labels) option corresponds to adding `key=value` comma separated pairs in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=alice=first,bob=second`. Such labels will result in labels.key=value attributes on the server, e.g. labels.alice=first");

custom/src/test/java/co/elastic/otel/ElasticAutoConfigurationCustomizerProviderTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
package co.elastic.otel;
2020

2121
import static co.elastic.otel.ElasticAutoConfigurationCustomizerProvider.propertiesCustomizer;
22-
import static org.assertj.core.api.Assertions.assertThat;
22+
import static co.elastic.otel.ElasticAutoConfigurationCustomizerProvider.resourceProviders;
23+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
2324

2425
import io.opentelemetry.javaagent.tooling.EmptyConfigProperties;
2526
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
27+
import io.opentelemetry.sdk.resources.Resource;
28+
import io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes;
2629
import java.util.HashMap;
2730
import java.util.Map;
2831
import org.junit.jupiter.api.Test;
@@ -84,4 +87,38 @@ void customizeMetricTemporalityPreference() {
8487
String value = config.get("otel.exporter.otlp.metrics.temporality.preference");
8588
assertThat(value).isEqualTo("LOWMEMORY");
8689
}
90+
91+
@Test
92+
void legacyDeploymentEnvironment() {
93+
Resource input =
94+
Resource.builder()
95+
.put(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT, "test")
96+
.put("other", "other")
97+
.build();
98+
Resource resource = resourceProviders().apply(input, null);
99+
100+
assertThat(resource.getAttributes())
101+
.hasSize(3)
102+
.describedAs("when legacy attribute set agent should send both")
103+
.containsEntry(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT, "test")
104+
.containsEntry(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME, "test")
105+
.containsEntry("other", "other");
106+
}
107+
108+
@Test
109+
void legacyDeploymentRemoveLegacyWhenBothSet() {
110+
Resource input =
111+
Resource.builder()
112+
.put(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT, "legacy")
113+
.put(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME, "new")
114+
.put("other", "other")
115+
.build();
116+
Resource resource = resourceProviders().apply(input, null);
117+
assertThat(resource.getAttributes())
118+
.hasSize(3)
119+
.describedAs("when both attributes are set, agent should send them as-is")
120+
.containsEntry(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT, "legacy")
121+
.containsEntry(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME, "new")
122+
.containsEntry("other", "other");
123+
}
87124
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.otel;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
import io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes;
24+
import org.junit.jupiter.api.Test;
25+
26+
public class SemconvTest {
27+
28+
@Test
29+
void checkIncubatingAttributes() {
30+
// Some of the incubating attributes are important, and we need to ensure that their definition
31+
// do not change or breaks. Doing a basic name check is the bare minimum we can do to easily
32+
// detect when something is getting deprecated or promoted to stable.
33+
34+
assertThat(DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME.getKey())
35+
.isEqualTo("deployment.environment.name");
36+
}
37+
}

docs/migrate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ This file is auto-generated. Please make changes in *Configuration.java (for exa
8383

8484
### `environment`
8585

86-
The Elastic [`environment`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-environment) option corresponds to setting the `deployment.environment` key in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=deployment.environment=testing`.
86+
The Elastic [`environment`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-environment) option corresponds to setting the `deployment.environment.name` key in [OTEL_RESOURCE_ATTRIBUTES](https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_resource_attributes). For example: `OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=testing`.
8787

8888
<!--
8989
This file is auto-generated. Please make changes in *Configuration.java (for example, CoreConfiguration.java) and execute ConfigurationExporter.

0 commit comments

Comments
 (0)