/** * Returns the method of the {@link Request}. This method is a shortcut to {@code context().method()}. * This method returns non-{@code null} regardless the current {@link RequestLogAvailability}. */ default HttpMethod method() { return context().method(); }
/** * Returns the absolute path part of the current {@link Request} URI, excluding the query part, * decoded in UTF-8. * This method is a shortcut to {@code context().decodedPath()}. * This method returns non-{@code null} regardless the current {@link RequestLogAvailability}. */ default String decodedPath() { return context().decodedPath(); }
/** * Returns the query part of the {@link Request} URI, without the leading {@code '?'}, * as defined in <a href="https://tools.ietf.org/html/rfc3986">RFC3986</a>. * This method is a shortcut to {@code context().query()}. * This property is available regardless the current {@link RequestLogAvailability}. */ @Nullable default String query() { return context().query(); }
@Nullable @Override Object getMessage0(RequestLog log) { final Attribute<?> value = log.context().attr(key); return value != null ? stringifer.apply(value.get()) : null; } }
/** * Returns the absolute path part of the {@link Request} URI, excluding the query part, * as defined in <a href="https://tools.ietf.org/html/rfc3986">RFC3986</a>. * This method is a shortcut to {@code context().path()}. * This method returns non-{@code null} regardless the current {@link RequestLogAvailability}. */ default String path() { return context().path(); }
/** * Writes an access log for the specified {@link RequestLog}. */ static void write(List<AccessLogComponent> format, RequestLog log) { final VirtualHost host = ((ServiceRequestContext) log.context()).virtualHost(); final Logger logger = host.accessLogger(); if (!format.isEmpty() && logger.isInfoEnabled()) { logger.info(format(format, log)); } }
/** * Invokes {@link RequestLogListener#onRequestLog(RequestLog)}. */ public static void invokeOnRequestLog(RequestLogListener listener, RequestLog log) { try (SafeCloseable ignored = log.context().push()) { listener.onRequestLog(log); } catch (Throwable e) { logger.warn("onRequestLog() failed with an exception:", e); } }
/** * Returns the host name of the {@link Request}. * * @deprecated Do not use this method. Get the remote or local address from {@link #context()} or get * the authority from {@link #authority()}. * * @return the host name. {@code null} if the {@link Request} has failed even before a connection is * established. * @throws RequestLogAvailabilityException if this property is not available yet */ @Nullable @Deprecated default String host() { final RequestContext ctx = context(); final InetSocketAddress addr; if (ctx instanceof ClientRequestContext) { addr = ctx.remoteAddress(); } else { addr = ctx.localAddress(); } return addr != null ? addr.getHostString() : null; }
switch (type) { case LOCAL_IP_ADDRESS: final InetSocketAddress local = log.context().localAddress(); return local == null || local.isUnresolved() ? null : local.getAddress().getHostAddress(); if ("c".equals(variable)) { final InetSocketAddress remote = log.context().remoteAddress(); return remote == null || remote.isUnresolved() ? null : remote.getAddress().getHostAddress(); } else { final ServiceRequestContext ctx = (ServiceRequestContext) log.context(); return ctx.clientAddress().getHostAddress(); final SocketAddress ra = log.context().remoteAddress(); return ra instanceof InetSocketAddress ? ((InetSocketAddress) ra).getHostString() : null; log.context().sessionProtocol()).uriText()); return requestLine.toString();
private void setRemoteEndpoint(Span span, RequestLog log) { final SocketAddress remoteAddress = log.context().remoteAddress(); final InetAddress address; final int port;
@Override public O serve(ServiceRequestContext ctx, I req) throws Exception { if (sampler.isSampled()) { ctx.log().addListener(log -> logRequest(((ServiceRequestContext) log.context()).logger(), log, requestLogLevel, requestHeadersSanitizer, requestContentSanitizer), RequestLogAvailability.REQUEST_END); ctx.log().addListener(log -> logResponse(((ServiceRequestContext) log.context()).logger(), log, requestLogLevel, requestHeadersSanitizer, requestContentSanitizer, successfulResponseLogLevel, failedResponseLogLevel, responseHeadersSanitizer, responseContentSanitizer), RequestLogAvailability.COMPLETE); } return delegate().serve(ctx, req); } }
private List<Tag> buildTags(RequestLog log) { final RequestContext ctx = log.context(); final Object requestContent = log.requestContent(); String methodName = null; if (requestContent instanceof RpcRequest) { methodName = ((RpcRequest) requestContent).method(); } if (methodName == null) { final HttpHeaders requestHeaders = log.requestHeaders(); final HttpMethod httpMethod = requestHeaders.method(); if (httpMethod != null) { methodName = httpMethod.name(); } } if (methodName == null) { methodName = MoreObjects.firstNonNull(log.method().name(), "__UNKNOWN_METHOD__"); } final List<Tag> tags = new ArrayList<>(4); // method, hostNamePattern, pathMapping, status tags.add(Tag.of("method", methodName)); if (ctx instanceof ServiceRequestContext) { final ServiceRequestContext sCtx = (ServiceRequestContext) ctx; tags.add(Tag.of("hostnamePattern", sCtx.virtualHost().hostnamePattern())); tags.add(Tag.of("pathMapping", sCtx.pathMapping().meterTag())); } return tags; } };
final String authority = log.responseHeaders().authority(); if ("?".equals(authority)) { final InetSocketAddress remoteAddr = log.context().remoteAddress(); assert remoteAddr != null; return remoteAddr.getHostString();
private static void onRequest(RequestLog log, MeterIdPrefixFunction meterIdPrefixFunction) { final RequestContext ctx = log.context(); final MeterRegistry registry = ctx.meterRegistry(); final MeterIdPrefix activeRequestsId = meterIdPrefixFunction.activeRequestPrefix(registry, log) .append("activeRequests"); final ActiveRequestMetrics activeRequestMetrics = MicrometerUtil.register( registry, activeRequestsId, ActiveRequestMetrics.class, (reg, prefix) -> reg.gauge(prefix.name(), prefix.tags(), new ActiveRequestMetrics(), ActiveRequestMetrics::doubleValue)); activeRequestMetrics.increment(); ctx.log().addListener(requestLog -> onResponse(requestLog, meterIdPrefixFunction, activeRequestMetrics), RequestLogAvailability.COMPLETE); }
private static void onResponse(RequestLog log, MeterIdPrefixFunction meterIdPrefixFunction, ActiveRequestMetrics activeRequestMetrics) { final RequestContext ctx = log.context(); final MeterRegistry registry = ctx.meterRegistry(); final MeterIdPrefix idPrefix = meterIdPrefixFunction.apply(registry, log); final RequestMetrics metrics = MicrometerUtil.register( registry, idPrefix, RequestMetrics.class, DefaultRequestMetrics::new); if (log.requestCause() != null) { metrics.failure().increment(); return; } metrics.requestDuration().record(log.requestDurationNanos(), TimeUnit.NANOSECONDS); metrics.requestLength().record(log.requestLength()); metrics.responseDuration().record(log.responseDurationNanos(), TimeUnit.NANOSECONDS); metrics.responseLength().record(log.responseLength()); metrics.totalDuration().record(log.totalDurationNanos(), TimeUnit.NANOSECONDS); if (isSuccess(log)) { metrics.success().increment(); } else { metrics.failure().increment(); } activeRequestMetrics.decrement(); }