Skip to content

Commit 1b3f6b0

Browse files
committed
enable more metrics
1 parent 414e6b4 commit 1b3f6b0

File tree

12 files changed

+467
-146
lines changed

12 files changed

+467
-146
lines changed

docs/instrumentation-list.yaml

Lines changed: 373 additions & 94 deletions
Large diffs are not rendered by default.

instrumentation-docs/collect.sh

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,25 @@ fi
1414

1515
readonly INSTRUMENTATIONS=(
1616
# <module path (colon-separated)> : <javaagent|library> : [ gradle-task-suffix ]
17-
# "alibaba-druid-1.0:javaagent:test"
18-
# "alibaba-druid-1.0:javaagent:testStableSemconv"
19-
# "apache-dbcp-2.0:javaagent:test"
20-
# "apache-dbcp-2.0:javaagent:testStableSemconv"
21-
# "apache-httpclient:apache-httpclient-5.0:javaagent:test"
22-
# "c3p0-0.9:javaagent:test"
23-
# "c3p0-0.9:javaagent:testStableSemconv"
24-
# "hikaricp-3.0:javaagent:test"
25-
# "hikaricp-3.0:javaagent:testStableSemconv"
26-
# "tomcat:tomcat-jdbc:javaagent:test"
27-
# "tomcat:tomcat-jdbc:javaagent:testStableSemconv"
28-
# "oracle-ucp-11.2:javaagent:test"
29-
# "oracle-ucp-11.2:javaagent:testStableSemconv"
17+
"alibaba-druid-1.0:javaagent:test"
18+
"alibaba-druid-1.0:javaagent:testStableSemconv"
19+
"apache-dbcp-2.0:javaagent:test"
20+
"apache-dbcp-2.0:javaagent:testStableSemconv"
21+
"apache-httpclient:apache-httpclient-5.0:javaagent:test"
22+
"c3p0-0.9:javaagent:test"
23+
"c3p0-0.9:javaagent:testStableSemconv"
24+
"clickhouse-client-0.5:javaagent:test"
25+
"clickhouse-client-0.5:javaagent:testStableSemconv"
26+
"hikaricp-3.0:javaagent:test"
27+
"hikaricp-3.0:javaagent:testStableSemconv"
28+
"tomcat:tomcat-jdbc:javaagent:test"
29+
"tomcat:tomcat-jdbc:javaagent:testStableSemconv"
30+
"oracle-ucp-11.2:javaagent:test"
31+
"oracle-ucp-11.2:javaagent:testStableSemconv"
3032
"oshi:javaagent:test"
3133
"oshi:javaagent:testExperimental"
32-
# "vibur-dbcp-11.0:javaagent:test"
34+
"vibur-dbcp-11.0:javaagent:test"
35+
"vibur-dbcp-11.0:javaagent:testStableSemconv"
3336
)
3437

3538
readonly TELEMETRY_DIR_NAME=".telemetry"

instrumentation-docs/readme.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ tasks {
2222
}
2323
```
2424

25+
Sometimes instrumentation will behave differently based on configuration options, and we can
26+
differentiate between these configurations by using the `metaDataConfig` system property. When the
27+
telemetry is written to a file, the value of this property will be included, or it will default to
28+
a `default` attribution.
29+
30+
For example, to collect and write metadata for the `otel.semconv-stability.opt-in=database` option
31+
set for an instrumentation:
32+
33+
```kotlin
34+
val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"
35+
36+
tasks {
37+
val testStableSemconv by registering(Test::class) {
38+
jvmArgs("-Dotel.semconv-stability.opt-in=database")
39+
40+
systemProperty("collectMetadata", collectMetadata)
41+
systemProperty("metaDataConfig", "otel.semconv-stability.opt-in=database")
42+
}
43+
44+
test {
45+
systemProperty("collectMetadata", collectMetadata)
46+
}
47+
48+
check {
49+
dependsOn(testStableSemconv)
50+
}
51+
}
52+
```
53+
2554
Then, prior to running the analyzer, run the following command to generate `.telemetry` files:
2655

2756
`./gradlew test -PcollectMetadata=true`
@@ -36,8 +65,6 @@ or use the helper script that will run only the currently supported tests (recom
3665
./instrumentation-docs/collect.sh
3766
```
3867

