001/*
002 * (C) Copyright 2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.runtime.stream;
020
021import java.util.Set;
022import java.util.concurrent.TimeUnit;
023
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026import org.nuxeo.lib.stream.computation.log.LogStreamManager;
027import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
028
029import io.dropwizard.metrics5.MetricAttribute;
030import io.dropwizard.metrics5.MetricFilter;
031import io.dropwizard.metrics5.MetricRegistry;
032import io.dropwizard.metrics5.ScheduledReporter;
033
034/**
035 * A Specialized Nuxeo Metrics Reporter that sends only Nuxeo Stream metrics into a Stream.
036 *
037 * @since 11.5
038 */
039public class StreamMetricsNuxeoReporter extends AbstractMetricsReporter {
040
041    private static final Logger log = LogManager.getLogger(StreamMetricsNuxeoReporter.class);
042
043    protected ScheduledReporter reporter;
044
045    protected static final MetricFilter STREAM_METRICS_FILTER = MetricFilter.startsWith("nuxeo.stream");
046
047    @Override
048    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
049        log.warn("Reporting Stream Metrics to: {}", LogStreamManager.METRICS_STREAM);
050        ScheduledReporter reporter = new StreamMetricsReporter(registry, STREAM_METRICS_FILTER);
051        reporter.start(getPollInterval(), TimeUnit.SECONDS);
052    }
053
054    @Override
055    public void stop() {
056        log.debug("Stop reporting");
057        if (reporter != null) {
058            reporter.stop();
059        }
060    }
061
062}