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

Update cassandra jmx metrics script to combine similar metrics into labelled metric #118

Merged
merged 6 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -16,6 +16,7 @@
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceResponse;
import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc;
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
Expand All @@ -24,6 +25,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer;
Expand Down Expand Up @@ -179,6 +181,19 @@ protected void assertTypedSum(
assertTypedPoints(metric.getSum().getDataPointsList(), types);
}

protected void assertSumWithAttributes(
Metric metric,
String name,
String description,
String unit,
List<Map<String, String>> attributeGroups) {
assertThat(metric.getName()).isEqualTo(name);
assertThat(metric.getDescription()).isEqualTo(description);
assertThat(metric.getUnit()).isEqualTo(unit);
assertThat(metric.hasSum()).isTrue();
assertAttributedPoints(metric.getSum().getDataPointsList(), attributeGroups);
}

private static final String expectedMeterVersion() {
// Automatically set by gradle when running the tests
String version = System.getProperty("gradle.project.version");
Expand Down Expand Up @@ -206,6 +221,34 @@ private static void assertTypedPoints(List<NumberDataPoint> points, List<String>
.toArray(Consumer[]::new));
}

@SuppressWarnings("unchecked")
private static void assertAttributedPoints(
List<NumberDataPoint> points, List<Map<String, String>> attributeGroups) {
assertThat(points)
.satisfiesExactlyInAnyOrder(
attributeGroups.stream()
.map(
attributeGroup ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit too intense of an assertion to understand easily. Is it just checking that every point contains every attributeGroup? How about just passing in a map of merged attributes instead of separate groups?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is providing a list of the labelled data points expected, and validating that all of the expected combinations exist. For example, if we expected one data point with { operation: Read, status: Timeout } and one data point with { operation Read, status: Ok }, the attributeGroups parameter would contain a list of those two maps. Are there ways I could make that more clear?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the complain is on the use of .stream() when you already have varargs to handle it.

E.g. I think you could do:

 assertThat(points)
        .extracting(point -> point.getAttributesList())
        .satisfiesExactlyInAnyOrder(
           list -> assertThat(list).contains(...),
           list -> assertThat(list).contains(...),
        );

Would also help if there was a "assertThatAttributeList(list, KV)" type helper method.

You can look in https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk/metrics-testing/src/main/java/io/opentelemetry/sdk/testing/assertj/metrics/MetricAssertions.java for ideas. Might be nice to have a good set of assertion libraries for the OTLP protos...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and validating that all of the expected combinations exist

I may not be understanding well but currently think this is the key point. In particular the word "all". If so, it seems the parameter passed to the assertion could have all of the attributes in one Map rather than a list of maps. Is this not the case?

Copy link
Contributor Author

@dehaansa dehaansa Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each item in the provided list represents one data point's AttributeList, and this method currently validates that we have N different data points that match N unique sets of AttributeLists, with one data point matching each one unique set of Attributes. I don't see a way to reduce this to a single map.

To expand on the example for this cassandra metric, we have 12 data points under one metric that we are validating, where each data point has a different combination of "operation" and "status" attributes. So the list contains the maps { operation: Read, status: Ok }, { operation: Read, status: Timeout }, { operation: Read, status: Unavailable }, { operation: Read, status: Failure }, { operation: Write, status: Ok }, { operation: Write, status: Timeout }, { operation: Write, status: Unavailable }, { operation: Write, status: Timeout }, { operation: Write, status: Failure }, { operation: RangeSlice, status: Ok },...

(Consumer<NumberDataPoint>)
point ->
assertThat(point.getAttributesList())
.satisfies(
list -> {
Map<String, String> pointAttributes =
list.stream()
.collect(
Collectors.toMap(
KeyValue::getKey,
keyValue ->
keyValue
.getValue()
.getStringValue()));
assertThat(pointAttributes)
.containsExactlyInAnyOrderEntriesOf(attributeGroup);
}))
.toArray(Consumer[]::new));
}

