Skip to content

Instantly share code, notes, and snippets.

@jmkgreen
Created October 24, 2016 10:20
Show Gist options
  • Save jmkgreen/a065e980c17cc14f69b4b4885a6ea1dc to your computer and use it in GitHub Desktop.
Save jmkgreen/a065e980c17cc14f69b4b4885a6ea1dc to your computer and use it in GitHub Desktop.
Why does postContruct() get called twice...
package com.foo.server;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
* Configurable use of `metrics-jvm` from DropWizard - collects garbage collection and memory usage by default.
*
* See also http://stackoverflow.com/questions/32215723/exporting-spring-boot-actuator-metrics-dropwizard-metrics-to-statsd
* Created by jgreen on 05/10/2016.
*/
@Configuration
@ConfigurationProperties(prefix = "dropwizard")
public class DropWizardMetricSets {
@Autowired
private MetricRegistry metricRegistry;
private boolean garbageCollectorReporting = false;
private boolean memoryUsageReporting = false;
public void setGarbageCollectorReporting(boolean garbageCollectorReporting) {
this.garbageCollectorReporting = garbageCollectorReporting;
}
public void setMemoryUsageReporting(boolean memoryUsageReporting) {
this.memoryUsageReporting = memoryUsageReporting;
}
@PostConstruct
public void postConstruct() {
if (garbageCollectorReporting) {
metricRegistry.register("jvm.gc", new GarbageCollectorMetricSet());
}
if (memoryUsageReporting) {
metricRegistry.register("jvm.mem", new MemoryUsageGaugeSet());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment