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

ObservationGrpcServerInterceptor does not store observation in a context #4218

Closed
AleksanderBrzozowski opened this issue Oct 11, 2023 · 9 comments · Fixed by #4284
Closed
Labels
enhancement A general enhancement module: micrometer-core An issue that is related to our core module
Milestone

Comments

@AleksanderBrzozowski
Copy link
Contributor

Please describe the feature request.
Let's say that we have a gRPC server written in kotlin with coroutines support enabled:

class ExampleServer(private val registry: ObservationRegistry) :ServiceGrpcKt.CoroutineImplBase() {
    override suspend fun exampleMethod(request: Request): Response {
        // logic
    }

And let's assume that ObservationGrpcServerInterceptor was added as an interceptor.

When we try to get the current observation from the registry, it always return null, which is expected as suspended function are ran in a different thread:

override suspend fun exampleMethod(request: Request): Response {
    this.registry.currentObservation // returns null
}

I wonder how we can fix this, so that we can get the current observation the same as we do for blocking API.

It would be nice if the existing function could be used to retrieve the current observation created by the interceptor:

class ExampleServer(private val registry: ObservationRegistry) :ServiceGrpcKt.CoroutineImplBase() {
    override suspend fun exampleMethod(request: Request): Response {
        coroutineContext.currentObservation()
    }

Rationale
It is currently not possible to create a child observation based on the parent one that was created in the gRPC Server Interceptor.

Additional context
#3256

AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Oct 27, 2023
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
AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Oct 27, 2023
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
AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Oct 27, 2023
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
AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Oct 27, 2023
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
@jonatan-ivanov
Copy link
Member

jonatan-ivanov commented Dec 14, 2023

So it seems there is a missing instrumentation and not the existing one having some bugs, right?
This could be related: micrometer-metrics/tracing#174

@AleksanderBrzozowski
Copy link
Contributor Author

@jonatan-ivanov
Yeah, exactly, you can see my PR to find how this might be added 🙂

I don't think that the discussion you have mentioned is related, though. It seems to be about context propagation to the REST calls, and in my case it is gRPC call 🙂

I will be happy if you can take a look at the PR that I provided 🙂

@marcingrzejszczak
Copy link
Contributor

Thank you @AleksanderBrzozowski for your PR. We will look into that ASAP (we haven't forgotten about your PR :) )

@AleksanderBrzozowski
Copy link
Contributor Author

@marcingrzejszczak Thanks! 🙂 I haven't forgotten either! 😄

@AleksanderBrzozowski
Copy link
Contributor Author

@marcingrzejszczak Hello! :) How is it going? Were you able to take a look at the PR? :)

@marcingrzejszczak
Copy link
Contributor

Not yet, the PR however needs some conflict resolution.

@KostadinAlmishev
Copy link

@AleksanderBrzozowski Can you fix conflicts, please. I have same problem and I am waiting this fix :)

@AleksanderBrzozowski
Copy link
Contributor Author

Yup, I will try to fix conflicts today 🙂

AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Jan 26, 2024
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
AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Jan 26, 2024
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
AleksanderBrzozowski pushed a commit to AleksanderBrzozowski/micrometer that referenced this issue Jan 26, 2024
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
@AleksanderBrzozowski
Copy link
Contributor Author

Conflicts fixed, @marcingrzejszczak you can proceed with review 😃

marcingrzejszczak pushed a commit that referenced this issue Feb 2, 2024
…er (#4284)

* 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: #4218

* set license & set 'since' property for a public class

---------

Co-authored-by: Aleksander Brzozowski <aleksander.brzozowski@sinch.com>
@jonatan-ivanov jonatan-ivanov added enhancement A general enhancement and removed waiting-for-triage labels Feb 2, 2024
@jonatan-ivanov jonatan-ivanov added this to the 1.13.0-M1 milestone Feb 2, 2024
@shakuzen shakuzen added the module: micrometer-core An issue that is related to our core module label Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A general enhancement module: micrometer-core An issue that is related to our core module
Projects
None yet
5 participants