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

Add Observation implementation #1438

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<bouncycastle.version>1.78</bouncycastle.version>
<byte-buddy.version>1.14.13</byte-buddy.version>
<spring-asciidoctor-backends.version>0.0.5</spring-asciidoctor-backends.version>
<micrometer-observation.version>1.13.5</micrometer-observation.version>
</properties>

<dependencyManagement>
Expand Down
12 changes: 12 additions & 0 deletions spring-ws-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
<version>${micrometer-observation.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down Expand Up @@ -244,6 +250,12 @@
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation-test</artifactId>
<version>${micrometer-observation.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 io.micrometer.observation.Observation;

/**
* ObservationConvention that describes how a WebServiceTemplate is observed.
* @author Johan Kindgren
*/
public class DefaultWebServiceTemplateConvention implements WebServiceTemplateConvention {

private static final KeyValue EXCEPTION_NONE = KeyValue.of(WebServiceTemplateObservationDocumentation.LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE);
private String name = "webservice.client";


@Override
public KeyValues getLowCardinalityKeyValues(WebServiceTemplateObservationContext context) {
return KeyValues.of(
johkin marked this conversation as resolved.
Show resolved Hide resolved
exception(context),
host(context),
localname(context),
namespace(context),
outcome(context),
soapAction(context));
}

private KeyValue localname(WebServiceTemplateObservationContext context) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.LOCALNAME
.withValue(context.getLocalname());
}

private KeyValue namespace(WebServiceTemplateObservationContext context) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.NAMESPACE
.withValue(context.getNamespace());
}
private KeyValue host(WebServiceTemplateObservationContext context) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.HOST
.withValue(context.getHost());
}


private KeyValue outcome(WebServiceTemplateObservationContext context) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.OUTCOME
.withValue(context.getOutcome());
}

private KeyValue soapAction(WebServiceTemplateObservationContext context) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.SOAPACTION
.withValue(context.getSoapAction());
}

private KeyValue exception(WebServiceTemplateObservationContext context) {
if (context.getError() != null) {
return WebServiceTemplateObservationDocumentation
.LowCardinalityKeyNames
.EXCEPTION
.withValue(context.getError().getClass().getSimpleName());
} else {
return EXCEPTION_NONE;
}
}

@Override
public KeyValues getHighCardinalityKeyValues(WebServiceTemplateObservationContext context) {
return KeyValues.empty();
}

@Override
public String getName() {
return name;
}

@Override
public String getContextualName(WebServiceTemplateObservationContext context) {
return "WebServiceTemplate " + context.getHost();
johkin marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof WebServiceTemplateObservationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.namespace.QName;
/**
* DefaultHandler that extracts the root elements namespace and name.
* @author Johan Kindgren
*/
public class RootElementSAXHandler extends DefaultHandler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be public or can it be package-private?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I was looking at old commits.


private QName rootElementName = null;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (rootElementName == null) {
rootElementName = new QName(uri, localName);
}
}

public QName getRootElementName() {
return rootElementName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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.ObservationRegistry;
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.transport.HeadersAwareSenderWebServiceConnection;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.context.TransportContext;
import org.springframework.ws.transport.context.TransportContextHolder;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
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 ObservationRegistry observationRegistry;
johkin marked this conversation as resolved.
Show resolved Hide resolved

private static final WebServiceTemplateConvention DEFAULT_CONVENTION = new DefaultWebServiceTemplateConvention();

private SAXParserFactory parserFactory;
private SAXParser saxParser;

private WebServiceTemplateConvention customConvention;

public WebServiceObservationInterceptor(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
parserFactory = SAXParserFactory.newNSInstance();
try {
saxParser = parserFactory.newSAXParser();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}


@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", observation);

return true;
}

@Override
public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {

Observation observation = (Observation) messageContext.getProperty("observation");
WebServiceTemplateObservationContext context = (WebServiceTemplateObservationContext) observation.getContext();

context.setHost(getHostFromConnection());
context.setError(ex);

WebServiceMessage request = messageContext.getRequest();
WebServiceMessage response = messageContext.getResponse();

if (request instanceof SoapMessage soapMessage) {

Source source = soapMessage.getSoapBody().getPayloadSource();
QName root = getRootElement(source);
if (root != null) {
context.setLocalname(root.getLocalPart());
context.setNamespace(root.getNamespaceURI());
}
context.setSoapAction((soapMessage).getSoapAction());
}

if (response instanceof FaultAwareWebServiceMessage faultAwareResponse) {
if (!faultAwareResponse.hasFault() && ex == null) {
context.setOutcome("success");
} else {
context.setOutcome("fault");
}
}

observation.stop();
}

public void setCustomConvention(WebServiceTemplateConvention customConvention) {
this.customConvention = customConvention;
johkin marked this conversation as resolved.
Show resolved Hide resolved
}

String getHostFromConnection() {
TransportContext transportContext = TransportContextHolder.getTransportContext();
WebServiceConnection connection = transportContext.getConnection();
try {
return connection.getUri().getHost();
} catch (URISyntaxException e) {
return WebServiceTemplateObservationContext.UNKNOWN;
}
}

QName getRootElement(Source source) {
if (source instanceof DOMSource) {
Node root = ((DOMSource) source).getNode();
return new QName(root.getNamespaceURI(), root.getLocalName());
}
if (source instanceof StreamSource) {
RootElementSAXHandler handler = new RootElementSAXHandler();
try {
saxParser.parse(((StreamSource) source).getInputStream(), handler);
return handler.getRootElementName();
} catch (Exception e) {
return new QName("unknown", "unknow");
}
}
if (source instanceof SAXSource) {
RootElementSAXHandler handler = new RootElementSAXHandler();
try {
saxParser.parse(((SAXSource) source).getInputSource(), handler);
return handler.getRootElementName();
} catch (Exception e) {
return new QName("unknown", "unknow");
}
}
return new QName("unknown", "unknow");
}

}

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;
}
}
Loading