Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

    /**
    * 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);
}

Code

...

examples

Expand
titleCustom metric within a service

/** */
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<>();
          

        // 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.");
    }

    /** {@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;
    }
}


Expand
titleCustom metrics within a computation

/** */
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);


            // 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) {
                // 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;
        }
    }
}


Further Steps

We already have implementations of more complex and useful metrics. We could also store custom metrics. Thus, the development stages might be:

...