private static class OtlpGrpcServer extends ServerExtension {

private final BlockingQueue<ExportMetricsServiceRequest> metricRequests =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import io.opentelemetry.contrib.jmxmetrics.AbstractIntegrationTest;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
Expand Down Expand Up @@ -48,30 +53,12 @@ void endToEnd() {
"cassandra.client.request.range_slice.latency.99p",
"Token range read request latency - 99th percentile",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.range_slice.latency.count",
"Number of token range read request operations",
"1"),
metric ->
assertGauge(
metric,
"cassandra.client.request.range_slice.latency.max",
"Maximum token range read request latency",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.range_slice.timeout.count",
"Number of token range read request timeouts encountered",
"1"),
metric ->
assertSum(
metric,
"cassandra.client.request.range_slice.unavailable.count",
"Number of token range read request unavailable exceptions encountered",
"1"),
metric ->
assertGauge(
metric,
Expand All @@ -84,30 +71,12 @@ void endToEnd() {
"cassandra.client.request.read.latency.99p",
"Standard read request latency - 99th percentile",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.read.latency.count",
"Number of standard read request operations",
"1"),
metric ->
assertGauge(
metric,
"cassandra.client.request.read.latency.max",
"Maximum standard read request latency",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.read.timeout.count",
"Number of standard read request timeouts encountered",
"1"),
metric ->
assertSum(
metric,
"cassandra.client.request.read.unavailable.count",
"Number of standard read request unavailable exceptions encountered",
"1"),
metric ->
assertGauge(
metric,
Expand All @@ -120,30 +89,12 @@ void endToEnd() {
"cassandra.client.request.write.latency.99p",
"Regular write request latency - 99th percentile",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.write.latency.count",
"Number of regular write request operations",
"1"),
metric ->
assertGauge(
metric,
"cassandra.client.request.write.latency.max",
"Maximum regular write request latency",
"µs"),
metric ->
assertSum(
metric,
"cassandra.client.request.write.timeout.count",
"Number of regular write request timeouts encountered",
"1"),
metric ->
assertSum(
metric,
"cassandra.client.request.write.unavailable.count",
"Number of regular write request unavailable exceptions encountered",
"1"),
metric ->
assertSum(
metric,
Expand Down Expand Up @@ -175,6 +126,32 @@ void endToEnd() {
"cassandra.storage.total_hints.in_progress.count",
"Number of hints attempting to be sent currently",
"1",
false));
false),
metric ->
assertSumWithAttributes(
metric,
"cassandra.client.request.count",
"Number of requests",
"1",
getRequestCountAttributes()));
}

private List<Map<String, String>> getRequestCountAttributes() {
List<String> operations = Arrays.asList("RangeSlice", "Read", "Write");
List<String> statuses = Arrays.asList("Ok", "Timeout", "Failure", "Unavailable");

return operations.stream()
.flatMap(
op ->
statuses.stream()
.map(
st ->
new HashMap<String, String>() {
{
put("operation", op);
put("status", st);
}
}))
.collect(Collectors.toList());
}
}
94 changes: 43 additions & 51 deletions jmx-metrics/src/main/resources/target-systems/cassandra.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
def cassandraMetrics = "org.apache.cassandra.metrics"
def clientRequest = "${cassandraMetrics}:type=ClientRequest"
def clientRequestRangeSlice = "${clientRequest},scope=RangeSlice"

