@Override public Integer call() throws Exception { final IntegrationTestSpanCollector mockSpanCollector = new IntegrationTestSpanCollector(); final Brave.Builder builder = new Brave.Builder("serviceName"); final Brave brave = builder.spanCollector(mockSpanCollector).build(); final ServerTracer serverTracer = brave.serverTracer(); final Random random = new Random(); final String serverSpanName = "server span name " + random.nextLong(); serverTracer.setStateCurrentTrace( random.nextLong(), random.nextLong(), random.nextLong(), serverSpanName); serverTracer.setServerReceived(); serverTracer.submitAnnotation("custom annotation"); // Simulate client. final ClientTracer clientTracer = brave.clientTracer(); final String clientSpanName = "client span name " + random.nextLong(); clientTracer.startNewSpan(clientSpanName); clientTracer.setClientSent(); clientTracer.setClientReceived(); serverTracer.setServerSend(); final List<Span> collectedSpans = mockSpanCollector.getCollectedSpans(); assertEquals("Expected 2 collected spans.", 2, collectedSpans.size()); final Span clientSpan = collectedSpans.get(0); final Span serverSpan = collectedSpans.get(1); assertTrue(serverSpan.getTrace_id() != 0); assertTrue(serverSpan.getId() != 0); assertTrue(serverSpan.getParent_id() != 0); assertTrue(clientSpan.getTrace_id() != 0); assertTrue(clientSpan.getId() != 0); assertTrue(clientSpan.getParent_id() != 0); assertEquals( "Should belong to same trace.", serverSpan.getTrace_id(), clientSpan.getTrace_id()); assertTrue("Span ids should be different.", serverSpan.getId() != clientSpan.getId()); assertEquals( "Parent span id of client span should be equal to server span id.", serverSpan.getId(), clientSpan.getParent_id()); assertEquals("Expect sr, ss and 1 custom annotation.", 3, serverSpan.getAnnotations().size()); assertEquals(2, clientSpan.getAnnotations().size()); return 2; }
/** * Handles outgoing request. * * @param adapter The adapter deals with implementation specific details. */ public void handle(ClientRequestAdapter adapter) { SpanId spanId = clientTracer.startNewSpan(adapter.getSpanName()); if (spanId == null) { // We will not trace this request. adapter.addSpanIdToRequest(null); } else { adapter.addSpanIdToRequest(spanId); clientTracer.setCurrentClientServiceName(adapter.getClientServiceName()); for (KeyValueAnnotation annotation : adapter.requestAnnotations()) { clientTracer.submitBinaryAnnotation(annotation.getKey(), annotation.getValue()); } clientTracer.setClientSent(); } }
@Override public Integer call() throws Exception { final IntegrationTestSpanCollector mockSpanCollector = new IntegrationTestSpanCollector(); final Brave.Builder builder = new Brave.Builder("serviceName"); final Brave brave = builder.spanCollector(mockSpanCollector).build(); final ServerTracer serverTracer = brave.serverTracer(); final Random random = new Random(); final String serverSpanName = "server span name " + random.nextLong(); serverTracer.setStateCurrentTrace(random.nextLong(), random.nextLong(), null, serverSpanName); serverTracer.setServerReceived(); serverTracer.submitAnnotation("custom annotation"); // Simulate client. final ClientTracer clientTracer = brave.clientTracer(); final String clientSpanName = "client span name " + random.nextLong(); clientTracer.startNewSpan(clientSpanName); clientTracer.setClientSent(); clientTracer.setClientReceived(); // Simulate local. final LocalTracer localTracer = brave.localTracer(); final String localSpanName = "local span name " + random.nextLong(); localTracer.startNewSpan("test", localSpanName); localTracer.finishSpan(); serverTracer.setServerSend(); final List<Span> collectedSpans = mockSpanCollector.getCollectedSpans(); assertEquals("Expected 3 collected spans.", 3, collectedSpans.size()); final Span clientSpan = collectedSpans.get(0); final Span localSpan = collectedSpans.get(1); final Span serverSpan = collectedSpans.get(2); assertTrue(serverSpan.trace_id != 0); assertFalse(serverSpan.isSetParent_id()); assertTrue(serverSpan.id != 0); assertEquals(serverSpanName, serverSpan.name); assertEquals(serverSpan.trace_id, clientSpan.trace_id); assertEquals(serverSpan.id, clientSpan.parent_id); assertTrue(clientSpan.id != 0); assertEquals(clientSpanName, clientSpan.name); assertEquals(serverSpan.trace_id, localSpan.trace_id); assertEquals(serverSpan.id, localSpan.parent_id); assertTrue(localSpan.id != 0); assertEquals(localSpanName, localSpan.name); assertEquals( "Span ids should be different.", 3, Stream.of(serverSpan.id, clientSpan.id, localSpan.id).distinct().count()); assertEquals("Expect sr, ss and 1 custom annotation.", 3, serverSpan.getAnnotations().size()); assertEquals(2, clientSpan.getAnnotations().size()); assertFalse(localSpan.isSetAnnotations()); return 2; }