GitBucket
4.23.0
Toggle navigation
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
sample-java
/
sample-prometheus1
Browse code
fix percentile sample
1 parent
c7bb2ca
commit
fad29ca5eee39cfd8a2ca89e31917bf634467f1b
yhornisse
authored
on 22 Oct 2023
Patch
Showing
1 changed file
src/main/java/com/sample/controller/SampleController.java
Ignore Space
Show notes
View
src/main/java/com/sample/controller/SampleController.java
package com.sample.controller; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.Timer; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.exporter.common.TextFormat; import java.util.Random; import java.util.concurrent.TimeUnit; import lombok.Data; import lombok.Builder; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/sample") public class SampleController { private final PrometheusMeterRegistry prometheusMeterRegistry; private final Counter helloCounter; public SampleController() { prometheusMeterRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); helloCounter = prometheusMeterRegistry.counter("hoge.counter", "group", "A", "sub-group", "B"); } @GetMapping("/prometheus") public ResponseEntity<String> prometheus() { return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, TextFormat.CONTENT_TYPE_004) .body(prometheusMeterRegistry.scrape()); } @GetMapping("/hello") public SampleDto hello() { helloCounter.increment(); Timer.builder("hoge.timer") .tag("group", "A") .publishPercentiles(0.5, 0.95, 0.99) .register(prometheusMeterRegistry) .record(() -> { try { Random rand = new Random(); int num = rand.nextInt(3); TimeUnit.SECONDS.sleep(num + 1); } catch (Exception e) { throw new RuntimeException(e); } }); return SampleDto.builder().msg("hello").build(); } @Builder @Data private static class SampleDto { private String msg; } }
package com.sample.controller; import io.micrometer.core.instrument.Counter; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.exporter.common.TextFormat; import lombok.Data; import lombok.Builder; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/sample") public class SampleController { private final PrometheusMeterRegistry prometheusMeterRegistry; private final Counter helloCounter; public SampleController() { prometheusMeterRegistry= new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); helloCounter = prometheusMeterRegistry.counter("hoge.counter", "group", "A", "sub-group", "B"); } @GetMapping("/prometheus") public ResponseEntity<String> prometheus() { return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, TextFormat.CONTENT_TYPE_004) .body(prometheusMeterRegistry.scrape()); } @GetMapping("/hello") public SampleDto hello() { helloCounter.increment(); return SampleDto.builder().msg("hello").build(); } @Builder @Data private static class SampleDto { private String msg; } }
Show line notes below