public Tracing build() { if (clock == null) clock = Platform.get().clock(); if (localIp == null) localIp = Platform.get().linkLocalIp(); if (spanReporter == null) spanReporter = new LoggingReporter(); return new Default(this); }
/** * This sets the client IP:port to the {@linkplain Channel#remoteAddress() remote address} if * {@link HttpServerAdapter#parseClientIpAndPort} fails. */ void parseChannelAddress(ChannelHandlerContext ctx, HttpRequest request, Span span) { if (adapter.parseClientIpFromXForwardedFor(request, span)) return; InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); if (remoteAddress.getAddress() == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
/** Generates a new 64-bit ID, taking care to dodge zero which can be confused with absent */ long nextId() { long nextId = Platform.get().randomLong(); while (nextId == 0L) { nextId = Platform.get().randomLong(); } return nextId; } }
traceIdHigh = traceId128Bit ? Platform.get().nextTraceIdHigh() : 0L; traceId = nextId; } else { // child of an existing span. ensure the shared flag is unset
/** * Constructs a new Brave instance that sends traces to the provided tracer. * * @param state for in-process propagation. Note {@link CommonSpanState#endpoint()} is ignored. */ public static Brave newBrave(Tracing tracing, ServerClientAndLocalSpanState state) { if (tracing == null) throw new NullPointerException("tracer == null"); if (state == null) throw new NullPointerException("state == null"); Clock delegate = brave.internal.Platform.get().clock(); return new Brave.Builder(state) .clock(new AnnotationSubmitter.Clock() { @Override public long currentTimeMicroseconds() { return delegate.currentTimeMicroseconds(); } @Override public String toString() { return delegate.toString(); } }) .spanFactory(new Brave4SpanFactory(tracing)) .recorder(new Brave4Recorder(tracing)).build(); }
@Benchmark @Group("mild_contention") @GroupThreads(2) public long mild_contention_nextTraceIdHigh_jre7() { return jre7.nextTraceIdHigh(); }
@Benchmark @Group("high_contention") @GroupThreads(8) public long high_contention_randomLong_jre6() { return jre6.randomLong(); }
String produceLinkLocalIp() { try { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); if (nics == null) return null; while (nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); Enumeration<InetAddress> addresses = nic.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isSiteLocalAddress()) return address.getHostAddress(); } } } catch (Exception e) { // don't crash the caller if there was a problem reading nics. log("error reading nics", e); } return null; }
@Override public String generate() { return Long.toHexString(Platform.get().nextTraceIdHigh()); } }
/** Generates a new 64-bit ID, taking care to dodge zero which can be confused with absent */ long nextId() { long nextId = Platform.get().randomLong(); while (nextId == 0L) { nextId = Platform.get().randomLong(); } return nextId; } }
@Benchmark @Group("no_contention") @GroupThreads(1) public long no_contention_nextTraceIdHigh_jre6() { return jre6.nextTraceIdHigh(); }
@Benchmark @Group("mild_contention") @GroupThreads(2) public long mild_contention_randomLong_jre7() { return jre7.randomLong(); }
String produceLinkLocalIp() { try { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); if (nics == null) return null; while (nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); Enumeration<InetAddress> addresses = nic.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isSiteLocalAddress()) return address.getHostAddress(); } } } catch (Exception e) { // don't crash the caller if there was a problem reading nics. log("error reading nics", e); } return null; }
boolean invalidIdLength(Object key, int length, int max) { if (length > 1 && length <= max) return false; assert max == 32 || max == 16; Platform.get().log(max == 32 ? "{0} should be a 1 to 32 character lower-hex string with no prefix" : "{0} should be a 1 to 16 character lower-hex string with no prefix", key, null); return true; }
@Override public String generate() { return Long.toHexString(Platform.get().nextTraceIdHigh()); } }
public Tracing build() { if (clock == null) clock = Platform.get().clock(); if (localIp == null) localIp = Platform.get().linkLocalIp(); if (spanReporter == null) spanReporter = new LoggingReporter(); return new Default(this); }
static void parseRemoteAddress(RpcContext rpcContext, Span span) { InetSocketAddress remoteAddress = rpcContext.getRemoteAddress(); if (remoteAddress == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
@Benchmark @Group("no_contention") @GroupThreads(1) public long no_contention_nextTraceIdHigh_jre7() { return jre7.nextTraceIdHigh(); }
@Benchmark @Group("no_contention") @GroupThreads(1) public long no_contention_randomLong_jre6() { return jre6.randomLong(); }