Search in sources :

Example 1 with Metrics

use of io.mantisrx.common.metrics.Metrics in project mantis by Netflix.

the class SchedulingService method setupSchedulingServiceWatcherMetric.

private void setupSchedulingServiceWatcherMetric() {
    logger.info("Setting up SchedulingServiceWatcher metrics");
    lastSchedulingResultCallback.set(System.currentTimeMillis());
    final String metricGroup = "SchedulingServiceWatcher";
    final GaugeCallback timeSinceLastSchedulingRunGauge = new GaugeCallback(new MetricId(metricGroup, "timeSinceLastSchedulingRunMs"), () -> (double) (System.currentTimeMillis() - lastSchedulingResultCallback.get()), SpectatorRegistryFactory.getRegistry());
    final Metrics schedulingServiceWatcherMetrics = new Metrics.Builder().id(metricGroup).addGauge(timeSinceLastSchedulingRunGauge).build();
    MetricsRegistry.getInstance().registerAndGet(schedulingServiceWatcherMetrics);
}
Also used : GaugeCallback(io.mantisrx.common.metrics.spectator.GaugeCallback) MetricId(io.mantisrx.common.metrics.spectator.MetricId) Metrics(io.mantisrx.common.metrics.Metrics)

Example 2 with Metrics

use of io.mantisrx.common.metrics.Metrics in project mantis by Netflix.

the class SseWorkerConnectionTest method testStreamContentDrops.

@Test
public void testStreamContentDrops() throws Exception {
    SpectatorRegistryFactory.setRegistry(new DefaultRegistry());
    String metricGroupString = "testmetric";
    MetricGroupId metricGroupId = new MetricGroupId(metricGroupString);
    SseWorkerConnection workerConnection = new SseWorkerConnection("connection_type", "hostname", 80, b -> {
    }, b -> {
    }, t -> {
    }, 600, false, new CopyOnWriteArraySet<>(), 1, null, true, metricGroupId);
    HttpClientResponse<ServerSentEvent> response = mock(HttpClientResponse.class);
    TestScheduler testScheduler = Schedulers.test();
    // Events are just "0", "1", "2", ...
    Observable<ServerSentEvent> contentObs = Observable.interval(1, TimeUnit.SECONDS, testScheduler).map(t -> new ServerSentEvent(Unpooled.copiedBuffer(Long.toString(t), Charset.defaultCharset())));
    when(response.getContent()).thenReturn(contentObs);
    TestSubscriber<MantisServerSentEvent> subscriber = new TestSubscriber<>(1);
    workerConnection.streamContent(response, b -> {
    }, 600, "delimiter").subscribeOn(testScheduler).subscribe(subscriber);
    testScheduler.advanceTimeBy(100, TimeUnit.SECONDS);
    subscriber.assertValueCount(1);
    List<MantisServerSentEvent> events = subscriber.getOnNextEvents();
    assertEquals("0", events.get(0).getEventAsString());
    Metrics metrics = MetricsRegistry.getInstance().getMetric(metricGroupId);
    Counter onNextCounter = metrics.getCounter(DropOperator.Counters.onNext.toString());
    Counter droppedCounter = metrics.getCounter(DropOperator.Counters.dropped.toString());
    logger.info("next: {}", onNextCounter.value());
    logger.info("drop: {}", droppedCounter.value());
    assertTrue(onNextCounter.value() < 10);
    assertTrue(droppedCounter.value() > 90);
}
Also used : ServerSentEvent(mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) Metrics(io.mantisrx.common.metrics.Metrics) Counter(io.mantisrx.common.metrics.Counter) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) TestSubscriber(rx.observers.TestSubscriber) MetricGroupId(io.mantisrx.common.metrics.spectator.MetricGroupId) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with Metrics

use of io.mantisrx.common.metrics.Metrics in project mantis by Netflix.

the class ObservableTrigger method groupTrigger.

