io.opentracing.Tracer.inject()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(152)

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

Tracer.inject介绍

[英]Inject a SpanContext into a carrier of a given type, presumably for propagation across process boundaries.

Example:

Tracer tracer = ... 
Span clientSpan = ... 
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest); 
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, httpHeadersCarrier);

[中]将SpanContext注入给定类型的“载体”,可能是为了跨流程边界传播。
例子:

Tracer tracer = ... 
Span clientSpan = ... 
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest); 
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, httpHeadersCarrier);

代码示例

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

@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
  delegate.inject(spanContext, format, carrier);
}

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

@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
  tracer.inject(spanContext, format, carrier);
}

代码示例来源: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: 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: 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: 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: stagemonitor/stagemonitor

@Override
public void onStart(SpanWrapper spanWrapper) {
  if (corePlugin.isStagemonitorActive()) {
    final MeasurementSession measurementSession = corePlugin.getMeasurementSession();
    if (measurementSession != null) {
      addToMdcIfNotNull("application", measurementSession.getApplicationName());
      addToMdcIfNotNull("host", measurementSession.getHostName());
      addToMdcIfNotNull("instance", measurementSession.getInstanceName());
    }
    // don't store the context in MDC if stagemonitor is not active
    // so that thread pools that get created on startup don't inherit the ids
    if (Stagemonitor.isStarted()) {
      tracingPlugin.getTracer().inject(spanWrapper.context(), B3HeaderFormat.INSTANCE, new B3HeaderFormat.B3InjectAdapter() {
        @Override
        public void setParentId(String value) {
        }
        @Override
        public void setSpanId(String value) {
          addToMdcIfNotNull("spanId", value);
        }
        @Override
        public void setTraceId(String value) {
          addToMdcIfNotNull("traceId", value);
        }
      });
    }
  }
}

代码示例来源:origin: io.opentracing.contrib/opentracing-api-extensions-tracer

@Override
public <C> void inject(SpanContext context, Format<C> format, C carrier) {
  if (wrappedTracer != null) {
    wrappedTracer.inject(context, format, carrier);
  }
}

代码示例来源:origin: yangfuhai/jboot

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

代码示例来源:origin: yangfuhai/jboot

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<Map.Entry<String, String>> iterator() {
      throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()");
    }
  });
}

代码示例来源:origin: sixt/ja-micro

span.setTag("correlation_id", orangeContext.getCorrelationId());
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(request.getHeaders()));

代码示例来源:origin: com.uber.jaeger/jaeger-jaxrs2

@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
 Span clientSpan = (Span) clientRequestContext.getProperty(Constants.CURRENT_SPAN_CONTEXT_KEY);
 tracer.inject(
   clientSpan.context(),
   Format.Builtin.HTTP_HEADERS,
   new ClientRequestCarrier(clientRequestContext));
}

代码示例来源:origin: apache/cxf

@Test
public void testThatNewInnerSpanIsCreated() throws MalformedURLException {
  final JaegerSpanContext spanId = fromRandom();
  final Map<String, List<String>> headers = new HashMap<>();
  tracer.inject(spanId, Builtin.HTTP_HEADERS, new TextMapInjectAdapter(headers));
  final BookStoreService service = createJaxWsService(headers);
  assertThat(service.getBooks().size(), equalTo(2));
  assertThat(TestSender.getAllSpans().size(), equalTo(2));
  assertThat(TestSender.getAllSpans().get(0).getOperationName(), equalTo("Get Books"));
  assertThat(TestSender.getAllSpans().get(1).getOperationName(), equalTo("POST /BookStore"));
}

相关文章