Java Code Examples for com.linecorp.armeria.common.RequestContext#attr()

The following examples show how to use com.linecorp.armeria.common.RequestContext#attr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestLoggingContext.java    From curiostack with MIT License 6 votes vote down vote up
public static void put(RequestContext ctx, String key, String value) {
  // Copy into a new map similar to what log4j2 does for thread-safety.
  ImmutableMap<String, String> oldLoggingContext = ctx.attr(LOGGING_CONTEXT);
  if (oldLoggingContext == null) {
    oldLoggingContext = ImmutableMap.of();
  }
  ImmutableMap.Builder<String, String> newLoggingContext =
      ImmutableMap.builderWithExpectedSize(oldLoggingContext.size() + 1);
  oldLoggingContext.forEach(
      (k, v) -> {
        if (!k.equals(key)) {
          newLoggingContext.put(k, v);
        }
      });
  newLoggingContext.put(key, value);
  ctx.setAttr(LOGGING_CONTEXT, newLoggingContext.build());
}
 
Example 2
Source File: RequestMetricSupport.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up request metrics.
 */
public static void setup(RequestContext ctx, AttributeKey<Boolean> requestMetricsSetKey,
                         MeterIdPrefixFunction meterIdPrefixFunction, boolean server) {
    final Boolean isRequestMetricsSet = ctx.attr(requestMetricsSetKey);

    if (Boolean.TRUE.equals(isRequestMetricsSet)) {
        return;
    }
    ctx.setAttr(requestMetricsSetKey, true);

    ctx.log()
       .whenAvailable(RequestLogProperty.REQUEST_START_TIME,
                      RequestLogProperty.REQUEST_HEADERS,
                      RequestLogProperty.NAME,
                      RequestLogProperty.SESSION)
       .thenAccept(log -> onRequest(log, meterIdPrefixFunction, server));
}
 
Example 3
Source File: GrpcUnsafeBufferUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the {@link ByteBuf} backing the specified {@link Message} to be released later using
 * {@link #releaseBuffer(Object, RequestContext)}.
 */
public static void storeBuffer(ByteBuf buf, Object message, RequestContext ctx) {
    IdentityHashMap<Object, ByteBuf> buffers = ctx.attr(BUFFERS);
    if (buffers == null) {
        buffers = new IdentityHashMap<>();
        ctx.setAttr(BUFFERS, buffers);
    }
    buffers.put(message, buf);
}
 
Example 4
Source File: GrpcUnsafeBufferUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Releases the {@link ByteBuf} backing the specified {@link Message}.
 */
public static void releaseBuffer(Object message, RequestContext ctx) {
    final IdentityHashMap<Object, ByteBuf> buffers = ctx.attr(BUFFERS);
    checkState(buffers != null,
               "Releasing buffer even though storeBuffer has not been called.");
    final ByteBuf removed = buffers.remove(message);
    if (removed == null) {
        throw new IllegalArgumentException("The provided message does not have a stored buffer.");
    }
    removed.release();
}
 
Example 5
Source File: TraceContextUtil.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nullable
public static TraceContext traceContext(RequestContext ctx) {
    return ctx.attr(TRACE_CONTEXT_KEY);
}