brave.propagation.TraceContextOrSamplingFlags类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中brave.propagation.TraceContextOrSamplingFlags类的一些代码示例,展示了TraceContextOrSamplingFlags类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TraceContextOrSamplingFlags类的具体详情如下:
包路径:brave.propagation.TraceContextOrSamplingFlags
类名称:TraceContextOrSamplingFlags

TraceContextOrSamplingFlags介绍

[英]Union type that contains only one of trace context, trace ID context or sampling flags. This type is designed for use with Tracer#nextSpan(TraceContextOrSamplingFlags).

Users should not create instances of this, rather use TraceContext.Extractor provided by a Propagation implementation such as Propagation#B3_STRING.

Those implementing Propagation should use the following advice:

If you have the trace and span ID, use  
#create(TraceContext) 
 If you have only a trace ID, use  
#create(TraceIdContext) 
 Otherwise, use  
#create(SamplingFlags)

If your propagation implementation adds extra data, append it via Builder#addExtra(Object).

This started as a port of com.github.kristofa.brave.TraceData, which served the same purpose.
[中]仅包含跟踪上下文、跟踪ID上下文或采样标志之一的联合类型。此类型设计用于Tracer#nextSpan(TraceContextOrSamplingFlags)。
用户不应该创建这样的实例,而应该使用TraceContext。由传播实现(如传播#B3_字符串)提供的提取器。
实施传播的人员应使用以下建议:

If you have the trace and span ID, use  
#create(TraceContext) 
 If you have only a trace ID, use  
#create(TraceIdContext) 
 Otherwise, use  
#create(SamplingFlags)

如果您的传播实现添加了额外的数据,请通过Builder#addExtra(对象)将其追加。
这是从com端口开始的。github。克里斯托法。勇敢的同样的目的。

代码示例

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

/**
 * Use this to create a span for processing the given message. Note: the result has no
 * name and is not started.
 * <p>
 * <p>
 * This creates a child from identifiers extracted from the message headers, or a new
 * span if one couldn't be extracted.
 */
public Span nextSpan(Message<?> message) {
  MessageHeaderAccessor headers = mutableHeaderAccessor(message);
  TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
  headers.setImmutable();
  Span result = this.tracer.nextSpan(extracted);
  if (extracted.context() == null && !result.isNoop()) {
    addTags(message, result, null);
  }
  if (log.isDebugEnabled()) {
    log.debug("Created a new span " + result);
  }
  return result;
}

代码示例来源:origin: openzipkin/brave

static TraceContextOrSamplingFlags tryParseSamplingFlags(CharSequence b3, int pos) {
 int flags = parseFlags(b3, pos);
 if (flags == 0) return null;
 return TraceContextOrSamplingFlags.create(SamplingFlags.toSamplingFlags(flags));
}

代码示例来源:origin: openzipkin/brave

/** Creates a potentially noop span representing this request */
// copy/pasted from HttpServerHandler.nextSpan
Span nextSpan(TraceContextOrSamplingFlags extracted, HttpRequest request) {
 Boolean sampled = extracted.sampled();
 // only recreate the context if the http sampler made a decision
 if (sampled == null && (sampled = sampler.trySample(adapter, request)) != null) {
  extracted = extracted.sampled(sampled.booleanValue());
 }
 return extracted.context() != null
   ? tracer.joinSpan(extracted.context())
   : tracer.nextSpan(extracted);
}

代码示例来源:origin: openzipkin/brave

/** @deprecated do not use object variant.. only set when you have a sampling decision */
@Deprecated
public TraceContextOrSamplingFlags sampled(@Nullable Boolean sampled) {
 if (sampled != null) return sampled(sampled.booleanValue());
 int flags = value.flags;
 flags &= ~FLAG_SAMPLED_SET;
 flags &= ~FLAG_SAMPLED;
 if (flags == value.flags) return this; // save effort if no change
 return withFlags(flags);
}

代码示例来源:origin: openzipkin/brave

TraceContextOrSamplingFlags withFlags(int flags) {
 switch (type) {
  case 1:
   TraceContext context = InternalPropagation.instance.withFlags((TraceContext) value, flags);
   return new TraceContextOrSamplingFlags(type, context, extra);
  case 2:
   TraceIdContext traceIdContext = idContextWithFlags(flags);
   return new TraceContextOrSamplingFlags(type, traceIdContext, extra);
  case 3:
   SamplingFlags samplingFlags = SamplingFlags.toSamplingFlags(flags);
   if (extra.isEmpty()) return create(samplingFlags);
   return new TraceContextOrSamplingFlags(type, samplingFlags, extra);
 }
 throw new AssertionError("programming error");
}

代码示例来源:origin: openzipkin/brave

TraceContext context = extracted.context();
if (context != null) return newChild(context);
TraceIdContext traceIdContext = extracted.traceIdContext();
if (traceIdContext != null) {
 return _toSpan(nextContext(
   InternalPropagation.instance.flags(extracted.traceIdContext()),
   traceIdContext.traceIdHigh(),
   traceIdContext.traceId(),
   0L,
   0L,
   extracted.extra()
 ));
SamplingFlags samplingFlags = extracted.samplingFlags();
List<Object> extra = extracted.extra();

代码示例来源:origin: openzipkin/brave

void verifyRoundTrip(TraceContextOrSamplingFlags expected) {
 TraceContextOrSamplingFlags extracted = propagation.extractor(mapEntry).extract(map);
 assertThat(extracted)
   .isEqualTo(expected);
 Map<K, String> injected = new LinkedHashMap<>();
 if (expected.context() != null) {
  propagation.injector(mapEntry).inject(expected.context(), injected);
 } else {
  inject(injected, expected.samplingFlags());
 }
 assertThat(map).isEqualTo(injected);
}

代码示例来源:origin: openzipkin/brave

/** Returns a mapping of any fields in the extraction result. */
public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) {
 if (extracted == null) throw new NullPointerException("extracted == null");
 TraceContext extractedContext = extracted.context();
 if (extractedContext != null) return getAll(extractedContext);
 PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra());
 return fields != null ? fields.toMap() : Collections.emptyMap();
}

