Last active
May 28, 2021 01:03
-
-
Save sandipchitale/8635cda5176b47f44c051e80921b678e to your computer and use it in GitHub Desktop.
Springboot custom metrics #springboot #actuator #metrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| spring: | |
| application: | |
| name: MetricsApplication | |
| management: | |
| endpoints: | |
| web: | |
| exposure: | |
| include: | |
| - metrics | |
| - prometheus |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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