forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for Observation propagation in kotlin gRPC coroutine server
This commit contains changes that allow to capture current observation when using kotlin gRPC coroutine server. In order to properly propagate current Observation to a coroutine server method, we need to propagate Observation as a context element by extending `CoroutineContextServerInterceptor`. Moreover, current Observation needs to be somehow allowed to capture by `CoroutineContextServerInterceptor`. To do this, we need to open current Observation scope inside `ObservationGrpcServerInterceptor`. It is important to keep these two interceptors in a proper order - first, we need to open current Observation scope, and later create a context element based on it. Fixes: micrometer-metrics#4218
- Loading branch information
Aleksander Brzozowski
committed
Jan 26, 2024
1 parent
8ad56c2
commit 75b3a0d
Showing
7 changed files
with
230 additions
and
2 deletions.
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
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
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
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
48 changes: 48 additions & 0 deletions
48
...tlin/io/micrometer/core/instrument/kotlin/ObservationCoroutineContextServerInterceptor.kt
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,48 @@ | ||
/* | ||
* Copyright 2013-2023 the original author or 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.micrometer.core.instrument.kotlin | ||
|
||
import io.grpc.Metadata | ||
import io.grpc.ServerCall | ||
import io.grpc.kotlin.CoroutineContextServerInterceptor | ||
import io.micrometer.observation.ObservationRegistry | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* This interceptor is meant to propagate observation context to a kotlin coroutine gRPC server method. | ||
* | ||
* Usage: | ||
* | ||
* ``` | ||
* val server = ServerBuilder.forPort(8080) | ||
* .intercept(ObservationCoroutineContextServerInterceptor(observationRegistry)) | ||
* .intercept(ObservationGrpcServerInterceptor(observationRegistry)) | ||
* .build(); | ||
* server.start() | ||
* ``` | ||
* | ||
* Please remember that order of interceptors matters, and it has to be the same as it is in the example above. | ||
* | ||
* @since ??? TODO should we set this property? | ||
*/ | ||
class ObservationCoroutineContextServerInterceptor( | ||
private val observationRegistry: ObservationRegistry, | ||
) : CoroutineContextServerInterceptor() { | ||
override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext { | ||
return observationRegistry.asContextElement() | ||
} | ||
} |
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
115 changes: 115 additions & 0 deletions
115
...re/src/test/kotlin/io/micrometer/core/instrument/kotlin/binder/grpc/GrpcCoroutinesTest.kt
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,115 @@ | ||
/* | ||
* Copyright 2013-2023 the original author or 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.micrometer.core.instrument.kotlin.binder.grpc | ||
|
||
import io.grpc.ManagedChannel | ||
import io.grpc.MethodDescriptor | ||
import io.grpc.Server | ||
import io.grpc.ServerServiceDefinition | ||
import io.grpc.inprocess.InProcessChannelBuilder | ||
import io.grpc.inprocess.InProcessServerBuilder | ||
import io.grpc.kotlin.AbstractCoroutineServerImpl | ||
import io.grpc.kotlin.ServerCalls | ||
import io.grpc.stub.annotations.RpcMethod | ||
import io.grpc.testing.protobuf.SimpleRequest | ||
import io.grpc.testing.protobuf.SimpleResponse | ||
import io.grpc.testing.protobuf.SimpleServiceGrpc | ||
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcServerInterceptor | ||
import io.micrometer.core.instrument.kotlin.ObservationCoroutineContextServerInterceptor | ||
import io.micrometer.observation.Observation | ||
import io.micrometer.observation.ObservationRegistry | ||
import io.micrometer.observation.ObservationTextPublisher | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.ThrowingConsumer | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class GrpcCoroutinesTest { | ||
|
||
val observationRegistry: ObservationRegistry = ObservationRegistry.create() | ||
val echoServiceCoroutine: EchoServiceCoroutine = EchoServiceCoroutine(observationRegistry) | ||
lateinit var server: Server | ||
lateinit var channel: ManagedChannel | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
server = InProcessServerBuilder.forName("sample") | ||
.intercept(ObservationCoroutineContextServerInterceptor(observationRegistry)) | ||
.intercept(ObservationGrpcServerInterceptor(observationRegistry)) | ||
.addService(echoServiceCoroutine) | ||
.build() | ||
server.start() | ||
channel = InProcessChannelBuilder.forName("sample").build() | ||
} | ||
|
||
@AfterEach | ||
fun cleanUp() { | ||
channel.shutdownNow() | ||
server.shutdownNow() | ||
} | ||
|
||
@Test | ||
fun `unary rpc should propagate observation`() { | ||
val stub = SimpleServiceGrpc.newBlockingStub(channel) | ||
val request = SimpleRequest.newBuilder() | ||
.setRequestMessage("hello") | ||
.build() | ||
observationRegistry.observationConfig() | ||
.observationHandler(ObservationTextPublisher()) | ||
|
||
stub.unaryRpc(request) | ||
|
||
assertThat<Observation>(echoServiceCoroutine.lastObservation).isNotNull() | ||
.satisfies( | ||
ThrowingConsumer { observation: Observation -> | ||
assertThat(observation.getContext().contextualName) | ||
.isEqualTo("grpc.testing.SimpleService/UnaryRpc") | ||
}, | ||
) | ||
} | ||
|
||
// This service has the same rpc method that the one defined in SimpleServiceGrpc | ||
class EchoServiceCoroutine(private val observationRegistry: ObservationRegistry) : AbstractCoroutineServerImpl() { | ||
|
||
var lastObservation: Observation? = null | ||
|
||
@RpcMethod( | ||
fullMethodName = "${SimpleServiceGrpc.SERVICE_NAME}/UnaryRpc", | ||
requestType = SimpleRequest::class, | ||
responseType = SimpleResponse::class, | ||
methodType = MethodDescriptor.MethodType.UNARY, | ||
) | ||
fun unaryRpc(request: SimpleRequest): SimpleResponse { | ||
lastObservation = observationRegistry.currentObservation | ||
return SimpleResponse.newBuilder() | ||
.setResponseMessage(request.getRequestMessage()) | ||
.build() | ||
} | ||
|
||
override fun bindService(): ServerServiceDefinition { | ||
return ServerServiceDefinition.builder(SimpleServiceGrpc.SERVICE_NAME) | ||
.addMethod( | ||
ServerCalls.unaryServerMethodDefinition( | ||
context = context, | ||
descriptor = SimpleServiceGrpc.getUnaryRpcMethod(), | ||
implementation = ::unaryRpc, | ||
), | ||
).build() | ||
} | ||
} | ||
} |