001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.metrics; 019 020import static com.codahale.metrics.MetricFilter.ALL; 021import static java.lang.management.ManagementFactory.getPlatformMBeanServer; 022import static java.util.concurrent.TimeUnit.MILLISECONDS; 023import static java.util.concurrent.TimeUnit.MINUTES; 024import static java.util.concurrent.TimeUnit.SECONDS; 025import static org.slf4j.LoggerFactory.getLogger; 026 027import javax.management.MBeanServer; 028 029import org.slf4j.Logger; 030 031import com.codahale.metrics.JmxReporter; 032import com.codahale.metrics.graphite.Graphite; 033import com.codahale.metrics.graphite.GraphiteReporter; 034 035/** 036 * Helpers for making the upstream metrics reporters play nice with Springg 037 * 038 * @author cbeer 039 * @since Mar 22, 2013 040 */ 041public class ReporterFactory { 042 043 private static final Logger LOGGER = getLogger(ReporterFactory.class); 044 045 private RegistryService registryService = RegistryService.getInstance(); 046 047 /** 048 * Start a new GraphiteReporter, with reports every minute 049 * 050 * @param prefix graphite metrics prefix 051 * @param g a graphite client instance 052 * @return a new GraphiteReporter 053 */ 054 public GraphiteReporter getGraphiteReporter(final String prefix, 055 final Graphite g) { 056 final GraphiteReporter reporter = 057 GraphiteReporter.forRegistry(registryService.getMetrics()).prefixedWith(prefix) 058 .convertRatesTo(SECONDS).convertDurationsTo( 059 MILLISECONDS).filter(ALL).build(g); 060 reporter.start(1, MINUTES); 061 LOGGER.debug("Started GraphiteReporter"); 062 return reporter; 063 } 064 065 /** 066 * Publish metrics to JMX 067 * 068 * @param prefix the prefix 069 * @return a JMXReporter 070 */ 071 public JmxReporter getJmxReporter(final String prefix) { 072 final MBeanServer mbs = getPlatformMBeanServer(); 073 final JmxReporter reporter = 074 JmxReporter.forRegistry(registryService.getMetrics()).registerWith(mbs) 075 .inDomain("org.fcrepo") 076 .convertDurationsTo(MILLISECONDS).convertRatesTo( 077 SECONDS).filter(ALL).build(); 078 reporter.start(); 079 LOGGER.debug("Started JmxReporter"); 080 return reporter; 081 } 082 083}