...
Probably should be named "MetricRegistry" with the renaming of internal "MetricRegistry" to "MetricRegistryImpl".
...
/**
* Metric registry. Allows to get, add or remove metrics.
*
* @see IgniteMetrics
* @see ReadOnlyMetricRegistry
*/
@IgniteExperimental
public interface IgniteMetricRegistry extends ReadOnlyMetricRegistry {
/**
* Registers an int metric which value will be queried from the specified supplier.
*
* @return {@code True} if new metric was added. {@code False} is other metric already exists with the same name.
*/
boolean gauge(String name, IntSupplier supplier, @Nullable String desc);
/**
* Registers a long which value will be queried from the specified supplier.
*
* @return {@code True} if new metric was added. {@code False} is other metric already exists with the same name.
*/
boolean gauge(String name, LongSupplier supplier, @Nullable String desc);
/**
* Registers a double metric which value will be queried from the specified supplier.
*
* @return {@code True} if new metric was added. {@code False} is other metric already exists with the same name.
*/
boolean gauge(String name, DoubleSupplier supplier, @Nullable String desc);
/**
* Registers an object metric which value will be queried from the specified supplier.
*
* @return {@code True} if new metric was added. {@code False} is other metric already exists with the same name.
*/
<T> boolean gauge(String name, Supplier<T> supplier, Class<T> type, @Nullable String desc);
/**
* Registers a boolean metric which value will be queried from the specified supplier.
*
* @return {@code True} if new metric was added. {@code False} is other metric already exists with the same name.
*/
boolean gauge(String name, BooleanSupplier supplier, @Nullable String desc);
/**
* Registers an updatable int metric.
*
* @return New {@link IntValueMetric} or previous one with the same name. {@code Null} if previous metric exists and
* is not a {@link IntValueMetric}.
*/
@Nullable IntValueMetric intMetric(String name, @Nullable String desc);
/**
* Registers an updatable long metric.
*
* @return New {@link LongValueMetric} or previous one with the same name. {@code Null} if previous metric exists and
* is not a {@link LongValueMetric}.
*/
@Nullable LongValueMetric longMetric(String name, @Nullable String desc);
/**
* Registers an updatable long adder metric.
*
* @return New {@link LongValueMetric} or previous one with the same name. {@code Null} if previous metric exists and
* is not a {@link LongValueMetric}.
*/
@Nullable LongSumMetric longAdderMetric(String name, @Nullable String desc);
/**
* Registers an updatable double metric.
*
* @return New {@link DoubleValueMetric} or previous one with the same name. {@code Null} if previous metric exists and
* is not a {@link DoubleValueMetric}.
*/
@Nullable DoubleValueMetric doubleMetric(String name, @Nullable String desc);
/**
* Registers an updatable double metric.
*
* @param <T> Metric value type.
* @return New {@link CustomMetric} or previous one with the same name. {@code Null} if previous metric exists and
* is not a {@link CustomMetric}.
*/
@Nullable <T> AnyValueMetric<T> objectMetric(String name, Class<T> type, @Nullable String desc);
/**
* Removes metrics with the {@code name}.
*
* @param name Metric name..
*/
void remove(String name);
/** Resets all metrics of this metric registry. */
void reset();
}
...
...
To the package "org.apache.ignite.metric" we add:
...
Names like "LongMetric" or "ObjectMetric" we already have in the package "org.apache.ignite.spi.metric".
...
package org.apache.ignite.metric;
...
Instead of #3, we could bring only a minimal metric management interface.
package org.apache.ignite;
...
/**
* Adds a long value custom metric.
*
* @return New or previously registered long value metric. {@code Null} if previous metric is not a {@link LongConsumer}.
*/
@Nullable LongConsumer longMetric(String registryName, String metricName, @Nullable String description);
/**
* Adds a double value custom metric.
*
* @return New or previously registered long value metric. {@code Null} if previous metric is not a {@link DoubleConsumer}.
*/
@Nullable DoubleConsumer doubleMetric(String registryName, String metricName, @Nullable String description);
/**
* Adds a int value custom metric.
*
* @return New or previously registered long value metric. {@code Null} if previous metric is not an {@link IntConsumer}.
*/
@Nullable IntConsumer booleanMetric(String registryName, String metricName, @Nullable String description);
/**
* Adds a long value custom metric.
*
* @return {@code True} if {@code supplier} has been registered as a new int metric. {@code False}, if a previous
* value supplier already exists.
*/
boolean longMetric(String registryName, String metricName, LongSupplier supplier, @Nullable String description);
/**
* Adds a double value custom metric.
*
* @return {@code True} if {@code supplier} has been registered as a new int metric. {@code False}, if a previous
* value supplier already exists.
*/
boolean doubleMetric(String registryName, String metricName, DoubleSupplier supplier, @Nullable String description);
/**
* Adds a int value custom metric.
*
* @return {@code True} if {@code supplier} has been registered as a new int metric. {@code False}, if a previous
* value supplier already exists.
*/
boolean intMetric(String registryName, String metricName, BooleanSupplier supplier, @Nullable String description);
/** Removes certain custom metric. */
void removeCustomMetric(String registryName, String metricName);
/** Removes entire custom metric registry. */
void removeCustomRegistry(String registryName);
/** Provides custom or internal read-only metric registry. */
@Nullable ReadOnlyMetricRegistry findRegistry(String registryName);
}
...
/** */
public static final class TestCustomMetricsService implements TestService {
/** */
@IgniteInstanceResource
private Ignite ignite;
/** */
@ServiceContextResource
private ServiceContext ctx;
/** */
private AtomicReference<UUID> remoteId;
/** */
private final AtomicInteger metricValue = new AtomicInteger();
/** {@inheritDoc} */
@Override public void init() throws Exception {
remoteId = new AtomicReference<>();
ignite.metrics().customRegistry(regName(ctx.name())).gauge("filteredInvocation", metricValue::get, "Counter of speceific service invocation.");
ignite.metrics().customRegistry(regName(ctx.name())).gauge(LOAD_THRESHOLD_METRIC_NAME, () -> metricValue.get() >= 100, "Load flag.");
ignite.metrics().customRegistry(regName(ctx.name())).gauge("loaded", () -> remoteId.get(), UUID.class, "Remote system class id.");
}
/** {@inheritDoc} */
@Override public void cancel() {
refresh();
ignite.metrics().customRegistry(regName(ctx.name())).remove(COUNTER_METRIC_NAME);
}
/** {@inheritDoc} */
@Override public void refresh() {
metricValue.set(0);
remoteId.set(null);
}
/** */
@Override public void invoke(int param) {
if (ctx.isCancelled())
return;
remoteId.compareAndSet(null, UUID.randomUUID());
// Updates metric sometimes.
if (!ctx.isCancelled() && param % 10 == 0)
metricValue.set(param / 10);
}
/** */
private static String regName(String svcName) {
return "service." + svcName;
}
}
/** */
private static final class TestCustomMetricsComputeTask extends ComputeTaskAdapter<Void, Long> {
/** */
private static final class TestComputeJob extends ComputeJobAdapter {
/** Ignite instance. */
@IgniteInstanceResource
private Ignite ignite;
/** {@inheritDoc} */
@Override public Long execute() throws IgniteException {
long val = 0;
// Some job limit.
long limit = 300 + ThreadLocalRandom.current().nextLong(700);
LongValueMetric metricCur = ignite.metrics().customRegistry(METRIC_REGISTRY).longMetric("task.test", null);
LongSumMetric metricTotal = ignite.metrics().customRegistry(METRIC_REGISTRY).longAdderMetric("total", null);
LongSumMetric metricTicks = ignite.metrics().customRegistry(METRIC_REGISTRY).longAdderMetric("ticks", null);
while (!isCancelled() && val < limit) {
// Does some job.
try {
U.sleep(ThreadLocalRandom.current().nextInt(50));
}
catch (IgniteInterruptedCheckedException ignored) {
//No op.
}
long increment = ThreadLocalRandom.current().nextLong(100);
val += increment;
metricTicks.increment()
}
metricCur.value(val);
metricTotal.add(val);
return isCancelled() ? 0 : val;
}
}
}
We already have implementations of more complex and useful metrics. We could also store custom metrics. Thus, the development stages might be:
...