本文整理汇总了Java中io.opentracing.SpanContext类的典型用法代码示例。如果您正苦于以下问题:Java SpanContext类的具体用法?Java SpanContext怎么用?Java SpanContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpanContext类属于io.opentracing包,在下文中一共展示了SpanContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildAndFinishChildSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
private void buildAndFinishChildSpan(ConsumerRecord<K, V> record) {
SpanContext parentContext = TracingKafkaUtils.extract(record.headers(), tracer);
if (parentContext != null) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("receive")
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
spanBuilder.addReference(References.FOLLOWS_FROM, parentContext);
Span span = spanBuilder.start();
SpanDecorator.onResponse(record, span);
span.finish();
// Inject created span context into record headers for extraction by client to continue span chain
TracingKafkaUtils.injectSecond(span.context(), record.headers(), tracer);
}
}
示例2: buildAndInjectSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
private Scope buildAndInjectSpan(ProducerRecord<K, V> record) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("send")
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
SpanContext spanContext = TracingKafkaUtils.extract(record.headers(), tracer);
if (spanContext != null) {
spanBuilder.asChildOf(spanContext);
}
Scope scope = spanBuilder.startActive(false);
SpanDecorator.onSend(record, scope.span());
try {
TracingKafkaUtils.inject(scope.span().context(), record.headers(), tracer);
} catch (Exception e) {
// it can happen if headers are read only (when record is sent second time)
logger.error("failed to inject span context. sending record second time?", e);
}
return scope;
}
示例3: buildChildSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
static Scope buildChildSpan(AMQP.BasicProperties props, Tracer tracer) {
SpanContext context = TracingUtils.extract(props, tracer);
if (context != null) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("receive")
.ignoreActiveSpan()
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CONSUMER);
spanBuilder.addReference(References.FOLLOWS_FROM, context);
Scope scope = spanBuilder.startActive(true);
SpanDecorator.onResponse(scope.span());
return scope;
}
return null;
}
示例4: getSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
protected Span getSpan(String methodName, Map<String, String> headers, OrangeContext context) {
Span span = null;
if (tracer != null) {
SpanContext spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
if (spanContext != null) {
span = tracer.buildSpan(methodName).asChildOf(spanContext).start();
} else {
span = tracer.buildSpan(methodName).start();
}
span.setTag("correlation_id", context.getCorrelationId());
Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_SERVER);
Tags.PEER_SERVICE.set(span, context.getRpcOriginService());
context.setTracingContext(span.context());
}
return span;
}
示例5: buildAndInjectSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
/**
* Build span and inject. Should be used by producers.
*
* @param message JMS message
* @return span
*/
public static Span buildAndInjectSpan(Destination destination, final Message message,
Tracer tracer)
throws JMSException {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(TracingMessageUtils.OPERATION_NAME_SEND)
.ignoreActiveSpan()
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER);
SpanContext parent = TracingMessageUtils.extract(message, tracer);
if (parent != null) {
spanBuilder.asChildOf(parent);
}
Span span = spanBuilder.startManual();
SpanJmsDecorator.onRequest(destination, span);
TracingMessageUtils.inject(span, message, tracer);
return span;
}
开发者ID:Estafet-LTD,项目名称:estafet-microservices-scrum-api-project-burndown,代码行数:27,代码来源:PropagatingTracingMessageUtils.java
示例6: buildAndInjectSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
/**
* Build span and inject. Should be used by producers.
*
* @param message JMS message
* @return span
*/
public static Span buildAndInjectSpan(Destination destination, final Message message,
Tracer tracer) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(TracingMessageUtils.OPERATION_NAME_SEND)
.ignoreActiveSpan()
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER);
SpanContext parent = TracingMessageUtils.extract(message, tracer);
if (parent != null) {
spanBuilder.asChildOf(parent);
}
Span span = spanBuilder.start();
SpanJmsDecorator.onRequest(destination, span);
TracingMessageUtils.inject(span, message, tracer);
return span;
}
示例7: extract
import io.opentracing.SpanContext; //导入依赖的package包/类
/**
* Extract context from headers or from active Span
*
* @param request http request
* @return extracted context
*/
private SpanContext extract(HttpRequest request) {
SpanContext spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS,
new HttpTextMapExtractAdapter(request));
if (spanContext != null) {
return spanContext;
}
Span span = tracer.activeSpan();
if (span != null) {
return span.context();
}
return null;
}
开发者ID:opentracing-contrib,项目名称:java-elasticsearch-client,代码行数:22,代码来源:TracingHttpClientConfigCallback.java
示例8: startServerSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
public static Scope startServerSpan(Tracer tracer, javax.ws.rs.core.HttpHeaders httpHeaders,
String operationName) {
// format the headers for extraction
MultivaluedMap<String, String> rawHeaders = httpHeaders.getRequestHeaders();
final HashMap<String, String> headers = new HashMap<String, String>();
for (String key : rawHeaders.keySet()) {
headers.put(key, rawHeaders.get(key).get(0));
}
Tracer.SpanBuilder spanBuilder;
try {
SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
if (parentSpanCtx == null) {
spanBuilder = tracer.buildSpan(operationName);
} else {
spanBuilder = tracer.buildSpan(operationName).asChildOf(parentSpanCtx);
}
} catch (IllegalArgumentException e) {
spanBuilder = tracer.buildSpan(operationName);
}
// TODO could add more tags like http.url
return spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).startActive(true);
}
示例9: extract
import io.opentracing.SpanContext; //导入依赖的package包/类
public SpanContext extract(com.google.cloud.trace.Tracer tracer, TextMap textMap) {
String traceId = null;
String spanId = null;
Iterator<Map.Entry<String, String>> iter = textMap.iterator();
while(iter.hasNext()) {
Map.Entry<String, String> currEntry = iter.next();
if (currEntry.getKey().compareToIgnoreCase(FIELD_TRACE_ID) == 0) {
traceId = currEntry.getValue();
} else if (currEntry.getKey().compareToIgnoreCase(FIELD_SPAN_ID) == 0) {
spanId = currEntry.getValue();
}
}
return buildSpanContext(tracer, traceId, spanId);
}
示例10: testLocalSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
@Test
public void testLocalSpan() throws Exception {
{
router.route("/localSpan").handler(routingContext -> {
SpanContext serverSpanContext = TracingHandler.serverSpanContext(routingContext);
io.opentracing.Tracer.SpanBuilder spanBuilder = mockTracer.buildSpan("localSpan");
spanBuilder.asChildOf(serverSpanContext)
.startManual()
.finish();
routingContext.response()
.setStatusCode(202)
.end();
});
request("/localSpan", HttpMethod.GET, 202);
Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(2));
}
List<MockSpan> mockSpans = mockTracer.finishedSpans();
Assert.assertEquals(2, mockSpans.size());
Assert.assertEquals(mockSpans.get(0).parentId(), mockSpans.get(1).context().spanId());
Assert.assertEquals(mockSpans.get(0).context().traceId(), mockSpans.get(1).context().traceId());
}
示例11: extract
import io.opentracing.SpanContext; //导入依赖的package包/类
@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
if (format.equals(Format.Builtin.TEXT_MAP) || format.equals(Format.Builtin.HTTP_HEADERS)) {
if (!(carrier instanceof TextMap)) {
throw new IllegalArgumentException("Unsupported payload: " + carrier);
}
return new TextMapContext((TextMap) carrier);
} else if (format.equals(Format.Builtin.BINARY)) {
if (!(carrier instanceof ByteBuffer)) {
throw new IllegalArgumentException("Unsupported payload: " + carrier);
}
return new ByteBufferContext((ByteBuffer) carrier);
} else {
throw new IllegalArgumentException("Unsupported format: " + format);
}
}
示例12: testByteBufferExtraction
import io.opentracing.SpanContext; //导入依赖的package包/类
@Test
public void testByteBufferExtraction() throws Exception {
byte[] key = "foo".getBytes(ByteBufferContext.CHARSET), value = "quxbaz".getBytes(ByteBufferContext.CHARSET);
ByteBuffer byteBuffer = ByteBuffer.allocate(2 + 2 * 4 + key.length + value.length);
byteBuffer.put(ByteBufferContext.ENTRY);
byteBuffer.putInt(key.length);
byteBuffer.putInt(value.length);
byteBuffer.put(key);
byteBuffer.put(value);
byteBuffer.put(ByteBufferContext.NO_ENTRY);
byteBuffer.flip();
SpanContext spanContext = tracer.extract(Format.Builtin.BINARY, byteBuffer);
Iterator<Map.Entry<String, String>> iterator = spanContext.baggageItems().iterator();
assertThat(iterator.hasNext(), is(true));
Map.Entry<String, String> entry = iterator.next();
assertThat(entry.getKey(), is("foo"));
assertThat(entry.getValue(), is("quxbaz"));
assertThat(iterator.hasNext(), is(false));
}
示例13: getSpanFromHeaders
import io.opentracing.SpanContext; //导入依赖的package包/类
/**
* Extract OpenTracing context from head, and create span.
* @param headers HTTP headers.
* @param operationName span's operation name.
* @return
*/
private Span getSpanFromHeaders(Map<String, String> headers, String operationName) {
Span span;
try {
SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS,
new TextMapExtractAdapter(headers));
if (parentSpanCtx == null) {
span = tracer.buildSpan(operationName).start();
} else {
span = tracer.buildSpan(operationName).asChildOf(parentSpanCtx).start();
}
} catch (IllegalArgumentException iae){
span = tracer.buildSpan(operationName)
.withTag("Error", "Extract failed and an IllegalArgumentException was thrown")
.start();
}
return span;
}
示例14: createSpan
import io.opentracing.SpanContext; //导入依赖的package包/类
private Span createSpan(
Instant start, String service, String procedure, String caller, HttpHeaders httpHeaders) {
SpanBuilder spanBuilder =
tracer
.buildSpan(procedure)
.withStartTimestamp(Instant.EPOCH.until(start, ChronoUnit.MICROS))
.withTag(SpanTags.CALLER, caller)
.withTag(SpanTags.SERVICE, service)
.withTag(SpanTags.ENCODING, httpHeaders.get(HeaderMapper.ENCODING))
.withTag(SpanTags.TRANSPORT, "http");
SpanContext parent =
tracer.extract(Builtin.HTTP_HEADERS, new TextMapExtractAdapter(httpHeaders));
if (parent != null) {
spanBuilder.asChildOf(parent);
}
return spanBuilder.start();
}
示例15: handle
import io.opentracing.SpanContext; //导入依赖的package包/类
@Override
public void handle(RoutingContext routingContext) {
logger.finest("Handling request on PlaceOrderHandler");
routingContext.request().bodyHandler(buf -> {
SpanContext spanCtx = tracer.extract(Format.Builtin.TEXT_MAP, new HttpHeadersExtractAdapter(routingContext.request().headers()));
Span ordersConsumerSpan = tracer.buildSpan("POST")
.asChildOf(spanCtx)
.withTag("http.url", "/orders")
.withTag("transaction", "Place Order")
.start();
JsonObject order = buf.toJsonObject();
HttpServerResponse response = routingContext.response();
if (orders.containsKey(order.getString("id"))) {
logger.warning("Order ID must not be defined");
sendError(400, "Order id must not be defined", response, ordersConsumerSpan);
} else {
logger.finest("Checking Account");
checkAccount(routingContext, order, response, ordersConsumerSpan);
logger.finest("Checked Account");
}
});
}