-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for Micrometer Observation (#3845)
* Initial support for Micrometer Observation * Add respective Observation dependencies * Refactor an `AbstractMessageHandler` logic for potential Observation hooks * Introduce an `ObservationPropagationChannelInterceptor` to propagate an `Observation` from one thread to another through message channels * Adds an example of propagation * Fixed the user code and receiving spans * * Clean up for Tracing unit test * Make `micrometer-observation` as an `api` dep - non-optional for direct API usage Co-authored-by: Marcin Grzejszczak <[email protected]>
- Loading branch information
1 parent
78fa297
commit 2bfcb32
Showing
7 changed files
with
465 additions
and
38 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
77 changes: 77 additions & 0 deletions
77
...ngframework/integration/channel/interceptor/ObservationPropagationChannelInterceptor.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,77 @@ | ||
/* | ||
* Copyright 2022 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 org.springframework.integration.channel.interceptor; | ||
|
||
import org.springframework.aop.support.AopUtils; | ||
import org.springframework.integration.channel.DirectChannel; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.MessageHandler; | ||
import org.springframework.util.Assert; | ||
|
||
import io.micrometer.common.lang.Nullable; | ||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
|
||
/** | ||
* The {@link org.springframework.messaging.support.ExecutorChannelInterceptor} | ||
* implementation responsible for an {@link Observation} propagation from one message | ||
* flow's thread to another through the {@link MessageChannel}s involved in the flow. | ||
* Opens a new {@link Observation.Scope} on another thread and cleans up it in the end. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0 | ||
*/ | ||
public class ObservationPropagationChannelInterceptor extends ThreadStatePropagationChannelInterceptor<Observation> { | ||
|
||
private final ThreadLocal<Observation.Scope> scopes = new ThreadLocal<>(); | ||
|
||
private final ObservationRegistry observationRegistry; | ||
|
||
public ObservationPropagationChannelInterceptor(ObservationRegistry observationRegistry) { | ||
Assert.notNull(observationRegistry, "'observationRegistry' must noty be null"); | ||
this.observationRegistry = observationRegistry; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
protected Observation obtainPropagatingContext(Message<?> message, MessageChannel channel) { | ||
if (!DirectChannel.class.isAssignableFrom(AopUtils.getTargetClass(channel))) { | ||
return this.observationRegistry.getCurrentObservation(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected void populatePropagatedContext(@Nullable Observation state, Message<?> message, MessageChannel channel) { | ||
if (state != null) { | ||
Observation.Scope scope = state.openScope(); | ||
this.scopes.set(scope); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler, Exception ex) { | ||
Observation.Scope scope = this.scopes.get(); | ||
if (scope != null && scope == this.observationRegistry.getCurrentObservationScope()) { | ||
scope.close(); | ||
this.scopes.remove(); | ||
} | ||
} | ||
|
||
} |
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
7 changes: 7 additions & 0 deletions
7
...ain/java/org/springframework/integration/support/management/observation/package-info.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,7 @@ | ||
/** | ||
* Provides classes to support of Micrometer Observation API. | ||
*/ | ||
|
||
@org.springframework.lang.NonNullApi | ||
@org.springframework.lang.NonNullFields | ||
package org.springframework.integration.support.management.observation; |
Oops, something went wrong.