zipkin2.reporter.kafka.KafkaSender Java Examples

The following examples show how to use zipkin2.reporter.kafka.KafkaSender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaZipkinFactory.java    From dropwizard-zipkin with Apache License 2.0 6 votes vote down vote up
/**
 * Build a new {@link HttpTracing} instance for interfacing with Zipkin
 *
 * @param environment Environment
 * @return Brave instance
 */
@Override
public Optional<HttpTracing> build(final Environment environment) {
  if (!isEnabled()) {
    LOGGER.warn("Zipkin tracing is disabled");
    return Optional.empty();
  }

  final KafkaSender sender =
      KafkaSender.newBuilder()
          .bootstrapServers(bootstrapServers)
          .topic(topic)
          .overrides(overrides)
          .build();

  LOGGER.info("Sending spans to Kafka topic \"{}\" at: {}", topic, bootstrapServers);

  return buildTracing(environment, sender);
}
 
Example #2
Source File: KafkaSenderBenchmarks.java    From zipkin-reporter-java with Apache License 2.0 6 votes vote down vote up
@Override protected Sender createSender() throws Exception {
  broker.start();
  kafka = new KafkaJunitRule(broker).waitForStartup();
  consumer = kafka.helper().createByteConsumer();
  consumer.subscribe(Collections.singletonList("zipkin"));

  new Thread(() -> {
    while (true) {
      Iterator<ConsumerRecord<byte[], byte[]>> messages = consumer.poll(1000L).iterator();
      while (messages.hasNext()) {
        messages.next();
      }
    }
  }).start();

  return KafkaSender.create(broker.getBrokerList().get());
}
 
Example #3
Source File: KafkaSenderFactoryBean.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Override protected KafkaSender createInstance() {
  KafkaSender.Builder builder = KafkaSender.newBuilder();
  if (bootstrapServers != null) builder.bootstrapServers(bootstrapServers);
  if (encoding != null) builder.encoding(encoding);
  if (topic != null) builder.topic(topic);
  if (messageMaxBytes != null) builder.messageMaxBytes(messageMaxBytes);
  return builder.build();
}
 
Example #4
Source File: ZipkinAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void overrideKafkaTopic() throws Exception {
	this.context = new AnnotationConfigApplicationContext();
	environment().setProperty("spring.zipkin.kafka.topic", "zipkin2");
	environment().setProperty("spring.zipkin.sender.type", "kafka");
	this.context.register(PropertyPlaceholderAutoConfiguration.class,
			KafkaAutoConfiguration.class, ZipkinAutoConfiguration.class,
			TraceAutoConfiguration.class);
	this.context.refresh();

	then(this.context.getBean(Sender.class)).isInstanceOf(KafkaSender.class);

	this.context.close();
}
 
Example #5
Source File: ZipkinKafkaSenderConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME)
Sender kafkaSender(KafkaProperties config) {
	Map<String, Object> properties = config.buildProducerProperties();
	properties.put("key.serializer", ByteArraySerializer.class.getName());
	properties.put("value.serializer", ByteArraySerializer.class.getName());
	// Kafka expects the input to be a String, but KafkaProperties returns a list
	Object bootstrapServers = properties.get("bootstrap.servers");
	if (bootstrapServers instanceof List) {
		properties.put("bootstrap.servers", join((List) bootstrapServers));
	}
	return KafkaSender.newBuilder().topic(this.topic).overrides(properties)
			.build();
}
 
Example #6
Source File: KafkaSenderFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class) public void close_closesSender() {
  context = new XmlBeans(""
      + "<bean id=\"sender\" class=\"zipkin2.reporter.beans.KafkaSenderFactoryBean\">\n"
      + "  <property name=\"bootstrapServers\" value=\"localhost:9092\"/>\n"
      + "</bean>"
  );

  KafkaSender sender = context.getBean("sender", KafkaSender.class);
  context.close();

  sender.sendSpans(Arrays.asList(new byte[] {'{', '}'}));
}
 
