ZipkinSender implements Sender { @Override public int append(JaegerSpan span) throws SenderException { byte[] next = encoder.encode(backFillHostOnAnnotations(ThriftSpanConverter.convertSpan(span))); int messageSizeOfNextSpan = delegate.messageSizeInBytes(Collections.singletonList(next)); if (messageSizeOfNextSpan > delegate.messageMaxBytes()) { throw new SenderException( delegate.toString() + " received a span that was too large", null, 1); } spanBuffer.add(next); int nextSizeInBytes = delegate.messageSizeInBytes(spanBuffer); if (nextSizeInBytes <= delegate.messageMaxBytes()) { if (nextSizeInBytes < delegate.messageMaxBytes()) { return 0; } return flush(); } spanBuffer.remove(spanBuffer.size() - 1); int n; try { n = flush(); } catch (SenderException e) { throw new SenderException(e.getMessage(), e.getCause(), e.getDroppedSpanCount() + 1); } spanBuffer.add(next); return n; } ZipkinSender(zipkin2.reporter.Sender delegate); static ZipkinSender create(String endpoint); static ZipkinSender create(zipkin2.reporter.Sender delegate); @Override int append(JaegerSpan span); @Override int flush(); @Override int close();  }
@Test public void testAppendSpanTooLarge() { JaegerSpan jaegerSpan = tracer.buildSpan("raza").start(); String msg = ""; for (int i = 0; i < 1001; i++) { msg += "."; } jaegerSpan.log(msg); try { sender.append(jaegerSpan); fail("The line above shoud throw"); } catch (SenderException e) { assertEquals(e.getDroppedSpanCount(), 1); } }
@Test public void testAppend() throws Exception { JaegerSpan jaegerSpan = tracer.buildSpan("raza").start(); com.twitter.zipkin.thriftjava.Span span = ThriftSpanConverter.convertSpan(jaegerSpan); int expectedNumSpans = 11; List<byte[]> spansToSend = new ArrayList<>(expectedNumSpans); for (int i = 0; i < expectedNumSpans; i++) { spansToSend.add(new ThriftSpanEncoder().encode(ZipkinSender.backFillHostOnAnnotations(span))); } int messageMaxBytes = sender.delegate.messageSizeInBytes(spansToSend); sender.close(); sender = newSender(messageMaxBytes); for (int i = 0; i < expectedNumSpans - 1; i++) { assertEquals(0, sender.append(jaegerSpan)); } int result = sender.append(jaegerSpan); assertEquals(expectedNumSpans, result); }