-
Notifications
You must be signed in to change notification settings - Fork 316
Add Observation implementation #1438
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
corneil
merged 15 commits into
spring-projects:main
from
johkin:feature/add-observation
Dec 18, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
904785e
Add Observation implementation
johkin 50b65c8
Move versions to parent pom properties
johkin cf3ce5e
Set micrometer dependency as optional
johkin 260e1fb
Add license header and documentation
johkin d98b61a
Add Javadoc
johkin f115593
Minor modifications after review
johkin 8ced9dd
Add Javadoc
johkin f6bd230
Updated ContextualName to match Span name conventions
johkin a7df585
Minor changes after review
johkin 8de1a8f
Replaced Assert with WarnThenDebugLogger
johkin a2910e1
Modified log-message
johkin dbdec8a
Added nullability annotations
johkin f486fdc
Improved exeptionhandling and logging
johkin 4cd4254
Extract common logic to ObservationHelper
johkin 7163362
Fix for DomSource-handling
johkin 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
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
106 changes: 106 additions & 0 deletions
106
...a/org/springframework/ws/client/core/observation/DefaultWebServiceTemplateConvention.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,106 @@ | ||
/* | ||
* Copyright 2005-2024 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 | ||
* | ||
* http://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.ws.client.core.observation; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
import org.springframework.ws.client.core.observation.WebServiceTemplateObservationDocumentation.LowCardinalityKeyNames; | ||
|
||
/** | ||
* ObservationConvention that describes how a WebServiceTemplate is observed. | ||
* @author Johan Kindgren | ||
*/ | ||
public class DefaultWebServiceTemplateConvention implements WebServiceTemplateConvention { | ||
|
||
private static final KeyValue EXCEPTION_NONE = KeyValue.of(LowCardinalityKeyNames.EXCEPTION, | ||
KeyValue.NONE_VALUE); | ||
private static final String NAME = "webservice.client"; | ||
|
||
@Override | ||
public KeyValues getHighCardinalityKeyValues(WebServiceTemplateObservationContext context) { | ||
if (context.getPath() != null) { | ||
return KeyValues.of(path(context)); | ||
} | ||
return KeyValues.empty(); | ||
} | ||
|
||
@Override | ||
public KeyValues getLowCardinalityKeyValues(WebServiceTemplateObservationContext context) { | ||
return KeyValues.of( | ||
exception(context), | ||
host(context), | ||
localname(context), | ||
namespace(context), | ||
outcome(context), | ||
soapAction(context)); | ||
} | ||
|
||
private KeyValue path(WebServiceTemplateObservationContext context) { | ||
|
||
return WebServiceTemplateObservationDocumentation.HighCardinalityKeyNames | ||
.PATH | ||
.withValue(context.getPath()); | ||
} | ||
|
||
private KeyValue localname(WebServiceTemplateObservationContext context) { | ||
return LowCardinalityKeyNames | ||
.LOCALPART | ||
.withValue(context.getLocalPart()); | ||
} | ||
|
||
private KeyValue namespace(WebServiceTemplateObservationContext context) { | ||
return LowCardinalityKeyNames | ||
.NAMESPACE | ||
.withValue(context.getNamespace()); | ||
} | ||
private KeyValue host(WebServiceTemplateObservationContext context) { | ||
return LowCardinalityKeyNames | ||
.HOST | ||
.withValue(context.getHost()); | ||
} | ||
|
||
|
||
private KeyValue outcome(WebServiceTemplateObservationContext context) { | ||
return LowCardinalityKeyNames | ||
.OUTCOME | ||
.withValue(context.getOutcome()); | ||
} | ||
|
||
private KeyValue soapAction(WebServiceTemplateObservationContext context) { | ||
return LowCardinalityKeyNames | ||
.SOAPACTION | ||
.withValue(context.getSoapAction()); | ||
} | ||
|
||
private KeyValue exception(WebServiceTemplateObservationContext context) { | ||
if (context.getError() != null) { | ||
return LowCardinalityKeyNames | ||
.EXCEPTION | ||
.withValue(context.getError().getClass().getSimpleName()); | ||
} | ||
return EXCEPTION_NONE; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public String getContextualName(WebServiceTemplateObservationContext context) { | ||
return context.getContextualName(); | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
...java/org/springframework/ws/client/core/observation/WebServiceObservationInterceptor.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,152 @@ | ||
/* | ||
* Copyright 2005-2024 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 | ||
* | ||
* http://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.ws.client.core.observation; | ||
|
||
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; | ||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.ws.FaultAwareWebServiceMessage; | ||
import org.springframework.ws.WebServiceMessage; | ||
import org.springframework.ws.client.WebServiceClientException; | ||
import org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter; | ||
import org.springframework.ws.context.MessageContext; | ||
import org.springframework.ws.soap.SoapMessage; | ||
import org.springframework.ws.support.ObservationHelper; | ||
import org.springframework.ws.transport.HeadersAwareSenderWebServiceConnection; | ||
import org.springframework.ws.transport.TransportConstants; | ||
import org.springframework.ws.transport.WebServiceConnection; | ||
import org.springframework.ws.transport.context.TransportContext; | ||
import org.springframework.ws.transport.context.TransportContextHolder; | ||
|
||
import javax.xml.namespace.QName; | ||
import javax.xml.transform.Source; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* Interceptor that creates an Observation for each operation. | ||
* | ||
* @author Johan Kindgren | ||
* @see Observation | ||
* @see io.micrometer.observation.ObservationConvention | ||
*/ | ||
public class WebServiceObservationInterceptor extends ClientInterceptorAdapter { | ||
|
||
private static final WarnThenDebugLogger WARN_THEN_DEBUG_LOGGER = new WarnThenDebugLogger(WebServiceObservationInterceptor.class); | ||
private static final String OBSERVATION_KEY = "observation"; | ||
private static final WebServiceTemplateConvention DEFAULT_CONVENTION = new DefaultWebServiceTemplateConvention(); | ||
|
||
private final ObservationRegistry observationRegistry; | ||
|
||
private final WebServiceTemplateConvention customConvention; | ||
private final ObservationHelper observationHelper; | ||
|
||
public WebServiceObservationInterceptor( | ||
@NonNull | ||
ObservationRegistry observationRegistry, | ||
@NonNull | ||
ObservationHelper observationHelper, | ||
@Nullable | ||
WebServiceTemplateConvention customConvention) { | ||
johkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.observationRegistry = observationRegistry; | ||
this.observationHelper = observationHelper; | ||
this.customConvention = customConvention; | ||
} | ||
|
||
|
||
@Override | ||
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException { | ||
|
||
TransportContext transportContext = TransportContextHolder.getTransportContext(); | ||
HeadersAwareSenderWebServiceConnection connection = | ||
(HeadersAwareSenderWebServiceConnection) transportContext.getConnection(); | ||
|
||
Observation observation = WebServiceTemplateObservationDocumentation.WEB_SERVICE_TEMPLATE.start( | ||
customConvention, | ||
DEFAULT_CONVENTION, | ||
() -> new WebServiceTemplateObservationContext(connection), | ||
observationRegistry); | ||
|
||
messageContext.setProperty(OBSERVATION_KEY, observation); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion(MessageContext messageContext, Exception ex) { | ||
|
||
Observation observation = (Observation) messageContext.getProperty(OBSERVATION_KEY); | ||
if (observation == null) { | ||
WARN_THEN_DEBUG_LOGGER.log("Missing expected Observation in messageContext; the request will not be observed."); | ||
return; | ||
} | ||
|
||
WebServiceTemplateObservationContext context = (WebServiceTemplateObservationContext) observation.getContext(); | ||
|
||
WebServiceMessage request = messageContext.getRequest(); | ||
WebServiceMessage response = messageContext.getResponse(); | ||
|
||
if (request instanceof SoapMessage soapMessage) { | ||
|
||
Source source = soapMessage.getSoapBody().getPayloadSource(); | ||
QName root = observationHelper.getRootElement(source); | ||
if (root != null) { | ||
context.setLocalPart(root.getLocalPart()); | ||
context.setNamespace(root.getNamespaceURI()); | ||
} | ||
if (soapMessage.getSoapAction() != null && !soapMessage.getSoapAction().equals(TransportConstants.EMPTY_SOAP_ACTION)) { | ||
context.setSoapAction(soapMessage.getSoapAction()); | ||
} | ||
} | ||
|
||
if (ex == null) { | ||
context.setOutcome("success"); | ||
} else { | ||
context.setError(ex); | ||
context.setOutcome("fault"); | ||
} | ||
|
||
if (response instanceof FaultAwareWebServiceMessage faultAwareResponse) { | ||
if (faultAwareResponse.hasFault()) { | ||
context.setOutcome("fault"); | ||
} | ||
} | ||
|
||
URI uri = getUriFromConnection(); | ||
if (uri != null) { | ||
context.setHost(uri.getHost()); | ||
context.setPath(uri.getPath()); | ||
} | ||
|
||
context.setContextualName("POST"); | ||
|
||
observation.stop(); | ||
} | ||
|
||
URI getUriFromConnection() { | ||
TransportContext transportContext = TransportContextHolder.getTransportContext(); | ||
WebServiceConnection connection = transportContext.getConnection(); | ||
try { | ||
return connection.getUri(); | ||
} catch (URISyntaxException e) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
...ain/java/org/springframework/ws/client/core/observation/WebServiceTemplateConvention.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,31 @@ | ||
/* | ||
* Copyright 2005-2024 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 | ||
* | ||
* http://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.ws.client.core.observation; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationConvention; | ||
|
||
/** | ||
* ObservationConvention that can be implemented to create a custom observation. | ||
* @author Johan Kindgren | ||
*/ | ||
public interface WebServiceTemplateConvention extends ObservationConvention<WebServiceTemplateObservationContext> { | ||
|
||
@Override | ||
default boolean supportsContext(Observation.Context context) { | ||
return context instanceof WebServiceTemplateObservationContext; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.