CounterConstructInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { final MeterId meterId = (MeterId) allArguments[0]; final Counter.Mode mode = (Counter.Mode) allArguments[1]; final org.apache.skywalking.apm.agent.core.meter.Counter counter = new org.apache.skywalking.apm.agent.core.meter.Counter(MeterIdConverter.convert(meterId), mode == Counter.Mode.RATE ? CounterMode.RATE : CounterMode.INCREMENT); if (METER_SERVICE == null) { METER_SERVICE = ServiceManager.INSTANCE.findService(MeterService.class); } objInst.setSkyWalkingDynamicField(METER_SERVICE.register(counter)); }  @Override void onConstruct(EnhancedInstance objInst, Object[] allArguments);  }
@Test public void testConstruct() { counterConstructInterceptor.onConstruct(enhancedInstance, new Object[] { new MeterId("test", MeterId.MeterType.COUNTER, Arrays.asList(new MeterId.Tag("k1", "v1"))), Counter.Mode.RATE }); 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 BaseMeter meterData = meterMap.values().iterator().next(); Assert.assertTrue(meterData instanceof org.apache.skywalking.apm.agent.core.meter.Counter); final org.apache.skywalking.apm.agent.core.meter.Counter counter = (org.apache.skywalking.apm.agent.core.meter.Counter) meterData; Assert.assertNotNull(counter.getId()); Assert.assertEquals("test", counter.getId().getName()); Assert.assertEquals(MeterType.COUNTER, counter.getId().getType()); Assert.assertEquals(Arrays.asList(new MeterTag("k1", "v1")), counter.getId().getTags()); }