kezhenxu94 commented on a change in pull request #6702: URL: https://github.com/apache/skywalking/pull/6702#discussion_r608569938
########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/EventHookCallback.java ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.core.alarm.provider; + +import org.apache.skywalking.apm.network.event.v3.Event; +import org.apache.skywalking.apm.network.event.v3.Source; +import org.apache.skywalking.apm.network.event.v3.Type; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerModule; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerService; +import org.apache.skywalking.oap.server.core.alarm.AlarmCallback; +import org.apache.skywalking.oap.server.core.alarm.AlarmMessage; +import org.apache.skywalking.oap.server.core.analysis.IDManager; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.library.module.ModuleManager; + +import java.util.List; +import java.util.UUID; + +/** + * EventCallBack: When an alert is present, an event is generated for each alert message. These events are then sent to the internal event analyzer. + * + */ +public class EventHookCallback implements AlarmCallback { + + private final ModuleManager manager; + + public EventHookCallback(ModuleManager manager) { + this.manager = manager; + } + + @Override + public void doAlarm(List<AlarmMessage> alarmMessage) { + if (null == manager) { + return ; + } + EventAnalyzerService analyzerService = manager.find(EventAnalyzerModule.NAME).provider().getService(EventAnalyzerService.class); + alarmMessage.forEach(a -> { + analyzerService.analyze(constructCurrentEvent(a)); + }); + } + + private Event constructCurrentEvent(AlarmMessage msg) { + long millis = System.currentTimeMillis(); + Event event = Event.newBuilder() + .setUuid(UUID.randomUUID().toString()) + .setName("Alarm") + .setStartTime(millis) + .setMessage(msg.getAlarmMessage()) + .setType(Type.Error) + .setEndTime(millis) + .build(); + + switch (msg.getScopeId()) { + case DefaultScopeDefine.SERVICE : + IDManager.ServiceID.ServiceIDDefinition serviceIdDef = IDManager.ServiceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setService(serviceIdDef.getName()) + .build() + ).build(); + break; + case DefaultScopeDefine.SERVICE_INSTANCE : + IDManager.ServiceInstanceID.InstanceIDDefinition instanceIdDef = IDManager.ServiceInstanceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setServiceInstance(instanceIdDef.getName()) Review comment: For service instance scope, you need to set the service name as well ########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/EventHookCallback.java ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.core.alarm.provider; + +import org.apache.skywalking.apm.network.event.v3.Event; +import org.apache.skywalking.apm.network.event.v3.Source; +import org.apache.skywalking.apm.network.event.v3.Type; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerModule; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerService; +import org.apache.skywalking.oap.server.core.alarm.AlarmCallback; +import org.apache.skywalking.oap.server.core.alarm.AlarmMessage; +import org.apache.skywalking.oap.server.core.analysis.IDManager; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.library.module.ModuleManager; + +import java.util.List; +import java.util.UUID; + +/** + * EventCallBack: When an alert is present, an event is generated for each alert message. These events are then sent to the internal event analyzer. + * + */ +public class EventHookCallback implements AlarmCallback { + + private final ModuleManager manager; + + public EventHookCallback(ModuleManager manager) { + this.manager = manager; + } + + @Override + public void doAlarm(List<AlarmMessage> alarmMessage) { + if (null == manager) { + return ; + } + EventAnalyzerService analyzerService = manager.find(EventAnalyzerModule.NAME).provider().getService(EventAnalyzerService.class); + alarmMessage.forEach(a -> { + analyzerService.analyze(constructCurrentEvent(a)); + }); + } + + private Event constructCurrentEvent(AlarmMessage msg) { + long millis = System.currentTimeMillis(); + Event event = Event.newBuilder() + .setUuid(UUID.randomUUID().toString()) + .setName("Alarm") + .setStartTime(millis) + .setMessage(msg.getAlarmMessage()) + .setType(Type.Error) + .setEndTime(millis) + .build(); Review comment: Don't `.build()` too early, so that we don't need to convert it back to a build again (line 72, line 80, line 88, etc.) ########## File path: oap-server/server-alarm-plugin/src/test/java/org/apache/skywalking/oap/server/core/alarm/provider/EventHookCallbackTest.java ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.core.alarm.provider; + +import org.apache.skywalking.oap.server.core.alarm.AlarmMessage; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.library.module.ModuleProvider; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +import static org.junit.Assert.assertTrue; + +/** + * EventHookCallbackTest. + * + */ +public class EventHookCallbackTest { + + private AlarmModuleProvider moduleProvider; + + @Before + public void setUp() throws Exception { + ServiceLoader<ModuleProvider> serviceLoader = ServiceLoader.load(ModuleProvider.class); + Iterator<ModuleProvider> providerIterator = serviceLoader.iterator(); + + assertTrue(providerIterator.hasNext()); + + moduleProvider = (AlarmModuleProvider) providerIterator.next(); + + moduleProvider.createConfigBeanIfAbsent(); + + moduleProvider.prepare(); + } + + @Test + public void testEventCallback() { + List<AlarmMessage> msgs = new ArrayList<>(); + AlarmMessage msg = constructAlarmMessage(); + msgs.add(msg); + new EventHookCallback(moduleProvider.getModuleManager()).doAlarm(msgs); + } Review comment: This test case doesn't make too much sense as there is no assertion in it ########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/EventHookCallback.java ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.core.alarm.provider; + +import org.apache.skywalking.apm.network.event.v3.Event; +import org.apache.skywalking.apm.network.event.v3.Source; +import org.apache.skywalking.apm.network.event.v3.Type; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerModule; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerService; +import org.apache.skywalking.oap.server.core.alarm.AlarmCallback; +import org.apache.skywalking.oap.server.core.alarm.AlarmMessage; +import org.apache.skywalking.oap.server.core.analysis.IDManager; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.library.module.ModuleManager; + +import java.util.List; +import java.util.UUID; + +/** + * EventCallBack: When an alert is present, an event is generated for each alert message. These events are then sent to the internal event analyzer. + * + */ +public class EventHookCallback implements AlarmCallback { + + private final ModuleManager manager; + + public EventHookCallback(ModuleManager manager) { + this.manager = manager; + } + + @Override + public void doAlarm(List<AlarmMessage> alarmMessage) { + if (null == manager) { + return ; + } + EventAnalyzerService analyzerService = manager.find(EventAnalyzerModule.NAME).provider().getService(EventAnalyzerService.class); + alarmMessage.forEach(a -> { + analyzerService.analyze(constructCurrentEvent(a)); + }); + } + + private Event constructCurrentEvent(AlarmMessage msg) { + long millis = System.currentTimeMillis(); + Event event = Event.newBuilder() + .setUuid(UUID.randomUUID().toString()) + .setName("Alarm") + .setStartTime(millis) + .setMessage(msg.getAlarmMessage()) + .setType(Type.Error) + .setEndTime(millis) + .build(); + + switch (msg.getScopeId()) { + case DefaultScopeDefine.SERVICE : + IDManager.ServiceID.ServiceIDDefinition serviceIdDef = IDManager.ServiceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setService(serviceIdDef.getName()) + .build() + ).build(); + break; + case DefaultScopeDefine.SERVICE_INSTANCE : + IDManager.ServiceInstanceID.InstanceIDDefinition instanceIdDef = IDManager.ServiceInstanceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setServiceInstance(instanceIdDef.getName()) + .build() + ).build(); + break; + case DefaultScopeDefine.ENDPOINT : + IDManager.EndpointID.EndpointIDDefinition endpointIDDef = IDManager.EndpointID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setEndpoint(endpointIDDef.getEndpointName()) + .build() + ).build(); + break; + default: + event = event.toBuilder().setSource( + Source.newBuilder() + .setService(msg.getName()) + .build() + ).build(); + break; + } + return event; Review comment: Let's invoke builder `.build()` here ########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/AlarmModuleProvider.java ########## @@ -91,4 +92,8 @@ public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleSta ConfigurationModule.NAME }; } + + ModuleManager getModuleManager() { + return getManager(); + } } Review comment: This method is only for testing purpose, we don't add methods just for testing, please use `Whitebox.getInternalState(moduleProvider, "manager");` to get the `manager` field in the unit test ########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/EventHookCallback.java ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.core.alarm.provider; + +import org.apache.skywalking.apm.network.event.v3.Event; +import org.apache.skywalking.apm.network.event.v3.Source; +import org.apache.skywalking.apm.network.event.v3.Type; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerModule; +import org.apache.skywalking.oap.server.analyzer.event.EventAnalyzerService; +import org.apache.skywalking.oap.server.core.alarm.AlarmCallback; +import org.apache.skywalking.oap.server.core.alarm.AlarmMessage; +import org.apache.skywalking.oap.server.core.analysis.IDManager; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.library.module.ModuleManager; + +import java.util.List; +import java.util.UUID; + +/** + * EventCallBack: When an alert is present, an event is generated for each alert message. These events are then sent to the internal event analyzer. + * + */ +public class EventHookCallback implements AlarmCallback { + + private final ModuleManager manager; + + public EventHookCallback(ModuleManager manager) { + this.manager = manager; + } + + @Override + public void doAlarm(List<AlarmMessage> alarmMessage) { + if (null == manager) { + return ; + } + EventAnalyzerService analyzerService = manager.find(EventAnalyzerModule.NAME).provider().getService(EventAnalyzerService.class); + alarmMessage.forEach(a -> { + analyzerService.analyze(constructCurrentEvent(a)); + }); + } + + private Event constructCurrentEvent(AlarmMessage msg) { + long millis = System.currentTimeMillis(); + Event event = Event.newBuilder() + .setUuid(UUID.randomUUID().toString()) + .setName("Alarm") + .setStartTime(millis) + .setMessage(msg.getAlarmMessage()) + .setType(Type.Error) + .setEndTime(millis) + .build(); + + switch (msg.getScopeId()) { + case DefaultScopeDefine.SERVICE : + IDManager.ServiceID.ServiceIDDefinition serviceIdDef = IDManager.ServiceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setService(serviceIdDef.getName()) + .build() + ).build(); + break; + case DefaultScopeDefine.SERVICE_INSTANCE : + IDManager.ServiceInstanceID.InstanceIDDefinition instanceIdDef = IDManager.ServiceInstanceID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setServiceInstance(instanceIdDef.getName()) + .build() + ).build(); + break; + case DefaultScopeDefine.ENDPOINT : + IDManager.EndpointID.EndpointIDDefinition endpointIDDef = IDManager.EndpointID.analysisId(msg.getId0()); + event = event.toBuilder().setSource( + Source.newBuilder() + .setEndpoint(endpointIDDef.getEndpointName()) Review comment: For endpoint, you need to set the service name as well -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
