Skip to content

Commit c886e85

Browse files
committed
fix span kinds
1 parent 26e6903 commit c886e85

File tree

13 files changed

+151
-75
lines changed

13 files changed

+151
-75
lines changed

docs/instrumentation-list.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ apache:
9898
javaagent:
9999
- commons-httpclient:commons-httpclient:[2.0,4.0)
100100
span_data:
101+
span_kinds:
102+
- SERVER
103+
- CLIENT
104+
- INTERNAL
101105
attributes:
102106
- name: server.address
103107
type: STRING
@@ -306,6 +310,9 @@ clickhouse:
306310
- name: server.port
307311
type: LONG
308312
span_data:
313+
span_kinds:
314+
- CLIENT
315+
- INTERNAL
309316
attributes:
310317
- name: server.address
311318
type: STRING

instrumentation-docs/collect.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
instrumentations=(
4+
"apache-httpclient:apache-httpclient-2.0:javaagent:test"
5+
"alibaba-druid-1.0:javaagent:test"
6+
"clickhouse-client-0.5:javaagent:testStableSemconv"
7+
)
8+
9+
# Initialize an empty string to hold the Gradle tasks
10+
gradle_tasks=""
11+
12+
# Iterate over each instrumentation
13+
for instrumentation in "${instrumentations[@]}"; do
14+
# Extract the parts of the instrumentation
15+
IFS=':' read -r -a parts <<< "$instrumentation"
16+
module="${parts[0]}"
17+
version="${parts[1]}"
18+
type="${parts[2]}"
19+
suffix="${parts[3]}"
20+
21+
# Assemble the path to the instrumentation
22+
path="instrumentation/$module/$version"
23+
24+
# Remove any occurrence of /javaagent/ or /library/ from the path
25+
path=$(echo "$path" | sed -e 's/\/javaagent//g' -e 's/\/library//g')
26+
27+
# Debugging: Print the path being checked
28+
echo "Checking path: $path/.telemetry"
29+
30+
# Check if the .telemetry directory exists and remove it if it does
31+
if [ -d "$path/.telemetry" ]; then
32+
echo "Removing directory: $path/.telemetry"
33+
rm -rf "$path/.telemetry"
34+
else
35+
echo "Directory does not exist: $path/.telemetry"
36+
fi
37+
38+
# Append the Gradle task to the gradle_tasks string with a colon between type and suffix if suffix is non-empty
39+
if [ -n "$suffix" ]; then
40+
gradle_tasks+=":instrumentation:$module:$version:$type:$suffix "
41+
else
42+
gradle_tasks+=":instrumentation:$module:$version:$type "
43+
fi
44+
done
45+
46+
# rerun-tasks is used to force re-running tests that might be cached
47+
echo Running: ./gradlew $gradle_tasks -PcollectMetadata=true --rerun-tasks
48+
./gradlew $gradle_tasks -PcollectMetadata=true --rerun-tasks
49+
./gradlew :instrumentation-docs:generateDocs

instrumentation-docs/readme.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ Prior to running the DocGeneratorApplication, run the following command to colle
2121
* Run tests to collect metadata
2222
* `./gradlew test -collectMetadata=true`
2323
* Run the doc generator
24-
* `./gradlew :instrumentation-docs`
24+
* `./gradlew :instrumentation-docs:generateDocs`
25+
26+
or use the helper script that will run only the currently supported tests:
27+
28+
```bash
29+
./instrumentation-docs/collect.sh
30+
```
2531

2632
## Instrumentation Hierarchy
2733

@@ -76,6 +82,7 @@ public class SpringWebInstrumentationModule extends InstrumentationModule
7682
* metrics
7783
* List of metrics that the instrumentation module collects, including the metric name, description, type, and attributes
7884
* span_data
85+
* List of span types that are emitted (internal, client, server)
7986
* List of span data that the instrumentation collects, including the attribute name and type
8087

8188
## Methodology

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

+50-42
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@
2222