39-
This script will also clean up all `.telemetry` files after the analysis is done.
40-
4168
## Instrumentation Hierarchy
4269

4370
An "InstrumentationModule" represents a module that that targets specific code in a
@@ -101,7 +128,8 @@ public class SpringWebInstrumentationModule extends InstrumentationModule
101128
* List of settings that are available for the instrumentation module
102129
* Each setting has a name, description, type, and default value
103130
* metrics
104-
* List of metrics that the instrumentation module collects, including the metric name, description, type, and attributes
131+
* List of metrics that the instrumentation module collects, including the metric name, description, type, and attributes.
132+
* Separate lists for the metrics emitted by default vs via configuration options.
105133

106134
## Methodology
107135

@@ -150,3 +178,6 @@ generate the metrics section of the instrumentation-list.yaml file.
150178

151179
The data is written into a `.telemetry` directory in the root of each instrumentation module. This
152180
data will be excluded from git and just generated on demand.
181+
182+
Each file has a `when` value along with the list of metrics that indicates whether the telemetry is emitted by default or via a
183+
configuration option.

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/InstrumentationAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ List<InstrumentationModule> analyze() throws IOException {
100100
if (entry.getValue() == null || entry.getValue().getMetrics() == null) {
101101
continue;
102102
}
103-
module.getMetrics().putIfAbsent(entry.getKey(), entry.getValue().getMetrics());
103+
module.getMetrics().put(entry.getKey(), entry.getValue().getMetrics());
104104
}
105105
}
106106
}

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/utils/YamlHelper.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.BufferedWriter;
1818
import java.util.ArrayList;
1919
import java.util.Collections;
20+
import java.util.Comparator;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
@@ -141,8 +142,12 @@ private static Map<String, Object> baseProperties(InstrumentationModule module)
141142
List<EmittedMetrics.Metric> metrics =
142143
module.getMetrics().getOrDefault(group, Collections.emptyList());
143144
List<Map<String, Object>> metricsList = new ArrayList<>();
145+
146+
// sort metrics by name for some determinism in the order
147+
metrics.sort(Comparator.comparing(EmittedMetrics.Metric::getName));
148+
144149
for (EmittedMetrics.Metric metric : metrics) {
145-
metricsList.add(getObjectMap(metric));
150+
metricsList.add(getMetricsMap(metric));
146151
}
147152
telemetryEntry.put("metrics", metricsList);
148153
telemetryList.add(telemetryEntry);
@@ -216,27 +221,7 @@ private static Map<String, Object> configurationToMap(ConfigurationOption config
216221
return conf;
217222
}
218223

