Repository: cxf Updated Branches: refs/heads/master a2c4be5e9 -> 3cf195384
[CXF-6440] Update Metrics to support Rest resources Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3cf19538 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3cf19538 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3cf19538 Branch: refs/heads/master Commit: 3cf195384c2175085c7840dc4469837a347c7d0a Parents: a2c4be5 Author: Daniel Kulp <dk...@apache.org> Authored: Thu Jun 4 13:06:18 2015 -0400 Committer: Daniel Kulp <dk...@apache.org> Committed: Thu Jun 4 13:06:18 2015 -0400 ---------------------------------------------------------------------- .../org/apache/cxf/metrics/MetricsFeature.java | 3 + .../org/apache/cxf/metrics/MetricsProvider.java | 1 + .../codahale/CodahaleMetricsProvider.java | 9 +++ .../AbstractMetricsInterceptor.java | 74 +++++++++++++++++--- .../MetricsMessageClientOutInterceptor.java | 5 +- .../MetricsMessageInPreInvokeInterceptor.java | 10 ++- .../AbstractMessageResponseTimeInterceptor.java | 4 +- 7 files changed, 84 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java index df31225..60fe71b 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java @@ -44,6 +44,9 @@ public class MetricsFeature extends AbstractFeature { public MetricsFeature() { this.providers = null; } + public MetricsFeature(MetricsProvider provider) { + this.providers = new MetricsProvider[] {provider}; + } public MetricsFeature(MetricsProvider ... providers) { this.providers = providers.length > 0 ? providers : null; } http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsProvider.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsProvider.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsProvider.java index e2bb994..0184723 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsProvider.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsProvider.java @@ -32,4 +32,5 @@ public interface MetricsProvider { MetricsContext createOperationContext(Endpoint endpoint, BindingOperationInfo boi, boolean asClient, String cid); + MetricsContext createResourceContext(Endpoint endpoint, String resourceName, boolean asClient, String cid); } http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/codahale/CodahaleMetricsProvider.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/codahale/CodahaleMetricsProvider.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/codahale/CodahaleMetricsProvider.java index 6e520c9..3858032 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/codahale/CodahaleMetricsProvider.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/codahale/CodahaleMetricsProvider.java @@ -133,4 +133,13 @@ public class CodahaleMetricsProvider implements MetricsProvider { return new CodahaleMetricsContext(buffer.toString(), registry); } + /** {@inheritDoc}*/ + @Override + public MetricsContext createResourceContext(Endpoint endpoint, String resourceName, + boolean asClient, String clientId) { + StringBuilder buffer = getBaseServiceName(endpoint, asClient, clientId); + buffer.append("Operation=").append(resourceName).append(','); + return new CodahaleMetricsContext(buffer.toString(), registry); + } + } http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/AbstractMetricsInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/AbstractMetricsInterceptor.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/AbstractMetricsInterceptor.java index 6955866..e885163 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/AbstractMetricsInterceptor.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/AbstractMetricsInterceptor.java @@ -25,6 +25,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.cxf.Bus; import org.apache.cxf.configuration.ConfiguredBeanLocator; @@ -41,6 +43,7 @@ import org.apache.cxf.service.model.BindingOperationInfo; public abstract class AbstractMetricsInterceptor extends AbstractPhaseInterceptor<Message> { + private static final String REST_METRICS_MAP = AbstractMetricsInterceptor.class.getName() + ".METRICS_MAP"; MetricsProvider providers[]; public AbstractMetricsInterceptor(String phase, MetricsProvider p[]) { super(phase); @@ -112,26 +115,75 @@ public abstract class AbstractMetricsInterceptor extends AbstractPhaseIntercepto return o; } - protected void addOperationMetrics(ExchangeMetrics ctx, Message m, BindingOperationInfo boi) { - if (boi.isUnwrapped()) { - boi = boi.getWrappedOperation(); + private Map<String, Object> getRestMetricsMap(Endpoint e) { + synchronized (e) { + Object mmo = e.get(REST_METRICS_MAP); + if (mmo == null) { + e.put(REST_METRICS_MAP, new ConcurrentHashMap<String, Object>()); + mmo = e.get(REST_METRICS_MAP); + } + return CastUtils.cast((Map<?, ?>)mmo); } - Object o = boi.getProperty(MetricsContext.class.getName()); - if (o == null) { - synchronized (boi) { - o = createMetricsContextForOperation(m, boi); + } + protected void addOperationMetrics(ExchangeMetrics ctx, Message m, BindingOperationInfo boi) { + Object metrics = null; + if (boi == null) { + //likely a REST service, let's see if we have a resource name + Object nameProperty = m.getExchange().get("org.apache.cxf.resource.operation.name"); + if (nameProperty != null) { + Map<String, Object> restMap = getRestMetricsMap(m.getExchange().getEndpoint()); + metrics = restMap.get(nameProperty.toString()); + if (metrics == null) { + metrics = createMetricsContextForRestResource(m, nameProperty.toString()); + } + } + } else { + if (boi.isUnwrapped()) { + boi = boi.getWrappedOperation(); + } + metrics = boi.getProperty(MetricsContext.class.getName()); + if (metrics == null) { + synchronized (boi) { + metrics = createMetricsContextForOperation(m, boi); + } } } - if (o instanceof List) { - List<MetricsContext> list = CastUtils.cast((List<?>)o); + if (metrics instanceof List) { + List<MetricsContext> list = CastUtils.cast((List<?>)metrics); for (MetricsContext c : list) { ctx.addContext(c); } - } else if (o instanceof MetricsContext) { - ctx.addContext((MetricsContext)o); + } else if (metrics instanceof MetricsContext) { + ctx.addContext((MetricsContext)metrics); } } + private synchronized Object createMetricsContextForRestResource(Message message, String resource) { + Map<String, Object> restMap = getRestMetricsMap(message.getExchange().getEndpoint()); + Object o = restMap.get(resource); + if (o != null) { + return o; + } + List<MetricsContext> contexts = new ArrayList<MetricsContext>(); + for (MetricsProvider p : getMetricProviders(message.getExchange().getBus())) { + MetricsContext c = p.createResourceContext(message.getExchange().getEndpoint(), + resource, MessageUtils.isRequestor(message), + (String)message.getContextualProperty(MetricsProvider.CLIENT_ID)); + if (c != null) { + contexts.add(c); + } + if (c instanceof Closeable) { + message.getExchange().getEndpoint().addCleanupHook((Closeable)c); + } + } + if (contexts.size() == 1) { + o = contexts.get(0); + } else { + o = contexts; + } + restMap.put(resource, o); + return o; + } private Object createMetricsContextForOperation(Message message, BindingOperationInfo boi) { Object o = boi.getProperty(MetricsContext.class.getName()); if (o == null) { http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageClientOutInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageClientOutInterceptor.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageClientOutInterceptor.java index b5689be..f943baa 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageClientOutInterceptor.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageClientOutInterceptor.java @@ -41,10 +41,7 @@ public class MetricsMessageClientOutInterceptor extends AbstractMetricsIntercept message.setContent(InputStream.class, newIn); message.getExchange().put(CountingInputStream.class, newIn); } - if (message.getExchange().getBindingOperationInfo() != null) { - //we now know the operation, start metrics for it - addOperationMetrics(ctx, message, message.getExchange().getBindingOperationInfo()); - } + addOperationMetrics(ctx, message, message.getExchange().getBindingOperationInfo()); ctx.start(); } } http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageInPreInvokeInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageInPreInvokeInterceptor.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageInPreInvokeInterceptor.java index 697d6cb..cdb92d8 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageInPreInvokeInterceptor.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/interceptors/MetricsMessageInPreInvokeInterceptor.java @@ -34,12 +34,10 @@ public class MetricsMessageInPreInvokeInterceptor extends AbstractMetricsInterce public void handleMessage(Message message) throws Fault { if (!isRequestor(message)) { Exchange ex = message.getExchange(); - if (ex.getBindingOperationInfo() != null) { - //we now know the operation, start metrics for it - ExchangeMetrics ctx = getExchangeMetrics(message, false); - if (ctx != null) { - addOperationMetrics(ctx, message, ex.getBindingOperationInfo()); - } + //we now know the operation, start metrics for it + ExchangeMetrics ctx = getExchangeMetrics(message, false); + if (ctx != null) { + addOperationMetrics(ctx, message, ex.getBindingOperationInfo()); } } } http://git-wip-us.apache.org/repos/asf/cxf/blob/3cf19538/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java b/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java index 625cc22..41fe5fd 100644 --- a/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java +++ b/rt/management/src/main/java/org/apache/cxf/management/interceptor/AbstractMessageResponseTimeInterceptor.java @@ -37,6 +37,7 @@ import org.apache.cxf.message.FaultMode; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.service.Service; +import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.OperationInfo; public abstract class AbstractMessageResponseTimeInterceptor extends AbstractPhaseInterceptor<Message> { @@ -158,7 +159,8 @@ public abstract class AbstractMessageResponseTimeInterceptor extends AbstractPha } protected ObjectName getOperationCounterName(Exchange ex, ObjectName sericeCounterName) { - OperationInfo opInfo = ex.getBindingOperationInfo().getOperationInfo(); + BindingOperationInfo bop = ex.getBindingOperationInfo(); + OperationInfo opInfo = bop == null ? null : bop.getOperationInfo(); String operationName = opInfo == null ? null : "\"" + opInfo.getName().getLocalPart() + "\""; if (operationName == null) {