wankai123 commented on code in PR #13570: URL: https://github.com/apache/skywalking/pull/13570#discussion_r2508611503
########## oap-server/server-query-plugin/status-query-plugin/src/main/java/org/apache/skywalking/oap/query/debug/AlarmStatusQueryService.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.query.debug; + +import com.google.gson.Gson; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.skywalking.oap.server.core.CoreModule; +import org.apache.skywalking.oap.server.core.alarm.AlarmModule; +import org.apache.skywalking.oap.server.core.alarm.AlarmRulesWatcherService; +import org.apache.skywalking.oap.server.core.alarm.AlarmStatusWatcherService; +import org.apache.skywalking.oap.server.core.alarm.provider.AlarmRulesWatcher; +import org.apache.skywalking.oap.server.core.alarm.provider.status.AlarmRuleDetail; +import org.apache.skywalking.oap.server.core.alarm.provider.status.AlarmRuleList; +import org.apache.skywalking.oap.server.core.alarm.provider.status.AlarmRunningContext; +import org.apache.skywalking.oap.server.core.alarm.provider.status.ClusterAlarmStatus; +import org.apache.skywalking.oap.server.core.alarm.provider.status.InstanceAlarmStatus; +import org.apache.skywalking.oap.server.core.remote.client.RemoteClient; +import org.apache.skywalking.oap.server.core.remote.client.RemoteClientManager; +import org.apache.skywalking.oap.server.core.remote.client.SelfRemoteClient; +import org.apache.skywalking.oap.server.core.remote.grpc.proto.AlarmRequest; +import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteServiceGrpc; +import org.apache.skywalking.oap.server.core.remote.grpc.proto.StatusRequest; +import org.apache.skywalking.oap.server.core.remote.grpc.proto.StatusResponse; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.apache.skywalking.oap.server.library.module.Service; + +@Slf4j +public class AlarmStatusQueryService implements Service { + private final ModuleManager moduleManager; + private final static Gson GSON = new Gson(); + private AlarmRulesWatcher alarmRulesWatcher; + private AlarmStatusWatcherService alarmStatusWatcher; + private RemoteClientManager remoteClientManager; + + public AlarmStatusQueryService(final ModuleManager manager) { + this.moduleManager = manager; + } + + private AlarmRulesWatcher getAlarmRulesWatcher() { + if (alarmRulesWatcher == null) { + alarmRulesWatcher = (AlarmRulesWatcher) moduleManager.find(AlarmModule.NAME) + .provider().getService(AlarmRulesWatcherService.class); + } + return alarmRulesWatcher; + } + + private AlarmStatusWatcherService getAlarmStatusWatcher() { + if (alarmStatusWatcher == null) { + alarmStatusWatcher = moduleManager.find(AlarmModule.NAME) + .provider().getService(AlarmStatusWatcherService.class); + } + return alarmStatusWatcher; + } + + private RemoteClientManager getRemoteClientManager() { + if (remoteClientManager == null) { + remoteClientManager = moduleManager.find(CoreModule.NAME) + .provider() + .getService(RemoteClientManager.class); + } + return remoteClientManager; + } + + public ClusterAlarmStatus<InstanceAlarmStatus<AlarmRuleList>> getAlarmRules() { + ClusterAlarmStatus<InstanceAlarmStatus<AlarmRuleList>> result = new ClusterAlarmStatus<>(); + List<RemoteClient> list = getRemoteClientManager().getRemoteClient(); + for (RemoteClient remoteClient : list) { + String rulesInfo = null; + String errorMsg = null; + try { + if (remoteClient instanceof SelfRemoteClient) { + rulesInfo = getAlarmStatusWatcher().getAlarmRules(); + } else { + // get from remote oap in the cluster + RemoteServiceGrpc.RemoteServiceBlockingStub stub = RemoteServiceGrpc.newBlockingStub( + remoteClient.getChannel()); + AlarmRequest alarmRequest = AlarmRequest.newBuilder() + .setRequestType(AlarmRequest.RequestType.GET_ALARM_RULES) + .build(); + StatusResponse statusResponse = stub.syncStatus( + StatusRequest.newBuilder().setAlarmRequest(alarmRequest).build()); + rulesInfo = statusResponse.getAlarmStatus(); + } + } catch (Exception e) { + log.warn("Failed to get alarm rule list.", e); + errorMsg = e.getMessage(); + } Review Comment: null is expected -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
