GaugeConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { final MeterId meterId = (MeterId) allArguments[0]; final Supplier<Double> getter = (Supplier<Double>) allArguments[1]; final Gauge gauge = new Gauge(MeterIdConverter.convert(meterId), getter); if (METER_SERVICE == null) { METER_SERVICE = ServiceManager.INSTANCE.findService(MeterService.class); } objInst.setSkyWalkingDynamicField(METER_SERVICE.register(gauge)); }  @Override void onConstruct(EnhancedInstance objInst, Object[] allArguments);  }
@Test public void testConstruct() { gaugeConstructInterceptor.onConstruct(enhancedInstance, new Object[] { new MeterId("test", MeterId.MeterType.GAUGE, Arrays.asList(new MeterId.Tag("k1", "v1"))), (Supplier<Double>) () -> 1d }); final MeterService service = ServiceManager.INSTANCE.findService(MeterService.class); final Map<MeterId, BaseMeter> meterMap = (Map<MeterId, BaseMeter>) Whitebox.getInternalState( service, "meterMap"); Assert.assertEquals(1, meterMap.size()); final Object field = meterMap.values().iterator().next(); Assert.assertNotNull(field); Assert.assertTrue(field instanceof Gauge); final Gauge gauge = (Gauge) field; Assert.assertNotNull(gauge.getId()); Assert.assertEquals("test", gauge.getId().getName()); Assert.assertEquals(MeterType.GAUGE, gauge.getId().getType()); Assert.assertEquals(Arrays.asList(new MeterTag("k1", "v1")), gauge.getId().getTags()); }