代码示例来源:origin: openzipkin/brave

@Override public TraceContextOrSamplingFlags extract(C carrier) {
  Tags tags = null;
  if (carrier instanceof Metadata) {
   tags = extractTags(((Metadata) carrier).get(GRPC_TAGS_BIN));
   byte[] bytes = ((Metadata) carrier).get(GRPC_TRACE_BIN);
   if (bytes != null) {
    TraceContext maybeContext = TraceContextBinaryFormat.parseBytes(bytes, tags);
    if (maybeContext != null) return TraceContextOrSamplingFlags.create(maybeContext);
   }
  }
  TraceContextOrSamplingFlags result = delegate.extract(carrier);
  if (tags == null) return result;
  return result.toBuilder().addExtra(tags).build();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public TraceContextOrSamplingFlags extract(C carrier) {
  if (carrier == null) throw new NullPointerException("carrier == null");
  // try to extract single-header format
  TraceContextOrSamplingFlags extracted = singleExtractor.extract(carrier);
  if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) return extracted;
  // Start by looking at the sampled state as this is used regardless
  // Official sampled value is 1, though some old instrumentation send true
  String sampled = getter.get(carrier, propagation.sampledKey);
  Boolean sampledV = sampled != null
    ? sampled.equals("1") || sampled.equalsIgnoreCase("true")
    : null;
  boolean debug = "1".equals(getter.get(carrier, propagation.debugKey));
  String traceIdString = getter.get(carrier, propagation.traceIdKey);
  // It is ok to go without a trace ID, if sampling or debug is set
  if (traceIdString == null) return TraceContextOrSamplingFlags.create(sampledV, debug);
  // Try to parse the trace IDs into the context
  TraceContext.Builder result = TraceContext.newBuilder();
  if (result.parseTraceId(traceIdString, propagation.traceIdKey)
    && result.parseSpanId(getter, carrier, propagation.spanIdKey)
    && result.parseParentId(getter, carrier, propagation.parentSpanIdKey)) {
   if (sampledV != null) result.sampled(sampledV.booleanValue());
   if (debug) result.debug(true);
   return TraceContextOrSamplingFlags.create(result.build());
  }
  return TraceContextOrSamplingFlags.EMPTY; // trace context is malformed so return empty
 }
}

代码示例来源:origin: io.zipkin.brave/brave-propagation-aws

