RequestScopedMdc { @Nullable public static String get(RequestContext ctx, String key) { requireNonNull(ctx, "ctx"); requireNonNull(key, "key"); final String value = getMap(ctx).get(key); if (value != null) { return value; } final RequestContext rootCtx = ctx.root(); if (rootCtx != null && rootCtx != ctx) { return getMap(rootCtx).get(key); } return null; } private  RequestScopedMdc(); @Nullable static String get(RequestContext ctx, String key); static Map<String, String> getAll(RequestContext ctx); static void put(RequestContext ctx, String key, @Nullable String value); static void putAll(RequestContext ctx, Map<String, String> map); static void copy(RequestContext ctx, String key); static void copyAll(RequestContext ctx); static void remove(RequestContext ctx, String key); static void clear(RequestContext ctx);  }
@Test void get() { final ServiceRequestContext ctx = newContext(); RequestScopedMdc.put(ctx, "foo", "1"); assertThat(RequestScopedMdc.get(ctx, "foo")).isEqualTo("1"); assertThat(MDC.get("foo")).isNull(); try (SafeCloseable ignored = ctx.push()) { assertThat(MDC.get("foo")).isEqualTo("1"); MDC.put("foo", "2"); assertThat(MDC.get("foo")).isEqualTo("1"); final ClientRequestContext cctx = ClientRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); assertThat(cctx.root()).isSameAs(ctx); assertThat(RequestScopedMdc.get(cctx, "foo")).isEqualTo("1"); RequestScopedMdc.put(cctx, "foo", "3"); assertThat(RequestScopedMdc.get(ctx, "foo")).isEqualTo("1"); assertThat(RequestScopedMdc.get(cctx, "foo")).isEqualTo("3"); try (SafeCloseable ignored2 = cctx.push()) { RequestScopedMdc.remove(ctx, "foo"); RequestScopedMdc.remove(cctx, "foo"); assertThat(MDC.get("foo")).isEqualTo("2"); } } }