Search in sources :

Example 1 with Builder

use of io.micrometer.core.instrument.Timer.Builder in project spring-boot by spring-projects.

the class MetricsRestTemplateCustomizerTests method whenAutoTimingIsDisabledUriTemplateHandlerDoesNotCaptureUris.

@Test
void whenAutoTimingIsDisabledUriTemplateHandlerDoesNotCaptureUris() {
    AtomicBoolean enabled = new AtomicBoolean();
    AutoTimer autoTimer = new AutoTimer() {

        @Override
        public boolean isEnabled() {
            return enabled.get();
        }

        @Override
        public void apply(Builder builder) {
        }
    };
    RestTemplate restTemplate = new RestTemplateBuilder(new MetricsRestTemplateCustomizer(this.registry, new DefaultRestTemplateExchangeTagsProvider(), "http.client.requests", autoTimer)).build();
    MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
    mockServer.expect(MockRestRequestMatchers.requestTo("/first/123")).andExpect(MockRestRequestMatchers.method(HttpMethod.GET)).andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
    mockServer.expect(MockRestRequestMatchers.requestTo("/second/456")).andExpect(MockRestRequestMatchers.method(HttpMethod.GET)).andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
    assertThat(restTemplate.getForObject("/first/{id}", String.class, 123)).isEqualTo("OK");
    assertThat(this.registry.find("http.client.requests").timer()).isNull();
    enabled.set(true);
    assertThat(restTemplate.getForObject(URI.create("/second/456"), String.class)).isEqualTo("OK");
    this.registry.get("http.client.requests").tags("uri", "/second/456").timer();
    this.mockServer.verify();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) Builder(io.micrometer.core.instrument.Timer.Builder) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.jupiter.api.Test)

Example 2 with Builder

use of io.micrometer.core.instrument.Timer.Builder in project spring-boot by spring-projects.

the class AutoTimer method builder.

/**
 * Factory method to create a new {@link Builder Timer.Builder} with auto-timer
 * settings {@link #apply(Timer.Builder) applied}.
 * @param supplier the builder supplier
 * @return a new builder instance with auto-settings applied
 */
default Timer.Builder builder(Supplier<Timer.Builder> supplier) {
    Timer.Builder builder = supplier.get();
    apply(builder);
    return builder;
}
Also used : Builder(io.micrometer.core.instrument.Timer.Builder) Timer(io.micrometer.core.instrument.Timer)

Aggregations

Builder (io.micrometer.core.instrument.Timer.Builder)2 Timer (io.micrometer.core.instrument.Timer)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.jupiter.api.Test)1 AutoTimer (org.springframework.boot.actuate.metrics.AutoTimer)1 RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)1 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)1 RestTemplate (org.springframework.web.client.RestTemplate)1