Skip to content

Commit ad21e19

Browse files
traskRashmiRam
authored andcommitted
Capture metric dimensions from end attributes also (open-telemetry#4430)
* Capture metric dimensions from end attributes also * Fix test * Update tests
1 parent a97e4b8 commit ad21e19

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetrics.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void end(Context context, Attributes endAttributes, long endNanos) {
7676
}
7777
duration.record(
7878
(endNanos - state.startTimeNanos()) / NANOS_PER_MS,
79-
applyDurationView(state.startAttributes()));
79+
applyDurationView(state.startAttributes(), endAttributes));
8080
}
8181

8282
@AutoValue

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetrics.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void end(Context context, Attributes endAttributes, long endNanos) {
8989
activeRequests.add(-1, applyActiveRequestsView(state.startAttributes()));
9090
duration.record(
9191
(endNanos - state.startTimeNanos()) / NANOS_PER_MS,
92-
applyDurationView(state.startAttributes()));
92+
applyDurationView(state.startAttributes(), endAttributes));
9393
}
9494

9595
@AutoValue

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsView.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,29 @@ private static Set<AttributeKey> buildActiveRequestsView() {
5252
return view;
5353
}
5454

55-
static Attributes applyDurationView(Attributes attributes) {
56-
return applyView(attributes, durationView);
55+
static Attributes applyDurationView(Attributes startAttributes, Attributes endAttributes) {
56+
AttributesBuilder filtered = Attributes.builder();
57+
applyView(filtered, startAttributes, durationView);
58+
applyView(filtered, endAttributes, durationView);
59+
return filtered.build();
5760
}
5861

5962
static Attributes applyActiveRequestsView(Attributes attributes) {
60-
return applyView(attributes, activeRequestsView);
63+
AttributesBuilder filtered = Attributes.builder();
64+
applyView(filtered, attributes, activeRequestsView);
65+
return filtered.build();
6166
}
6267

6368
@SuppressWarnings("unchecked")
64-
private static Attributes applyView(Attributes attributes, Set<AttributeKey> view) {
65-
AttributesBuilder filtered = Attributes.builder();
69+
private static void applyView(
70+
AttributesBuilder filtered, Attributes attributes, Set<AttributeKey> view) {
6671
attributes.forEach(
6772
(BiConsumer<AttributeKey, Object>)
6873
(key, value) -> {
6974
if (view.contains(key)) {
7075
filtered.put(key, value);
7176
}
7277
});
73-
return filtered.build();
7478
}
7579

7680
private TemporaryMetricsView() {}

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientMetricsTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ void collectsMetrics() {
3838
.put("net.host.port", 1234)
3939
.build();
4040

41-
// Currently ignored.
4241
Attributes responseAttributes =
4342
Attributes.builder()
4443
.put("http.flavor", "2.0")
@@ -89,6 +88,9 @@ void collectsMetrics() {
8988
attributeEntry("http.host", "host"),
9089
attributeEntry("http.method", "GET"),
9190
attributeEntry("http.scheme", "https"),
91+
attributeEntry("http.flavor", "2.0"),
92+
attributeEntry("http.server_name", "server"),
93+
attributeEntry("http.status_code", 200),
9294
attributeEntry("net.host.name", "localhost"),
9395
attributeEntry("net.host.port", 1234L))));
9496
});

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerMetricsTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ void collectsMetrics() {
3838
.put("net.host.port", 1234)
3939
.build();
4040

41-
// Currently ignored.
4241
Attributes responseAttributes =
4342
Attributes.builder()
4443
.put("http.flavor", "2.0")
@@ -124,6 +123,9 @@ void collectsMetrics() {
124123
attributeEntry("http.host", "host"),
125124
attributeEntry("http.method", "GET"),
126125
attributeEntry("http.scheme", "https"),
126+
attributeEntry("http.flavor", "2.0"),
127+
attributeEntry("http.server_name", "server"),
128+
attributeEntry("http.status_code", 200),
127129
attributeEntry("net.host.name", "localhost"),
128130
attributeEntry("net.host.port", 1234L))));
129131
});

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsViewTest.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ public class TemporaryMetricsViewTest {
1818

1919
@Test
2020
public void shouldApplyDurationView() {
21-
Attributes attributes =
21+
Attributes startAttributes =
2222
Attributes.builder()
2323
.put(SemanticAttributes.HTTP_METHOD, "GET")
2424
.put(SemanticAttributes.HTTP_URL, "http://somehost/high/cardinality/12345")
2525
.put(SemanticAttributes.NET_PEER_NAME, "somehost")
2626
.build();
2727

28-
OpenTelemetryAssertions.assertThat(applyDurationView(attributes))
28+
Attributes endAttributes =
29+
Attributes.builder()
30+
.put(SemanticAttributes.HTTP_STATUS_CODE, 500)
31+
.put(SemanticAttributes.NET_PEER_NAME, "somehost2")
32+
.build();
33+
34+
OpenTelemetryAssertions.assertThat(applyDurationView(startAttributes, endAttributes))
2935
.containsOnly(
30-
attributeEntry("http.method", "GET"), attributeEntry("net.peer.name", "somehost"));
36+
attributeEntry(SemanticAttributes.HTTP_METHOD.getKey(), "GET"),
37+
attributeEntry(SemanticAttributes.NET_PEER_NAME.getKey(), "somehost2"),
38+
attributeEntry(SemanticAttributes.HTTP_STATUS_CODE.getKey(), 500));
3139
}
3240

3341
@Test

0 commit comments

Comments
 (0)