Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codefromthecrypt/4574ec38af4122a24da7c0058328cb30 to your computer and use it in GitHub Desktop.
Save codefromthecrypt/4574ec38af4122a24da7c0058328cb30 to your computer and use it in GitHub Desktop.
normal pattern
HttpServletRequest httpRequest = (HttpServletRequest) request;
TraceContextOrSamplingFlags contextOrFlags = contextExtractor.extract(httpRequest);
Span span = contextOrFlags.context() != null
? tracer.joinSpan(contextOrFlags.context())
: tracer.newTrace(contextOrFlags.samplingFlags());
@codefromthecrypt
Copy link
Author

codefromthecrypt commented Feb 9, 2017

  /**
   * Joining is re-using the same trace and span ids extracted from an incoming request. Here, we
   * ensure a sampling decision has been made. If the span passed sampling, we assume this is a
   * shared span, one where the caller and the current tracer report to the same span IDs. If no
   * sampling decision occurred yet, we have exclusive access to this span ID.
   *
   * <p>Here's an example of conditionally joining a span, depending on if a trace context was
   * extracted from an incoming request.
   *
   * <pre>{@code
   * contextOrFlags = extractor.extract(request);
   * span = contextOrFlags.context() != null
   *          ? tracer.joinSpan(contextOrFlags.context())
   *          : tracer.newTrace(contextOrFlags.samplingFlags());
   * }</pre>
   *
   * @see Propagation
   * @see TraceContext.Extractor#extract(Object)
   * @see TraceContextOrSamplingFlags#context()
   */
  public final Span joinSpan(TraceContext context) {

  /**
   * Like {@link #newTrace()}, but supports parameterized sampling, for example limiting on
   * operation or url pattern.
   *
   * <p>For example, to sample all requests for a specific url:
   * <pre>{@code
   * Span newTrace(Request input) {
   *   SamplingFlags flags = SamplingFlags.NONE;
   *   if (input.url().startsWith("/experimental")) {
   *     flags = SamplingFlags.SAMPLED;
   *   } else if (input.url().startsWith("/static")) {
   *     flags = SamplingFlags.NOT_SAMPLED;
   *   }
   *   return tracer.newTrace(flags);
   * }
   * }</pre>
   */
  public Span newTrace(SamplingFlags samplingFlags) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment