use of io.opentracing.SpanContext in project hono by eclipse.
the class AbstractRequestResponseEndpointTest method getEndpoint.
private AbstractRequestResponseEndpoint<ServiceConfigProperties> getEndpoint() {
final AbstractRequestResponseEndpoint<ServiceConfigProperties> endpoint = new AbstractRequestResponseEndpoint<>(vertx) {
@Override
public String getName() {
return "test";
}
@Override
protected boolean passesFormalVerification(final ResourceIdentifier targetAddress, final Message message) {
return formalMessageVerification.apply(targetAddress, message);
}
@Override
protected Future<Message> handleRequestMessage(final Message requestMessage, final ResourceIdentifier targetAddress, final SpanContext spanContext) {
return requestMessageHandler.apply(requestMessage, targetAddress);
}
};
endpoint.setConfiguration(new ServiceConfigProperties());
endpoint.setAuthorizationService(authService);
return endpoint;
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class SQL method runTransactionally.
/**
* Run operation transactionally.
* <p>
* This function will perform the following operations:
* <ul>
* <li>Open a new connection</li>
* <li>Turn off auto-commit mode</li>
* <li>Call the provided function</li>
* <li>If the provided function failed, perform a <em>Rollback</em> operation</li>
* <li>If the provided function succeeded, perform a <em>Commit</em> operation</li>
* <li>Close the connection</li>
* </ul>
*
* @param client The client to use.
* @param tracer The tracer to use.
* @param function The function to execute while the transaction is open.
* @param context The span to log to.
* @param <T> The type of the result.
* @return A future, tracking the outcome of the operation.
*/
public static <T> Future<T> runTransactionally(final SQLClient client, final Tracer tracer, final SpanContext context, final BiFunction<SQLConnection, SpanContext, Future<T>> function) {
final Span span = startSqlSpan(tracer, context, "run transactionally", builder -> {
});
final Promise<SQLConnection> promise = Promise.promise();
client.getConnection(promise);
return promise.future().onSuccess(x -> {
final Map<String, Object> log = new HashMap<>();
log.put(Fields.EVENT, "success");
log.put(Fields.MESSAGE, "connection opened");
span.log(log);
}).flatMap(connection -> SQL.setAutoCommit(tracer, span.context(), connection, false).flatMap(y -> function.apply(connection, span.context()).compose(v -> SQL.commit(tracer, span.context(), connection).map(v), x -> SQL.rollback(tracer, span.context(), connection).flatMap(unused -> Future.failedFuture(x)))).onComplete(x -> connection.close())).onComplete(x -> span.finish());
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class TracingHandler method serverSpanContext.
/**
* Helper method for accessing server span context associated with current request.
*
* @param routingContext routing context
* @return server span context or null if not present
*/
public static SpanContext serverSpanContext(final RoutingContext routingContext) {
SpanContext serverContext = null;
final Object object = routingContext.get(CURRENT_SPAN);
if (object instanceof Span) {
final Span span = (Span) object;
serverContext = span.context();
} else {
log.warn("Server SpanContext is null or not an instance of SpanContext");
}
return serverContext;
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class DelegatingCommandRouterAmqpEndpoint method processEnableCommandRouting.
/**
* Processes an <em>enable command request</em> request message.
*
* @param request The request message.
* @param targetAddress The address the message is sent to.
* @param spanContext The span context representing the request to be processed.
* @return The response to send to the client via the event bus.
*/
protected Future<Message> processEnableCommandRouting(final Message request, final ResourceIdentifier targetAddress, final SpanContext spanContext) {
final Span span = TracingHelper.buildServerChildSpan(tracer, spanContext, SPAN_NAME_ENABLE_COMMAND_ROUTING, getClass().getSimpleName()).start();
final Future<Message> response = parseTenantIdentifiers(request).compose(tenantIds -> {
span.log(Map.of("no_of_tenants", tenantIds.size()));
return getService().enableCommandRouting(tenantIds, span);
}).map(result -> CommandRouterConstants.getAmqpReply(targetAddress.getEndpoint(), null, request, result));
return finishSpanOnFutureCompletion(span, response);
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class DelegatingCommandRouterAmqpEndpoint method processUnregisterCommandConsumer.
/**
* Processes a <em>unregister command consumer</em> request message.
*
* @param request The request message.
* @param targetAddress The address the message is sent to.
* @param spanContext The span context representing the request to be processed.
* @return The response to send to the client via the event bus.
*/
protected Future<Message> processUnregisterCommandConsumer(final Message request, final ResourceIdentifier targetAddress, final SpanContext spanContext) {
final String tenantId = targetAddress.getTenantId();
final String deviceId = MessageHelper.getDeviceId(request);
final String adapterInstanceId = MessageHelper.getApplicationProperty(request.getApplicationProperties(), MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, String.class);
final Span span = TracingHelper.buildServerChildSpan(tracer, spanContext, SPAN_NAME_UNREGISTER_COMMAND_CONSUMER, getClass().getSimpleName()).start();
final Future<Message> resultFuture;
if (tenantId == null || deviceId == null || adapterInstanceId == null) {
TracingHelper.logError(span, "missing tenant, device and/or adapter instance id");
resultFuture = Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
} else {
TracingHelper.TAG_TENANT_ID.set(span, tenantId);
TracingHelper.TAG_DEVICE_ID.set(span, deviceId);
span.setTag(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
logger.debug("unregister command consumer [tenant-id: {}, device-id: {}, adapter-instance-id {}]", tenantId, deviceId, adapterInstanceId);
resultFuture = getService().unregisterCommandConsumer(tenantId, deviceId, adapterInstanceId, span).map(res -> CommandRouterConstants.getAmqpReply(CommandRouterConstants.COMMAND_ROUTER_ENDPOINT, tenantId, request, res));
}
return finishSpanOnFutureCompletion(span, resultFuture);
}
Aggregations