io.opentracing.Span.context()方法的使用及代码示例

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

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

Span.context介绍

[英]Retrieve the associated SpanContext. This may be called at any time, including after calls to finish().
[中]检索关联的上下文。这可以在任何时候调用,包括在调用finish()之后。

代码示例

代码示例来源:origin: stagemonitor/stagemonitor

@Override
public SpanContext context() {
  return delegate.context();
}

代码示例来源:origin: oracle/helidon

@Override
public SpanContext spanContext() {
  return requestSpan.context();
}

代码示例来源:origin: oracle/helidon

@Override
public SpanContext context() {
  return span.context();
}

代码示例来源:origin: weibocom/motan

protected void attachTraceInfo(Tracer tracer, Span span, final Request request) {
  tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() {
    @Override
    public void put(String key, String value) {
      request.setAttachment(key, value);
    }
    @Override
    public Iterator<Entry<String, String>> iterator() {
      throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()");
    }
  });
}

代码示例来源:origin: fengjiachun/Jupiter

private void injectContext(Tracer tracer, Span span, final JRequest request) {
  tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() {
    @Override
    public Iterator<Map.Entry<String, String>> iterator() {
      throw new UnsupportedOperationException("iterator");
    }
    @Override
    public void put(String key, String value) {
      request.putAttachment(key, value);
    }
  });
}

代码示例来源:origin: fengjiachun/Jupiter

private void injectContext(Tracer tracer, Span span, final JRequest request) {
  tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() {
    @Override
    public Iterator<Map.Entry<String, String>> iterator() {
      throw new UnsupportedOperationException("iterator");
    }
    @Override
    public void put(String key, String value) {
      request.putAttachment(key, value);
    }
  });
}

代码示例来源:origin: stagemonitor/stagemonitor

public static String getTraceId(Tracer tracer, final Span span) {
  final String[] spanId = new String[1];
  tracer.inject(span.context(), B3HeaderFormat.INSTANCE, new B3HeaderFormat.B3InjectAdapter() {
    @Override
    public void setParentId(String value) {
    }
    @Override
    public void setSpanId(String value) {
    }
    @Override
    public void setTraceId(String value) {
      spanId[0] = value;
    }
  });
  return spanId[0];
}

代码示例来源:origin: stagemonitor/stagemonitor

public static boolean isRoot(Tracer tracer, final Span span) {
  final AtomicBoolean root = new AtomicBoolean(true);
  tracer.inject(span.context(), B3HeaderFormat.INSTANCE, new B3HeaderFormat.B3InjectAdapter() {
    @Override
    public void setParentId(String value) {
      root.set(false);
    }
    @Override
    public void setSpanId(String value) {
    }
    @Override
    public void setTraceId(String value) {
    }
  });
  return root.get();
}

代码示例来源:origin: oracle/helidon

private Map<String, List<String>> tracingHeaders(Tracer tracer, Span currentSpan) {
  Map<String, String> tracerHeaders = new HashMap<>();
  tracer.inject(currentSpan.context(),
         Format.Builtin.HTTP_HEADERS,
         new TextMapInjectAdapter(tracerHeaders));
  return new HashMap<>(tracerHeaders.entrySet()
                 .stream()
                 .collect(Collectors.toMap(Map.Entry::getKey,
                              entry -> CollectionsHelper
                                  .listOf(entry.getValue()))));
}

代码示例来源:origin: jersey/jersey

.property(OpenTracingFeature.SPAN_CONTEXT_PROPERTY, providedSpan.context())  // <--- span propagation
    .post(Entity.text("Hello"));
    .property(OpenTracingFeature.SPAN_CONTEXT_PROPERTY, childSpan.context())  // <--- span propagation
    .post(Entity.text("World"));
childSpan.log("2st Response received from managed client");

代码示例来源:origin: stagemonitor/stagemonitor

public static B3Identifiers getB3Identifiers(Tracer tracer, final Span span) {
  final B3Identifiers.Builder b3Identifiers = B3Identifiers.builder();
  tracer.inject(span.context(), B3HeaderFormat.INSTANCE, new B3HeaderFormat.B3InjectAdapter() {
    @Override
    public void setParentId(String value) {
      b3Identifiers.parentSpanId(value);
    }
    @Override
    public void setSpanId(String value) {
      b3Identifiers.spanId(value);
    }
    @Override
    public void setTraceId(String value) {
      b3Identifiers.traceId(value);
    }
  });
  return b3Identifiers.build();
}

