|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.parallax.server.blocklyprop.monitoring; |
| 7 | + |
| 8 | +import com.codahale.metrics.ConsoleReporter; |
| 9 | +import com.codahale.metrics.MetricFilter; |
| 10 | +import com.codahale.metrics.MetricRegistry; |
| 11 | +import com.codahale.metrics.graphite.GraphiteReporter; |
| 12 | +import com.codahale.metrics.graphite.PickledGraphite; |
| 13 | +import com.codahale.metrics.jvm.GarbageCollectorMetricSet; |
| 14 | +import com.codahale.metrics.jvm.MemoryUsageGaugeSet; |
| 15 | +import com.codahale.metrics.log4j.InstrumentedAppender; |
| 16 | +import com.google.inject.Inject; |
| 17 | +import com.google.inject.Singleton; |
| 18 | +import java.net.InetSocketAddress; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import org.apache.commons.configuration.Configuration; |
| 21 | +import org.apache.log4j.LogManager; |
| 22 | + |
| 23 | +/** |
| 24 | + * |
| 25 | + * @author Michel |
| 26 | + */ |
| 27 | +@Singleton |
| 28 | +public class Monitor { |
| 29 | + |
| 30 | + private static final MetricRegistry metrics = new MetricRegistry(); |
| 31 | + |
| 32 | + private final boolean consoleEnabled; |
| 33 | + private final int consoleReportingInterval; |
| 34 | + |
| 35 | + private final boolean graphiteEnabled; |
| 36 | + private final String graphitePrefix; |
| 37 | + private final String graphiteServerAddress; |
| 38 | + private final int graphiteServerPort; |
| 39 | + private final int graphiteReportingInterval; |
| 40 | + |
| 41 | + @Inject |
| 42 | + public Monitor(Configuration configuration) { |
| 43 | + consoleEnabled = configuration.getBoolean("monitor.console.enabled", false); |
| 44 | + consoleReportingInterval = configuration.getInt("monitor.console.interval", 300); |
| 45 | + |
| 46 | + graphiteEnabled = configuration.getBoolean("monitor.graphite.enabled", false); |
| 47 | + graphitePrefix = configuration.getString("monitor.graphite.prefix", "blocklyprop"); |
| 48 | + graphiteServerAddress = configuration.getString("monitor.graphite.address", "localhost"); |
| 49 | + graphiteServerPort = configuration.getInt("monitor.graphite.port", 2003); |
| 50 | + graphiteReportingInterval = configuration.getInt("monitor.graphite.interval", 30); |
| 51 | + |
| 52 | + init(); |
| 53 | + } |
| 54 | + |
| 55 | + private void init() { |
| 56 | + if (consoleEnabled) { |
| 57 | + ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertDurationsTo(TimeUnit.MILLISECONDS).build(); |
| 58 | + reporter.start(consoleReportingInterval, TimeUnit.SECONDS); |
| 59 | + } |
| 60 | + |
| 61 | + if (graphiteEnabled) { |
| 62 | + final PickledGraphite pickledGraphite = new PickledGraphite(new InetSocketAddress(graphiteServerAddress, graphiteServerPort)); |
| 63 | + final GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(metrics).prefixedWith(graphitePrefix).convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL).build(pickledGraphite); |
| 64 | + graphiteReporter.start(graphiteReportingInterval, TimeUnit.SECONDS); |
| 65 | + } |
| 66 | + |
| 67 | + InstrumentedAppender appender = new InstrumentedAppender(metrics); |
| 68 | + appender.activateOptions(); |
| 69 | + |
| 70 | + LogManager.getRootLogger().addAppender(appender); |
| 71 | + |
| 72 | + MemoryUsageGaugeSet memoryUsageGaugeSet = new MemoryUsageGaugeSet(); |
| 73 | + metrics.registerAll(memoryUsageGaugeSet); |
| 74 | + |
| 75 | + GarbageCollectorMetricSet garbageCollectorMetricSet = new GarbageCollectorMetricSet(); |
| 76 | + metrics.registerAll(garbageCollectorMetricSet); |
| 77 | + } |
| 78 | + |
| 79 | + public static MetricRegistry metrics() { |
| 80 | + return metrics; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments