Skip to content

Commit

Permalink
Capture metric dimensions from end attributes also (#4430)
Browse files Browse the repository at this point in the history
* Capture metric dimensions from end attributes also

* Fix test

* Update tests
  • Loading branch information
trask committed Oct 20, 2021
1 parent 923797d commit 35dd350
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void end(Context context, Attributes endAttributes, long endNanos) {
}
duration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS,
applyDurationView(state.startAttributes()));
applyDurationView(state.startAttributes(), endAttributes));
}

@AutoValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void end(Context context, Attributes endAttributes, long endNanos) {
activeRequests.add(-1, applyActiveRequestsView(state.startAttributes()));
duration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS,
applyDurationView(state.startAttributes()));
applyDurationView(state.startAttributes(), endAttributes));
}

@AutoValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,29 @@ private static Set<AttributeKey> buildActiveRequestsView() {
return view;
}

static Attributes applyDurationView(Attributes attributes) {
return applyView(attributes, durationView);
static Attributes applyDurationView(Attributes startAttributes, Attributes endAttributes) {
AttributesBuilder filtered = Attributes.builder();
applyView(filtered, startAttributes, durationView);
applyView(filtered, endAttributes, durationView);
return filtered.build();
}

static Attributes applyActiveRequestsView(Attributes attributes) {
return applyView(attributes, activeRequestsView);
AttributesBuilder filtered = Attributes.builder();
applyView(filtered, attributes, activeRequestsView);
return filtered.build();
}

@SuppressWarnings("unchecked")
private static Attributes applyView(Attributes attributes, Set<AttributeKey> view) {
AttributesBuilder filtered = Attributes.builder();
private static void applyView(
AttributesBuilder filtered, Attributes attributes, Set<AttributeKey> view) {
attributes.forEach(
(BiConsumer<AttributeKey, Object>)
(key, value) -> {
if (view.contains(key)) {
filtered.put(key, value);
}
});
return filtered.build();
}

private TemporaryMetricsView() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void collectsMetrics() {
.put("net.host.port", 1234)
.build();

// Currently ignored.
Attributes responseAttributes =
Attributes.builder()
.put("http.flavor", "2.0")
Expand Down Expand Up @@ -89,6 +88,9 @@ void collectsMetrics() {
attributeEntry("http.host", "host"),
attributeEntry("http.method", "GET"),
attributeEntry("http.scheme", "https"),
attributeEntry("http.flavor", "2.0"),
attributeEntry("http.server_name", "server"),
attributeEntry("http.status_code", 200),
attributeEntry("net.host.name", "localhost"),
attributeEntry("net.host.port", 1234L))));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void collectsMetrics() {
.put("net.host.port", 1234)
.build();

// Currently ignored.
Attributes responseAttributes =
Attributes.builder()
.put("http.flavor", "2.0")
Expand Down Expand Up @@ -124,6 +123,9 @@ void collectsMetrics() {
attributeEntry("http.host", "host"),
attributeEntry("http.method", "GET"),
attributeEntry("http.scheme", "https"),
attributeEntry("http.flavor", "2.0"),
attributeEntry("http.server_name", "server"),
attributeEntry("http.status_code", 200),
attributeEntry("net.host.name", "localhost"),
attributeEntry("net.host.port", 1234L))));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@ public class TemporaryMetricsViewTest {

@Test
public void shouldApplyDurationView() {
Attributes attributes =
Attributes startAttributes =
Attributes.builder()
.put(SemanticAttributes.HTTP_METHOD, "GET")
.put(SemanticAttributes.HTTP_URL, "http://somehost/high/cardinality/12345")
.put(SemanticAttributes.NET_PEER_NAME, "somehost")
.build();

OpenTelemetryAssertions.assertThat(applyDurationView(attributes))
Attributes endAttributes =
Attributes.builder()
.put(SemanticAttributes.HTTP_STATUS_CODE, 500)
.put(SemanticAttributes.NET_PEER_NAME, "somehost2")
.build();

OpenTelemetryAssertions.assertThat(applyDurationView(startAttributes, endAttributes))
.containsOnly(
attributeEntry("http.method", "GET"), attributeEntry("net.peer.name", "somehost"));
attributeEntry(SemanticAttributes.HTTP_METHOD.getKey(), "GET"),
attributeEntry(SemanticAttributes.NET_PEER_NAME.getKey(), "somehost2"),
attributeEntry(SemanticAttributes.HTTP_STATUS_CODE.getKey(), 500));
}

@Test
Expand Down

0 comments on commit 35dd350

Please sign in to comment.