219-
// private static Map<String, List<Map<String, Object>>> getMetricsList(
220-
// InstrumentationModule module) {
221-
// Map<String, List<Map<String, Object>>> metricsMap = new LinkedHashMap<>();
222-
// if (module.getMetrics() == null) {
223-
// return metricsMap;
224-
// }
225-
//
226-
// for (Map.Entry<String, List<EmittedMetrics.Metric>> metricEntry :
227-
// module.getMetrics().entrySet()) {
228-
// List<Map<String, Object>> metricsList = new ArrayList<>();
229-
// for (EmittedMetrics.Metric metric : metricEntry.getValue()) {
230-
// Map<String, Object> innerMetricMap = getObjectMap(metric);
231-
// metricsList.add(innerMetricMap);
232-
// }
233-
// metricsMap.put(metricEntry.getKey(), metricsList);
234-
// }
235-
//
236-
// return metricsMap;
237-
// }
238-
239-
private static Map<String, Object> getObjectMap(EmittedMetrics.Metric metric) {
224+
private static Map<String, Object> getMetricsMap(EmittedMetrics.Metric metric) {
240225
Map<String, Object> innerMetricMap = new LinkedHashMap<>();
241226
innerMetricMap.put("name", metric.getName());
242227
innerMetricMap.put("description", metric.getDescription());

instrumentation/clickhouse-client-0.5/javaagent/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ dependencies {
2222
testLibrary("org.apache.httpcomponents.client5:httpclient5:5.2.3")
2323
}
2424

25+
val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"
26+
2527
tasks {
2628
test {
2729
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
30+
systemProperty("collectMetadata", collectMetadata)
2831
}
2932

3033
val testStableSemconv by registering(Test::class) {
3134
jvmArgs("-Dotel.semconv-stability.opt-in=database")
35+
systemProperty("collectMetadata", collectMetadata)
36+
systemProperty("metaDataConfig", "otel.semconv-stability.opt-in=database")
3237
}
3338

3439
check {

instrumentation/clickhouse-client-0.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55

66
package io.opentelemetry.javaagent.instrumentation.clickhouse;
77

8+
import static io.opentelemetry.instrumentation.testing.junit.db.DbClientMetricsTestUtil.assertDurationMetric;
89
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
910
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
1011
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
1112
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
1213
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
1314
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME;
15+
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAMESPACE;
1416
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
17+
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME;
1518
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_RESPONSE_STATUS_CODE;
1619
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
1720
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
21+
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM_NAME;
1822
import static java.util.Arrays.asList;
1923
import static org.assertj.core.api.Assertions.assertThat;
2024
import static org.assertj.core.api.Assertions.catchThrowable;
@@ -115,6 +119,15 @@ void testConnectionStringWithoutDatabaseSpecifiedStillGeneratesSpans()
115119
.hasNoParent()
116120
.hasAttributesSatisfyingExactly(
117121
attributeAssertions("select * from " + tableName, "SELECT"))));
122+
123+
assertDurationMetric(
124+
testing,
125+
"io.opentelemetry.clickhouse-client-0.5",
126+
DB_SYSTEM_NAME,
127+
DB_OPERATION_NAME,
128+
DB_NAMESPACE,
129+
SERVER_ADDRESS,
130+
SERVER_PORT);
118131
}
119132

120133
@Test

instrumentation/oshi/javaagent/build.gradle.kts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ dependencies {
2222
testImplementation(project(":instrumentation:oshi:testing"))
2323
}
2424

25-
val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"
26-
27-
2825
tasks {
29-
test {
30-
systemProperty("collectMetadata", collectMetadata)
26+
withType<Test>().configureEach {
27+
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
3128
}
3229

3330
val testExperimental by registering(Test::class) {
3431
jvmArgs("-Dotel.instrumentation.oshi.experimental-metrics.enabled=true")
35-
jvmArgs("-Dotel.semconv-stability.opt-in=database")
36-
37-
systemProperty("collectMetadata", collectMetadata)
38-
systemProperty("metaDataConfig", "otel.semconv-stability.opt-in=database")
32+
systemProperty("testExperimental", "true")
33+
systemProperty("metaDataConfig", "otel.instrumentation.oshi.experimental-metrics.enabled=true")
3934
}
4035

36+
check {
37+
dependsOn(testExperimental)
38+
}
4139
}

instrumentation/oshi/metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
description: When the OSHI library is detected on the classpath, this instrumentation will use the system class loader to load classes from the oshi-core jar that are then used to generate system metrics.
22
configurations:
33
- name: otel.instrumentation.oshi.experimental-metrics.enabled
4-
description: Enable the OSHI runtime metrics.
4+
description: Enable the OSHI process runtime metrics.
55
type: boolean
66
default: false

instrumentation/oshi/testing/src/main/java/io/opentelemetry/instrumentation/oshi/AbstractProcessMetricsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
1111
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
1213

1314
public abstract class AbstractProcessMetricsTest {
1415

@@ -17,6 +18,7 @@ public abstract class AbstractProcessMetricsTest {
1718
protected abstract InstrumentationExtension testing();
1819

1920
@Test
21+
@EnabledIfSystemProperty(named = "testExperimental", matches = "true")
2022
void test() {
2123
// when
2224
registerMetrics();

0 commit comments

Comments
 (0)