Example #7
Source File: KafkaSenderFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void encoding() {
  context = new XmlBeans(""
      + "<bean id=\"sender\" class=\"zipkin2.reporter.beans.KafkaSenderFactoryBean\">\n"
      + "  <property name=\"bootstrapServers\" value=\"localhost:9092\"/>\n"
      + "  <property name=\"encoding\" value=\"PROTO3\"/>\n"
      + "</bean>"
  );

  assertThat(context.getBean("sender", KafkaSender.class))
      .extracting("encoding")
      .isEqualTo(Encoding.PROTO3);
}
 
Example #8
Source File: KafkaSenderFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void messageMaxBytes() {
  context = new XmlBeans(""
      + "<bean id=\"sender\" class=\"zipkin2.reporter.beans.KafkaSenderFactoryBean\">\n"
      + "  <property name=\"bootstrapServers\" value=\"localhost:9092\"/>\n"
      + "  <property name=\"messageMaxBytes\" value=\"1024\"/>\n"
      + "</bean>"
  );

  assertThat(context.getBean("sender", KafkaSender.class))
      .extracting("messageMaxBytes")
      .isEqualTo(1024);
}
 
Example #9
Source File: KafkaSenderFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void topic() {
  context = new XmlBeans(""
      + "<bean id=\"sender\" class=\"zipkin2.reporter.beans.KafkaSenderFactoryBean\">\n"
      + "  <property name=\"bootstrapServers\" value=\"localhost:9092\"/>\n"
      + "  <property name=\"topic\" value=\"zipkin2\"/>\n"
      + "</bean>"
  );

  assertThat(context.getBean("sender", KafkaSender.class))
      .isEqualToComparingFieldByField(KafkaSender.newBuilder()
          .bootstrapServers("localhost:9092")
          .topic("zipkin2").build());
}
 
Example #10
Source File: KafkaSenderFactoryBeanTest.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
@Test public void bootstrapServers() {
  context = new XmlBeans(""
      + "<bean id=\"sender\" class=\"zipkin2.reporter.beans.KafkaSenderFactoryBean\">\n"
      + "  <property name=\"bootstrapServers\" value=\"localhost:9092\"/>\n"
      + "</bean>"
  );

  assertThat(context.getBean("sender", KafkaSender.class))
      .isEqualToComparingFieldByField(KafkaSender.newBuilder()
          .bootstrapServers("localhost:9092")
          .build());
}
 
Example #11
Source File: KafkaZipkinTracerIntegrationTest.java    From zipkin-finagle with Apache License 2.0 5 votes vote down vote up
@Test public void whenKafkaIsDown() throws Exception {
  broker.stop();

  // Make a new tracer that fails faster than 60 seconds
  tracer.close();
  stats.clear();
  Map<String, String> overrides = new LinkedHashMap<>();
  overrides.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, "100");
  tracer = new KafkaZipkinTracer(KafkaSender.newBuilder()
      .bootstrapServers(config.bootstrapServers())
      .topic(config.topic())
      .overrides(overrides)
      .build(), config, stats);

  tracer.record(new Record(FinagleTestObjects.root, Time.fromMilliseconds(FinagleTestObjects.TODAY), new ServiceName("web"), none));
  tracer.record(new Record(FinagleTestObjects.root, Time.fromMilliseconds(FinagleTestObjects.TODAY), new Rpc("get"), none));
  tracer.record(new Record(FinagleTestObjects.root, Time.fromMilliseconds(FinagleTestObjects.TODAY), ClientSend$.MODULE$, none));
  tracer.record(new Record(
      FinagleTestObjects.root, Time.fromMilliseconds(FinagleTestObjects.TODAY + 1), ClientRecv$.MODULE$, none));

  Thread.sleep(1500); // wait for kafka request attempt to go through

  assertThat(mapAsJavaMap(stats.counters())).containsOnly(
      entry(FinagleTestObjects.seq("spans"), 1L),
      entry(FinagleTestObjects.seq("span_bytes"), 185L),
      entry(FinagleTestObjects.seq("spans_dropped"), 1L),
      entry(FinagleTestObjects.seq("messages"), 1L),
      entry(FinagleTestObjects.seq("message_bytes"), 187L),
      entry(FinagleTestObjects.seq("messages_dropped"), 1L),
      entry(FinagleTestObjects.seq("messages_dropped", "org.apache.kafka.common.errors.TimeoutException"), 1L)
  );
}
 
