Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disabling metrics #882

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import java.util.Optional;

import static io.micronaut.core.util.StringUtils.TRUE;
import static io.micronaut.core.util.StringUtils.FALSE;
import static io.micronaut.http.HttpAttributes.URI_TEMPLATE;

/**
Expand All @@ -44,7 +44,7 @@
*/
@ClientFilter("${micronaut.metrics.http.client.path:/**}")
@RequiresMetrics
@Requires(bean = HttpMetricsConfig.class, beanProperty = "enabled", value = TRUE)
@Requires(property = HttpMetricsConfig.ENABLED, notEquals = FALSE)
@Requires(condition = WebMetricsClientCondition.class)
@Internal
final class ClientMetricsFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.Optional;
import java.util.function.Supplier;

import static io.micronaut.core.util.StringUtils.TRUE;
import static io.micronaut.core.util.StringUtils.FALSE;

/**
* Registers the timers and meters for each request.
Expand All @@ -47,7 +47,7 @@
*/
@ServerFilter("${micronaut.metrics.http.path:/**}")
@RequiresMetrics
@Requires(bean = HttpMetricsConfig.class, beanProperty = "enabled", value = TRUE)
@Requires(property = HttpMetricsConfig.ENABLED, notEquals = FALSE)
@Requires(condition = WebMetricsServerCondition.class)
@Internal
final class ServerMetricsFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpAttributes;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpResponseProvider;
Expand All @@ -31,8 +30,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.configuration.metrics.binder.web.config;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.Context;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.util.Toggleable;

Expand All @@ -27,6 +28,7 @@
* @author Denis Stepanov
* @since 5.9
*/
@Context
@ConfigurationProperties(HttpMetricsConfig.PATH)
public final class HttpMetricsConfig implements Toggleable {

Expand All @@ -35,6 +37,8 @@ public final class HttpMetricsConfig implements Toggleable {
*/
public static final String PATH = MICRONAUT_METRICS_BINDERS + ".web";

public static final String ENABLED = MICRONAUT_METRICS_BINDERS + ".web.enabled";

private boolean enabled = true;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.micronaut.configuration.metrics.binder.web


import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.search.MeterNotFoundException
import io.micronaut.configuration.metrics.binder.web.config.HttpClientMeterConfig
import io.micronaut.configuration.metrics.binder.web.config.HttpServerMeterConfig
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Context
import io.micronaut.context.annotation.Requires
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.Specification

class HttpContextMetricsSpec extends Specification {

void "test disabling metrics"() {
when:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, ['micronaut.metrics.binders.web.enabled' : false, 'spec.name': getClass().getSimpleName()])
def context = embeddedServer.applicationContext
TestClient client = context.getBean(TestClient)

then:
client.index() == 'ok'

when:
MeterRegistry registry = context.getBean(MeterRegistry)

registry.get(HttpServerMeterConfig.REQUESTS_METRIC).tags('uri', '/test-http-metrics').timer()
registry.get(HttpClientMeterConfig.REQUESTS_METRIC).tags('uri', '/test-http-metrics').timer()

then:
thrown(MeterNotFoundException)

cleanup:
embeddedServer.close()

}

@Requires(property = "spec.name", value = "HttpContextMetricsSpec")
@Context
@Client('/')
static interface TestClient {

@Get('/test-http-metrics')
String index()

}

@Requires(property = "spec.name", value = "HttpContextMetricsSpec")
@Context
@Controller('/')
static class TestController {

@Get('/test-http-metrics')
String index() { "ok" }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.micrometer.core.instrument.search.MeterNotFoundException
import io.micronaut.configuration.metrics.binder.web.config.HttpClientMeterConfig
import io.micronaut.configuration.metrics.binder.web.config.HttpServerMeterConfig
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.core.util.CollectionUtils
import io.micronaut.http.HttpResponse
import io.micronaut.http.annotation.Controller
Expand Down Expand Up @@ -38,7 +39,7 @@ class HttpMetricsSpec extends Specification {

void "test client / server metrics with #cfg #setting"() {
when:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [(cfg): setting])
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [(cfg): setting, 'spec.name': getClass().getSimpleName()])
def context = embeddedServer.applicationContext
TestClient client = context.getBean(TestClient)

Expand Down Expand Up @@ -154,6 +155,7 @@ class HttpMetricsSpec extends Specification {
when:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'micronaut.metrics.binders.web.client-errors-uris.enabled': false,
'spec.name': getClass().getSimpleName()
])
def context = embeddedServer.getApplicationContext()
TestClient client = context.getBean(TestClient)
Expand Down Expand Up @@ -216,7 +218,7 @@ class HttpMetricsSpec extends Specification {

void "test getting the beans #cfg #setting"() {
when:
ApplicationContext context = ApplicationContext.run([(cfg): setting])
ApplicationContext context = ApplicationContext.run([(cfg): setting, 'spec.name': getClass().getSimpleName()])

then:
context.findBean(ClientMetricsFilter).isPresent() == setting
Expand All @@ -235,14 +237,15 @@ class HttpMetricsSpec extends Specification {

void "test websocket"() {
when:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [(MICRONAUT_METRICS_ENABLED): true])
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [(MICRONAUT_METRICS_ENABLED): true, 'spec.name': getClass().getSimpleName()])
MeterRegistry registry = embeddedServer.getApplicationContext().getBean(MeterRegistry)
createWebSocketClient(embeddedServer.getApplicationContext(), embeddedServer.getPort(), "Travolta")

then:
registry.get(HttpServerMeterConfig.REQUESTS_METRIC).tags('uri', '/ws/{username}').timer()
}

@Requires(property = "spec.name", value = "HttpMetricsSpec")
@ClientWebSocket
static abstract class TestWebSocketClient implements AutoCloseable {
abstract void send(@NonNull @NotBlank String message);
Expand All @@ -263,6 +266,7 @@ class HttpMetricsSpec extends Specification {
return Flux.from(client).blockFirst()
}

@Requires(property = "spec.name", value = "HttpMetricsSpec")
@Client('/')
static interface TestClient {
@Get
Expand All @@ -287,6 +291,7 @@ class HttpMetricsSpec extends Specification {
HttpResponse notFound()
}

@Requires(property = "spec.name", value = "HttpMetricsSpec")
@Controller('/')
static class TestController {
@Get
Expand Down Expand Up @@ -323,6 +328,7 @@ class HttpMetricsSpec extends Specification {

}

@Requires(property = "spec.name", value = "HttpMetricsSpec")
@ServerWebSocket("/ws/{username}")
static class TestWSController {

Expand Down
Loading