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.util.Set;
022
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.nuxeo.common.utils.DurationUtils;
026import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
027
028import io.dropwizard.metrics5.MetricAttribute;
029import io.dropwizard.metrics5.MetricFilter;
030import io.dropwizard.metrics5.MetricRegistry;
031import io.opencensus.common.Duration;
032import io.opencensus.exporter.trace.jaeger.JaegerExporterConfiguration;
033import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter;
034
035/**
036 * Reports traces to Jaeger.
037 *
038 * @since 11.1
039 */
040public class JaegerReporter extends AbstractMetricsReporter {
041
042    private static final Logger log = LogManager.getLogger(JaegerReporter.class);
043
044    protected boolean activated;
045
046    @Override
047    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
048        log.warn("Creating Jaeger reporter");
049        String url = options.get(URL_OPTION);
050        Duration timeout = Duration.create(
051                DurationUtils.parsePositive(options.get(TIMEOUT_OPTION), DEFAULT_TIMEOUT).getSeconds(), 0);
052        String service = options.getOrDefault(SERVICE_OPTION, DEFAULT_SERVICE);
053        JaegerExporterConfiguration configuration = JaegerExporterConfiguration.builder()
054                                                                               .setServiceName(service)
055                                                                               .setThriftEndpoint(url)
056                                                                               .setDeadline(timeout)
057                                                                               .build();
058        JaegerTraceExporter.createAndRegister(configuration);
059        activated = true;
060        enableTracing();
061    }
062
063    @Override
064    public void stop() {
065        log.debug("Stop reporting");
066        if (activated) {
067            JaegerTraceExporter.unregister();
068            activated = false;
069        }
070    }
071}