001/* 002 * (C) Copyright 2006-2015 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * dmetzler 016 */ 017package org.nuxeo.datadog.reporter; 018 019import static org.coursera.metrics.datadog.DatadogReporter.Expansion.COUNT; 020import static org.coursera.metrics.datadog.DatadogReporter.Expansion.MEDIAN; 021import static org.coursera.metrics.datadog.DatadogReporter.Expansion.P95; 022import static org.coursera.metrics.datadog.DatadogReporter.Expansion.P99; 023import static org.coursera.metrics.datadog.DatadogReporter.Expansion.RATE_15_MINUTE; 024import static org.coursera.metrics.datadog.DatadogReporter.Expansion.RATE_1_MINUTE; 025 026import java.util.EnumSet; 027import java.util.concurrent.TimeUnit; 028 029import org.apache.commons.lang.StringUtils; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.coursera.metrics.datadog.DatadogReporter; 033import org.coursera.metrics.datadog.DatadogReporter.Expansion; 034import org.coursera.metrics.datadog.DefaultMetricNameFormatter; 035import org.coursera.metrics.datadog.transport.HttpTransport; 036import org.nuxeo.runtime.metrics.MetricsService; 037import org.nuxeo.runtime.model.ComponentContext; 038import org.nuxeo.runtime.model.ComponentInstance; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041import com.codahale.metrics.MetricRegistry; 042import com.codahale.metrics.SharedMetricRegistries; 043 044public class DatadogReporterServiceImpl extends DefaultComponent implements DatadogReporterService { 045 046 protected final MetricRegistry metrics = SharedMetricRegistries.getOrCreate(MetricsService.class.getName()); 047 048 private DatadogReporter reporter; 049 050 private DatadogReporterConfDescriptor conf; 051 052 private static final Log log = LogFactory.getLog(DatadogReporterService.class); 053 054 @Override 055 public void applicationStarted(ComponentContext context) { 056 if (reporter != null) { 057 startReporter(); 058 } 059 } 060 061 @Override 062 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 063 if ("configuration".equals(extensionPoint)) { 064 setConfiguration((DatadogReporterConfDescriptor) contribution); 065 } 066 } 067 068 private void setConfiguration(DatadogReporterConfDescriptor conf) { 069 if (StringUtils.isBlank(conf.getApiKey())) { 070 log.error("Datadog reporter service is not well configured : apiKey is empty. Your metrics won't be sent."); 071 } else { 072 this.conf = conf; 073 buildReporter(); 074 } 075 } 076 077 private void buildReporter() { 078 079 HttpTransport httpTransport = new HttpTransport.Builder().withApiKey(conf.getApiKey()).build(); 080 reporter = DatadogReporter.forRegistry(metrics)// 081 .withHost(conf.getHost())// 082 .withTags(conf.getTags()) 083 .withTransport(httpTransport)// 084 .withExpansions(Expansion.ALL)// 085 .withMetricNameFormatter(new DefaultMetricNameFormatter())// 086 .build(); 087 088 } 089 090 @Override 091 public void startReporter() { 092 if (reporter != null) { 093 log.info("Starting Datadog reporter"); 094 reporter.start(conf.getPollInterval(), TimeUnit.SECONDS); 095 } 096 } 097 098 @Override 099 public void stopReporter() { 100 log.info("Stopping Datadog reporter"); 101 reporter.stop(); 102 } 103 104 DatadogReporter getReporter() { 105 return reporter; 106 } 107 108 DatadogReporterConfDescriptor getConfig() { 109 return conf; 110 } 111 112}