NotifyHandler implements MetricsNotify { @Override public void notify(Metrics metrics) { WithMetadata withMetadata = (WithMetadata) metrics; MetricsMetaInfo meta = withMetadata.getMeta(); int scope = meta.getScope(); if (!DefaultScopeDefine.inServiceCatalog(scope) && !DefaultScopeDefine.inServiceInstanceCatalog(scope) && !DefaultScopeDefine.inEndpointCatalog(scope) && !DefaultScopeDefine.inServiceRelationCatalog(scope) && !DefaultScopeDefine.inServiceInstanceRelationCatalog(scope) && !DefaultScopeDefine.inEndpointRelationCatalog(scope)) { return; } MetaInAlarm metaInAlarm; if (DefaultScopeDefine.inServiceCatalog(scope)) { final String serviceId = meta.getId(); final IDManager.ServiceID.ServiceIDDefinition serviceIDDefinition = IDManager.ServiceID.analysisId( serviceId); ServiceMetaInAlarm serviceMetaInAlarm = new ServiceMetaInAlarm(); serviceMetaInAlarm.setMetricsName(meta.getMetricsName()); serviceMetaInAlarm.setId(serviceId); serviceMetaInAlarm.setName(serviceIDDefinition.getName()); metaInAlarm = serviceMetaInAlarm; } else if (DefaultScopeDefine.inServiceInstanceCatalog(scope)) { final String instanceId = meta.getId(); final IDManager.ServiceInstanceID.InstanceIDDefinition instanceIDDefinition = IDManager.ServiceInstanceID.analysisId( instanceId); final IDManager.ServiceID.ServiceIDDefinition serviceIDDefinition = IDManager.ServiceID.analysisId( instanceIDDefinition.getServiceId()); ServiceInstanceMetaInAlarm instanceMetaInAlarm = new ServiceInstanceMetaInAlarm(); instanceMetaInAlarm.setMetricsName(meta.getMetricsName()); instanceMetaInAlarm.setId(instanceId); instanceMetaInAlarm.setName(instanceIDDefinition.getName() + " of " + serviceIDDefinition.getName()); metaInAlarm = instanceMetaInAlarm; } else if (DefaultScopeDefine.inEndpointCatalog(scope)) { final String endpointId = meta.getId(); final IDManager.EndpointID.EndpointIDDefinition endpointIDDefinition = IDManager.EndpointID.analysisId( endpointId); final IDManager.ServiceID.ServiceIDDefinition serviceIDDefinition = IDManager.ServiceID.analysisId( endpointIDDefinition.getServiceId()); EndpointMetaInAlarm endpointMetaInAlarm = new EndpointMetaInAlarm(); endpointMetaInAlarm.setMetricsName(meta.getMetricsName()); endpointMetaInAlarm.setId(meta.getId()); endpointMetaInAlarm.setName( endpointIDDefinition.getEndpointName() + " in " + serviceIDDefinition.getName()); metaInAlarm = endpointMetaInAlarm; } else if (DefaultScopeDefine.inServiceRelationCatalog(scope)) { final String serviceRelationId = meta.getId(); final IDManager.ServiceID.ServiceRelationDefine serviceRelationDefine = IDManager.ServiceID.analysisRelationId( serviceRelationId); final IDManager.ServiceID.ServiceIDDefinition sourceIdDefinition = IDManager.ServiceID.analysisId( serviceRelationDefine.getSourceId()); final IDManager.ServiceID.ServiceIDDefinition destIdDefinition = IDManager.ServiceID.analysisId( serviceRelationDefine.getDestId()); ServiceRelationMetaInAlarm serviceRelationMetaInAlarm = new ServiceRelationMetaInAlarm(); serviceRelationMetaInAlarm.setMetricsName(meta.getMetricsName()); serviceRelationMetaInAlarm.setId(serviceRelationId); serviceRelationMetaInAlarm.setName(sourceIdDefinition.getName() + " to " + destIdDefinition.getName()); metaInAlarm = serviceRelationMetaInAlarm; } else if (DefaultScopeDefine.inServiceInstanceRelationCatalog(scope)) { final String instanceRelationId = meta.getId(); final IDManager.ServiceInstanceID.ServiceInstanceRelationDefine serviceRelationDefine = IDManager.ServiceInstanceID.analysisRelationId( instanceRelationId); final IDManager.ServiceInstanceID.InstanceIDDefinition sourceIdDefinition = IDManager.ServiceInstanceID.analysisId( serviceRelationDefine.getSourceId()); final IDManager.ServiceID.ServiceIDDefinition sourceServiceId = IDManager.ServiceID.analysisId( sourceIdDefinition.getServiceId()); final IDManager.ServiceInstanceID.InstanceIDDefinition destIdDefinition = IDManager.ServiceInstanceID.analysisId( serviceRelationDefine.getDestId()); final IDManager.ServiceID.ServiceIDDefinition destServiceId = IDManager.ServiceID.analysisId( destIdDefinition.getServiceId()); ServiceInstanceRelationMetaInAlarm instanceRelationMetaInAlarm = new ServiceInstanceRelationMetaInAlarm(); instanceRelationMetaInAlarm.setMetricsName(meta.getMetricsName()); instanceRelationMetaInAlarm.setId(instanceRelationId); instanceRelationMetaInAlarm.setName(sourceIdDefinition.getName() + " of " + sourceServiceId.getName() + " to " + destIdDefinition.getName() + " of " + destServiceId.getName()); metaInAlarm = instanceRelationMetaInAlarm; } else if (DefaultScopeDefine.inEndpointRelationCatalog(scope)) { final String endpointRelationId = meta.getId(); final IDManager.EndpointID.EndpointRelationDefine endpointRelationDefine = IDManager.EndpointID.analysisRelationId( endpointRelationId); final IDManager.ServiceID.ServiceIDDefinition sourceService = IDManager.ServiceID.analysisId( endpointRelationDefine.getSourceServiceId()); final IDManager.ServiceID.ServiceIDDefinition destService = IDManager.ServiceID.analysisId( endpointRelationDefine.getDestServiceId()); EndpointRelationMetaInAlarm endpointRelationMetaInAlarm = new EndpointRelationMetaInAlarm(); endpointRelationMetaInAlarm.setMetricsName(meta.getMetricsName()); endpointRelationMetaInAlarm.setId(endpointRelationId); endpointRelationMetaInAlarm.setName(endpointRelationDefine.getSource() + " in " + sourceService.getName() + " to " + endpointRelationDefine.getDest() + " in " + destService.getName()); metaInAlarm = endpointRelationMetaInAlarm; } else { return; } List<RunningRule> runningRules = core.findRunningRule(meta.getMetricsName()); if (runningRules == null) { return; } runningRules.forEach(rule -> rule.in(metaInAlarm, metrics)); } NotifyHandler(AlarmRulesWatcher alarmRulesWatcher); @Override void notify(Metrics metrics); void init(AlarmCallback... callbacks);  }
@Test public void testNotifyWithEndpointCatalog() { prepareNotify(); String metricsName = "endpoint-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inEndpointCatalog(0)).thenReturn(true); String endpointInventoryName = "endpoint-inventory-name"; EndpointTraffic endpointTraffic = mock(EndpointTraffic.class); when(endpointTraffic.getName()).thenReturn(endpointInventoryName); String serviceInventoryName = "service-inventory-name"; final String serviceId = IDManager.ServiceID.buildId(serviceInventoryName, NodeType.Normal); final String endpointId = IDManager.EndpointID.buildId(serviceId, endpointInventoryName); when(metadata.getId()).thenReturn(endpointId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof EndpointMetaInAlarm); assertEquals("c2VydmljZS1pbnZlbnRvcnktbmFtZQ==.1_ZW5kcG9pbnQtaW52ZW50b3J5LW5hbWU=", metaInAlarm.getId0()); assertEquals(DefaultScopeDefine.ENDPOINT_CATALOG_NAME, metaInAlarm.getScope()); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals(endpointInventoryName + " in " + serviceInventoryName, metaInAlarm.getName()); assertEquals(DefaultScopeDefine.ENDPOINT, metaInAlarm.getScopeId()); }
@Test public void testNotifyWithServiceInstanceCatalog() { prepareNotify(); String metricsName = "service-instance-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inServiceInstanceCatalog(0)).thenReturn(true); String instanceInventoryName = "instance-inventory-name"; final String serviceId = IDManager.ServiceID.buildId("service", NodeType.Normal); final String instanceId = IDManager.ServiceInstanceID.buildId(serviceId, instanceInventoryName); when(metadata.getId()).thenReturn(instanceId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof ServiceInstanceMetaInAlarm); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals("c2VydmljZQ==.1_aW5zdGFuY2UtaW52ZW50b3J5LW5hbWU=", metaInAlarm.getId0()); assertEquals(DefaultScopeDefine.SERVICE_INSTANCE_CATALOG_NAME, metaInAlarm.getScope()); assertEquals("instance-inventory-name of service", metaInAlarm.getName()); assertEquals(DefaultScopeDefine.SERVICE_INSTANCE, metaInAlarm.getScopeId()); }
@Test public void testNotifyWithServiceCatalog() { prepareNotify(); String metricsName = "service-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inServiceCatalog(0)).thenReturn(true); final String serviceId = IDManager.ServiceID.buildId("service", NodeType.Normal); when(metadata.getId()).thenReturn(serviceId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof ServiceMetaInAlarm); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals("c2VydmljZQ==.1", metaInAlarm.getId0()); assertEquals(DefaultScopeDefine.SERVICE_CATALOG_NAME, metaInAlarm.getScope()); assertEquals("service", metaInAlarm.getName()); assertEquals(DefaultScopeDefine.SERVICE, metaInAlarm.getScopeId()); }
@Test public void testNotifyWithServiceRelationCatalog() { prepareNotify(); String metricsName = "service-relation-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inServiceRelationCatalog(0)).thenReturn(true); final String serviceRelationId = IDManager.ServiceID.buildRelationId(new IDManager.ServiceID.ServiceRelationDefine( IDManager.ServiceID.buildId("from-service", true), IDManager.ServiceID.buildId("dest-service", true) )); when(metadata.getId()).thenReturn(serviceRelationId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof ServiceRelationMetaInAlarm); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals("ZnJvbS1zZXJ2aWNl.1", metaInAlarm.getId0()); assertEquals("ZGVzdC1zZXJ2aWNl.1", metaInAlarm.getId1()); assertEquals(DefaultScopeDefine.SERVICE_RELATION_CATALOG_NAME, metaInAlarm.getScope()); assertEquals("from-service to dest-service", metaInAlarm.getName()); assertEquals(DefaultScopeDefine.SERVICE_RELATION, metaInAlarm.getScopeId()); }
@Test public void testNotifyWithServiceInstanceRelationCatalog() { prepareNotify(); String metricsName = "service-instance-relation-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inServiceInstanceRelationCatalog(0)).thenReturn(true); final String serviceInstanceRelationId = IDManager.ServiceInstanceID.buildRelationId(new IDManager.ServiceInstanceID.ServiceInstanceRelationDefine( IDManager.ServiceInstanceID.buildId(IDManager.ServiceID.buildId("from-service", true), "from-service-instance"), IDManager.ServiceInstanceID.buildId(IDManager.ServiceID.buildId("dest-service", true), "dest-service-instance") )); when(metadata.getId()).thenReturn(serviceInstanceRelationId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof ServiceInstanceRelationMetaInAlarm); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals("ZnJvbS1zZXJ2aWNl.1_ZnJvbS1zZXJ2aWNlLWluc3RhbmNl", metaInAlarm.getId0()); assertEquals("ZGVzdC1zZXJ2aWNl.1_ZGVzdC1zZXJ2aWNlLWluc3RhbmNl", metaInAlarm.getId1()); assertEquals(DefaultScopeDefine.SERVICE_INSTANCE_RELATION_CATALOG_NAME, metaInAlarm.getScope()); assertEquals("from-service-instance of from-service to dest-service-instance of dest-service", metaInAlarm.getName()); assertEquals(DefaultScopeDefine.SERVICE_INSTANCE_RELATION, metaInAlarm.getScopeId()); }
@Test public void testNotifyWithEndpointRelationCatalog() { prepareNotify(); String metricsName = "endpoint-relation-metrics"; when(metadata.getMetricsName()).thenReturn(metricsName); when(DefaultScopeDefine.inEndpointRelationCatalog(0)).thenReturn(true); final String serviceInstanceRelationId = IDManager.EndpointID.buildRelationId(new IDManager.EndpointID.EndpointRelationDefine( IDManager.ServiceID.buildId("from-service", true), "/source-path", IDManager.ServiceID.buildId("dest-service", true), "/dest-path" )); when(metadata.getId()).thenReturn(serviceInstanceRelationId); ArgumentCaptor<MetaInAlarm> metaCaptor = ArgumentCaptor.forClass(MetaInAlarm.class); notifyHandler.notify(metrics); verify(rule).in(metaCaptor.capture(), any()); MetaInAlarm metaInAlarm = metaCaptor.getValue(); assertTrue(metaInAlarm instanceof EndpointRelationMetaInAlarm); assertEquals(metricsName, metaInAlarm.getMetricsName()); assertEquals("ZnJvbS1zZXJ2aWNl.1_L3NvdXJjZS1wYXRo", metaInAlarm.getId0()); assertEquals("ZGVzdC1zZXJ2aWNl.1_L2Rlc3QtcGF0aA==", metaInAlarm.getId1()); assertEquals(DefaultScopeDefine.ENDPOINT_RELATION_CATALOG_NAME, metaInAlarm.getScope()); assertEquals("/source-path in from-service to /dest-path in dest-service", metaInAlarm.getName()); assertEquals(DefaultScopeDefine.ENDPOINT_RELATION, metaInAlarm.getScopeId()); }
@Test public void dontNotify() { MetricsMetaInfo metadata = mock(MetricsMetaInfo.class); when(metadata.getScope()).thenReturn(DefaultScopeDefine.ALL); MockMetrics mockMetrics = mock(MockMetrics.class); when(mockMetrics.getMeta()).thenReturn(metadata); notifyHandler.notify(mockMetrics); }