return TraceContextOrSamplingFlags.newBuilder()
   .extra(extra)
   .samplingFlags(SamplingFlags.Builder.build(sampled))
   .build();
} else if (parent == null) {
 return TraceContextOrSamplingFlags.newBuilder()
   .extra(extra)
   .traceIdContext(TraceIdContext.newBuilder()
   .build();
return TraceContextOrSamplingFlags.create(TraceContext.newBuilder()
  .traceIdHigh(traceIdHigh)
  .traceId(traceId)

代码示例来源:origin: openzipkin/brave

if (extracted.samplingFlags() != null && extracted.extra().isEmpty()) {
 Span span = consumerSpansForTopic.get(topic);
 if (span == null) {

代码示例来源:origin: openzipkin/brave

TraceContextOrSamplingFlags extractAndClearHeaders(Headers headers) {
 TraceContextOrSamplingFlags extracted = extractor.extract(headers);
 // clear propagation headers if we were able to extract a span
 if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) {
  clearHeaders(headers);
 }
 return extracted;
}

代码示例来源:origin: io.zipkin.brave/brave

TraceContext context = extracted.context();
if (context != null) return newChild(context);
TraceIdContext traceIdContext = extracted.traceIdContext();
if (traceIdContext != null) {
 return _toSpan(nextContext(
   InternalPropagation.instance.flags(extracted.traceIdContext()),
   traceIdContext.traceIdHigh(),
   traceIdContext.traceId(),
   0L,
   0L,
   extracted.extra()
 ));
SamplingFlags samplingFlags = extracted.samplingFlags();
List<Object> extra = extracted.extra();

代码示例来源:origin: io.zipkin.brave/brave-tests

void verifyRoundTrip(TraceContextOrSamplingFlags expected) {
 TraceContextOrSamplingFlags extracted = propagation.extractor(mapEntry).extract(map);
 assertThat(extracted)
   .isEqualTo(expected);
 Map<K, String> injected = new LinkedHashMap<>();
 if (expected.context() != null) {
  propagation.injector(mapEntry).inject(expected.context(), injected);
 } else {
  inject(injected, expected.samplingFlags());
 }
 assertThat(map).isEqualTo(injected);
}

代码示例来源:origin: io.zipkin.brave/brave

/** Returns a mapping of any fields in the extraction result. */
public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) {
 if (extracted == null) throw new NullPointerException("extracted == null");
 TraceContext extractedContext = extracted.context();
 if (extractedContext != null) return getAll(extractedContext);
 PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra());
 return fields != null ? fields.toMap() : Collections.emptyMap();
}

代码示例来源:origin: io.zipkin.brave/brave-instrumentation-grpc

@Override public TraceContextOrSamplingFlags extract(C carrier) {
  Tags tags = null;
  if (carrier instanceof Metadata) {
   tags = extractTags(((Metadata) carrier).get(GRPC_TAGS_BIN));
   byte[] bytes = ((Metadata) carrier).get(GRPC_TRACE_BIN);
   if (bytes != null) {
    TraceContext maybeContext = TraceContextBinaryFormat.parseBytes(bytes, tags);
    if (maybeContext != null) return TraceContextOrSamplingFlags.create(maybeContext);
   }
  }
  TraceContextOrSamplingFlags result = delegate.extract(carrier);
  if (tags == null) return result;
  return result.toBuilder().addExtra(tags).build();
 }
}

代码示例来源:origin: io.zipkin.brave/brave

TraceContextOrSamplingFlags withFlags(int flags) {
 switch (type) {
  case 1:
   TraceContext context = InternalPropagation.instance.withFlags((TraceContext) value, flags);
   return new TraceContextOrSamplingFlags(type, context, extra);
  case 2:
   TraceIdContext traceIdContext = idContextWithFlags(flags);
   return new TraceContextOrSamplingFlags(type, traceIdContext, extra);
  case 3:
   SamplingFlags samplingFlags = SamplingFlags.toSamplingFlags(flags);
   if (extra.isEmpty()) return create(samplingFlags);
   return new TraceContextOrSamplingFlags(type, samplingFlags, extra);
 }
 throw new AssertionError("programming error");
}

代码示例来源:origin: io.zipkin.brave/brave

@Override public TraceContextOrSamplingFlags extract(C carrier) {
  if (carrier == null) throw new NullPointerException("carrier == null");
  // try to extract single-header format
  TraceContextOrSamplingFlags extracted = singleExtractor.extract(carrier);
  if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) return extracted;
  // Start by looking at the sampled state as this is used regardless
  // Official sampled value is 1, though some old instrumentation send true
  String sampled = getter.get(carrier, propagation.sampledKey);
  Boolean sampledV = sampled != null
    ? sampled.equals("1") || sampled.equalsIgnoreCase("true")
    : null;
  boolean debug = "1".equals(getter.get(carrier, propagation.debugKey));
  String traceIdString = getter.get(carrier, propagation.traceIdKey);
  // It is ok to go without a trace ID, if sampling or debug is set
  if (traceIdString == null) return TraceContextOrSamplingFlags.create(sampledV, debug);
  // Try to parse the trace IDs into the context
  TraceContext.Builder result = TraceContext.newBuilder();
  if (result.parseTraceId(traceIdString, propagation.traceIdKey)
    && result.parseSpanId(getter, carrier, propagation.spanIdKey)
    && result.parseParentId(getter, carrier, propagation.parentSpanIdKey)) {
   if (sampledV != null) result.sampled(sampledV.booleanValue());
   if (debug) result.debug(true);
   return TraceContextOrSamplingFlags.create(result.build());
  }
  return TraceContextOrSamplingFlags.EMPTY; // trace context is malformed so return empty
 }
}

代码示例来源:origin: io.zipkin.brave/brave-instrumentation-kafka-clients

if (extracted.samplingFlags() != null && extracted.extra().isEmpty()) {
 Span span = consumerSpansForTopic.get(topic);
 if (span == null) {

相关文章