Skip to content

Commit aee0cbb

Browse files
committed
address review comments, add more resource attributes
1 parent f09202b commit aee0cbb

File tree

6 files changed

+131
-40
lines changed

6 files changed

+131
-40
lines changed

instrumentation/jdbc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ muzzle {
1313

1414
dependencies {
1515
compileOnly("io.opentelemetry:opentelemetry-api")
16+
compileOnly("io.opentelemetry.semconv:opentelemetry-semconv-incubating")
1617
compileOnly(project(":custom"))
1718

1819
testInstrumentation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-jdbc")

instrumentation/jdbc/src/main/java/com/splunk/opentelemetry/instrumentation/jdbc/PropagatorInitializer.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,33 @@
1616

1717
package com.splunk.opentelemetry.instrumentation.jdbc;
1818

19+
import static com.splunk.opentelemetry.instrumentation.jdbc.SqlCommenterInitializer.defaultPropagator;
20+
import static com.splunk.opentelemetry.instrumentation.jdbc.SqlCommenterInitializer.traceContextPropagator;
1921
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;
2022
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
23+
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
24+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
2125

2226
import com.google.auto.service.AutoService;
2327
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
24-
import io.opentelemetry.context.Context;
25-
import io.opentelemetry.context.propagation.TextMapGetter;
2628
import io.opentelemetry.context.propagation.TextMapPropagator;
27-
import io.opentelemetry.context.propagation.TextMapSetter;
2829
import io.opentelemetry.javaagent.extension.AgentListener;
2930
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
3031
import io.opentelemetry.sdk.resources.Resource;
31-
import java.util.Collection;
32-
import java.util.Collections;
3332

3433
@AutoService(AgentListener.class)
3534
public class PropagatorInitializer implements AgentListener {
36-
// propagates service.name attribute
37-
static TextMapPropagator defaultPropagator = TextMapPropagator.noop();
38-
// propagates service.name attribute and traceparent
39-
static TextMapPropagator traceContextPropagator = W3CTraceContextPropagator.getInstance();
4035

4136
@Override
4237
public void afterAgent(AutoConfiguredOpenTelemetrySdk sdk) {
4338
Resource resource = getResource(sdk);
4439
String serviceName = resource.getAttribute(SERVICE_NAME);
45-
if (!"unknown_service:java".equals(serviceName)) {
46-
defaultPropagator = new ServiceAttributePropagator(serviceName);
47-
traceContextPropagator =
48-
TextMapPropagator.composite(defaultPropagator, traceContextPropagator);
49-
}
50-
}
51-
52-
private static class ServiceAttributePropagator implements TextMapPropagator {
53-
private final String serviceName;
54-
55-
ServiceAttributePropagator(String serviceName) {
56-
this.serviceName = serviceName;
57-
}
58-
59-
@Override
60-
public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
61-
setter.set(carrier, SERVICE_NAME.getKey(), serviceName);
62-
}
63-
64-
@Override
65-
public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter) {
66-
return context;
67-
}
40+
String serviceNamespace = resource.getAttribute(SERVICE_NAMESPACE);
41+
String deploymentEnvironment = resource.getAttribute(DEPLOYMENT_ENVIRONMENT_NAME);
6842

69-
@Override
70-
public Collection<String> fields() {
71-
return Collections.emptyList();
72-
}
43+
defaultPropagator =
44+
new ServiceAttributePropagator(serviceName, serviceNamespace, deploymentEnvironment);
45+
traceContextPropagator =
46+
TextMapPropagator.composite(defaultPropagator, W3CTraceContextPropagator.getInstance());
7347
}
7448
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Splunk Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.splunk.opentelemetry.instrumentation.jdbc;
18+
19+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
20+
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
21+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
22+
23+
import io.opentelemetry.context.Context;
24+
import io.opentelemetry.context.propagation.TextMapGetter;
25+
import io.opentelemetry.context.propagation.TextMapPropagator;
26+
import io.opentelemetry.context.propagation.TextMapSetter;
27+
import java.util.Collection;
28+
import java.util.Collections;
29+
30+
class ServiceAttributePropagator implements TextMapPropagator {
31+
private final String serviceName;
32+
private final String serviceNamespace;
33+
private final String deploymentEnvironment;
34+
35+
ServiceAttributePropagator(
36+
String serviceName, String serviceNamespace, String deploymentEnvironment) {
37+
this.serviceName = serviceName;
38+
this.serviceNamespace = serviceNamespace;
39+
this.deploymentEnvironment = deploymentEnvironment;
40+
}
41+
42+
@Override
43+
public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) {
44+
setIfNotNull(setter, carrier, SERVICE_NAME.getKey(), serviceName);
45+
setIfNotNull(setter, carrier, SERVICE_NAMESPACE.getKey(), serviceNamespace);
46+
setIfNotNull(setter, carrier, DEPLOYMENT_ENVIRONMENT_NAME.getKey(), deploymentEnvironment);
47+
}
48+
49+
private static <C> void setIfNotNull(
50+
TextMapSetter<C> setter, C carrier, String key, String value) {
51+
if (value != null) {
52+
setter.set(carrier, key, value);
53+
}
54+
}
55+
56+
@Override
57+
public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter) {
58+
return context;
59+
}
60+
61+
@Override
62+
public Collection<String> fields() {
63+
return Collections.emptyList();
64+
}
65+
}

