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

The following examples show how to use com.linecorp.armeria.common.RequestContext#setAttr() . 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: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Binds the specified request-scoped {@link MDC} property to the specified {@link RequestContext}.
 *
 * @param ctx   the {@link RequestContext}
 * @param key   the key of the request-scoped {@link MDC} property
 * @param value the value of the request-scoped {@link MDC} property
 */
public static void put(RequestContext ctx, String key, @Nullable String value) {
    requireNonNull(ctx, "ctx");
    requireNonNull(key, "key");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        final Object2ObjectMap<String, String> newMap;
        if (oldMap.isEmpty()) {
            newMap = Object2ObjectMaps.singleton(key, value);
        } else {
            final Object2ObjectMap<String, String> tmp =
                    new Object2ObjectOpenHashMap<>(oldMap.size() + 1);
            tmp.putAll(oldMap);
            tmp.put(key, value);
            newMap = Object2ObjectMaps.unmodifiable(tmp);
        }
        ctx.setAttr(MAP, newMap);
    }
}
 
Example 4
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Binds the specified request-scoped {@link MDC} properties to the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 * @param map the {@link Map} that contains the request-scoped {@link MDC} properties
 */
public static void putAll(RequestContext ctx, Map<String, String> map) {
    requireNonNull(ctx, "ctx");
    requireNonNull(map, "map");
    if (map.isEmpty()) {
        return;
    }

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        final Object2ObjectMap<String, String> newMap;
        if (oldMap.isEmpty()) {
            newMap = new Object2ObjectOpenHashMap<>(map);
        } else {
            newMap = new Object2ObjectOpenHashMap<>(oldMap.size() + map.size());
            newMap.putAll(oldMap);
            newMap.putAll(map);
        }
        ctx.setAttr(MAP, Object2ObjectMaps.unmodifiable(newMap));
    }
}
 
Example 5
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Unbinds the specified request-scoped {@link MDC} property from the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 * @param key the key of the request-scoped {@link MDC} property to unbind
 */
public static void remove(RequestContext ctx, String key) {
    requireNonNull(ctx, "ctx");
    requireNonNull(key, "key");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        if (!oldMap.containsKey(key)) {
            return;
        }

        final Object2ObjectMap<String, String> newMap;
        if (oldMap.size() == 1) {
            newMap = Object2ObjectMaps.emptyMap();
        } else {
            final Object2ObjectOpenHashMap<String, String> tmp = new Object2ObjectOpenHashMap<>(oldMap);
            tmp.remove(key);
            newMap = Object2ObjectMaps.unmodifiable(tmp);
        }
        ctx.setAttr(MAP, newMap);
    }
}
 
Example 6
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 7
Source File: RequestScopedMdc.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Unbinds all request-scoped {@link MDC} properties from the specified {@link RequestContext}.
 *
 * @param ctx the {@link RequestContext}
 */
public static void clear(RequestContext ctx) {
    requireNonNull(ctx, "ctx");

    synchronized (ctx) {
        final Object2ObjectMap<String, String> oldMap = getMap(ctx);
        if (!oldMap.isEmpty()) {
            ctx.setAttr(MAP, Object2ObjectMaps.emptyMap());
        }
    }
}
 
Example 8
Source File: TraceContextUtil.java    From armeria with Apache License 2.0 4 votes vote down vote up
public static void setTraceContext(RequestContext ctx, TraceContext traceContext) {
    ctx.setAttr(TRACE_CONTEXT_KEY, traceContext);
}