Search in sources :

Example 21 with SpanContext

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;
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) SpanContext(io.opentracing.SpanContext) Message(org.apache.qpid.proton.message.Message) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties)

Example 22 with SpanContext

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());
}
Also used : Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) BiFunction(java.util.function.BiFunction) UrlEscapers(com.google.common.net.UrlEscapers) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) HashMap(java.util.HashMap) SpanBuilder(io.opentracing.Tracer.SpanBuilder) Tags(io.opentracing.tag.Tags) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) List(java.util.List) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) Fields(io.opentracing.log.Fields) URI(java.net.URI) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLClient(io.vertx.ext.sql.SQLClient) HashMap(java.util.HashMap) SQLConnection(io.vertx.ext.sql.SQLConnection) Span(io.opentracing.Span)

Example 23 with SpanContext

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;
}
Also used : SpanContext(io.opentracing.SpanContext) Span(io.opentracing.Span)

Example 24 with SpanContext

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) AbstractDelegatingRequestResponseEndpoint(org.eclipse.hono.service.amqp.AbstractDelegatingRequestResponseEndpoint) MessageHelper(org.eclipse.hono.util.MessageHelper) Collectors(java.util.stream.Collectors) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Duration(java.time.Duration) Map(java.util.Map) Span(io.opentracing.Span) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Message(org.apache.qpid.proton.message.Message) Span(io.opentracing.Span)

Example 25 with SpanContext

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) AbstractDelegatingRequestResponseEndpoint(org.eclipse.hono.service.amqp.AbstractDelegatingRequestResponseEndpoint) MessageHelper(org.eclipse.hono.util.MessageHelper) Collectors(java.util.stream.Collectors) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Duration(java.time.Duration) Map(java.util.Map) Span(io.opentracing.Span) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Message(org.apache.qpid.proton.message.Message) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Span(io.opentracing.Span)

Aggregations

SpanContext (io.opentracing.SpanContext)118 Span (io.opentracing.Span)81 Future (io.vertx.core.Future)70 Tracer (io.opentracing.Tracer)60 Objects (java.util.Objects)57 HttpURLConnection (java.net.HttpURLConnection)56 JsonObject (io.vertx.core.json.JsonObject)55 TracingHelper (org.eclipse.hono.tracing.TracingHelper)55 Logger (org.slf4j.Logger)54 LoggerFactory (org.slf4j.LoggerFactory)54 List (java.util.List)51 Promise (io.vertx.core.Promise)45 Optional (java.util.Optional)45 Map (java.util.Map)40 ClientErrorException (org.eclipse.hono.client.ClientErrorException)39 MessageHelper (org.eclipse.hono.util.MessageHelper)33 UUID (java.util.UUID)31 Collectors (java.util.stream.Collectors)31 HashMap (java.util.HashMap)25 Vertx (io.vertx.core.Vertx)24