代码示例来源:origin: qunarcorp/qmq

public static void inject(Message message, Tracer tracer) {
  Scope scope = tracer.scopeManager().active();
  if (scope == null) return;
  tracer.inject(scope.span().context(), Format.Builtin.TEXT_MAP, new QmqMessageInjectAdapter(message));
}

代码示例来源:origin: jersey/jersey

@Override
  public void filter(ClientRequestContext requestContext) throws IOException {
    Tracer.SpanBuilder spanBuilder = GlobalTracer.get()
        .buildSpan(LocalizationMessages.OPENTRACING_SPAN_PREFIX_CLIENT() + requestContext.getMethod())
        .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
        .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
        .withTag(Tags.HTTP_URL.getKey(), requestContext.getUri().toASCIIString())
        .withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod())
        .withTag(LocalizationMessages.OPENTRACING_TAG_HAS_REQUEST_ENTITY(), requestContext.hasEntity())
        .withTag(LocalizationMessages.OPENTRACING_TAG_ACCEPTABLE_MEDIA_TYPES(), requestContext.getAcceptableMediaTypes()
            .stream()
            .map(MediaType::toString)
            .collect(Collectors.joining(", ")))
        .withTag(LocalizationMessages.OPENTRACING_TAG_REQUEST_HEADERS(),
            OpenTracingUtils.headersAsString(requestContext.getHeaders()));

    // if pre-stored "span" property is found, propagate the stored context
    final Object property = requestContext.getProperty(OpenTracingFeature.SPAN_CONTEXT_PROPERTY);
    if (property != null && property instanceof SpanContext) {
      spanBuilder = spanBuilder.asChildOf((SpanContext) property);
    }
    Span span = spanBuilder.startManual();

    requestContext.setProperty(OpenTracingFeature.SPAN_CONTEXT_PROPERTY, span);
    Map<String, String> addedHeaders = new HashMap<>();
    GlobalTracer.get().inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(addedHeaders));
    addedHeaders.forEach((key, value) -> requestContext.getHeaders().add(key, value));
  }
}

代码示例来源:origin: oracle/helidon