2323
public class YamlHelper {
2424

25-
public static EmittedScope emittedScopeParser(String input) {
26-
return new Yaml().loadAs(input, EmittedScope.class);
27-
}
28-
29-
public static EmittedMetrics emittedMetricsParser(String input) {
30-
return new Yaml().loadAs(input, EmittedMetrics.class);
31-
}
32-
33-
public static EmittedSpans emittedSpansParser(String input) {
34-
return new Yaml().loadAs(input, EmittedSpans.class);
35-
}
36-
3725
public static void printInstrumentationList(
3826
List<InstrumentationEntity> list, BufferedWriter writer) {
3927
Map<String, List<InstrumentationEntity>> groupedByGroup =
@@ -58,22 +46,7 @@ public static void printInstrumentationList(
5846
entityMap.put("srcPath", entity.getSrcPath());
5947

6048
if (entity.getScope() != null) {
61-
Map<String, Object> scopeMap = new LinkedHashMap<>();
62-
scopeMap.put("name", entity.getScope().getName());
63-
scopeMap.put("version", entity.getScope().getVersion());
64-
scopeMap.put("schemaUrl", entity.getScope().getSchemaUrl());
65-
66-
if (entity.getScope().getAttributes() != null
67-
&& !entity.getScope().getAttributes().isEmpty()) {
68-
69-
Map<String, Object> attributesMap = new LinkedHashMap<>();
70-
entity
71-
.getScope()
72-
.getAttributes()
73-
.forEach((key, value) -> attributesMap.put(String.valueOf(key), value));
74-
scopeMap.put("attributes", attributesMap);
75-
}
76-
49+
Map<String, Object> scopeMap = getScopeMap(entity);
7750
entityMap.put("scope", scopeMap);
7851
}
7952

@@ -92,20 +65,7 @@ public static void printInstrumentationList(
9265
entityMap.put("metrics", metricsList);
9366
}
9467

95-
Map<String, Object> spanDataMap = new LinkedHashMap<>();
96-
if (entity.getSpanKinds() != null && !entity.getSpanKinds().isEmpty()) {
97-
spanDataMap.put("span_kinds", entity.getSpanKinds());
98-
}
99-
if (entity.getSpanAttributes() != null && !entity.getSpanAttributes().isEmpty()) {
100-
List<Map<String, Object>> attributesList = new ArrayList<>();
101-
for (EmittedSpans.EmittedSpanAttribute attribute : entity.getSpanAttributes()) {
102-
Map<String, Object> attributeMap = new LinkedHashMap<>();
103-
attributeMap.put("name", attribute.getName());
104-
attributeMap.put("type", attribute.getType());
105-
attributesList.add(attributeMap);
106-
}
107-
spanDataMap.put("attributes", attributesList);
108-
}
68+
Map<String, Object> spanDataMap = getSpanDataMap(entity);
10969
if (!spanDataMap.isEmpty()) {
11070
entityMap.put("span_data", spanDataMap);
11171
}
@@ -123,6 +83,42 @@ public static void printInstrumentationList(
12383
yaml.dump(output, writer);
12484
}
12585

86+
private static Map<String, Object> getSpanDataMap(InstrumentationEntity entity) {
87+
Map<String, Object> spanDataMap = new LinkedHashMap<>();
88+
if (entity.getSpanKinds() != null && !entity.getSpanKinds().isEmpty()) {
89+
spanDataMap.put("span_kinds", entity.getSpanKinds());
90+
}
91+
if (entity.getSpanAttributes() != null && !entity.getSpanAttributes().isEmpty()) {
92+
List<Map<String, Object>> attributesList = new ArrayList<>();
93+
for (EmittedSpans.EmittedSpanAttribute attribute : entity.getSpanAttributes()) {
94+
Map<String, Object> attributeMap = new LinkedHashMap<>();
95+
attributeMap.put("name", attribute.getName());
96+
attributeMap.put("type", attribute.getType());
97+
attributesList.add(attributeMap);
98+
}
99+
spanDataMap.put("attributes", attributesList);
100+
}
101+
return spanDataMap;
102+
}
103+
104+
private static Map<String, Object> getScopeMap(InstrumentationEntity entity) {
105+
Map<String, Object> scopeMap = new LinkedHashMap<>();
106+
scopeMap.put("name", entity.getScope().getName());
107+
scopeMap.put("version", entity.getScope().getVersion());
108+
scopeMap.put("schemaUrl", entity.getScope().getSchemaUrl());
109+
110+
if (entity.getScope().getAttributes() != null && !entity.getScope().getAttributes().isEmpty()) {
111+
112+
Map<String, Object> attributesMap = new LinkedHashMap<>();
113+
entity
114+
.getScope()
115+
.getAttributes()
116+
.forEach((key, value) -> attributesMap.put(String.valueOf(key), value));
117+
scopeMap.put("attributes", attributesMap);
118+
}
119+
return scopeMap;
120+
}
121+
126122
private static List<Map<String, Object>> getMetricsList(InstrumentationEntity entity) {
127123
List<Map<String, Object>> metricsList = new ArrayList<>();
128124
for (EmittedMetrics.Metric metric : entity.getMetrics()) {
@@ -149,5 +145,17 @@ public static InstrumentationMetaData metaDataParser(String input) {
149145
return new Yaml().loadAs(input, InstrumentationMetaData.class);
150146
}
151147

148+
public static EmittedScope emittedScopeParser(String input) {
149+
return new Yaml().loadAs(input, EmittedScope.class);
150+
}
151+
152+
public static EmittedMetrics emittedMetricsParser(String input) {
153+
return new Yaml().loadAs(input, EmittedMetrics.class);
154+
}
155+
156+
public static EmittedSpans emittedSpansParser(String input) {
157+
return new Yaml().loadAs(input, EmittedSpans.class);
158+
}
159+
152160
private YamlHelper() {}
153161
}

instrumentation-docs/src/test/java/io/opentelemetry/instrumentation/docs/utils/YamlHelperTest.java

+23-25
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99
import static org.junit.jupiter.api.Assertions.assertNotNull;
1010

11+
import io.opentelemetry.api.trace.SpanKind;
1112
import io.opentelemetry.instrumentation.docs.internal.EmittedMetrics;
1213
import io.opentelemetry.instrumentation.docs.internal.EmittedScope;
1314
import io.opentelemetry.instrumentation.docs.internal.EmittedSpans;
1415
import io.opentelemetry.instrumentation.docs.internal.InstrumentationEntity;
1516
import io.opentelemetry.instrumentation.docs.internal.InstrumentationMetaData;
1617
import io.opentelemetry.instrumentation.docs.internal.InstrumentationType;
1718
import java.io.BufferedWriter;
18-
import java.io.IOException;
1919
import java.io.StringWriter;
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
@@ -147,7 +147,7 @@ void testPrintInstrumentationList() throws Exception {
147147
}
148148

149149
@Test
150-
public void testEmittedScopeParser() throws IOException {
150+
public void testEmittedScopeParser() {
151151
String yamlContent =
152152
"""
153153
scope:
@@ -166,35 +166,33 @@ public void testEmittedScopeParser() throws IOException {
166166
}
167167

168168
@Test
169-
public void testEmittedSpanParser() throws IOException {
169+
public void testEmittedSpanParser() {
170170
String yamlContent =
171171
"""
172-
attributes:
173-
- name: server.address
174-
type: STRING
175-
- name: http.response.header.x-test-response
176-
type: STRING_ARRAY
177-
- name: error.type
178-
type: STRING
179-
- name: test.request.id
180-
type: LONG
181-
- name: http.request.method
182-
type: STRING
183-
- name: network.protocol.version
184-
type: STRING
185-
- name: server.port
186-
type: LONG
187-
- name: http.request.header.x-test-request
188-
type: STRING_ARRAY
189-
- name: http.response.status_code
190-
type: LONG
191-
- name: url.full
192-
type: STRING
172+
spanKinds:
173+
- INTERNAL
174+
- CLIENT
175+
attributes:
176+
- name: server.address
177+
type: STRING
178+
- name: server.port
179+
type: LONG
180+
- name: db.system.name
181+
type: STRING
182+
- name: db.query.text
183+
type: STRING
184+
- name: db.namespace
185+
type: STRING
186+
- name: db.operation.name
187+
type: STRING
193188
""";
194189

195190
EmittedSpans emittedSpans = YamlHelper.emittedSpansParser(yamlContent);
196191

197192
assertNotNull(emittedSpans.getAttributes());
198-
assertThat(emittedSpans.getAttributes()).hasSize(10);
193+
assertThat(emittedSpans.getAttributes()).hasSize(6);
194+
assertThat(emittedSpans.getSpanKinds()).hasSize(2);
195+
assertThat(emittedSpans.getSpanKinds())
196+
.containsExactlyInAnyOrder(SpanKind.CLIENT.toString(), SpanKind.INTERNAL.toString());
199197
}
200198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spanKinds:

instrumentation/apache-httpclient/apache-httpclient-2.0/.telemetry/spans-d85d6e17-2eed-4f98-8f94-9c8e146f1b48.yaml instrumentation/apache-httpclient/apache-httpclient-2.0/.telemetry/spans-e69afd2a-3c98-4f09-96cb-ca3ae39d8b05.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
spanKinds:
2+
- SERVER
3+
- CLIENT
4+
- INTERNAL
15
attributes:
26
- name: server.address
37
type: STRING

instrumentation/clickhouse-client-0.5/.telemetry/spans-d57ec65f-fd91-4642-aa49-f7e314235b62.yaml instrumentation/clickhouse-client-0.5/.telemetry/spans-c41c140d-ee68-4266-8340-6797868907fa.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
spanKinds:
2+
- CLIENT
3+
- INTERNAL
14
attributes:
25
- name: server.address
36
type: STRING

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/AgentTestRunner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public void afterTestClass() throws IOException {
6464
int muzzleFailureCount = TestAgentListenerAccess.getAndResetMuzzleFailureCount();
6565
assert muzzleFailureCount == 0 : muzzleFailureCount + " Muzzle failures during test";
6666

67-
// Generates emitted_telemetry.yaml file with all emitted telemetry to be used
68-
// by the instrumentation-docs Doc generator.
67+
// Generates files in a `.telemetry` directory within the instrumentation module with all
68+
// captured emitted metadata to be used by the instrumentation-docs Doc generator.
6969
if (Boolean.getBoolean("collectMetadata")) {
7070
URL resource = this.getClass().getClassLoader().getResource("");
7171
if (resource == null) {

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/InstrumentationTestRunner.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Collection;
2828
import java.util.Comparator;
2929
import java.util.HashMap;
30+
import java.util.HashSet;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Set;
@@ -51,7 +52,7 @@ public abstract class InstrumentationTestRunner {
5152
private final TestInstrumenters testInstrumenters;
5253

5354
protected InstrumentationScopeInfo instrumentationScope;
54-
protected Set<SpanKind> spanKinds;
55+
protected Set<SpanKind> spanKinds = new HashSet<>();
5556
protected Map<String, String> attributeKeys = new HashMap<>();
5657
protected Map<String, MetricData> metrics = new HashMap<>();
5758

@@ -156,9 +157,7 @@ private <T extends Consumer<TraceAssert>> void doAssertTraces(
156157
public void getMetadataFromTraces(List<List<SpanData>> traces) {
157158
for (List<SpanData> trace : traces) {
158159
for (SpanData span : trace) {
159-
if (spanKinds != null) {
160-
spanKinds.add(span.getKind());
161-
}
160+
spanKinds.add(span.getKind());
162161
span.getAttributes()
163162
.forEach((key, value) -> attributeKeys.put(key.getKey(), key.getType().name()));
164163

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/internal/MetaDataCollector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static void writeSpanData(
132132
try (BufferedWriter writer = Files.newBufferedWriter(spansPath.toFile().toPath(), UTF_8)) {
133133

134134
if (spanKinds != null) {
135-
writer.write("span_kinds:\n");
135+
writer.write("spanKinds:\n");
136136
for (SpanKind spanKind : spanKinds) {
137137
writer.write(" - " + spanKind + "\n");
138138
}

0 commit comments

Comments
 (0)