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.metrics.reporter;
020
021import java.io.IOException;
022import java.util.Collections;
023import java.util.Set;
024
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
027import org.nuxeo.common.utils.DurationUtils;
028import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
029
030import io.dropwizard.metrics5.MetricAttribute;
031import io.dropwizard.metrics5.MetricFilter;
032import io.dropwizard.metrics5.MetricRegistry;
033import io.opencensus.common.Duration;
034import io.opencensus.contrib.dropwizard5.DropWizardMetrics;
035import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
036import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
037import io.opencensus.metrics.Metrics;
038
039/**
040 * Reports metrics to Google Stackdriver.
041 *
042 * @since 11.4
043 */
044public class StackdriverReporter extends AbstractMetricsReporter {
045
046    private static final Logger log = LogManager.getLogger(StackdriverReporter.class);
047
048    protected static final String PREFIX_OPTION = "prefix";
049
050    protected static final String DEFAULT_PREFIX = "custom.googleapis.com/nuxeo/";
051
052    protected boolean activated;
053
054    @Override
055    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
056        log.warn("Creating Stackdriver metrics reporter");
057        DropWizardMetrics registries = new DropWizardMetrics(Collections.singletonList(registry), filter);
058        Metrics.getExportComponent().getMetricProducerManager().add(registries);
059        Duration timeout = Duration.create(
060                DurationUtils.parsePositive(options.get(TIMEOUT_OPTION), DEFAULT_TIMEOUT).getSeconds(), 0);
061        String projectId = StackdriverTraceReporter.getGcpProjectId(options);
062        String prefix = options.getOrDefault(PREFIX_OPTION, DEFAULT_PREFIX);
063        Duration interval = Duration.fromMillis(getPollInterval() * 1000);
064        StackdriverStatsConfiguration configuration = StackdriverStatsConfiguration.builder()
065                                                                                   .setDeadline(timeout)
066                                                                                   .setProjectId(projectId)
067                                                                                   .setMetricNamePrefix(prefix)
068                                                                                   .setExportInterval(interval)
069                                                                                   .build();
070        try {
071            StackdriverStatsExporter.createAndRegister(configuration);
072        } catch (IOException e) {
073            log.error("Fail to create a Stackdriver metric reporter", e);
074            return;
075        }
076        activated = true;
077    }
078
079    @Override
080    public void stop() {
081        log.debug("Stop reporting");
082        if (activated) {
083            StackdriverStatsExporter.unregister();
084            activated = false;
085        }
086    }
087}