diff --git a/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java index 1d03225a..df259d0a 100644 --- a/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/EndToEndBenchmarks.java @@ -28,7 +28,6 @@ import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import zipkin2.reporter.AsyncReporter; -import zipkin2.reporter.Reporter; import static javax.servlet.DispatcherType.REQUEST; diff --git a/instrumentation/benchmarks/src/main/java/brave/NoopSender.java b/instrumentation/benchmarks/src/main/java/brave/NoopSender.java index 5112433c..13889d8b 100644 --- a/instrumentation/benchmarks/src/main/java/brave/NoopSender.java +++ b/instrumentation/benchmarks/src/main/java/brave/NoopSender.java @@ -27,6 +27,10 @@ final class NoopSender extends Sender { return encoding().listSizeInBytes(encodedSpans); } + @Override public int messageSizeInBytes(int encodedSizeInBytes) { + return encoding().listSizeInBytes(encodedSizeInBytes); + } + @Override public Call sendSpans(List encodedSpans) { messageEncoder.encode(encodedSpans); return Call.create(null); diff --git a/pom.xml b/pom.xml index 1694295d..975369ef 100755 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ 3.1.4.Final 2.4.1 - 2.1.4 + 2.2.0 1.1.2 6.45.0 2.8.2 diff --git a/spring-beans/README.md b/spring-beans/README.md index d34cf570..ccac2279 100644 --- a/spring-beans/README.md +++ b/spring-beans/README.md @@ -4,15 +4,14 @@ tracing with only XML ## Configuration Bean Factories exist for the following types: -* AsyncReporterFactoryBean - for configuring how often spans are sent to Zipkin * EndpointFactoryBean - for configuring the service name, IP etc representing this host * TracingFactoryBean - wires most together, like reporter and log integration * HttpTracingFactoryBean - for http tagging and sampling policy Here are some example beans using the factories in this module: ```xml - - + + diff --git a/spring-beans/pom.xml b/spring-beans/pom.xml index 93172331..7b0c2845 100644 --- a/spring-beans/pom.xml +++ b/spring-beans/pom.xml @@ -28,15 +28,15 @@ ${project.version} - org.springframework - spring-beans - ${spring.version} - provided + io.zipkin.reporter2 + zipkin-reporter-spring-beans + ${zipkin-reporter2.version} org.springframework - spring-context - test + spring-beans + 2.5.6 + provided diff --git a/spring-beans/src/main/java/brave/spring/beans/AsyncReporterFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/AsyncReporterFactoryBean.java index acf59296..e54d1b92 100644 --- a/spring-beans/src/main/java/brave/spring/beans/AsyncReporterFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/AsyncReporterFactoryBean.java @@ -1,75 +1,6 @@ package brave.spring.beans; -import java.util.concurrent.TimeUnit; -import org.springframework.beans.factory.config.AbstractFactoryBean; -import zipkin2.codec.SpanBytesEncoder; -import zipkin2.reporter.AsyncReporter; -import zipkin2.reporter.ReporterMetrics; -import zipkin2.reporter.Sender; - -/** Spring XML config does not support chained builders. This converts accordingly */ -public class AsyncReporterFactoryBean extends AbstractFactoryBean { - Sender sender; - SpanBytesEncoder encoder = SpanBytesEncoder.JSON_V1; - ReporterMetrics metrics; - Integer messageMaxBytes; - Integer messageTimeout; - Integer closeTimeout; - Integer queuedMaxSpans; - Integer queuedMaxBytes; - - @Override public Class getObjectType() { - return AsyncReporter.class; - } - - @Override protected AsyncReporter createInstance() throws Exception { - AsyncReporter.Builder builder = AsyncReporter.builder(sender); - if (metrics != null) builder.metrics(metrics); - if (messageMaxBytes != null) builder.messageMaxBytes(messageMaxBytes); - if (messageTimeout != null) builder.messageTimeout(messageTimeout, TimeUnit.MILLISECONDS); - if (closeTimeout != null) builder.closeTimeout(closeTimeout, TimeUnit.MILLISECONDS); - if (queuedMaxSpans != null) builder.queuedMaxSpans(queuedMaxSpans); - if (queuedMaxBytes != null) builder.queuedMaxBytes(queuedMaxBytes); - return builder.build(encoder); - } - - @Override protected void destroyInstance(AsyncReporter instance) throws Exception { - instance.close(); - } - - @Override public boolean isSingleton() { - return true; - } - - public void setSender(Sender sender) { - this.sender = sender; - } - - public void setEncoder(SpanBytesEncoder encoder) { - this.encoder = encoder; - } - - public void setMetrics(ReporterMetrics metrics) { - this.metrics = metrics; - } - - public void setMessageMaxBytes(Integer messageMaxBytes) { - this.messageMaxBytes = messageMaxBytes; - } - - public void setMessageTimeout(Integer messageTimeout) { - this.messageTimeout = messageTimeout; - } - - public void setCloseTimeout(Integer closeTimeout) { - this.closeTimeout = closeTimeout; - } - - public void setQueuedMaxSpans(Integer queuedMaxSpans) { - this.queuedMaxSpans = queuedMaxSpans; - } - - public void setQueuedMaxBytes(Integer queuedMaxBytes) { - this.queuedMaxBytes = queuedMaxBytes; - } +/** @deprecated use {@link zipkin2.reporter.beans.AsyncReporterFactoryBean} */ +@Deprecated +public class AsyncReporterFactoryBean extends zipkin2.reporter.beans.AsyncReporterFactoryBean { } diff --git a/spring-beans/src/main/java/brave/spring/beans/EndpointFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/EndpointFactoryBean.java index 00d7365c..b0c4abdf 100644 --- a/spring-beans/src/main/java/brave/spring/beans/EndpointFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/EndpointFactoryBean.java @@ -4,7 +4,7 @@ import zipkin2.Endpoint; /** Spring XML config does not support chained builders. This converts accordingly */ -public class EndpointFactoryBean implements FactoryBean { +public class EndpointFactoryBean implements FactoryBean { String serviceName; String ip; diff --git a/spring-beans/src/main/java/brave/spring/beans/HttpTracingFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/HttpTracingFactoryBean.java index 037e4d55..4af60fc8 100644 --- a/spring-beans/src/main/java/brave/spring/beans/HttpTracingFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/HttpTracingFactoryBean.java @@ -8,7 +8,7 @@ import org.springframework.beans.factory.FactoryBean; /** Spring XML config does not support chained builders. This converts accordingly */ -public class HttpTracingFactoryBean implements FactoryBean { +public class HttpTracingFactoryBean implements FactoryBean { Tracing tracing; HttpClientParser clientParser; diff --git a/spring-beans/src/main/java/brave/spring/beans/TracingFactoryBean.java b/spring-beans/src/main/java/brave/spring/beans/TracingFactoryBean.java index 4195a742..cfa21a20 100644 --- a/spring-beans/src/main/java/brave/spring/beans/TracingFactoryBean.java +++ b/spring-beans/src/main/java/brave/spring/beans/TracingFactoryBean.java @@ -11,7 +11,7 @@ import zipkin2.reporter.Reporter; /** Spring XML config does not support chained builders. This converts accordingly */ -public class TracingFactoryBean extends AbstractFactoryBean { +public class TracingFactoryBean extends AbstractFactoryBean { String localServiceName; Endpoint localEndpoint; @@ -37,8 +37,8 @@ public class TracingFactoryBean extends AbstractFactoryBean { return builder.build(); } - @Override protected void destroyInstance(Tracing instance) throws Exception { - instance.close(); + @Override protected void destroyInstance(Object instance) throws Exception { + ((Tracing) instance).close(); } @Override public Class getObjectType() { diff --git a/spring-beans/src/test/java/brave/spring/beans/AsyncReporterFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/AsyncReporterFactoryBeanTest.java deleted file mode 100755 index 04004949..00000000 --- a/spring-beans/src/test/java/brave/spring/beans/AsyncReporterFactoryBeanTest.java +++ /dev/null @@ -1,160 +0,0 @@ -package brave.spring.beans; - -import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.Test; -import zipkin2.codec.Encoding; -import zipkin2.codec.SpanBytesEncoder; -import zipkin2.reporter.AsyncReporter; -import zipkin2.reporter.ReporterMetrics; -import zipkin2.reporter.Sender; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class AsyncReporterFactoryBeanTest { - - public static Sender SENDER = mock(Sender.class); - public static ReporterMetrics METRICS = mock(ReporterMetrics.class); - - static { - when(SENDER.encoding()).thenReturn(Encoding.JSON); - when(SENDER.messageMaxBytes()).thenReturn(1024); - } - - XmlBeans context; - - @After public void close() { - if (context != null) context.close(); - } - - @Test public void sender() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("sender") - .containsExactly(SENDER); - } - - @Test public void metrics() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("metrics") - .containsExactly(METRICS); - } - - @Test public void messageMaxBytes() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("messageMaxBytes") - .containsExactly(512); - } - - @Test public void messageTimeout() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("messageTimeoutNanos") - .containsExactly(TimeUnit.MILLISECONDS.toNanos(500)); - } - - @Test public void closeTimeout() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("closeTimeoutNanos") - .containsExactly(TimeUnit.MILLISECONDS.toNanos(500)); - } - - @Test public void queuedMaxSpans() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("pending.maxSize") - .containsExactly(10); - } - - @Test public void queuedMaxBytes() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("pending.maxBytes") - .containsExactly(512); - } - - @Test public void encoder() { - context = new XmlBeans("" - + "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + "" - ); - context.refresh(); - - assertThat(context.getBean(AsyncReporter.class)) - .extracting("encoder") - .containsExactly(SpanBytesEncoder.JSON_V2); - } -} diff --git a/spring-beans/src/test/java/brave/spring/beans/EndpointFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/EndpointFactoryBeanTest.java index 036f13e5..1296a220 100755 --- a/spring-beans/src/test/java/brave/spring/beans/EndpointFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/EndpointFactoryBeanTest.java @@ -21,9 +21,8 @@ public class EndpointFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Endpoint.class)) + assertThat(context.getBean("localEndpoint", Endpoint.class)) .isEqualTo(Endpoint.newBuilder().serviceName("brave-webmvc-example").build()); } @@ -34,9 +33,8 @@ public class EndpointFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Endpoint.class)) + assertThat(context.getBean("localEndpoint", Endpoint.class)) .isEqualTo(Endpoint.newBuilder() .serviceName("brave-webmvc-example") .ip("1.2.3.4") @@ -50,10 +48,9 @@ public class EndpointFactoryBeanTest { + " \n" + "" ); - context.refresh(); try { - context.getBean(Endpoint.class); + context.getBean("localEndpoint", Endpoint.class); failBecauseExceptionWasNotThrown(BeanCreationException.class); } catch (BeanCreationException e) { assertThat(e) @@ -69,9 +66,8 @@ public class EndpointFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Endpoint.class)) + assertThat(context.getBean("localEndpoint", Endpoint.class)) .isEqualTo(Endpoint.newBuilder() .serviceName("brave-webmvc-example") .ip("1.2.3.4") diff --git a/spring-beans/src/test/java/brave/spring/beans/HttpTracingFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/HttpTracingFactoryBeanTest.java index 72afd53e..6b42a7f6 100755 --- a/spring-beans/src/test/java/brave/spring/beans/HttpTracingFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/HttpTracingFactoryBeanTest.java @@ -31,9 +31,8 @@ public class HttpTracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(HttpTracing.class)) + assertThat(context.getBean("httpTracing", HttpTracing.class)) .extracting("tracing") .containsExactly(TRACING); } @@ -49,9 +48,8 @@ public class HttpTracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(HttpTracing.class)) + assertThat(context.getBean("httpTracing", HttpTracing.class)) .extracting("clientParser") .containsExactly(CLIENT_PARSER); } @@ -67,9 +65,8 @@ public class HttpTracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(HttpTracing.class)) + assertThat(context.getBean("httpTracing", HttpTracing.class)) .extracting("serverParser") .containsExactly(SERVER_PARSER); } @@ -85,9 +82,8 @@ public class HttpTracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(HttpTracing.class)) + assertThat(context.getBean("httpTracing", HttpTracing.class)) .extracting("clientSampler") .containsExactly(HttpSampler.NEVER_SAMPLE); } @@ -103,9 +99,8 @@ public class HttpTracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(HttpTracing.class)) + assertThat(context.getBean("httpTracing", HttpTracing.class)) .extracting("serverSampler") .containsExactly(HttpSampler.NEVER_SAMPLE); } diff --git a/spring-beans/src/test/java/brave/spring/beans/TracingFactoryBeanTest.java b/spring-beans/src/test/java/brave/spring/beans/TracingFactoryBeanTest.java index b441e31a..96a7b168 100755 --- a/spring-beans/src/test/java/brave/spring/beans/TracingFactoryBeanTest.java +++ b/spring-beans/src/test/java/brave/spring/beans/TracingFactoryBeanTest.java @@ -27,7 +27,7 @@ public class TracingFactoryBeanTest { context = new XmlBeans("" + "\n" ); - context.refresh(); + context.getBean("tracing", Tracing.class); assertThat(Tracing.current()).isNotNull(); @@ -44,9 +44,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.recorder.spanMap.localEndpoint") .extracting("serviceName") .containsExactly("brave-webmvc-example"); @@ -64,9 +63,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.recorder.spanMap.localEndpoint") .containsExactly(Endpoint.newBuilder() .serviceName("brave-webmvc-example") @@ -82,9 +80,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.recorder.reporter") .containsExactly(Reporter.CONSOLE); } @@ -97,9 +94,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.clock") .containsExactly(CLOCK); } @@ -112,9 +108,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.sampler") .containsExactly(Sampler.NEVER_SAMPLE); } @@ -127,9 +122,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.currentTraceContext") .allMatch(o -> o instanceof StrictCurrentTraceContext); } @@ -151,9 +145,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class).propagation()) + assertThat(context.getBean("tracing", Tracing.class).propagation()) .isInstanceOf(ExtraFieldPropagation.class) .extracting("nameToKey") .allSatisfy(m -> assertThat((Map) m) @@ -166,9 +159,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.traceId128Bit") .containsExactly(true); } @@ -179,9 +171,8 @@ public class TracingFactoryBeanTest { + " \n" + "" ); - context.refresh(); - assertThat(context.getBean(Tracing.class)) + assertThat(context.getBean("tracing", Tracing.class)) .extracting("tracer.supportsJoin") .containsExactly(true); } diff --git a/spring-beans/src/test/java/brave/spring/beans/XmlBeans.java b/spring-beans/src/test/java/brave/spring/beans/XmlBeans.java index c8049388..91780f8d 100644 --- a/spring-beans/src/test/java/brave/spring/beans/XmlBeans.java +++ b/spring-beans/src/test/java/brave/spring/beans/XmlBeans.java @@ -1,25 +1,23 @@ package brave.spring.beans; import java.nio.charset.Charset; -import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.Resource; -class XmlBeans extends AbstractXmlApplicationContext { +class XmlBeans { static final Charset UTF_8 = Charset.forName("UTF-8"); - final ByteArrayResource resource; + final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeans(String... beans) { StringBuilder joined = new StringBuilder(); for (String bean : beans) { joined.append(bean).append('\n'); } - this.resource = new ByteArrayResource(beans(joined.toString()).getBytes(UTF_8)); - } - - @Override protected Resource[] getConfigResources() { - return new Resource[] {resource}; + new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions( + new ByteArrayResource(beans(joined.toString()).getBytes(UTF_8)) + ); } static String beans(String bean) { @@ -28,10 +26,18 @@ static String beans(String bean) { + " xmlns:util=\"http://www.springframework.org/schema/util\"\n" + " xsi:schemaLocation=\"\n" + " http://www.springframework.org/schema/beans\n" - + " http://www.springframework.org/schema/beans/spring-beans-3.2.xsd\n" + + " http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\n" + " http://www.springframework.org/schema/util\n" - + " http://www.springframework.org/schema/util/spring-util-3.2.xsd\">\n" + + " http://www.springframework.org/schema/util/spring-util-2.5.xsd\">\n" + bean + ""; } -} + + T getBean(String name, Class requiredType) { + return (T) beanFactory.getBean(name, requiredType); + } + + void close(){ + beanFactory.destroySingletons(); + } +} \ No newline at end of file