...
ID | IEP-114 |
Author | |
Sponsor | |
Created |
|
Status | |
Motivation
It is useful for user to work with own metrics, not only with provided by Ignite. Current The current public metrics API doesn't expose any method to add or delete additional metrics.
...
- Some metrics may not be provided yet and user registers own.
- Metrics for load balancing.
- Custom conflict resolver successes/fails.
- Various metrics of business processes or related to user application.
- Administration metrics.
- Security-related metrics.
Implementation proposal
...
- Custom Metrics base based on the New Metric System.
- Allow to register essential ready-to-use metrics like LongMetric which can be exported by a providing metric exporter.
- User can add own metrics into different registries.
- Custom metrics are separated from the internals by registry name prefix "custom." (lower cased).
- Names of custom metrics and their registries can't contain any spaces.
- Management of custom metrics might require a permission.
- If we expose only few simple metrics, we should document examples of effecient metrics implementations like LongAdder.
Risks and Assumptions
- Custom metrics are just another way to hit performance and consume affect performance and take additional resources.
- There could be race conditions on metric registrations. Metrics with incompatible types can be concurrently registered with the same names.
- We do not provide read-only metric wrappers for the internal metrics. User can cast read-only metric to writeable implementation or interface and change value even of an iternal metric. But we already have public Metric#reset(), IgniteSpiContext#removeMetricRegistry(). And current ReadOnlyMetricRegistry doesn't return write-protected metric implementation.expose API of all internal metrics. At least with the first tickets.
- Custom metrics aren't stored and require re-registration after node restart. We do not expose API of all internal metrics. At least with the first tickets.
- Custom metrics aren't stored and require re-registration after node restart. At least with the first tickets.
API
1. APIs description
An API to expose internals read-only internal metrics was already proposed. Might be joined with the custom metrics.
To give an user the ability to register additional metrics, we could either:
- Refactor a bit our current metrics. Create interfaces for writable metric registry, metric manager. Create interfaces for metrics like int, double, long, longAdder, boolean, object and the gauge metrics (IntGauge, LongGauge, etc.).
- Provide tiny-enough facade with consumers and suppliers for int, long, double value metrics.
These two approaches are shown below.
2. Obtaining Custom Metrics
...
public interface Ignite {
IgniteMetrics metrics();
}
3. Interfaces for existing metrics
Instead of #4, we could create interfaces for the existing metrics implementations.
3.1 IgniteMetric
package org.apache.ignite.metric;
...
3.2 IgniteMetricRegistry
Probably should be named "MetricRegistry" with the renaming of internal "MetricRegistry" to "MetricRegistryImpl".
...
3.3 Updatable metric interfaces list
To the package "org.apache.ignite.metric" we add:
- BooleanValueMetric
- IntValueMetric
- LongValueMetric
- LongSumMetric (a long adder)
- DoubleValueMetric
- AnyValueMetric<T> (an object metric)
Names like "LongMetric" or "ObjectMetric" we already have in the package "org.apache.ignite.spi.metric".
3.4 Examples of updatable metrics
...
/** Updatable simple long value metric. */
@IgniteExperimental
public interface LongValueMetric extends LongSumMetric {
/** Sets long metric value. */
void value(long value);
}
4. Minimal custom interface
Instead of #3, we could bring only a minimal metric management interface.
...
API
The APIs are supposed to be experimental.
Interfaces for existing metrics
Code Block |
---|
|
package org.apache.ignite.metric;
public interface IgniteMetrics extends Iterable<ReadOnlyMetricRegistry> {
MetricRegistry customRegistry(String registryName);
@Nullable ReadOnlyMetricRegistry findRegistry(String registryName);
void removeCustomRegistry(String registryName);
} |
Names like "LongMetric" or "ObjectMetric" we already have in the package "org.apache.ignite.spi.metric".
Code Block |
---|
|
package org.apache.ignite.metric;
public interface MetricRegistry extends ReadOnlyMetricRegistry {
IntMetric register(String name, IntSupplier supplier, @Nullable String desc);
LongMetric register(String name, LongSupplier supplier, @Nullable String desc);
DoubleMetric register(String name, DoubleSupplier supplier, @Nullable String desc);
<T> ObjectMetric<T> register(String name, Supplier<T> supplier, Class<T> type, @Nullable String desc);
BooleanMetric register(String name, BooleanSupplier supplier, @Nullable String desc);
IntValueMetric intMetric(String name, @Nullable String desc);
LongValueMetric longMetric(String name, @Nullable String desc);
LongSumMetric longAdderMetric(String name, @Nullable String desc);
DoubleValueMetric doubleMetric(String name, @Nullable String desc);
<T> ObjectValueMetric<T> objectMetric(String name, Class<T> type, @Nullable String desc);
void remove(String name);
void reset();
} |
Examples of updatable metrics
Code Block |
---|
|
package org.apache.ignite.metric;
public interface ObjectValueMetric<T> extends ObjectMetric<T> {
void value(T value);
}
public interface DoubleValueMetric extends DoubleMetric {
void add(double value);
void value(double value);
}
public interface LongSumMetric extends LongMetric {
void add(long value);
void increment();
void decrement();
}
public interface LongValueMetric extends LongSumMetric {
void value(long value);
} |
Code examples
Expand |
---|
title | Custom metric within a service |
---|
|
Code Block |
---|
| 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();
@Override public void init() throws Exception {
remoteId = new AtomicReference<>();
// Registers metric "custom.service.svc.filteredInvocation"
|
|
Code example
Custom metric within a service
...
// Registers metric "custom.service.svc.filteredInvocation"
...
ignite.metrics().customRegistry(regName(ctx.name())).gauge("filteredInvocation", metricValue::get, "Counter of speceific service invocation."); |
|
...
// Registers metric "custom.service.svc.loaded" |
|
...
ignite.metrics().customRegistry(regName(ctx.name())).gauge("loaded", () -> metricValue.get() >= 100, "Load flag."); |
|
...
// Registers metric "custom.service.svc.remote.classId" |
|
...
ignite.metrics().customRegistry(regName(ctx.name())).gauge("remote.classId", |
|
...
() -> remoteId.get(), UUID.class, "Remote system class id."); |
|
...
...
...
@Override public void cancel() { |
|
...
...
ignite.metrics().customRegistry(regName(ctx.name())).remove(COUNTER_METRIC_NAME); |
|
...
...
}
@Override public void refresh() { |
|
...
...
...
...
...
@Override public void invoke(int param) { |
|
...
...
...
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; |
|
...
...
title | Custom metrics within a computation |
---|
|
...
...
| private static final class TestCustomMetricsComputeTask extends ComputeTaskAdapter<Void, |
|
...
Long> {
private static final class TestComputeJob extends ComputeJobAdapter {
@IgniteInstanceResource
private Ignite ignite;
@Override public Long execute() throws IgniteException {
long val = 0;
// Some job limit.
long limit = 300 + ThreadLocalRandom.current().nextLong(700); |
|
...
...
// Registers metric "custom.task.test.current" |
|
...
LongValueMetric metricCur = ignite.metrics().customRegistry("task.test").longMetric("current", null); |
|
...
...
// Registers metric "custom.task.test.total.sum" |
|
...
LongSumMetric metricTotal = ignite.metrics().customRegistry("task.test").longAdderMetric("total.sum", null); |
|
...
...
// Registers metric "custom.task.test.ticks" |
|
...
LongSumMetric metricTicks = ignite.metrics().customRegistry("task.test").longAdderMetric("ticks", null); |
|
...
while (!isCancelled() && val < limit) { |
|
...
...
...
U.sleep(ThreadLocalRandom.current().nextInt(50)); |
|
...
...
catch (IgniteInterruptedCheckedException ignored) { |
|
...
...
...
long increment = ThreadLocalRandom.current().nextLong(100); |
|
...
...
...
...
...
...
...
return isCancelled() ? 0 : val; |
|
...
...
...
Further Steps
We already have implementations of more complex and useful metrics. We could also store custom metrics. Thus, the development stages might be:
- An API to expose internals read-only internal metrics was already suggested. Might be joined with the custom metrics.
...
- By the methods like "findRegistry" we can also return read-only internal metrics.
- Extending the initial API with more complex metrics like Histogram or HitRate.
- Introduce a permission of for custom metric management.
- Storing registered custom metrics.
- Allowing to change settings of configurable custom metrics like histograms.
...
- IEP-35 Monitoring & Profiling
- New Metric System
- Ticket of a public metric API
- IEP-116 : Ignite 3 metric
Discussion Links
https://lists.apache.org/thread/9zs988jbw3jc6fzj6zhz0t5g8bl3lx5t
Tickets