Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active May 28, 2021 01:03
Show Gist options
  • Select an option

  • Save sandipchitale/8635cda5176b47f44c051e80921b678e to your computer and use it in GitHub Desktop.

Select an option

Save sandipchitale/8635cda5176b47f44c051e80921b678e to your computer and use it in GitHub Desktop.
Springboot custom metrics #springboot #actuator #metrics
spring:
application:
name: MetricsApplication
management:
endpoints:
web:
exposure:
include:
- metrics
- prometheus
package com.example.metrics;
import java.util.Random;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
@SpringBootApplication
public class MetricsApplication {
@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return (registry) -> registry.config().commonTags("product", "MetricsApplication");
}
@Component
public static class ZeroToHundredGauge {
private Random random = new Random();
public ZeroToHundredGauge(MeterRegistry registry) {
Gauge
.builder("zero_to_hundred_gauge", () -> {
return random.nextInt(100);
})
.register(registry);
}
}
public static void main(String[] args) {
SpringApplication.run(MetricsApplication.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment