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:08 UTC
[4/6] camel git commit: CAMEL-11267 Add SpanDecorator for the 'rest'
component
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));
+ }
+
+}