All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zipkin.server.brave.BraveConfiguration Maven / Gradle / Ivy

There is a newer version: 2.9.4
Show newest version
/**
 * Copyright 2015-2016 The OpenZipkin Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package zipkin.server.brave;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.ServerClientAndLocalSpanState;
import com.github.kristofa.brave.SpanCollectorMetricsHandler;
import com.github.kristofa.brave.ThreadLocalServerClientAndLocalSpanState;
import com.github.kristofa.brave.local.LocalSpanCollector;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import zipkin.Endpoint;
import zipkin.collector.CollectorMetrics;
import zipkin.server.ConditionalOnSelfTracing;
import zipkin.storage.StorageComponent;

@Configuration
@ConditionalOnSelfTracing
@Import(ApiTracerConfiguration.class)
public class BraveConfiguration {

  /** This gets the lanIP without trying to lookup its name. */
  // http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet
  @Bean
  @Scope Endpoint local(@Value("${server.port:9411}") int port) {
    int ipv4;
    try {
      ipv4 = Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
          .flatMap(i -> Collections.list(i.getInetAddresses()).stream())
          .filter(ip -> ip instanceof Inet4Address && ip.isSiteLocalAddress())
          .map(InetAddress::getAddress)
          .map(bytes -> new BigInteger(bytes).intValue())
          .findAny().get();
    } catch (Exception ignored) {
      ipv4 = 127 << 24 | 1;
    }
    return Endpoint.create("zipkin-server", ipv4, port);
  }

  @Bean LocalSpanCollector spanCollector(StorageComponent storage,
      @Value("${zipkin.self-tracing.flush-interval:1}") int flushInterval,
      final CollectorMetrics metrics) {
    LocalSpanCollector.Config config = LocalSpanCollector.Config.builder()
        .flushInterval(flushInterval).build();
    return LocalSpanCollector.create(storage, config, new SpanCollectorMetricsHandler() {
      CollectorMetrics local = metrics.forTransport("local");
      @Override public void incrementAcceptedSpans(int i) {
        local.incrementSpans(i);
      }

      @Override public void incrementDroppedSpans(int i) {
        local.incrementSpansDropped(i);
      }
    });
  }

  @Bean ServerClientAndLocalSpanState braveState(@Qualifier("local") Endpoint localEndpoint) {
    return new ThreadLocalServerClientAndLocalSpanState(localEndpoint.ipv4, localEndpoint.port,
        localEndpoint.serviceName);
  }

  @Bean Brave brave(ServerClientAndLocalSpanState braveState, LocalSpanCollector spanCollector) {
    return new Brave.Builder(braveState)
        .spanCollector(spanCollector)
        .build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy