/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.constellation.conf;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.FileDescriptorRatioGauge;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import static org.constellation.setup.CstlInstaller.HEALTH_CHECK_REGISTRY;
import static org.constellation.setup.CstlInstaller.METRIC_REGISTRY;
@Configuration
@EnableMetrics(proxyTargetClass = true)
public class MetricsConfiguration extends MetricsConfigurerAdapter {
private final Logger log = LoggerFactory.getLogger(MetricsConfiguration.class);
@Inject
private Environment env;
@Inject
private DataSource dataSource;
// @Inject
// private JavaMailSenderImpl javaMailSender;
@Override
public MetricRegistry getMetricRegistry() {
return METRIC_REGISTRY;
}
@Override
public HealthCheckRegistry getHealthCheckRegistry() {
return HEALTH_CHECK_REGISTRY;
}
@PostConstruct
public void init() {
log.debug("Registring JVM gauges");
METRIC_REGISTRY.register("jvm.memory", new MemoryUsageGaugeSet());
METRIC_REGISTRY.register("jvm.garbage", new GarbageCollectorMetricSet());
METRIC_REGISTRY.register("jvm.threads", new ThreadStatesGaugeSet());
METRIC_REGISTRY.register("jvm.files", new FileDescriptorRatioGauge());
METRIC_REGISTRY.register("jvm.buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()));
log.debug("Initializing Metrics healthchecks");
// HEALTH_CHECK_REGISTRY.register("database", new DatabaseHealthCheck(dataSource));
// HEALTH_CHECK_REGISTRY.register("email", new JavaMailHealthCheck(javaMailSender));
}
@Override
public void configureReporters(MetricRegistry metricRegistry) {
log.info("Initializing Metrics JMX reporting");
final JmxReporter jmxReporter = JmxReporter.forRegistry(METRIC_REGISTRY).build();
jmxReporter.start();
if (env.acceptsProfiles("prod")) {
String graphiteHost = env.getProperty("metrics.graphite.host");
if (graphiteHost != null) {
log.info("Initializing Metrics Graphite reporting");
Integer graphitePort = env.getProperty("metrics.graphite.port", Integer.class);
Graphite graphite = new Graphite(new InetSocketAddress(graphiteHost, graphitePort));
GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(METRIC_REGISTRY)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(graphite);
graphiteReporter.start(1, TimeUnit.MINUTES);
} else {
log.warn("Graphite server is not configured, unable to send any data to Graphite");
}
}
}
}