Created
March 28, 2022 23:10
-
-
Save mesketh/bd21c5733f084cccdebdb4c85531ce35 to your computer and use it in GitHub Desktop.
example percentiles - spring boot
This file contains 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 org.eclipse.jkube.maven.sample.spring.boot; | |
import io.prometheus.client.CollectorRegistry; | |
import io.prometheus.client.Histogram; | |
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Lazy; | |
@Configuration | |
@ConditionalOnProperty(value = "custom-metric.enabled", havingValue = "true") | |
public class AppConfig { | |
@Bean | |
@Lazy | |
public Histogram requestDurationHistogram(CollectorRegistry collectorRegistry) { | |
return Histogram.build() | |
.name("request_duration") | |
.help("Latency for HTTP request.") | |
.linearBuckets(0.1,0.1, 40) | |
.labelNames() | |
.register(collectorRegistry); | |
} | |
} |
This file contains 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 org.eclipse.jkube.maven.sample.spring.boot; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface CollectRequestDuration { | |
} |
This file contains 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 org.eclipse.jkube.maven.sample.spring.boot; | |
import io.prometheus.client.Histogram; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | |
import org.springframework.stereotype.Component; | |
@Aspect | |
@Component | |
@ConditionalOnBean(AppConfig.class) | |
public class RecordRequestDurationMetric { | |
private Histogram requestDurationHistogram; | |
public RecordRequestDurationMetric(Histogram requestDurationHistogram) { | |
this.requestDurationHistogram = requestDurationHistogram; | |
} | |
@Around("@annotation(CollectRequestDuration)") | |
public Object recordRequestDuration(ProceedingJoinPoint pjp) throws Throwable { | |
Histogram.Timer timer = requestDurationHistogram.startTimer(); | |
Object result; | |
try { | |
result = pjp.proceed(); | |
} finally { | |
timer.observeDuration(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment