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 java.lang.Integer.parseInt; 021import static java.lang.System.getProperty; 022 023import java.net.InetSocketAddress; 024 025import org.springframework.context.annotation.Bean; 026import org.springframework.context.annotation.Configuration; 027import org.springframework.context.annotation.Profile; 028 029import com.codahale.metrics.JmxReporter; 030import com.codahale.metrics.graphite.Graphite; 031import com.codahale.metrics.graphite.GraphiteReporter; 032 033/** 034 * Configuration class for Metrics reporting to Graphite and JMX. 035 * <p> 036 * To enable Metrics reporting to Graphite, activate the Spring profile 037 * "metrics.graphite". The system properties fcrepo.metrics.host and 038 * fcrepo.metrics.port can also be set (defaults to "localhost" and 2003, 039 * respectively. 040 * </p> 041 * <p> 042 * To enable Metrics reporting to JMX, activate the Spring profile 043 * "metrics.jmx". 044 * </p> 045 * <p> 046 * To enable both Graphite and JMX reporting, the Spring profile "metrics", can 047 * be used instead of specifying both metrics.graphite and metrics.jmx, e.g.: 048 * </p> 049 * <blockquote><code>-Dspring.profiles.active="metrics"</code></blockquote> 050 * 051 * @author Edwin Shin 052 */ 053@Configuration 054public class MetricsConfig { 055 056 public static final String METRIC_PREFIX = getProperty("fcrepo.metrics.prefix", "org.fcrepo"); 057 058 /** 059 * Provide the reporter factory to Spring 060 * 061 * @return the reporter factory 062 */ 063 @Bean 064 public ReporterFactory reporterFactory() { 065 return new ReporterFactory(); 066 } 067 068 /** 069 * <p> 070 * Metrics configuration for Graphite reporting. 071 * </p> 072 * <p> 073 * Graphite reporting can be enabled by activating the "metrics.graphite" 074 * Spring profile. 075 * </p> 076 */ 077 @Configuration 078 @Profile({"metrics", "metrics.graphite"}) 079 public static class GraphiteConfig { 080 081 /** 082 * <p> 083 * Host and port may be configured with system properties 084 * "fcrepo.metrics.host" and "fcrepo.metrics.port", respectively. 085 * </p> 086 * <p> 087 * Host and port default to "localhost" and "2003", respectively. 088 * </p> 089 * 090 * @return a Graphite client to a Carbon server 091 */ 092 @Bean 093 public Graphite graphiteClient() { 094 final String hostname = 095 getProperty("fcrepo.metrics.host", "localhost"); 096 final int port = 097 parseInt(getProperty("fcrepo.metrics.port", "2003")); 098 099 return new Graphite(new InetSocketAddress(hostname, port)); 100 } 101 102 /** 103 * @return a Reporter which publishes metrics to a Graphite server 104 */ 105 @Bean 106 public GraphiteReporter graphiteReporter() { 107 final MetricsConfig cfg = new MetricsConfig(); 108 final String prefix = METRIC_PREFIX; 109 return cfg.reporterFactory().getGraphiteReporter(prefix, 110 graphiteClient()); 111 } 112 } 113 114 /** 115 * <p> 116 * JMX configuration for metrics reporting. 117 * </p> 118 * <p> 119 * JMX reporting can be enabled by activating the "metrics.jmx" Spring 120 * profile. 121 * </p> 122 */ 123 @Configuration 124 @Profile({"metrics", "metrics.jmx"}) 125 public static class JmxConfig { 126 127 /** 128 * @return a Reporter that exposes metrics under the "org.fcrepo" prefix 129 */ 130 @Bean 131 public JmxReporter jmxReporter() { 132 final MetricsConfig cfg = new MetricsConfig(); 133 final String prefix = METRIC_PREFIX; 134 return cfg.reporterFactory().getJmxReporter(prefix); 135 } 136 } 137}