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