Example #12
Source File: KafkaZipkinTracer.java    From zipkin-finagle with Apache License 2.0 5 votes vote down vote up
KafkaZipkinTracer(Config config, StatsReceiver stats) {
  this(KafkaSender.newBuilder()
      .encoding(Encoding.JSON)
      .bootstrapServers(config.bootstrapServers())
      .topic(config.topic())
      .build(), config, stats);
}
 
Example #13
Source File: KafkaIngressTest.java    From pitchfork with Apache License 2.0 5 votes vote down vote up
/**
 * Create reporter.
 */
private static AsyncReporter<zipkin2.Span> setupReporter() {
    var sender = KafkaSender.newBuilder()
            .encoding(Encoding.PROTO3)
            .bootstrapServers(kafkaContainer.getBootstrapServers())
            .build();
    return AsyncReporter.create(sender);
}
 
Example #14
Source File: TracingBuilderTest.java    From brave-kafka-interceptor with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuildKafkaSenderWithList() {
  // Given
  Map<String, Object> map = new HashMap<>();
  map.put(SENDER_TYPE_CONFIG, TracingBuilder.SenderBuilder.SenderType.KAFKA.name());
  map.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG,
    Arrays.asList("localhost:9092", "localhost:9094"));
  TracingConfiguration config = new TracingConfiguration(map);
  // When
  Sender sender = new TracingBuilder.SenderBuilder(config).build();
  // Then
  assertTrue(sender instanceof KafkaSender);
}
 
Example #15
Source File: TracingBuilderTest.java    From brave-kafka-interceptor with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuildKafkaSenderWithDefault() {
  // Given
  Map<String, String> map = new HashMap<>();
  map.put(SENDER_TYPE_CONFIG, TracingBuilder.SenderBuilder.SenderType.KAFKA.name());
  map.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
  TracingConfiguration config = new TracingConfiguration(map);
  // When
  Sender sender = new TracingBuilder.SenderBuilder(config).build();
  // Then
  assertTrue(sender instanceof KafkaSender);
}
 
Example #16
Source File: TracingBuilderTest.java    From brave-kafka-interceptor with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBuildKafkaSenderWithConfig() {
  // Given
  Map<String, String> map = new HashMap<>();
  map.put(SENDER_TYPE_CONFIG, TracingBuilder.SenderBuilder.SenderType.KAFKA.name());
  map.put(KAFKA_BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
  TracingConfiguration config = new TracingConfiguration(map);
  // When
  Sender sender = new TracingBuilder.SenderBuilder(config).build();
  // Then
  assertTrue(sender instanceof KafkaSender);
}
 
Example #17
Source File: TracingBuilder.java    From brave-kafka-interceptor with Apache License 2.0 4 votes vote down vote up
Sender build(Encoding encoding) {
  return KafkaSender.newBuilder().bootstrapServers(bootstrapServers).encoding(encoding).build();
}
 
Example #18
Source File: KafkaSenderFactoryBean.java    From zipkin-reporter-java with Apache License 2.0 4 votes vote down vote up
@Override public Class<? extends KafkaSender> getObjectType() {
  return KafkaSender.class;
}
 
Example #19
Source File: KafkaSenderFactoryBean.java    From zipkin-reporter-java with Apache License 2.0 4 votes vote down vote up
@Override protected void destroyInstance(Object instance) {
  ((KafkaSender) instance).close();
}
 
Example #20
Source File: KafkaZipkinTracer.java    From zipkin-finagle with Apache License 2.0 4 votes vote down vote up
KafkaZipkinTracer(KafkaSender kafka, Config config, StatsReceiver stats) {
  super(kafka, config, stats);
  this.kafka = kafka;
}