-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beans for grpc server and client metrics interceptors (#321)
* 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
1 parent
c0e39c4
commit c8072af
Showing
3 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
.../main/java/io/micronaut/configuration/metrics/binder/grpc/GrpcMetricsListenerFactory.java
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
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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...c/test/groovy/io/micronaut/configuration/metrics/binder/grpc/GrpcMetricsBinderSpec.groovy
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
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() | ||
} | ||
} |