/** * Marks an <em>OpenTracing</em> span as erroneous and logs an exception. * <p> * This method does <em>not</em> finish the span. * * @param span The span to mark. * @param error The exception that has occurred. * @throws NullPointerException if error is {@code null}. */ public static void logError(final Span span, final Throwable error) { if (span != null) { logError(span, getErrorLogItems(error)); } }
@Override public void onFailure(final Throwable throwable, final HttpServerResponse response, final Span span) { LOG.trace("logging failed processing of request"); TracingHelper.logError(span, throwable); } }
/** * Extracts a {@code SpanContext} out of the delivery annotations of the given {@code Message}. * * @param message The AMQP message. * @return The extracted {@code SpanContext} (may be {@code null}). * @throws NullPointerException if the message is {@code null}. */ protected final SpanContext extractSpanContext(final Message message) { return tracer.extract(Format.Builtin.TEXT_MAP, new MessageAnnotationsExtractAdapter(message)); } }
@Override public void put(final String key, final String value) { getDeliveryAnnotations().getValue().put(Symbol.getSymbol(key), value); }
@Override public Iterator<Entry<String, String>> iterator() { final Iterator<Entry<Symbol, Object>> entries = getDeliveryAnnotations().getValue().entrySet().iterator(); return new Iterator<Map.Entry<String, String>>() { @Override public boolean hasNext() { return entries.hasNext(); } @Override public Entry<String, String> next() { final Entry<Symbol, Object> nextEntry = entries.next(); return new AbstractMap.SimpleEntry<String, String>(nextEntry.getKey().toString(), nextEntry.getValue().toString()); } }; }
/** * Injects a {@code SpanContext} into a JSON object. * <p> * The span context will be injected into a new JSON object under key <em>span-context</em>. * * @param tracer The Tracer to use for injecting the context. * @param spanContext The context to inject. * @param jsonObject The JSON object to inject the context into. * @throws NullPointerException if any of the parameters are {@code null}. */ public static void injectSpanContext(final Tracer tracer, final SpanContext spanContext, final JsonObject jsonObject) { Objects.requireNonNull(tracer); Objects.requireNonNull(spanContext); Objects.requireNonNull(jsonObject); final JsonObject spanContextJson = new JsonObject(); jsonObject.put(JSON_KEY_SPAN_CONTEXT, spanContextJson); tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new JsonObjectInjectAdapter(spanContextJson)); }
@Override public void parseCredentials(final RoutingContext context, final Handler<AsyncResult<JsonObject>> handler) { super.parseCredentials(context, ar -> { if (ar.succeeded()) { final SpanContext spanContext = TracingHandler.serverSpanContext(context); if (spanContext != null && !(spanContext instanceof NoopSpanContext)) { TracingHelper.injectSpanContext(tracer, spanContext, ar.result()); } } handler.handle(ar); }); } }
/** * Extracts a {@code SpanContext} from a JSON object. * <p> * The span context will be read from a JSON object under key <em>span-context</em>. * * @param tracer The Tracer to use for extracting the context. * @param jsonObject The JSON object to extract the context from. * @return The context or {@code null} if the given JSON object does not contain a context. * @throws NullPointerException if any of the parameters are {@code null}. */ public static SpanContext extractSpanContext(final Tracer tracer, final JsonObject jsonObject) { Objects.requireNonNull(tracer); Objects.requireNonNull(jsonObject); final Object spanContextContainer = jsonObject.getValue(JSON_KEY_SPAN_CONTEXT); return spanContextContainer instanceof JsonObject ? tracer.extract(Format.Builtin.TEXT_MAP, new JsonObjectExtractAdapter((JsonObject) spanContextContainer)) : null; } }
@Override public final void authenticate(final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) { final T credentials = getCredentials(Objects.requireNonNull(authInfo)); if (credentials == null) { resultHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "malformed credentials"))); } else { authenticate(credentials, TracingHelper.extractSpanContext(tracer, authInfo), s -> { if (s.succeeded()) { resultHandler.handle(Future.succeededFuture(s.result())); } else { resultHandler.handle(Future.failedFuture(s.cause())); } }); } }
/** * Creates {@code DeliveryOptions} that contain the given {@code SpanContext}. * <p> * To be used when sending a message on the vert.x event bus. * * @param spanContext The {@code SpanContext} (may be {@code null}). * @return The {@code DeliveryOptions}. */ protected final DeliveryOptions createEventBusMessageDeliveryOptions(final SpanContext spanContext) { final DeliveryOptions deliveryOptions = new DeliveryOptions(); if (spanContext != null && !(spanContext instanceof NoopSpanContext)) { final MultiMap multiMap = new CaseInsensitiveHeaders(); tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new MultiMapInjectAdapter(multiMap)); deliveryOptions.setHeaders(multiMap); } return deliveryOptions; }
@Override public void onFailure(final Throwable throwable, final HttpServerResponse response, final Span span) { LOG.trace("logging failed processing of request"); TracingHelper.logError(span, throwable); } }
/** * Marks an <em>OpenTracing</em> span as erroneous and logs an exception. * <p> * This method does <em>not</em> finish the span. * * @param span The span to mark. * @param error The exception that has occurred. * @throws NullPointerException if error is {@code null}. */ public static void logError(final Span span, final Throwable error) { if (span != null) { logError(span, getErrorLogItems(error)); } }
@Override public void put(final String key, final String value) { getDeliveryAnnotations().getValue().put(Symbol.getSymbol(key), value); }
@Override public Iterator<Entry<String, String>> iterator() { final Iterator<Entry<Symbol, Object>> entries = getDeliveryAnnotations().getValue().entrySet().iterator(); return new Iterator<Map.Entry<String, String>>() { @Override public boolean hasNext() { return entries.hasNext(); } @Override public Entry<String, String> next() { final Entry<Symbol, Object> nextEntry = entries.next(); return new AbstractMap.SimpleEntry<String, String>(nextEntry.getKey().toString(), nextEntry.getValue().toString()); } }; }
/** * Marks an <em>OpenTracing</em> span as erroneous and logs a message. * <p> * This method does <em>not</em> finish the span. * * @param span The span to mark. * @param message The message to log on the span. * @throws NullPointerException if message is {@code null}. */ public static void logError(final Span span, final String message) { if (span != null) { Objects.requireNonNull(message); logError(span, Collections.singletonMap(Fields.MESSAGE, message)); } }
/** * Marks an <em>OpenTracing</em> span as erroneous and logs a message. * <p> * This method does <em>not</em> finish the span. * * @param span The span to mark. * @param message The message to log on the span. * @throws NullPointerException if message is {@code null}. */ public static void logError(final Span span, final String message) { if (span != null) { Objects.requireNonNull(message); logError(span, Collections.singletonMap(Fields.MESSAGE, message)); } }
/** * Marks an <em>OpenTracing</em> span as erroneous and logs an exception. * <p> * This method does <em>not</em> finish the span. * * @param span The span to mark. * @param error The exception that has occurred. If the exception is a * {@link ServiceInvocationException} then a {@link Tags#HTTP_STATUS} * tag is added containing the exception's error code property value. * @throws NullPointerException if error is {@code null}. */ protected final void logError(final Span span, final Throwable error) { if (span != null) { if (ServiceInvocationException.class.isInstance(error)) { final ServiceInvocationException e = (ServiceInvocationException) error; Tags.HTTP_STATUS.set(span, e.getErrorCode()); } TracingHelper.logError(span, error); } }
TenantResult<JsonObject> getTenantResult(final String tenantId, final Span span) { final TenantObject tenant = tenants.get(tenantId); if (tenant == null) { TracingHelper.logError(span, "tenant not found"); return TenantResult.from(HttpURLConnection.HTTP_NOT_FOUND); } else { return TenantResult.from( HttpURLConnection.HTTP_OK, JsonObject.mapFrom(tenant), CacheDirective.maxAgeDirective(MAX_AGE_GET_TENANT)); } }
private TenantResult<JsonObject> getForCertificateAuthority(final X500Principal subjectDn, final Span span) { if (subjectDn == null) { TracingHelper.logError(span, "missing subject DN"); return TenantResult.from(HttpURLConnection.HTTP_BAD_REQUEST); } else { final TenantObject tenant = getByCa(subjectDn); if (tenant == null) { TracingHelper.logError(span, "no tenant found for subject DN"); return TenantResult.from(HttpURLConnection.HTTP_NOT_FOUND); } else { return TenantResult.from(HttpURLConnection.HTTP_OK, JsonObject.mapFrom(tenant), CacheDirective.maxAgeDirective(MAX_AGE_GET_TENANT)); } } }
/** * Composes the given future so that the given <em>OpenTracing</em> span is finished when the future completes. * <p> * The result or exception of the given future will be used to set a {@link Tags#HTTP_STATUS} tag on the span * and to set a {@link Tags#ERROR} tag in case of an exception or a result with error status. * * @param span The span to finish. * @param resultFuture The future to be composed. * @return The composed future. */ protected Future<EventBusMessage> finishSpanOnFutureCompletion(final Span span, final Future<EventBusMessage> resultFuture) { return resultFuture.compose(eventBusMessage -> { Tags.HTTP_STATUS.set(span, eventBusMessage.getStatus()); if (eventBusMessage.hasErrorStatus()) { Tags.ERROR.set(span, true); } span.finish(); return Future.succeededFuture(eventBusMessage); }).recover(t -> { Tags.HTTP_STATUS.set(span, ServiceInvocationException.extractStatusCode(t)); TracingHelper.logError(span, t); span.finish(); return Future.failedFuture(t); }); } }