def clientRequestRangeSliceLatency = otel.mbean("${clientRequestRangeSlice},name=Latency")
otel.instrument(clientRequestRangeSliceLatency,
"cassandra.client.request.range_slice.latency.50p",
Expand All @@ -28,28 +29,11 @@ otel.instrument(clientRequestRangeSliceLatency,
"Token range read request latency - 99th percentile", "µs", "99thPercentile",
otel.&doubleValueCallback)

otel.instrument(clientRequestRangeSliceLatency,
"cassandra.client.request.range_slice.latency.count",
"Number of token range read request operations", "1", "Count",
otel.&longCounterCallback)

otel.instrument(clientRequestRangeSliceLatency,
"cassandra.client.request.range_slice.latency.max",
"Maximum token range read request latency", "µs", "Max",
otel.&doubleValueCallback)

def clientRequestRangeSliceTimeouts = otel.mbean("${clientRequestRangeSlice},name=Timeouts")
otel.instrument(clientRequestRangeSliceTimeouts,
"cassandra.client.request.range_slice.timeout.count",
"Number of token range read request timeouts encountered", "1", "Count",
otel.&longCounterCallback)

def clientRequestRangeSliceUnavailables = otel.mbean("${clientRequestRangeSlice},name=Unavailables")
otel.instrument(clientRequestRangeSliceUnavailables,
"cassandra.client.request.range_slice.unavailable.count",
"Number of token range read request unavailable exceptions encountered", "1", "Count",
otel.&longCounterCallback)

def clientRequestRead = "${clientRequest},scope=Read"
def clientRequestReadLatency = otel.mbean("${clientRequestRead},name=Latency")
otel.instrument(clientRequestReadLatency,
Expand All @@ -62,28 +46,11 @@ otel.instrument(clientRequestReadLatency,
"Standard read request latency - 99th percentile", "µs", "99thPercentile",
otel.&doubleValueCallback)

otel.instrument(clientRequestReadLatency,
"cassandra.client.request.read.latency.count",
"Number of standard read request operations", "1", "Count",
otel.&longCounterCallback)

otel.instrument(clientRequestReadLatency,
"cassandra.client.request.read.latency.max",
"Maximum standard read request latency", "µs", "Max",
otel.&doubleValueCallback)

def clientRequestReadTimeouts = otel.mbean("${clientRequestRead},name=Timeouts")
otel.instrument(clientRequestReadTimeouts,
"cassandra.client.request.read.timeout.count",
"Number of standard read request timeouts encountered", "1", "Count",
otel.&longCounterCallback)

def clientRequestReadUnavailables = otel.mbean("${clientRequestRead},name=Unavailables")
otel.instrument(clientRequestReadUnavailables,
"cassandra.client.request.read.unavailable.count",
"Number of standard read request unavailable exceptions encountered", "1", "Count",
otel.&longCounterCallback)

def clientRequestWrite = "${clientRequest},scope=Write"
def clientRequestWriteLatency = otel.mbean("${clientRequestWrite},name=Latency")
otel.instrument(clientRequestWriteLatency,
Expand All @@ -96,28 +63,11 @@ otel.instrument(clientRequestWriteLatency,
"Regular write request latency - 99th percentile", "µs", "99thPercentile",
otel.&doubleValueCallback)

otel.instrument(clientRequestWriteLatency,
"cassandra.client.request.write.latency.count",
"Number of regular write request operations", "1", "Count",
otel.&longCounterCallback)

otel.instrument(clientRequestWriteLatency,
"cassandra.client.request.write.latency.max",
"Maximum regular write request latency", "µs", "Max",
otel.&doubleValueCallback)

def clientRequestWriteTimeouts = otel.mbean("${clientRequestWrite},name=Timeouts")
otel.instrument(clientRequestWriteTimeouts,
"cassandra.client.request.write.timeout.count",
"Number of regular write request timeouts encountered", "1", "Count",
otel.&longCounterCallback)

def clientRequestWriteUnavailables = otel.mbean("${clientRequestWrite},name=Unavailables")
otel.instrument(clientRequestWriteUnavailables,
"cassandra.client.request.write.unavailable.count",
"Number of regular write request unavailable exceptions encountered", "1", "Count",
otel.&longCounterCallback)

def storage = "${cassandraMetrics}:type=Storage"
def storageLoad = otel.mbean("${storage},name=Load")
otel.instrument(storageLoad,
Expand All @@ -137,6 +87,7 @@ otel.instrument(storageTotalHintsInProgress,
"Number of hints attempting to be sent currently", "1", "Count",
otel.&longUpDownCounterCallback)


def compaction = "${cassandraMetrics}:type=Compaction"
def compactionPendingTasks = otel.mbean("${compaction},name=PendingTasks")
otel.instrument(compactionPendingTasks,
Expand All @@ -149,3 +100,44 @@ otel.instrument(compactionCompletedTasks,
"cassandra.compaction.tasks.completed",
"Number of completed compactions since server [re]start", "1", "Value",
otel.&longCounterCallback)


def clientRequests = otel.mbeans([
"${clientRequestRangeSlice},name=Latency",
"${clientRequestRangeSlice},name=Unavailables",
"${clientRequestRangeSlice},name=Timeouts",
"${clientRequestRangeSlice},name=Failures",
"${clientRequestRead},name=Latency",
"${clientRequestRead},name=Unavailables",
"${clientRequestRead},name=Timeouts",
"${clientRequestRead},name=Failures",
"${clientRequestWrite},name=Latency",
"${clientRequestWrite},name=Unavailables",
"${clientRequestWrite},name=Timeouts",
"${clientRequestWrite},name=Failures",
])

otel.instrument(clientRequests,
"cassandra.client.request.count",
"Number of requests",
rmfitzpatrick marked this conversation as resolved.
Show resolved Hide resolved
"1",
[
"operation" : { mbean -> mbean.name().getKeyProperty("scope") },
"status" : {
mbean -> switch(mbean.name().getKeyProperty("name")) {
case "Latency":
return "Ok"
rmfitzpatrick marked this conversation as resolved.
Show resolved Hide resolved
break
case "Unavailables":
return "Unavailable"
break
case "Timeouts":
return "Timeout"
break
case "Failures":
return "Failure"
break
}
}
],
"Count", otel.&longCounterCallback)