-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add observation for message channels #3944
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
Merged
garyrussell
merged 3 commits into
spring-projects:main
from
artembilan:channel_observation
Nov 15, 2022
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.springframework.integration.channel; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.Deque; | ||
|
@@ -26,6 +27,8 @@ | |
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
|
||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.core.OrderComparator; | ||
import org.springframework.core.log.LogAccessor; | ||
|
@@ -34,13 +37,18 @@ | |
import org.springframework.integration.context.IntegrationContextUtils; | ||
import org.springframework.integration.context.IntegrationObjectSupport; | ||
import org.springframework.integration.history.MessageHistory; | ||
import org.springframework.integration.support.MutableMessage; | ||
import org.springframework.integration.support.management.IntegrationManagedResource; | ||
import org.springframework.integration.support.management.IntegrationManagement; | ||
import org.springframework.integration.support.management.TrackableComponent; | ||
import org.springframework.integration.support.management.metrics.MeterFacade; | ||
import org.springframework.integration.support.management.metrics.MetricsCaptor; | ||
import org.springframework.integration.support.management.metrics.SampleFacade; | ||
import org.springframework.integration.support.management.metrics.TimerFacade; | ||
import org.springframework.integration.support.management.observation.DefaultMessageSenderObservationConvention; | ||
import org.springframework.integration.support.management.observation.IntegrationObservation; | ||
import org.springframework.integration.support.management.observation.MessageSenderContext; | ||
import org.springframework.integration.support.management.observation.MessageSenderObservationConvention; | ||
import org.springframework.integration.support.utils.IntegrationUtils; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.messaging.Message; | ||
|
@@ -75,22 +83,27 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport | |
|
||
protected final Set<MeterFacade> meters = ConcurrentHashMap.newKeySet(); // NOSONAR | ||
|
||
private volatile boolean shouldTrack = false; | ||
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP; | ||
|
||
@Nullable | ||
private MessageSenderObservationConvention observationConvention; | ||
|
||
private volatile Class<?>[] datatypes = new Class<?>[0]; | ||
private boolean shouldTrack = false; | ||
|
||
private volatile String fullChannelName; | ||
private Class<?>[] datatypes = new Class<?>[0]; | ||
|
||
private volatile MessageConverter messageConverter; | ||
private MessageConverter messageConverter; | ||
|
||
private volatile boolean loggingEnabled = true; | ||
private boolean loggingEnabled = true; | ||
|
||
private MetricsCaptor metricsCaptor; | ||
|
||
private TimerFacade successTimer; | ||
|
||
private TimerFacade failureTimer; | ||
|
||
private volatile String fullChannelName; | ||
|
||
@Override | ||
public String getComponentType() { | ||
return "channel"; | ||
|
@@ -138,10 +151,7 @@ public void setLoggingEnabled(boolean loggingEnabled) { | |
* @see #setMessageConverter(MessageConverter) | ||
*/ | ||
public void setDatatypes(Class<?>... datatypes) { | ||
this.datatypes = | ||
(datatypes != null && datatypes.length > 0) | ||
? datatypes | ||
: new Class<?>[0]; | ||
this.datatypes = Arrays.copyOf(datatypes, datatypes.length); | ||
} | ||
|
||
/** | ||
|
@@ -192,6 +202,10 @@ public void setMessageConverter(MessageConverter messageConverter) { | |
this.messageConverter = messageConverter; | ||
} | ||
|
||
public void setObservationConvention(@Nullable MessageSenderObservationConvention observationConvention) { | ||
this.observationConvention = observationConvention; | ||
} | ||
|
||
/** | ||
* Return a read-only list of the configured interceptors. | ||
*/ | ||
|
@@ -224,6 +238,12 @@ public ManagementOverrides getOverrides() { | |
return this.managementOverrides; | ||
} | ||
|
||
@Override | ||
public void registerObservationRegistry(ObservationRegistry observationRegistry) { | ||
Assert.notNull(observationRegistry, "'observationRegistry' must not be null"); | ||
this.observationRegistry = observationRegistry; | ||
} | ||
|
||
@Override | ||
protected void onInit() { | ||
super.onInit(); | ||
|
@@ -276,15 +296,14 @@ public boolean send(Message<?> message) { | |
* Send a message on this channel. If the channel is at capacity, this | ||
* method will block until either the timeout occurs or the sending thread | ||
* is interrupted. If the specified timeout is 0, the method will return | ||
* immediately. If less than zero, it will block indefinitely (see | ||
* {@link #send(Message)}). | ||
* immediately. If less than zero, it will block indefinitely (see {@link #send(Message)}). | ||
* @param messageArg the Message to send | ||
* @param timeout the timeout in milliseconds | ||
* @return <code>true</code> if the message is sent successfully, | ||
* <code>false</code> if the message cannot be sent within the allotted | ||
* time or the sending thread is interrupted. | ||
*/ | ||
@Override // NOSONAR complexity | ||
@Override | ||
public boolean send(Message<?> messageArg, long timeout) { | ||
Assert.notNull(messageArg, "message must not be null"); | ||
Assert.notNull(messageArg.getPayload(), "message payload must not be null"); | ||
|
@@ -293,11 +312,45 @@ public boolean send(Message<?> messageArg, long timeout) { | |
message = MessageHistory.write(message, this, getMessageBuilderFactory()); | ||
} | ||
|
||
if (!ObservationRegistry.NOOP.equals(this.observationRegistry)) { | ||
return sendWithObservation(message, timeout); | ||
} | ||
else if (this.metricsCaptor != null) { | ||
return sendWithMetrics(message, timeout); | ||
} | ||
else { | ||
return sendInternal(message, timeout); | ||
} | ||
} | ||
|
||
private boolean sendWithObservation(Message<?> message, long timeout) { | ||
MutableMessage<?> messageToSend = MutableMessage.of(message); | ||
MessageSenderContext context = new MessageSenderContext(messageToSend, getComponentName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary local variable. |
||
return IntegrationObservation.PRODUCER.observation( | ||
this.observationConvention, | ||
DefaultMessageSenderObservationConvention.INSTANCE, | ||
() -> context, | ||
this.observationRegistry) | ||
.observe(() -> sendInternal(messageToSend, timeout)); | ||
} | ||
|
||
private boolean sendWithMetrics(Message<?> message, long timeout) { | ||
SampleFacade sample = this.metricsCaptor.start(); | ||
try { | ||
boolean sent = sendInternal(message, timeout); | ||
sample.stop(sendTimer(sent)); | ||
return sent; | ||
} | ||
catch (RuntimeException ex) { | ||
sample.stop(buildSendTimer(false, ex.getClass().getSimpleName())); | ||
throw ex; | ||
} | ||
} | ||
|
||
private boolean sendInternal(Message<?> message, long timeout) { | ||
Deque<ChannelInterceptor> interceptorStack = null; | ||
boolean sent = false; | ||
boolean metricsProcessed = false; | ||
ChannelInterceptorList interceptorList = this.interceptors; | ||
SampleFacade sample = null; | ||
try { | ||
message = convertPayloadIfNecessary(message); | ||
boolean debugEnabled = this.loggingEnabled && this.logger.isDebugEnabled(); | ||
|
@@ -311,14 +364,8 @@ public boolean send(Message<?> messageArg, long timeout) { | |
return false; | ||
} | ||
} | ||
if (this.metricsCaptor != null) { | ||
sample = this.metricsCaptor.start(); | ||
} | ||
|
||
sent = doSend(message, timeout); | ||
if (sample != null) { | ||
sample.stop(sendTimer(sent)); | ||
} | ||
metricsProcessed = true; | ||
|
||
if (debugEnabled) { | ||
logger.debug("postSend (sent=" + sent + ") on channel '" + this + "', message: " + message); | ||
|
@@ -330,9 +377,6 @@ public boolean send(Message<?> messageArg, long timeout) { | |
return sent; | ||
} | ||
catch (Exception ex) { | ||
if (!metricsProcessed && sample != null) { | ||
sample.stop(buildSendTimer(false, ex.getClass().getSimpleName())); | ||
} | ||
if (interceptorStack != null) { | ||
interceptorList.afterSendCompletion(message, this, sent, ex, interceptorStack); | ||
} | ||
|
@@ -411,7 +455,7 @@ private Message<?> convertPayloadIfNecessary(Message<?> message) { | |
* accepted or the blocking thread is interrupted. | ||
* @param message The message. | ||
* @param timeout The timeout. | ||
* @return true if the send was successful. | ||
* @return true if the {@code send} was successful. | ||
*/ | ||
protected abstract boolean doSend(Message<?> message, long timeout); | ||
|
||
|
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
...integration/support/management/observation/DefaultMessageSenderObservationConvention.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* 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.support.management.observation; | ||
|
||
import io.micrometer.common.KeyValues; | ||
|
||
/** | ||
* A default {@link MessageSenderObservationConvention} implementation. | ||
* Provides low cardinalities as a {@link IntegrationObservation.ProducerTags} values. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0 | ||
*/ | ||
public class DefaultMessageSenderObservationConvention implements MessageSenderObservationConvention { | ||
|
||
/** | ||
* A shared singleton instance for {@link DefaultMessageSenderObservationConvention}. | ||
*/ | ||
public static final DefaultMessageSenderObservationConvention INSTANCE = | ||
new DefaultMessageSenderObservationConvention(); | ||
|
||
|
||
@Override | ||
public KeyValues getLowCardinalityKeyValues(MessageSenderContext context) { | ||
return KeyValues | ||
// See IntegrationObservation.ProducerTags.COMPONENT_NAME - to avoid class tangle | ||
.of("spring.integration.name", context.getProducerName()) | ||
// See IntegrationObservation.ProducerTags.COMPONENT_TYPE - to avoid class tangle | ||
.and("spring.integration.type", "producer"); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the
MessageSenderContext
implementation.The
MessageChannel.send()
is really a point in Spring Integration where we produce messages.According to
PRODUCER
entity of the observation, we have to expose someSetter
which would carry on values from the current context to downstream consumers - propagation, essentially.Therefore a
MutableMessage
to be able to modify headers in the tracePropagator
.See
ObservationPropagationChannelInterceptorTests
.Or is your "Why?" about this new
of()
factory method? 😄Sure! I can add more info into commit message if we are really on the same page about the solution after this review and discussion.
Thank you for understanding!
P.S. Perhaps I deliberately have left so little info in the PR, therefore you would pay attention for what is going on and we would have a healthy discussion like this 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I missed the
getHeaders().put(...)
; now I see it.I will be honest in that I still don't see why we need to observe channel sends, it seems much too fine-grained to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if channel is distributed, then we just lose the producer point of the trace.
It is really up to end-user now what to chose for instrumentation.
Perhaps they indeed would apply only inbound and outbound channel adapters.
However the current solution with Sleuth is only channel instrumentation 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok; just address my other comment and will merge.