CAMEL-9759: camel-zipkin - Instrument Camel. Work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c15d1cb2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c15d1cb2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c15d1cb2 Branch: refs/heads/master Commit: c15d1cb228ca0a50266af705dccaf5f4e8a50725 Parents: d4e9cc6 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Mar 29 11:41:03 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Mar 29 14:41:54 2016 +0200 ---------------------------------------------------------------------- .../camel/zipkin/ZipkinEventNotifier.java | 16 ++-- .../zipkin/scribe/ZipkinOneRouteScribe.java | 17 +---- .../scribe/ZipkinRouteConcurrentScribe.java | 11 ++- .../zipkin/scribe/ZipkinSimpleRouteScribe.java | 12 +-- .../zipkin/scribe/ZipkinTimerRouteScribe.java | 1 + .../zipkin/scribe/ZipkinTwoRouteScribe.java | 77 ++++++++++++++++++++ .../src/test/resources/log4j.properties | 2 +- 7 files changed, 105 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java index ad0742e..9e9edbc 100644 --- a/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java +++ b/components/camel-zipkin/src/main/java/org/apache/camel/zipkin/ZipkinEventNotifier.java @@ -256,7 +256,9 @@ public class ZipkinEventNotifier extends EventNotifierSupport { ClientSpanThreadBinder binder = brave.clientSpanThreadBinder(); brave.clientRequestInterceptor().handle(new ZipkinClientRequestAdapter(serviceName, event.getExchange(), event.getEndpoint())); Span span = binder.getCurrentClientSpan(); - event.getExchange().setProperty("CamelZipkinClientSpan", span); + + String key = "CamelZipkinClientSpan-" + serviceName; + event.getExchange().setProperty(key, span); if (log.isDebugEnabled()) { log.debug("clientRequest: service={}, id={} ", serviceName, span != null ? span.getId() : "<null>"); @@ -265,7 +267,8 @@ public class ZipkinEventNotifier extends EventNotifierSupport { private void clientResponse(Brave brave, String serviceName, ExchangeSentEvent event) { ClientSpanThreadBinder binder = brave.clientSpanThreadBinder(); - Span span = event.getExchange().getProperty("CamelZipkinClientSpan", Span.class); + String key = "CamelZipkinClientSpan-" + serviceName; + Span span = event.getExchange().getProperty(key, Span.class); binder.setCurrentSpan(span); brave.clientResponseInterceptor().handle(new ZipkinClientResponseAdaptor(event.getExchange(), event.getEndpoint())); binder.setCurrentSpan(null); @@ -279,7 +282,8 @@ public class ZipkinEventNotifier extends EventNotifierSupport { ServerSpanThreadBinder binder = brave.serverSpanThreadBinder(); brave.serverRequestInterceptor().handle(new ZipkinServerRequestAdapter(event.getExchange())); ServerSpan span = binder.getCurrentServerSpan(); - event.getExchange().setProperty("CamelZipkinServerSpan", span); + String key = "CamelZipkinServerSpan-" + serviceName; + event.getExchange().setProperty(key, span); if (log.isDebugEnabled()) { log.debug("serverRequest: service={}, id={} ", serviceName, span != null ? span.getSpan().getId() : "<null>"); @@ -288,7 +292,8 @@ public class ZipkinEventNotifier extends EventNotifierSupport { private void serverResponse(Brave brave, String serviceName, ExchangeCompletedEvent event) { ServerSpanThreadBinder binder = brave.serverSpanThreadBinder(); - ServerSpan span = event.getExchange().getProperty("CamelZipkinServerSpan", ServerSpan.class); + String key = "CamelZipkinServerSpan-" + serviceName; + ServerSpan span = event.getExchange().getProperty(key, ServerSpan.class); binder.setCurrentSpan(span); brave.serverResponseInterceptor().handle(new ZipkinServerResponseAdapter(event.getExchange())); binder.setCurrentSpan(null); @@ -300,7 +305,8 @@ public class ZipkinEventNotifier extends EventNotifierSupport { private void serverResponse(Brave brave, String serviceName, ExchangeFailedEvent event) { ServerSpanThreadBinder binder = brave.serverSpanThreadBinder(); - ServerSpan span = event.getExchange().getProperty("CamelZipkinServerSpan", ServerSpan.class); + String key = "CamelZipkinServerSpan-" + serviceName; + ServerSpan span = event.getExchange().getProperty(key, ServerSpan.class); binder.setCurrentSpan(span); brave.serverResponseInterceptor().handle(new ZipkinServerResponseAdapter(event.getExchange())); binder.setCurrentSpan(null); http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinOneRouteScribe.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinOneRouteScribe.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinOneRouteScribe.java index c14a026..de48945 100644 --- a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinOneRouteScribe.java +++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinOneRouteScribe.java @@ -16,12 +16,9 @@ */ package org.apache.camel.zipkin.scribe; -import java.util.concurrent.TimeUnit; - import com.github.kristofa.brave.scribe.ScribeSpanCollector; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; -import org.apache.camel.builder.NotifyBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.camel.zipkin.ZipkinEventNotifier; @@ -40,14 +37,12 @@ public class ZipkinOneRouteScribe extends CamelTestSupport { private String ip = "192.168.99.100"; private ZipkinEventNotifier zipkin; - // TODO: producer template also - // TODO: message id added x2 - @Override protected CamelContext createCamelContext() throws Exception { CamelContext context = super.createCamelContext(); zipkin = new ZipkinEventNotifier(); + // we have one route as service zipkin.addServiceMapping("seda:cat", "cat"); zipkin.setSpanCollector(new ScribeSpanCollector(ip, 9410)); context.getManagementStrategy().addEventNotifier(zipkin); @@ -57,13 +52,7 @@ public class ZipkinOneRouteScribe extends CamelTestSupport { @Test public void testZipkinRoute() throws Exception { - NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); - - for (int i = 0; i < 1; i++) { - template.sendBody("seda:cat", "Hello Cat"); - } - - assertTrue(notify.matches(30, TimeUnit.SECONDS)); + template.requestBody("direct:start", "Hello Cat"); } @Override @@ -71,6 +60,8 @@ public class ZipkinOneRouteScribe extends CamelTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { + from("direct:start").to("seda:cat"); + from("seda:cat").routeId("cat") .log("routing at ${routeId}") .delay(simple("${random(1000,2000)}")); http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinRouteConcurrentScribe.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinRouteConcurrentScribe.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinRouteConcurrentScribe.java index 65fdfdf..3b91b00 100644 --- a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinRouteConcurrentScribe.java +++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinRouteConcurrentScribe.java @@ -45,6 +45,7 @@ public class ZipkinRouteConcurrentScribe extends CamelTestSupport { CamelContext context = super.createCamelContext(); zipkin = new ZipkinEventNotifier(); + // we have 2 routes as services zipkin.addServiceMapping("seda:foo", "foo"); zipkin.addServiceMapping("seda:bar", "bar"); zipkin.setSpanCollector(new ScribeSpanCollector(ip, 9410)); @@ -55,10 +56,10 @@ public class ZipkinRouteConcurrentScribe extends CamelTestSupport { @Test public void testZipkinRoute() throws Exception { - NotifyBuilder notify = new NotifyBuilder(context).whenDone(5).create(); + NotifyBuilder notify = new NotifyBuilder(context).from("seda:foo").whenDone(5).create(); for (int i = 0; i < 5; i++) { - template.sendBody("seda:foo", "Hello World"); + template.requestBody("direct:foo", "Hello World"); } assertTrue(notify.matches(60, TimeUnit.SECONDS)); @@ -69,12 +70,14 @@ public class ZipkinRouteConcurrentScribe extends CamelTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - from("seda:foo?concurrentConsumers=5").routeId("foo") + from("direct:start").to("sed:foo"); + + from("seda:foo?concurrentConsumers=2").routeId("foo") .log("routing at ${routeId}") .delay(simple("${random(1000,2000)}")) .to("seda:bar"); - from("seda:bar?concurrentConsumers=5").routeId("bar") + from("seda:bar?concurrentConsumers=2").routeId("bar") .log("routing at ${routeId}") .delay(simple("${random(0,500)}")); } http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinSimpleRouteScribe.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinSimpleRouteScribe.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinSimpleRouteScribe.java index fc898c8..5032366 100644 --- a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinSimpleRouteScribe.java +++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinSimpleRouteScribe.java @@ -16,12 +16,9 @@ */ package org.apache.camel.zipkin.scribe; -import java.util.concurrent.TimeUnit; - import com.github.kristofa.brave.scribe.ScribeSpanCollector; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; -import org.apache.camel.builder.NotifyBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.test.junit4.CamelTestSupport; import org.apache.camel.zipkin.ZipkinEventNotifier; @@ -45,6 +42,7 @@ public class ZipkinSimpleRouteScribe extends CamelTestSupport { CamelContext context = super.createCamelContext(); zipkin = new ZipkinEventNotifier(); + // we have one route as service zipkin.addServiceMapping("seda:dude", "dude"); zipkin.setSpanCollector(new ScribeSpanCollector(ip, 9410)); context.getManagementStrategy().addEventNotifier(zipkin); @@ -54,13 +52,9 @@ public class ZipkinSimpleRouteScribe extends CamelTestSupport { @Test public void testZipkinRoute() throws Exception { - NotifyBuilder notify = new NotifyBuilder(context).whenDone(5).create(); - for (int i = 0; i < 5; i++) { - template.sendBody("seda:dude", "Hello World"); + template.requestBody("direct:start", "Hello World"); } - - assertTrue(notify.matches(30, TimeUnit.SECONDS)); } @Override @@ -68,6 +62,8 @@ public class ZipkinSimpleRouteScribe extends CamelTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { + from("direct:start").to("seda:dude"); + from("seda:dude").routeId("dude") .log("routing at ${routeId}") .delay(simple("${random(1000,2000)}")); http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTimerRouteScribe.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTimerRouteScribe.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTimerRouteScribe.java index 017102c..9e03d66 100644 --- a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTimerRouteScribe.java +++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTimerRouteScribe.java @@ -46,6 +46,7 @@ public class ZipkinTimerRouteScribe extends CamelTestSupport { CamelContext context = super.createCamelContext(); zipkin = new ZipkinEventNotifier(); + // we have one route as service zipkin.addServiceMapping("seda:timer", "timer"); zipkin.setSpanCollector(new ScribeSpanCollector(ip, 9410)); context.getManagementStrategy().addEventNotifier(zipkin); http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTwoRouteScribe.java ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTwoRouteScribe.java b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTwoRouteScribe.java new file mode 100644 index 0000000..7e72c17 --- /dev/null +++ b/components/camel-zipkin/src/test/java/org/apache/camel/zipkin/scribe/ZipkinTwoRouteScribe.java @@ -0,0 +1,77 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.zipkin.scribe; + +import com.github.kristofa.brave.scribe.ScribeSpanCollector; +import org.apache.camel.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.camel.zipkin.ZipkinEventNotifier; +import org.junit.Test; + +/** + * Integration test requires running Zipkin/Scribe running + * + * The easiest way is to run using zipkin-docker: https://github.com/openzipkin/docker-zipkin + * + * Adjust the IP address to what IP docker-machines have assigned, you can use + * <tt>docker-machines ls</tt> + */ +public class ZipkinTwoRouteScribe extends CamelTestSupport { + + private String ip = "192.168.99.100"; + private ZipkinEventNotifier zipkin; + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + zipkin = new ZipkinEventNotifier(); + // we have 2 routes as services + zipkin.addServiceMapping("seda:cat", "cat"); + zipkin.addServiceMapping("seda:dog", "dog"); + zipkin.setSpanCollector(new ScribeSpanCollector(ip, 9410)); + context.getManagementStrategy().addEventNotifier(zipkin); + + return context; + } + + @Test + public void testZipkinRoute() throws Exception { + template.requestBody("direct:start", "Hello Cat and Dog"); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("seda:cat"); + + from("seda:cat").routeId("cat") + .log("routing at ${routeId}") + .delay(simple("${random(1000,2000)}")) + .to("seda:dog"); + + from("seda:dog").routeId("dog") + .log("routing at ${routeId}") + .delay(simple("${random(0,500)}")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c15d1cb2/components/camel-zipkin/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-zipkin/src/test/resources/log4j.properties b/components/camel-zipkin/src/test/resources/log4j.properties index ab7b29f..538c5bd 100644 --- a/components/camel-zipkin/src/test/resources/log4j.properties +++ b/components/camel-zipkin/src/test/resources/log4j.properties @@ -18,7 +18,7 @@ # # The logging properties used # -log4j.rootLogger=INFO, file +log4j.rootLogger=INFO, out #log4j.logger.org.apache.camel=DEBUG log4j.logger.org.apache.camel.zipkin=DEBUG