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 static org.apache.commons.lang3.StringUtils.defaultIfBlank;
022
023import java.io.IOException;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.logging.log4j.LogManager;
028import org.apache.logging.log4j.Logger;
029import org.nuxeo.common.utils.DurationUtils;
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
032
033import io.dropwizard.metrics5.MetricAttribute;
034import io.dropwizard.metrics5.MetricFilter;
035import io.dropwizard.metrics5.MetricRegistry;
036import io.opencensus.common.Duration;
037import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
038import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
039
040/**
041 * Reports trace to Google Stackdriver.
042 *
043 * @since 11.4
044 */
045public class StackdriverTraceReporter extends AbstractMetricsReporter {
046
047    private static final Logger log = LogManager.getLogger(StackdriverTraceReporter.class);
048
049    public static final String GCP_PROJECT_ID_ENV_PROP = "GCP_PROJECT_ID";
050
051    public static final String GCP_PROJECT_ID_OPTION_PROP = "gcpProjectId";
052
053    protected boolean activated;
054
055    @Override
056    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
057        log.warn("Creating Stackdriver trace reporter");
058        Duration timeout = Duration.create(
059                DurationUtils.parsePositive(options.get(TIMEOUT_OPTION), DEFAULT_TIMEOUT).getSeconds(), 0);
060        String projectId = getGcpProjectId(options);
061        StackdriverTraceConfiguration configuration = StackdriverTraceConfiguration.builder()
062                                                                                   .setDeadline(timeout)
063                                                                                   .setProjectId(projectId)
064                                                                                   .build();
065        try {
066            StackdriverTraceExporter.createAndRegister(configuration);
067        } catch (IOException e) {
068            log.error("Fail to create a Stackdriver trace reporter", e);
069            return;
070        }
071        activated = true;
072        enableTracing();
073    }
074
075    protected static String getGcpProjectId(Map<String, String> options) {
076        return defaultIfBlank(options.get(GCP_PROJECT_ID_OPTION_PROP), Framework.getProperty(GCP_PROJECT_ID_ENV_PROP));
077    }
078
079    @Override
080    public void stop() {
081        log.debug("Stop reporting");
082        if (activated) {
083            StackdriverTraceExporter.unregister();
084            activated = false;
085        }
086    }
087}