use of io.opentracing.SpanContext in project hono by eclipse.
the class ProtonBasedCommandRouterClient method enableCommandRouting.
@Override
public Future<Void> enableCommandRouting(final List<String> tenantIds, final SpanContext context) {
Objects.requireNonNull(tenantIds);
if (tenantIds.isEmpty()) {
return Future.succeededFuture();
}
final Span currentSpan = newChildSpan(context, "enable command routing");
currentSpan.log(Map.of("no_of_tenants", tenantIds.size()));
final JsonArray payload = new JsonArray(tenantIds);
final Future<RequestResponseResult<JsonObject>> resultTracker = getOrCreateClient(tenantIds.get(0)).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.ENABLE_COMMAND_ROUTING.getSubject(), null, payload.toBuffer(), MessageHelper.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, currentSpan));
return mapResultAndFinishSpan(resultTracker, result -> {
switch(result.getStatus()) {
case HttpURLConnection.HTTP_NO_CONTENT:
log.info("successfully enabled routing of commands for {} tenants in Command Router", tenantIds.size());
return null;
default:
final ServiceInvocationException e = StatusCodeMapper.from(result);
log.info("failed to enable routing of commands in Command Router", e);
throw e;
}
}, currentSpan).mapEmpty();
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class TelemetrySenderTest method testSendAndWaitForOutcomeWithTracing.
/**
* Verifies that {@link TraceableTelemetrySender#sendAndWaitForOutcome(String, byte[], String, java.util.Map, SpanContext)}
* uses the given SpanContext.
*
* @param ctx The test context to use for running asynchronous tests.
*/
@Test
public void testSendAndWaitForOutcomeWithTracing(final VertxTestContext ctx) {
// GIVEN a TraceableTelemetrySender instance
final TraceableTelemetrySender telemetrySender = ((TraceableTelemetrySender) createTelemetrySender());
// WHEN sending a message using the API...
final SpanContext spanContext = mock(SpanContext.class);
final Future<ProtonDelivery> deliveryFuture = telemetrySender.sendAndWaitForOutcome(DEVICE_ID, PAYLOAD, CONTENT_TYPE, APPLICATION_PROPERTIES, spanContext);
// ...AND WHEN the disposition is updated by the peer
updateDisposition();
deliveryFuture.onComplete(ctx.succeeding(delivery -> {
// THEN the given SpanContext is used
ctx.verify(() -> {
verify(spanBuilder).addReference(any(), eq(spanContext));
assertMessageConformsAmqpAdapterSpec(ADDRESS);
});
ctx.completeNow();
}));
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class CommandRouterCommandConsumerFactory method doCreateCommandConsumer.
private Future<CommandConsumer> doCreateCommandConsumer(final String tenantId, final String deviceId, final String gatewayId, final Handler<CommandContext> commandHandler, final Duration lifespan, final SpanContext context) {
// lifespan greater than what can be expressed in nanoseconds (i.e. 292 years) is considered unlimited, preventing ArithmeticExceptions down the road
final Duration sanitizedLifespan = lifespan == null || lifespan.isNegative() || lifespan.getSeconds() > (Long.MAX_VALUE / 1000_000_000L) ? Duration.ofSeconds(-1) : lifespan;
LOG.trace("create command consumer [tenant-id: {}, device-id: {}, gateway-id: {}]", tenantId, deviceId, gatewayId);
// register the command handler
// for short-lived command consumers, let the consumer creation span context be used as reference in the command span
final SpanContext consumerCreationContextToUse = !sanitizedLifespan.isNegative() && sanitizedLifespan.toSeconds() <= TenantConstants.DEFAULT_MAX_TTD ? context : null;
final CommandHandlerWrapper commandHandlerWrapper = new CommandHandlerWrapper(tenantId, deviceId, gatewayId, commandHandler, Vertx.currentContext(), consumerCreationContextToUse);
commandHandlers.putCommandHandler(commandHandlerWrapper);
final Instant lifespanStart = Instant.now();
return commandRouterClient.registerCommandConsumer(tenantId, deviceId, adapterInstanceId, sanitizedLifespan, context).onFailure(thr -> {
LOG.info("error registering consumer with the command router service [tenant: {}, device: {}]", tenantId, deviceId, thr);
// handler association failed - unregister the handler
commandHandlers.removeCommandHandler(tenantId, deviceId);
}).map(v -> {
return new CommandConsumer() {
@Override
public Future<Void> close(final SpanContext spanContext) {
return removeCommandConsumer(commandHandlerWrapper, sanitizedLifespan, lifespanStart, spanContext);
}
};
});
}
use of io.opentracing.SpanContext in project cxf by apache.
the class AbstractOpenTracingProvider method startTraceSpan.
protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders, URI uri, String method) {
SpanContext parent = tracer.extract(Builtin.HTTP_HEADERS, new TextMapAdapter(requestHeaders.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::getFirstValueOrEmpty))));
final Span activeSpan;
final Scope scope;
if (parent == null) {
activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).start();
scope = tracer.scopeManager().activate(activeSpan);
} else {
activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).asChildOf(parent).start();
scope = tracer.scopeManager().activate(activeSpan);
}
// Set additional tags
activeSpan.setTag(Tags.HTTP_METHOD.getKey(), method);
activeSpan.setTag(Tags.HTTP_URL.getKey(), uri.toString());
// If the service resource is using asynchronous processing mode, the trace
// scope will be closed in another thread and as such should be detached.
Span span = null;
if (isAsyncResponse()) {
// Do not modify the current context span
span = activeSpan;
propagateContinuationSpan(span);
scope.close();
}
return new TraceScopeHolder<TraceScope>(new TraceScope(activeSpan, scope), span != null);
}
use of io.opentracing.SpanContext in project jaeger-client-java by jaegertracing.
the class FilterIntegrationTest method testExtractorReturnsNullWhenTracerStateHeaderIsMissing.
/*
* This test exists because opentracing's convention around missing tracer
* state headers may change to stop supporting the automatic creation of
* building a span.
*/
@Test
public void testExtractorReturnsNullWhenTracerStateHeaderIsMissing() {
ContainerRequestContext reqContext = mock(ContainerRequestContext.class);
given(reqContext.getHeaders()).willReturn(new MultivaluedHashMap<String, String>());
ServerRequestCarrier carrier = new ServerRequestCarrier(reqContext);
SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, carrier);
assertNull(spanCtx);
}
Aggregations