001/*
002 * (C) Copyright 2006-2018 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 *     dmetzler
018 */
019package org.nuxeo.datadog.reporter;
020
021import java.util.concurrent.TimeUnit;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.coursera.metrics.datadog.DatadogReporter;
027import org.coursera.metrics.datadog.DatadogReporter.Expansion;
028import org.coursera.metrics.datadog.DefaultMetricNameFormatter;
029import org.coursera.metrics.datadog.transport.HttpTransport;
030import org.nuxeo.runtime.metrics.MetricsService;
031import org.nuxeo.runtime.model.ComponentContext;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035import com.codahale.metrics.MetricRegistry;
036import com.codahale.metrics.SharedMetricRegistries;
037
038public class DatadogReporterServiceImpl extends DefaultComponent implements DatadogReporterService {
039
040    protected final MetricRegistry metrics = SharedMetricRegistries.getOrCreate(MetricsService.class.getName());
041
042    private DatadogReporter reporter;
043
044    private DatadogReporterConfDescriptor conf;
045
046    private static final Log log = LogFactory.getLog(DatadogReporterService.class);
047
048    @Override
049    public void start(ComponentContext context) {
050        if (reporter != null) {
051            startReporter();
052        }
053    }
054
055    @Override
056    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
057        if ("configuration".equals(extensionPoint)) {
058            setConfiguration((DatadogReporterConfDescriptor) contribution);
059        }
060    }
061
062    private void setConfiguration(DatadogReporterConfDescriptor conf) {
063        if (StringUtils.isBlank(conf.getApiKey())) {
064            log.error("Datadog reporter service is not well configured : apiKey is empty. Your metrics won't be sent.");
065        } else {
066            this.conf = conf;
067            buildReporter();
068        }
069    }
070
071    private void buildReporter() {
072        HttpTransport httpTransport = new HttpTransport.Builder().withApiKey(conf.getApiKey()).build();
073        reporter = DatadogReporter.forRegistry(metrics)//
074                                  .withHost(conf.getHost())//
075                                  .withTags(conf.getTags())
076                                  .withTransport(httpTransport)//
077                                  .withExpansions(Expansion.ALL)//
078                                  .withMetricNameFormatter(new DefaultMetricNameFormatter())//
079                                  .build();
080
081    }
082
083    @Override
084    public void startReporter() {
085        if (reporter != null) {
086            log.info("Starting Datadog reporter");
087            reporter.start(conf.getPollInterval(), TimeUnit.SECONDS);
088        }
089    }
090
091    @Override
092    public void stopReporter() {
093        log.info("Stopping Datadog reporter");
094        reporter.stop();
095    }
096
097    DatadogReporter getReporter() {
098        return reporter;
099    }
100
101    DatadogReporterConfDescriptor getConfig() {
102        return conf;
103    }
104
105}