HttpSender extends ThriftSender { @Override public void send(Process process, List<Span> spans) throws SenderException { Batch batch = new Batch(process, spans); byte[] bytes = null; try { bytes = serialize(batch); } catch (Exception e) { throw new SenderException(String.format("Failed to serialize %d spans", spans.size()), e, spans.size()); } RequestBody body = RequestBody.create(MEDIA_TYPE_THRIFT, bytes); Request request = requestBuilder.post(body).build(); Response response; try { response = httpClient.newCall(request).execute(); } catch (IOException e) { throw new SenderException(String.format("Could not send %d spans", spans.size()), e, spans.size()); } if (response.isSuccessful()) { response.close(); return; } String responseBody; try { responseBody = response.body() != null ? response.body().string() : "null"; } catch (IOException e) { responseBody = "unable to read response"; } String exceptionMessage = String.format("Could not send %d spans, response %d: %s", spans.size(), response.code(), responseBody); throw new SenderException(exceptionMessage, null, spans.size()); } protected  HttpSender(Builder builder); @Override void send(Process process, List<Span> spans);  }
@Test public void sendHappy() throws Exception { new HttpSender.Builder(target("/api/traces").getUri().toString()) .build() .send(new Process("robotrock"), generateSpans()); new HttpSender.Builder(target("/api/traces").getUri().toString()).withMaxPacketSize(6500).build() .send(new Process("name"), generateSpans()); new HttpSender.Builder(target("/api/traces").getUri().toString()).withMaxPacketSize(6500) .withClient(new OkHttpClient()).build() .send(new Process("name"), generateSpans()); }
@Test(expected = Exception.class) public void sendServerError() throws Exception { HttpSender sender = new HttpSender.Builder(target("/api/tracesErr").getUri().toString()) .build(); sender.send(new Process("robotrock"), generateSpans()); }
@Test(expected = Exception.class) public void serverDoesntExist() throws Exception { HttpSender sender = new HttpSender.Builder("http: .build(); sender.send(new Process("robotrock"), generateSpans()); }
@Test(expected = SenderException.class) public void senderFail() throws Exception { HttpSender sender = new HttpSender.Builder("http: .build(); sender.send(null, generateSpans()); }
@Test public void sendWithoutAuthData() throws Exception { System.setProperty(Configuration.JAEGER_ENDPOINT, target("/api/traces").getUri().toString()); HttpSender sender = (HttpSender) Configuration.SenderConfiguration.fromEnv().getSender(); sender.send(new Process("robotrock"), generateSpans()); }
@Test public void sendWithBasicAuth() throws Exception { System.setProperty(Configuration.JAEGER_ENDPOINT, target("/api/basic-auth").getUri().toString()); System.setProperty(Configuration.JAEGER_USER, "jdoe"); System.setProperty(Configuration.JAEGER_PASSWORD, "password"); HttpSender sender = (HttpSender) Configuration.SenderConfiguration.fromEnv().getSender(); sender.send(new Process("robotrock"), generateSpans()); }
@Test public void sendWithTokenAuth() throws Exception { System.setProperty(Configuration.JAEGER_ENDPOINT, target("/api/bearer").getUri().toString()); System.setProperty(Configuration.JAEGER_AUTH_TOKEN, "thetoken"); HttpSender sender = (HttpSender) Configuration.SenderConfiguration.fromEnv().getSender(); sender.send(new Process("robotrock"), generateSpans()); }
@Test public void sanityTestForTokenAuthTest() throws Exception { System.setProperty(Configuration.JAEGER_ENDPOINT, target("/api/bearer").getUri().toString()); System.setProperty(Configuration.JAEGER_AUTH_TOKEN, "invalid-token"); HttpSender sender = (HttpSender) Configuration.SenderConfiguration.fromEnv().getSender(); try { sender.send(new Process("robotrock"), generateSpans()); fail("expecting exception"); } catch (Exception te) { assertTrue(te.getMessage().contains("response 401")); } }