Skip to content

Commit

Permalink
Add beans for grpc server and client metrics interceptors (#321)
Browse files Browse the repository at this point in the history
* Add beans for grpc server and client metrics interceptors

* Add license

* Make factory package private

* Add tests. Add ability to disable all/client/server

* Make factory internal
  • Loading branch information
jameskleeh committed Nov 22, 2021
1 parent c0e39c4 commit c8072af
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
5 changes: 4 additions & 1 deletion micrometer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
api "io.micronaut:micronaut-inject:$micronautVersion"
implementation "io.projectreactor:reactor-core"
compileOnly "io.micronaut:micronaut-management"
compileOnly "io.grpc:grpc-api"
compileOnly "io.micronaut.sql:micronaut-jdbc"
compileOnly 'io.micronaut.cache:micronaut-cache-core'
compileOnly "io.netty:netty-buffer"
Expand All @@ -31,4 +32,6 @@ dependencies {
testImplementation "io.netty:netty-transport-native-epoll"
testImplementation "io.netty:netty-transport-native-kqueue"
testImplementation "io.micronaut:micronaut-http-server-netty"
}
testImplementation "io.grpc:grpc-api"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2017-2021 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.configuration.metrics.binder.grpc;

import io.grpc.ClientInterceptor;
import io.grpc.Internal;
import io.grpc.ServerInterceptor;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.grpc.MetricCollectingClientInterceptor;
import io.micrometer.core.instrument.binder.grpc.MetricCollectingServerInterceptor;
import io.micronaut.configuration.metrics.annotation.RequiresMetrics;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;

import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS;

/**
* Provides interceptor beans to collect metrics.
*
* @author James Kleeh
* @since 4.1.0
*/
@Internal
@RequiresMetrics
@Requires(property = MICRONAUT_METRICS_BINDERS + ".grpc.enabled", notEquals = StringUtils.FALSE)
@Factory
class GrpcMetricsListenerFactory {

@Singleton
@Requires(classes = ServerInterceptor.class)
@Requires(property = MICRONAUT_METRICS_BINDERS + ".grpc.server.enabled", notEquals = StringUtils.FALSE)
MetricCollectingServerInterceptor grpcServerMetrics(MeterRegistry meterRegistry) {
return new MetricCollectingServerInterceptor(meterRegistry);
}

@Singleton
@Requires(classes = ClientInterceptor.class)
@Requires(property = MICRONAUT_METRICS_BINDERS + ".grpc.client.enabled", notEquals = StringUtils.FALSE)
MetricCollectingClientInterceptor grpcClientMetrics(MeterRegistry meterRegistry) {
return new MetricCollectingClientInterceptor(meterRegistry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.micronaut.configuration.metrics.binder.grpc

import io.micrometer.core.instrument.binder.grpc.MetricCollectingClientInterceptor
import io.micrometer.core.instrument.binder.grpc.MetricCollectingServerInterceptor
import io.micronaut.context.ApplicationContext
import spock.lang.Specification

class GrpcMetricsBinderSpec extends Specification {

void "test beans are created by default"() {
given:
ApplicationContext ctx = ApplicationContext.run()

expect:
ctx.containsBean(MetricCollectingServerInterceptor)
ctx.containsBean(MetricCollectingClientInterceptor)

cleanup:
ctx.close()
}

void "test disabling all grpc metrics"() {
given:
ApplicationContext ctx = ApplicationContext.run([
"micronaut.metrics.binders.grpc.enabled": false
])

expect:
!ctx.containsBean(MetricCollectingServerInterceptor)
!ctx.containsBean(MetricCollectingClientInterceptor)

cleanup:
ctx.close()
}

void "test disabling all metrics"() {
given:
ApplicationContext ctx = ApplicationContext.run([
"micronaut.metrics.enabled": false
])

expect:
!ctx.containsBean(MetricCollectingServerInterceptor)
!ctx.containsBean(MetricCollectingClientInterceptor)

cleanup:
ctx.close()
}

void "test disabling client grpc metrics"() {
given:
ApplicationContext ctx = ApplicationContext.run([
"micronaut.metrics.binders.grpc.client.enabled": false
])

expect:
ctx.containsBean(MetricCollectingServerInterceptor)
!ctx.containsBean(MetricCollectingClientInterceptor)

cleanup:
ctx.close()
}

void "test disabling server grpc metrics"() {
given:
ApplicationContext ctx = ApplicationContext.run([
"micronaut.metrics.binders.grpc.server.enabled": false
])

expect:
!ctx.containsBean(MetricCollectingServerInterceptor)
ctx.containsBean(MetricCollectingClientInterceptor)

cleanup:
ctx.close()
}
}

0 comments on commit c8072af

Please sign in to comment.