WebServer webServer = bareRequest.webServer();
Span span = createRequestSpan(tracer(webServer), bareRequest);
RoutedResponse response = new RoutedResponse(webServer, bareResponse, span.context());
response.whenSent()
    .thenRun(() -> {

代码示例来源:origin: oracle/helidon

@Override
public void filter(ContainerRequestContext request) {
  boolean closeParentSpan = false;
  SpanContext requestSpanContext = parentSpanContextProvider.get();
  if (null == requestSpanContext) {
    closeParentSpan = true;
    Span requestSpan = security().tracer().buildSpan("security-parent").start();
    request.setProperty(PROP_PARENT_SPAN, requestSpan);
    requestSpanContext = requestSpan.context();
  }
  request.setProperty(PROP_CLOSE_PARENT_SPAN, closeParentSpan);
  // create a new security context
  SecurityContext securityContext = security()
      .contextBuilder(Integer.toString(CONTEXT_COUNTER.incrementAndGet(), Character.MAX_RADIX))
      .tracingSpan(requestSpanContext)
      .executorService(executorService)
      .build();
  injectionManager.<Ref<SecurityContext>>getInstance((new GenericType<Ref<SecurityContext>>() { }).getType())
      .set(securityContext);
  if (featureConfig().shouldUsePrematchingAuthentication()) {
    doFilter(request, securityContext);
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
  final Scope scope = new ExternalHttpRequest(tracingPlugin.getTracer(), request.getMethod().toString(), removeQuery(request.getURI()), request.getURI().getHost(), request.getURI().getPort()).createScope();
  try {
    Profiler.start(request.getMethod().toString() + " " + request.getURI() + " ");
    tracingPlugin.getTracer().inject(scope.span().context(), Format.Builtin.HTTP_HEADERS, new SpringHttpRequestInjectAdapter(request));
    return execution.execute(request, body);
  } finally {
    Profiler.stop();
    scope.close();
  }
}

代码示例来源:origin: oracle/helidon

@Override
public void filter(ContainerRequestContext requestContext) {
  if (!tracingEnabled(requestContext)) {
    return;
  }
  ServerRequest serverRequest = this.request.get();
  Tracer tracer = serverRequest.webServer().configuration().tracer();
  SpanContext parentSpan = TracingContext.get()
      .map(TracingContext::parentSpan)
      .orElseGet(serverRequest::spanContext);
  Tracer.SpanBuilder spanBuilder = tracer
      .buildSpan(spanName(requestContext))
      .asChildOf(parentSpan)
      .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
      .withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod())
      .withTag(Tags.HTTP_URL.getKey(), requestContext.getUriInfo().getRequestUri().toString())
      .withTag(Tags.COMPONENT.getKey(), "jaxrs");
  configureSpan(spanBuilder);
  Span span = spanBuilder.start();
  requestContext.setProperty(SPAN_PROPERTY, span);
  // set the client tracing context
  TracingContext.compute(() -> TracingContext.create(tracer, requestContext.getHeaders()))
      .parentSpan(span.context());
}

代码示例来源:origin: oracle/helidon

protected void authenticate(SecurityFilter.FilterContext context, Span securitySpan, SecurityContext securityContext) {
  Span atnSpan = startNewSpan(securitySpan.context(), "security:atn");
  try {
    SecurityDefinition methodSecurity = context.getMethodSecurity();
    if (methodSecurity.requiresAuthentication()) {
      //authenticate request
      SecurityClientBuilder<AuthenticationResponse> clientBuilder = securityContext
          .atnClientBuilder()
          .optional(methodSecurity.authenticationOptional())
          .requestMessage(toRequestMessage(context))
          .responseMessage(context.getResponseMessage())
          .tracingSpan(atnSpan);
      clientBuilder.explicitProvider(methodSecurity.getAuthenticator());
      processAuthentication(context, clientBuilder, methodSecurity);
    }
  } finally {
    if (context.isTraceSuccess()) {
      List<String> logs = new LinkedList<>();
      securityContext.user()
          .ifPresent(user -> logs.add("security.user: " + user.principal().getName()));
      securityContext.service()
          .ifPresent(service -> logs.add("security.service: " + service.principal().getName()));
      finishSpan(atnSpan, logs);
    } else {
      HttpUtil.traceError(atnSpan, context.getTraceThrowable(), context.getTraceDescription());
    }
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Override
protected void handleOutboundSOAPMessage(SOAPMessageContext context) {
  final Tracer.SpanBuilder spanBuilder = tracingPlugin.getTracer()
      .buildSpan(getOperationName(context))
      .withTag(SpanUtils.OPERATION_TYPE, "soap")
      .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
  if (soapTracingPlugin.isSoapClientRecordRequestMessages()) {
    spanBuilder.withTag("soap.request", getSoapMessageAsString(context));
  }
  final Span span = spanBuilder.startActive(true).span();
  tracingPlugin.getTracer().inject(span.context(), Format.Builtin.HTTP_HEADERS, new SOAPMessageInjectAdapter(context));
}

代码示例来源:origin: oracle/helidon

protected void authorize(SecurityFilter.FilterContext context, Span securitySpan, SecurityContext securityContext) {
  if (context.getMethodSecurity().isAtzExplicit()) {
    // authorization is explicitly done by user, we MUST skip it here
    context.setExplicitAtz(true);
    return;
  }
  Span atzSpan = startNewSpan(securitySpan.context(), "security:atz");
  try {
    //now authorize (also authorize anonymous requests, as we may have a path-based authorization that allows public
    // access
    if (context.getMethodSecurity().requiresAuthorization()) {
      SecurityClientBuilder<AuthorizationResponse> clientBuilder = securityContext.atzClientBuilder()
          .tracingSpan(atzSpan)
          .explicitProvider(context.getMethodSecurity().getAuthorizer());
      processAuthorization(context, clientBuilder);
    }
  } finally {
    if (!context.isTraceSuccess()) {
      HttpUtil.traceError(atzSpan, context.getTraceThrowable(), context.getTraceDescription());
    } else {
      finishSpan(atzSpan, CollectionsHelper.listOf());
    }
  }
}

相关文章