Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture metric dimensions from end attributes also #4430

Merged
merged 4 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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