instrumentation/jdbc/src/main/java/com/splunk/opentelemetry/instrumentation/jdbc/SqlCommenterInitializer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
package com.splunk.opentelemetry.instrumentation.jdbc;
1818

1919
import com.google.auto.service.AutoService;
20+
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
21+
import io.opentelemetry.context.propagation.TextMapPropagator;
2022
import io.opentelemetry.instrumentation.api.incubator.semconv.db.internal.SqlCommenterBuilder;
2123
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
2224
import io.opentelemetry.javaagent.bootstrap.internal.sqlcommenter.SqlCommenterCustomizer;
2325

2426
@AutoService(SqlCommenterCustomizer.class)
2527
public class SqlCommenterInitializer implements SqlCommenterCustomizer {
28+
// propagates service.name and other static attribute
29+
static TextMapPropagator defaultPropagator = TextMapPropagator.noop();
30+
// propagates static attributes and traceparent
31+
static TextMapPropagator traceContextPropagator = W3CTraceContextPropagator.getInstance();
2632

2733
@Override
2834
public void customize(SqlCommenterBuilder sqlCommenterBuilder) {
@@ -34,12 +40,12 @@ public void customize(SqlCommenterBuilder sqlCommenterBuilder) {
3440
// for postgres we add traceparent to comments, oracle and mssql use other means to
3541
// propagate context and other databases are currently unsupported
3642
if (connection.getClass().getName().startsWith("org.postgresql.jdbc")) {
37-
return PropagatorInitializer.traceContextPropagator;
43+
return traceContextPropagator;
3844
}
3945

4046
// note that besides jdbc this applies to r2dbc and other data access apis that upstream
4147
// has sqlcommenter support for
42-
return PropagatorInitializer.defaultPropagator;
48+
return defaultPropagator;
4349
});
4450
}
4551
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Splunk Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.splunk.opentelemetry.instrumentation.jdbc;
18+
19+
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
20+
import static io.opentelemetry.semconv.incubating.DeploymentIncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME;
21+
import static io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes.SERVICE_NAMESPACE;
22+
import static java.util.Map.entry;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
import io.opentelemetry.context.Context;
26+
import io.opentelemetry.context.propagation.TextMapPropagator;
27+
import java.util.LinkedHashMap;
28+
import java.util.Map;
29+
import org.junit.jupiter.api.Test;
30+
31+
class ServiceAttributePropagatorTest {
32+
33+
@Test
34+
void testPropagation() {
35+
TextMapPropagator propagator =
36+
new ServiceAttributePropagator(
37+
"service-name", "service-namespace", "deployment-environment");
38+
Map<String, String> carrier = new LinkedHashMap<>();
39+
propagator.inject(Context.root(), carrier, (map, key, value) -> map.put(key, value));
40+
41+
assertThat(carrier)
42+
.containsExactly(
43+
entry(SERVICE_NAME.getKey(), "service-name"),
44+
entry(SERVICE_NAMESPACE.getKey(), "service-namespace"),
45+
entry(DEPLOYMENT_ENVIRONMENT_NAME.getKey(), "deployment-environment"));
46+
}
47+
}

instrumentation/jdbc/src/test/java/com/splunk/opentelemetry/instrumentation/jdbc/postgresql/PostgreSqlServerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.ArrayList;
3131
import java.util.List;
3232
import org.junit.jupiter.api.AfterAll;
33-
import org.junit.jupiter.api.AfterEach;
3433
import org.junit.jupiter.api.BeforeAll;
3534
import org.junit.jupiter.api.BeforeEach;
3635
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -73,7 +72,6 @@ static void cleanup() {
7372
}
7473

7574
@BeforeEach
76-
@AfterEach
7775
void cleanupTest() {
7876
executedSql.clear();
7977
}

0 commit comments

Comments
 (0)