MetricsEnvironment { public static @Nullable MetricsContainer setCurrentContainer( @Nullable MetricsContainer container) { MetricsContainer previous = CONTAINER_FOR_THREAD.get(); if (container == null) { CONTAINER_FOR_THREAD.remove(); } else { CONTAINER_FOR_THREAD.set(container); } return previous; }  static @Nullable MetricsContainer setCurrentContainer(
      @Nullable MetricsContainer container); static void setMetricsSupported(boolean supported); static boolean isMetricsSupported(); static Closeable scopedMetricsContainer(MetricsContainer container); static @Nullable MetricsContainer getCurrentContainer();  }
@Test public void testUsesAppropriateMetricsContainer() { Counter counter = Metrics.counter("ns", "name"); MetricsContainer c1 = Mockito.mock(MetricsContainer.class); MetricsContainer c2 = Mockito.mock(MetricsContainer.class); Counter counter1 = Mockito.mock(Counter.class); Counter counter2 = Mockito.mock(Counter.class); when(c1.getCounter(MetricName.named("ns", "name"))).thenReturn(counter1); when(c2.getCounter(MetricName.named("ns", "name"))).thenReturn(counter2); MetricsEnvironment.setCurrentContainer(c1); counter.inc(); MetricsEnvironment.setCurrentContainer(c2); counter.dec(); MetricsEnvironment.setCurrentContainer(null); verify(counter1).inc(1L); verify(counter2).inc(-1L); verifyNoMoreInteractions(counter1, counter2); }