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.net.MalformedURLException;
022import java.util.Set;
023
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026import org.nuxeo.common.utils.DurationUtils;
027import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
028
029import io.dropwizard.metrics5.MetricAttribute;
030import io.dropwizard.metrics5.MetricFilter;
031import io.dropwizard.metrics5.MetricRegistry;
032import io.opencensus.common.Duration;
033import io.opencensus.exporter.trace.datadog.DatadogTraceConfiguration;
034import io.opencensus.exporter.trace.datadog.DatadogTraceExporter;
035
036/**
037 * Reports traces to Datadog agent.
038 *
039 * @since 11.1
040 */
041public class DatadogTraceReporter extends AbstractMetricsReporter {
042
043    private static final Logger log = LogManager.getLogger(DatadogTraceReporter.class);
044
045    protected static final String TYPE_OPTION = "type";
046
047    protected static final String DEFAULT_TYPE = "web";
048
049    protected boolean activated;
050
051    @Override
052    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
053        log.warn("Creating Datadog Trace reporter");
054        String url = options.get(URL_OPTION);
055        String service = options.getOrDefault(SERVICE_OPTION, DEFAULT_SERVICE);
056        String type = options.getOrDefault(TYPE_OPTION, DEFAULT_TYPE);
057        Duration timeout = Duration.create(
058                DurationUtils.parsePositive(options.get(TIMEOUT_OPTION), DEFAULT_TIMEOUT).getSeconds(), 0);
059        DatadogTraceConfiguration config = DatadogTraceConfiguration.builder()
060                                                                    .setAgentEndpoint(url)
061                                                                    .setService(service)
062                                                                    .setType(type)
063                                                                    .setDeadline(timeout)
064                                                                    .build();
065        try {
066            DatadogTraceExporter.createAndRegister(config);
067        } catch (MalformedURLException e) {
068            throw new IllegalArgumentException(e);
069        }
070        activated = true;
071        enableTracing();
072    }
073
074    @Override
075    public void stop() {
076        log.debug("Stop reporting");
077        if (activated) {
078            DatadogTraceExporter.unregister();
079            activated = false;
080        }
081    }
082}