You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/05/14 08:05:05 UTC

[1/6] camel git commit: CAMEL-11267 Add SpanDecorator for the 'rest' component

Repository: camel
Updated Branches:
  refs/heads/camel-2.19.x 9b2d5aaca -> 926f9def7
  refs/heads/master d28c8fc56 -> ffae0bcdc


CAMEL-11267 Add SpanDecorator for the 'rest' component


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/dfb3080c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/dfb3080c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/dfb3080c

Branch: refs/heads/master
Commit: dfb3080c61b965e15422cda8467e9c5003e10135
Parents: d28c8fc
Author: Gary Brown <ga...@brownuk.com>
Authored: Fri May 12 12:02:08 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:02:48 2017 +0200

----------------------------------------------------------------------
 .../decorators/RestSpanDecorator.java           | 84 +++++++++++++++++
 .../org.apache.camel.opentracing.SpanDecorator  |  1 +
 .../decorators/RestSpanDecoratorTest.java       | 98 ++++++++++++++++++++
 3 files changed, 183 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/dfb3080c/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
new file mode 100644
index 0000000..7f8f0c0
--- /dev/null
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.opentracing.decorators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import io.opentracing.Span;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+
+public class RestSpanDecorator extends AbstractHttpSpanDecorator {
+
+    @Override
+    public String getComponent() {
+        return "rest";
+    }
+
+    @Override
+    public String getOperationName(Exchange exchange, Endpoint endpoint) {
+        String path = exchange.getFromEndpoint().getEndpointUri();
+        int queryIndex = path.indexOf('?');
+        if (queryIndex != -1) {
+            path = path.substring(0, queryIndex);
+        }
+        return path;
+    }
+
+    @Override
+    public void pre(Span span, Exchange exchange, Endpoint endpoint) {
+        super.pre(span, exchange, endpoint);
+
+        getParameters(exchange.getFromEndpoint().getEndpointUri()).forEach(param -> {
+            Object value = exchange.getIn().getHeader(param);
+            if (value != null) {
+                if (value instanceof String) {
+                    span.setTag(param, (String)value);
+                } else if (value instanceof Number) {
+                    span.setTag(param, (Number)value);
+                } else if (value instanceof Boolean) {
+                    span.setTag(param, (Boolean)value);
+                }
+            }
+        });
+        
+    }
+
+    public static List<String> getParameters(String uri) {
+        List<String> parameters = null;
+
+        int startIndex = uri.indexOf('(');
+        while (startIndex != -1) {
+            int endIndex = uri.indexOf(')', startIndex);
+            if (endIndex != -1) {
+                if (parameters == null) {
+                    parameters = new ArrayList<>();
+                }
+                parameters.add(uri.substring(startIndex + 1, endIndex));
+                startIndex = uri.indexOf('(', endIndex);
+            } else {
+                // Break out of loop as no valid end token
+                startIndex = -1;
+            }
+        }
+
+        return parameters == null ? Collections.emptyList() : parameters;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/dfb3080c/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator b/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
index 835743a..7c11838 100644
--- a/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
+++ b/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
@@ -42,6 +42,7 @@ org.apache.camel.opentracing.decorators.NettyHttpSpanDecorator
 org.apache.camel.opentracing.decorators.PahoSpanDecorator
 org.apache.camel.opentracing.decorators.RabbitmqSpanDecorator
 org.apache.camel.opentracing.decorators.RestletSpanDecorator
+org.apache.camel.opentracing.decorators.RestSpanDecorator
 org.apache.camel.opentracing.decorators.SedaSpanDecorator
 org.apache.camel.opentracing.decorators.ServletSpanDecorator
 org.apache.camel.opentracing.decorators.SjmsSpanDecorator

http://git-wip-us.apache.org/repos/asf/camel/blob/dfb3080c/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
new file mode 100644
index 0000000..970fd3f
--- /dev/null
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.opentracing.decorators;
+
+import java.util.Arrays;
+
+import io.opentracing.mock.MockSpan;
+import io.opentracing.mock.MockTracer;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.opentracing.SpanDecorator;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class RestSpanDecoratorTest {
+
+    @Test
+    public void testGetOperation() {
+        String query = "?restletMethods=PUT";
+        String path = "/persons/(personId)";
+        String uri = path + query;
+
+        Endpoint endpoint = Mockito.mock(Endpoint.class);
+        Exchange exchange = Mockito.mock(Exchange.class);
+
+        Mockito.when(endpoint.getEndpointUri()).thenReturn(uri);
+        Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
+
+        SpanDecorator decorator = new RestSpanDecorator();
+
+        assertEquals(path, decorator.getOperationName(exchange, endpoint));
+    }
+
+    @Test
+    public void testGetParameters() {
+        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/(id1)/(id2)"));
+    }
+
+    @Test
+    public void testGetParametersNone() {
+        assertTrue(RestSpanDecorator.getParameters("/context/hello/world").isEmpty());
+    }
+
+    @Test
+    public void testPreStringParameter() {
+        testParameter("strParam", "strValue");
+    }
+
+    @Test
+    public void testPreNumberParameter() {
+        testParameter("numParam", 5.6);
+    }
+
+    @Test
+    public void testPreBooleanParameter() {
+        testParameter("boolParam", Boolean.TRUE);
+    }
+
+    protected void testParameter(String paramName, Object paramValue) {
+        Endpoint endpoint = Mockito.mock(Endpoint.class);
+        Exchange exchange = Mockito.mock(Exchange.class);
+        Message message = Mockito.mock(Message.class);
+
+        Mockito.when(endpoint.getEndpointUri()).thenReturn("/context/(" + paramName + ")");
+        Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
+        Mockito.when(exchange.getIn()).thenReturn(message);
+        Mockito.when(message.getHeader(paramName)).thenReturn(paramValue);
+
+        SpanDecorator decorator = new RestSpanDecorator();
+
+        MockTracer tracer = new MockTracer();
+        MockSpan span = (MockSpan)tracer.buildSpan("TestSpan").start();
+
+        decorator.pre(span, exchange, endpoint);
+
+        assertEquals(paramValue, span.tags().get(paramName));
+    }
+
+}


[4/6] camel git commit: CAMEL-11267 Add SpanDecorator for the 'rest' component

Posted by da...@apache.org.
CAMEL-11267 Add SpanDecorator for the 'rest' component


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/63dc5f7f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/63dc5f7f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/63dc5f7f

Branch: refs/heads/camel-2.19.x
Commit: 63dc5f7fa8e4d75bbdabdc993c2ba6f47534ef57
Parents: 9b2d5aa
Author: Gary Brown <ga...@brownuk.com>
Authored: Fri May 12 12:02:08 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:04:20 2017 +0200

----------------------------------------------------------------------
 .../decorators/RestSpanDecorator.java           | 84 +++++++++++++++++
 .../org.apache.camel.opentracing.SpanDecorator  |  1 +
 .../decorators/RestSpanDecoratorTest.java       | 98 ++++++++++++++++++++
 3 files changed, 183 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/63dc5f7f/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
new file mode 100644
index 0000000..7f8f0c0
--- /dev/null
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.opentracing.decorators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import io.opentracing.Span;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+
+public class RestSpanDecorator extends AbstractHttpSpanDecorator {
+
+    @Override
+    public String getComponent() {
+        return "rest";
+    }
+
+    @Override
+    public String getOperationName(Exchange exchange, Endpoint endpoint) {
+        String path = exchange.getFromEndpoint().getEndpointUri();
+        int queryIndex = path.indexOf('?');
+        if (queryIndex != -1) {
+            path = path.substring(0, queryIndex);
+        }
+        return path;
+    }
+
+    @Override
+    public void pre(Span span, Exchange exchange, Endpoint endpoint) {
+        super.pre(span, exchange, endpoint);
+
+        getParameters(exchange.getFromEndpoint().getEndpointUri()).forEach(param -> {
+            Object value = exchange.getIn().getHeader(param);
+            if (value != null) {
+                if (value instanceof String) {
+                    span.setTag(param, (String)value);
+                } else if (value instanceof Number) {
+                    span.setTag(param, (Number)value);
+                } else if (value instanceof Boolean) {
+                    span.setTag(param, (Boolean)value);
+                }
+            }
+        });
+        
+    }
+
+    public static List<String> getParameters(String uri) {
+        List<String> parameters = null;
+
+        int startIndex = uri.indexOf('(');
+        while (startIndex != -1) {
+            int endIndex = uri.indexOf(')', startIndex);
+            if (endIndex != -1) {
+                if (parameters == null) {
+                    parameters = new ArrayList<>();
+                }
+                parameters.add(uri.substring(startIndex + 1, endIndex));
+                startIndex = uri.indexOf('(', endIndex);
+            } else {
+                // Break out of loop as no valid end token
+                startIndex = -1;
+            }
+        }
+
+        return parameters == null ? Collections.emptyList() : parameters;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/63dc5f7f/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator b/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
index 835743a..7c11838 100644
--- a/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
+++ b/components/camel-opentracing/src/main/resources/META-INF/services/org.apache.camel.opentracing.SpanDecorator
@@ -42,6 +42,7 @@ org.apache.camel.opentracing.decorators.NettyHttpSpanDecorator
 org.apache.camel.opentracing.decorators.PahoSpanDecorator
 org.apache.camel.opentracing.decorators.RabbitmqSpanDecorator
 org.apache.camel.opentracing.decorators.RestletSpanDecorator
+org.apache.camel.opentracing.decorators.RestSpanDecorator
 org.apache.camel.opentracing.decorators.SedaSpanDecorator
 org.apache.camel.opentracing.decorators.ServletSpanDecorator
 org.apache.camel.opentracing.decorators.SjmsSpanDecorator

http://git-wip-us.apache.org/repos/asf/camel/blob/63dc5f7f/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
new file mode 100644
index 0000000..970fd3f
--- /dev/null
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.opentracing.decorators;
+
+import java.util.Arrays;
+
+import io.opentracing.mock.MockSpan;
+import io.opentracing.mock.MockTracer;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.opentracing.SpanDecorator;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class RestSpanDecoratorTest {
+
+    @Test
+    public void testGetOperation() {
+        String query = "?restletMethods=PUT";
+        String path = "/persons/(personId)";
+        String uri = path + query;
+
+        Endpoint endpoint = Mockito.mock(Endpoint.class);
+        Exchange exchange = Mockito.mock(Exchange.class);
+
+        Mockito.when(endpoint.getEndpointUri()).thenReturn(uri);
+        Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
+
+        SpanDecorator decorator = new RestSpanDecorator();
+
+        assertEquals(path, decorator.getOperationName(exchange, endpoint));
+    }
+
+    @Test
+    public void testGetParameters() {
+        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/(id1)/(id2)"));
+    }
+
+    @Test
+    public void testGetParametersNone() {
+        assertTrue(RestSpanDecorator.getParameters("/context/hello/world").isEmpty());
+    }
+
+    @Test
+    public void testPreStringParameter() {
+        testParameter("strParam", "strValue");
+    }
+
+    @Test
+    public void testPreNumberParameter() {
+        testParameter("numParam", 5.6);
+    }
+
+    @Test
+    public void testPreBooleanParameter() {
+        testParameter("boolParam", Boolean.TRUE);
+    }
+
+    protected void testParameter(String paramName, Object paramValue) {
+        Endpoint endpoint = Mockito.mock(Endpoint.class);
+        Exchange exchange = Mockito.mock(Exchange.class);
+        Message message = Mockito.mock(Message.class);
+
+        Mockito.when(endpoint.getEndpointUri()).thenReturn("/context/(" + paramName + ")");
+        Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
+        Mockito.when(exchange.getIn()).thenReturn(message);
+        Mockito.when(message.getHeader(paramName)).thenReturn(paramValue);
+
+        SpanDecorator decorator = new RestSpanDecorator();
+
+        MockTracer tracer = new MockTracer();
+        MockSpan span = (MockSpan)tracer.buildSpan("TestSpan").start();
+
+        decorator.pre(span, exchange, endpoint);
+
+        assertEquals(paramValue, span.tags().get(paramName));
+    }
+
+}


[5/6] camel git commit: CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces

Posted by da...@apache.org.
CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a2487f5e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a2487f5e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a2487f5e

Branch: refs/heads/camel-2.19.x
Commit: a2487f5ee533495dc81fd6adad9d67d8367cd8ae
Parents: 63dc5f7
Author: Gary Brown <ga...@brownuk.com>
Authored: Fri May 12 17:17:38 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:04:26 2017 +0200

----------------------------------------------------------------------
 .../decorators/RestSpanDecorator.java           | 48 +++++++++++++++-----
 .../decorators/RestSpanDecoratorTest.java       | 15 +++---
 2 files changed, 45 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a2487f5e/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
index 7f8f0c0..1842abc 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.opentracing.decorators;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -24,9 +26,13 @@ import io.opentracing.Span;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
+    private static final Logger LOG = LoggerFactory.getLogger(RestSpanDecorator.class);
+
     @Override
     public String getComponent() {
         return "rest";
@@ -34,19 +40,14 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
     @Override
     public String getOperationName(Exchange exchange, Endpoint endpoint) {
-        String path = exchange.getFromEndpoint().getEndpointUri();
-        int queryIndex = path.indexOf('?');
-        if (queryIndex != -1) {
-            path = path.substring(0, queryIndex);
-        }
-        return path;
+        return getPath(endpoint.getEndpointUri());
     }
 
     @Override
     public void pre(Span span, Exchange exchange, Endpoint endpoint) {
         super.pre(span, exchange, endpoint);
 
-        getParameters(exchange.getFromEndpoint().getEndpointUri()).forEach(param -> {
+        getParameters(getPath(endpoint.getEndpointUri())).forEach(param -> {
             Object value = exchange.getIn().getHeader(param);
             if (value != null) {
                 if (value instanceof String) {
@@ -61,18 +62,41 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
         
     }
 
-    public static List<String> getParameters(String uri) {
+    protected static String getPath(String uri) {
+        // Obtain the 'path' part of the URI format: rest://method:path[:uriTemplate]?[options]
+        String path = null;
+        int index = uri.indexOf(':');
+        if (index != -1) {
+            index = uri.indexOf(':', index + 1);
+            if (index != -1) {
+                path = uri.substring(index + 1);
+                index = path.indexOf('?');
+                if (index != -1) {
+                    path = path.substring(0, index);
+                }
+                path = path.replaceAll(":", "");
+                try {
+                    path = URLDecoder.decode(path, "UTF-8");
+                } catch (UnsupportedEncodingException e) {
+                    LOG.error("Failed to decode URL path '" + path + "'", e);
+                }
+            }
+        }
+        return path;
+    }
+
+    protected static List<String> getParameters(String path) {
         List<String> parameters = null;
 
-        int startIndex = uri.indexOf('(');
+        int startIndex = path.indexOf('{');
         while (startIndex != -1) {
-            int endIndex = uri.indexOf(')', startIndex);
+            int endIndex = path.indexOf('}', startIndex);
             if (endIndex != -1) {
                 if (parameters == null) {
                     parameters = new ArrayList<>();
                 }
-                parameters.add(uri.substring(startIndex + 1, endIndex));
-                startIndex = uri.indexOf('(', endIndex);
+                parameters.add(path.substring(startIndex + 1, endIndex));
+                startIndex = path.indexOf('{', endIndex);
             } else {
                 // Break out of loop as no valid end token
                 startIndex = -1;

http://git-wip-us.apache.org/repos/asf/camel/blob/a2487f5e/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
index 970fd3f..0724e8f 100644
--- a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
@@ -35,9 +35,8 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetOperation() {
-        String query = "?restletMethods=PUT";
-        String path = "/persons/(personId)";
-        String uri = path + query;
+        String path = "/persons/{personId}";
+        String uri = "rest://put:/persons:/%7BpersonId%7D?routeId=route4";
 
         Endpoint endpoint = Mockito.mock(Endpoint.class);
         Exchange exchange = Mockito.mock(Exchange.class);
@@ -52,12 +51,12 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetParameters() {
-        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/(id1)/(id2)"));
+        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/{id1}/{id2}"));
     }
 
     @Test
     public void testGetParametersNone() {
-        assertTrue(RestSpanDecorator.getParameters("/context/hello/world").isEmpty());
+        assertTrue(RestSpanDecorator.getParameters("rest://put:/persons/hello/world?routeId=route4").isEmpty());
     }
 
     @Test
@@ -80,7 +79,7 @@ public class RestSpanDecoratorTest {
         Exchange exchange = Mockito.mock(Exchange.class);
         Message message = Mockito.mock(Message.class);
 
-        Mockito.when(endpoint.getEndpointUri()).thenReturn("/context/(" + paramName + ")");
+        Mockito.when(endpoint.getEndpointUri()).thenReturn("rest://put:/context:/%7B" + paramName + "%7D?routeId=route4");
         Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
         Mockito.when(exchange.getIn()).thenReturn(message);
         Mockito.when(message.getHeader(paramName)).thenReturn(paramValue);
@@ -95,4 +94,8 @@ public class RestSpanDecoratorTest {
         assertEquals(paramValue, span.tags().get(paramName));
     }
 
+    @Test
+    public void testGetPath() {
+        assertEquals("/persons/{personId}", RestSpanDecorator.getPath("rest://put:/persons:/%7BpersonId%7D?routeId=route4"));
+    }
 }


[3/6] camel git commit: CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces

Posted by da...@apache.org.
CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/defc6b18
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/defc6b18
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/defc6b18

Branch: refs/heads/master
Commit: defc6b18db23a724680bf45392b46294dff4caf6
Parents: dfb3080
Author: Gary Brown <ga...@brownuk.com>
Authored: Fri May 12 17:17:38 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:02:49 2017 +0200

----------------------------------------------------------------------
 .../decorators/RestSpanDecorator.java           | 48 +++++++++++++++-----
 .../decorators/RestSpanDecoratorTest.java       | 15 +++---
 2 files changed, 45 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/defc6b18/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
index 7f8f0c0..1842abc 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.opentracing.decorators;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -24,9 +26,13 @@ import io.opentracing.Span;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
+    private static final Logger LOG = LoggerFactory.getLogger(RestSpanDecorator.class);
+
     @Override
     public String getComponent() {
         return "rest";
@@ -34,19 +40,14 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
     @Override
     public String getOperationName(Exchange exchange, Endpoint endpoint) {
-        String path = exchange.getFromEndpoint().getEndpointUri();
-        int queryIndex = path.indexOf('?');
-        if (queryIndex != -1) {
-            path = path.substring(0, queryIndex);
-        }
-        return path;
+        return getPath(endpoint.getEndpointUri());
     }
 
     @Override
     public void pre(Span span, Exchange exchange, Endpoint endpoint) {
         super.pre(span, exchange, endpoint);
 
-        getParameters(exchange.getFromEndpoint().getEndpointUri()).forEach(param -> {
+        getParameters(getPath(endpoint.getEndpointUri())).forEach(param -> {
             Object value = exchange.getIn().getHeader(param);
             if (value != null) {
                 if (value instanceof String) {
@@ -61,18 +62,41 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
         
     }
 
-    public static List<String> getParameters(String uri) {
+    protected static String getPath(String uri) {
+        // Obtain the 'path' part of the URI format: rest://method:path[:uriTemplate]?[options]
+        String path = null;
+        int index = uri.indexOf(':');
+        if (index != -1) {
+            index = uri.indexOf(':', index + 1);
+            if (index != -1) {
+                path = uri.substring(index + 1);
+                index = path.indexOf('?');
+                if (index != -1) {
+                    path = path.substring(0, index);
+                }
+                path = path.replaceAll(":", "");
+                try {
+                    path = URLDecoder.decode(path, "UTF-8");
+                } catch (UnsupportedEncodingException e) {
+                    LOG.error("Failed to decode URL path '" + path + "'", e);
+                }
+            }
+        }
+        return path;
+    }
+
+    protected static List<String> getParameters(String path) {
         List<String> parameters = null;
 
-        int startIndex = uri.indexOf('(');
+        int startIndex = path.indexOf('{');
         while (startIndex != -1) {
-            int endIndex = uri.indexOf(')', startIndex);
+            int endIndex = path.indexOf('}', startIndex);
             if (endIndex != -1) {
                 if (parameters == null) {
                     parameters = new ArrayList<>();
                 }
-                parameters.add(uri.substring(startIndex + 1, endIndex));
-                startIndex = uri.indexOf('(', endIndex);
+                parameters.add(path.substring(startIndex + 1, endIndex));
+                startIndex = path.indexOf('{', endIndex);
             } else {
                 // Break out of loop as no valid end token
                 startIndex = -1;

http://git-wip-us.apache.org/repos/asf/camel/blob/defc6b18/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
index 970fd3f..0724e8f 100644
--- a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
@@ -35,9 +35,8 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetOperation() {
-        String query = "?restletMethods=PUT";
-        String path = "/persons/(personId)";
-        String uri = path + query;
+        String path = "/persons/{personId}";
+        String uri = "rest://put:/persons:/%7BpersonId%7D?routeId=route4";
 
         Endpoint endpoint = Mockito.mock(Endpoint.class);
         Exchange exchange = Mockito.mock(Exchange.class);
@@ -52,12 +51,12 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetParameters() {
-        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/(id1)/(id2)"));
+        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/{id1}/{id2}"));
     }
 
     @Test
     public void testGetParametersNone() {
-        assertTrue(RestSpanDecorator.getParameters("/context/hello/world").isEmpty());
+        assertTrue(RestSpanDecorator.getParameters("rest://put:/persons/hello/world?routeId=route4").isEmpty());
     }
 
     @Test
@@ -80,7 +79,7 @@ public class RestSpanDecoratorTest {
         Exchange exchange = Mockito.mock(Exchange.class);
         Message message = Mockito.mock(Message.class);
 
-        Mockito.when(endpoint.getEndpointUri()).thenReturn("/context/(" + paramName + ")");
+        Mockito.when(endpoint.getEndpointUri()).thenReturn("rest://put:/context:/%7B" + paramName + "%7D?routeId=route4");
         Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
         Mockito.when(exchange.getIn()).thenReturn(message);
         Mockito.when(message.getHeader(paramName)).thenReturn(paramValue);
@@ -95,4 +94,8 @@ public class RestSpanDecoratorTest {
         assertEquals(paramValue, span.tags().get(paramName));
     }
 
+    @Test
+    public void testGetPath() {
+        assertEquals("/persons/{personId}", RestSpanDecorator.getPath("rest://put:/persons:/%7BpersonId%7D?routeId=route4"));
+    }
 }


[6/6] camel git commit: Change error to debug

Posted by da...@apache.org.
Change error to debug


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/926f9def
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/926f9def
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/926f9def

Branch: refs/heads/camel-2.19.x
Commit: 926f9def75b32a5861d3dfe5028222efe0b7307b
Parents: a2487f5
Author: Gary Brown <ga...@brownuk.com>
Authored: Sat May 13 17:51:04 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:04:33 2017 +0200

----------------------------------------------------------------------
 .../org/apache/camel/opentracing/decorators/RestSpanDecorator.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/926f9def/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
index 1842abc..cadc856 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -78,7 +78,7 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
                 try {
                     path = URLDecoder.decode(path, "UTF-8");
                 } catch (UnsupportedEncodingException e) {
-                    LOG.error("Failed to decode URL path '" + path + "'", e);
+                    LOG.debug("Failed to decode URL path '" + path + "', ignoring exception", e);
                 }
             }
         }


[2/6] camel git commit: Change error to debug

Posted by da...@apache.org.
Change error to debug


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ffae0bcd
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ffae0bcd
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ffae0bcd

Branch: refs/heads/master
Commit: ffae0bcdcdcc1233791e07fdabbbd222b84a2eab
Parents: defc6b1
Author: Gary Brown <ga...@brownuk.com>
Authored: Sat May 13 17:51:04 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 14 10:02:49 2017 +0200

----------------------------------------------------------------------
 .../org/apache/camel/opentracing/decorators/RestSpanDecorator.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ffae0bcd/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
index 1842abc..cadc856 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -78,7 +78,7 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
                 try {
                     path = URLDecoder.decode(path, "UTF-8");
                 } catch (UnsupportedEncodingException e) {
-                    LOG.error("Failed to decode URL path '" + path + "'", e);
+                    LOG.debug("Failed to decode URL path '" + path + "', ignoring exception", e);
                 }
             }
         }