private static <K, V> PushTrigger<KeyValuePair<K, V>> groupTrigger(final String name, final Observable<GroupedObservable<K, V>> o, final Action0 doOnComplete, final Action1<Throwable> doOnError, final long groupExpirySeconds, final Func1<K, byte[]> keyEncoder, final HashFunction hashFunction) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStart = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(final MonitoredQueue<KeyValuePair<K, V>> queue) {
            subRef.set(o.observeOn(Schedulers.computation()).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).flatMap((final GroupedObservable<K, V> group) -> {
                final byte[] keyBytes = keyEncoder.call(group.getKey());
                final long keyBytesHashed = hashFunction.computeHash(keyBytes);
                return group.timeout(groupExpirySeconds, TimeUnit.SECONDS, (Observable<? extends V>) Observable.empty()).lift(new DisableBackPressureOperator<V>()).buffer(250, TimeUnit.MILLISECONDS).filter((List<V> t1) -> t1 != null && !t1.isEmpty()).map((List<V> list) -> {
                    List<KeyValuePair<K, V>> keyPairList = new ArrayList<>(list.size());
                    for (V data : list) {
                        keyPairList.add(new KeyValuePair<K, V>(keyBytesHashed, keyBytes, data));
                    }
                    return keyPairList;
                });
            }).subscribe((List<KeyValuePair<K, V>> list) -> {
                for (KeyValuePair<K, V> data : list) {
                    queue.write(data);
                }
            }, (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<KeyValuePair<K, V>>> doOnStop = new Action1<MonitoredQueue<KeyValuePair<K, V>>>() {

        @Override
        public void call(MonitoredQueue<KeyValuePair<K, V>> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0. Do not propagate unsubscribe");
            // subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : DisableBackPressureOperator(io.reactivx.mantis.operators.DisableBackPressureOperator) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Gauge(io.mantisrx.common.metrics.Gauge) Metrics(io.mantisrx.common.metrics.Metrics) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) GroupedObservable(rx.observables.GroupedObservable)

Example 4 with Metrics

use of io.mantisrx.common.metrics.Metrics in project mantis by Netflix.

the class ObservableTrigger method ssetrigger.

private static <T> PushTrigger<T> ssetrigger(final String name, final Observable<T> o, final Action0 doOnComplete, final Action1<Throwable> doOnError) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<T>> doOnStart = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(final MonitoredQueue<T> queue) {
            subRef.set(o.filter((T t1) -> t1 != null).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).subscribe((T data) -> queue.write(data), (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<T>> doOnStop = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(MonitoredQueue<T> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0 for SSE stage. propagate unsubscribe");
                subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : Metrics(io.mantisrx.common.metrics.Metrics) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription) Gauge(io.mantisrx.common.metrics.Gauge)

Example 5 with Metrics

use of io.mantisrx.common.metrics.Metrics in project mantis by Netflix.

the class ObservableTrigger method trigger.

private static <T> PushTrigger<T> trigger(final String name, final Observable<T> o, final Action0 doOnComplete, final Action1<Throwable> doOnError) {
    final AtomicReference<Subscription> subRef = new AtomicReference<>();
    final Gauge subscriptionActive;
    Metrics metrics = new Metrics.Builder().name("ObservableTrigger_" + name).addGauge("subscriptionActive").build();
    subscriptionActive = metrics.getGauge("subscriptionActive");
    Action1<MonitoredQueue<T>> doOnStart = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(final MonitoredQueue<T> queue) {
            subRef.set(o.filter((T t1) -> t1 != null).doOnSubscribe(() -> {
                logger.info("Subscription is ACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(1);
            }).doOnUnsubscribe(() -> {
                logger.info("Subscription is INACTIVE for observable trigger with name: " + name);
                subscriptionActive.set(0);
            }).subscribe((T data) -> queue.write(data), (Throwable e) -> {
                logger.warn("Observable used to push data errored, on server with name: " + name, e);
                if (doOnError != null) {
                    doOnError.call(e);
                }
            }, () -> {
                logger.info("Observable used to push data completed, on server with name: " + name);
                if (doOnComplete != null) {
                    doOnComplete.call();
                }
            }));
        }
    };
    Action1<MonitoredQueue<T>> doOnStop = new Action1<MonitoredQueue<T>>() {

        @Override
        public void call(MonitoredQueue<T> t1) {
            if (subRef.get() != null) {
                logger.warn("Connections from next stage has dropped to 0. Do not propagate unsubscribe");
            // subRef.get().unsubscribe();
            }
        }
    };
    return new PushTrigger<>(doOnStart, doOnStop, metrics);
}
Also used : Metrics(io.mantisrx.common.metrics.Metrics) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription) Gauge(io.mantisrx.common.metrics.Gauge)

Aggregations

Metrics (io.mantisrx.common.metrics.Metrics)15 Counter (io.mantisrx.common.metrics.Counter)6 Subscription (rx.Subscription)6 Action1 (rx.functions.Action1)5 Gauge (io.mantisrx.common.metrics.Gauge)4 List (java.util.List)4 Map (java.util.Map)4 MetricGroupId (io.mantisrx.common.metrics.spectator.MetricGroupId)3 Query (io.mantisrx.mql.jvm.core.Query)3 InetSocketAddress (java.net.InetSocketAddress)3 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BasicTag (com.netflix.spectator.api.BasicTag)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)2 ByteBuf (io.netty.buffer.ByteBuf)2 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)2 DisableBackPressureOperator (io.reactivx.mantis.operators.DisableBackPressureOperator)2 HashMap (java.util.HashMap)2 ServerSentEvent (mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent)2 Test (org.junit.Test)2