001/** 002 * Copyright 2015 DuraSpace, Inc. 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 */ 016package org.fcrepo.metrics; 017 018import static com.codahale.metrics.MetricFilter.ALL; 019import static java.lang.management.ManagementFactory.getPlatformMBeanServer; 020import static java.util.concurrent.TimeUnit.MILLISECONDS; 021import static java.util.concurrent.TimeUnit.MINUTES; 022import static java.util.concurrent.TimeUnit.SECONDS; 023import static org.slf4j.LoggerFactory.getLogger; 024 025import javax.management.MBeanServer; 026 027import org.slf4j.Logger; 028 029import com.codahale.metrics.JmxReporter; 030import com.codahale.metrics.graphite.Graphite; 031import com.codahale.metrics.graphite.GraphiteReporter; 032 033/** 034 * Helpers for making the upstream metrics reporters play nice with Springg 035 * 036 * @author cbeer 037 * @since Mar 22, 2013 038 */ 039public class ReporterFactory { 040 041 private static final Logger LOGGER = getLogger(ReporterFactory.class); 042 043 private RegistryService registryService = RegistryService.getInstance(); 044 045 /** 046 * Start a new GraphiteReporter, with reports every minute 047 * 048 * @param prefix graphite metrics prefix 049 * @param g a graphite client instance 050 * @return a new GraphiteReporter 051 */ 052 public GraphiteReporter getGraphiteReporter(final String prefix, 053 final Graphite g) { 054 final GraphiteReporter reporter = 055 GraphiteReporter.forRegistry(registryService.getMetrics()).prefixedWith(prefix) 056 .convertRatesTo(SECONDS).convertDurationsTo( 057 MILLISECONDS).filter(ALL).build(g); 058 reporter.start(1, MINUTES); 059 LOGGER.debug("Started GraphiteReporter"); 060 return reporter; 061 } 062 063 /** 064 * Publish metrics to JMX 065 * 066 * @param prefix the prefix 067 * @return a JMXReporter 068 */ 069 public JmxReporter getJmxReporter(final String prefix) { 070 final MBeanServer mbs = getPlatformMBeanServer(); 071 final JmxReporter reporter = 072 JmxReporter.forRegistry(registryService.getMetrics()).registerWith(mbs) 073 .inDomain("org.fcrepo") 074 .convertDurationsTo(MILLISECONDS).convertRatesTo( 075 SECONDS).filter(ALL).build(); 076 reporter.start(); 077 LOGGER.debug("Started JmxReporter"); 078 return reporter; 079 } 080 081}