Skip to content

Commit

Permalink
IGNITE-21105 Added histogram bounds to the metric command output (#11106
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Nastya828 authored Dec 20, 2023
1 parent 2fc50de commit 2428b23
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.ignite.internal.processors.metric.MetricRegistry;
import org.apache.ignite.internal.processors.metric.impl.HistogramMetricImpl;
import org.apache.ignite.internal.processors.metric.impl.HitRateMetric;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.junit.Test;

Expand All @@ -37,6 +38,7 @@
import static org.apache.ignite.internal.processors.metric.GridMetricManager.IGNITE_METRICS;
import static org.apache.ignite.internal.processors.metric.GridMetricManager.SYS_METRICS;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.SEPARATOR;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.histogramBucketNames;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
import static org.apache.ignite.testframework.GridTestUtils.assertContains;
import static org.apache.ignite.util.SystemViewCommandTest.NODE_ID;
Expand Down Expand Up @@ -256,12 +258,20 @@ public void testHistogramMetrics() {
assertEquals("1", metric(ignite0, metricName(mregName, "histogram_0_50")));
assertEquals("2", metric(ignite0, metricName(mregName, "histogram_50_500")));
assertEquals("3", metric(ignite0, metricName(mregName, "histogram_500_inf")));
assertEquals("[1, 2, 3]", metric(ignite0, metricName(mregName, "histogram")));

assertEquals("1", metric(ignite0, metricName(mregName, "histogram_with_underscore_0_50")));
assertEquals("2", metric(ignite0, metricName(mregName, "histogram_with_underscore_50_500")));
assertEquals("3", metric(ignite0, metricName(mregName, "histogram_with_underscore_500_inf")));
assertEquals("[1, 2, 3]", metric(ignite0, metricName(mregName, "histogram_with_underscore")));

Map<String, String> expHistOut = F.asMap(histogram.name(), "[1, 2, 3]");

String[] names = histogramBucketNames(histogram);

for (int i = 0; i < names.length; i++)
expHistOut.put(names[i], String.valueOf(histogram.value()[i]));

assertEqualsMaps(expHistOut, metrics(ignite0, histogram.name()));
assertTrue(metrics(ignite0, mregName).entrySet().containsAll(expHistOut.entrySet()));
}

/** */
Expand Down Expand Up @@ -448,7 +458,7 @@ private Map<String, String> metrics(IgniteEx node, String name) {
*/
private String metric(IgniteEx node, String name) {
Map<String, String> metrics = metrics(node, name);

assertEquals(1, metrics.size());

return metrics.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.ignite.internal.management.metric;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
Expand All @@ -29,6 +29,7 @@
import org.apache.ignite.internal.visor.VisorOneNodeTask;
import org.apache.ignite.spi.metric.BooleanMetric;
import org.apache.ignite.spi.metric.DoubleMetric;
import org.apache.ignite.spi.metric.HistogramMetric;
import org.apache.ignite.spi.metric.IntMetric;
import org.apache.ignite.spi.metric.LongMetric;
import org.apache.ignite.spi.metric.Metric;
Expand All @@ -37,6 +38,7 @@
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.SEPARATOR;
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.histogramBucketNames;
import static org.apache.ignite.spi.metric.jmx.MetricRegistryMBean.searchHistogram;

/** Reperesents visor task for obtaining metric values. */
Expand Down Expand Up @@ -92,9 +94,9 @@ else if (arg instanceof MetricConfigureHitrateCommandArg) {
String mregName = mreg.name();

if (mregName.equals(name)) {
Map<String, Object> res = new HashMap<>();
Map<String, Object> res = new LinkedHashMap<>();

mreg.forEach(metric -> res.put(metric.name(), valueOf(metric)));
mreg.forEach(metric -> res.putAll(exportMetric(metric)));

return res;
}
Expand All @@ -112,7 +114,7 @@ else if (arg instanceof MetricConfigureHitrateCommandArg) {
Metric metric = mreg.findMetric(metricName);

if (metric != null)
return Collections.singletonMap(name, valueOf(metric));
return exportMetric(metric);

Object val = searchHistogram(metricName, mreg);

Expand Down Expand Up @@ -143,5 +145,25 @@ else if (metric instanceof ObjectMetric)

throw new IllegalArgumentException("Unknown metric class [class=" + metric.getClass() + ']');
}

/** @return The metric value prepared to output. */
private Map<String, Object> exportMetric(Metric metric) {
if (metric instanceof HistogramMetric) {
HistogramMetric hist = (HistogramMetric)metric;
String[] names = histogramBucketNames(hist);

Map<String, Object> res = new LinkedHashMap<>();

for (int i = 0; i < names.length; i++)
res.put(names[i], hist.value()[i]);

// For compatibility with old output.
res.put(metric.name(), valueOf(metric));

return res;
}

return Collections.singletonMap(metric.name(), valueOf(metric));
}
}
}

0 comments on commit 2428b23

Please sign in to comment.