kezhenxu94 commented on a change in pull request #5364: URL: https://github.com/apache/skywalking/pull/5364#discussion_r474446631
########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/slack/SlackhookCallback.java ########## @@ -0,0 +1,118 @@ +/* + * 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.slack; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import io.netty.handler.codec.http.HttpHeaderValues; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.List; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +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.alarm.provider.AlarmRulesWatcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Use SkyWalking alarm slack webhook API call a remote endpoints. + */ +public class SlackhookCallback implements AlarmCallback { + private static final Logger logger = LoggerFactory.getLogger(SlackhookCallback.class); + private static final int HTTP_CONNECT_TIMEOUT = 1000; + private static final int HTTP_CONNECTION_REQUEST_TIMEOUT = 1000; + private static final int HTTP_SOCKET_TIMEOUT = 10000; + private static final Gson GSON = new Gson(); + private AlarmRulesWatcher alarmRulesWatcher; + private RequestConfig requestConfig; + private List<String> webhooks; + + public SlackhookCallback(final AlarmRulesWatcher alarmRulesWatcher) { + this.alarmRulesWatcher = alarmRulesWatcher; + this.requestConfig = RequestConfig.custom() + .setConnectTimeout(HTTP_CONNECT_TIMEOUT) + .setConnectionRequestTimeout(HTTP_CONNECTION_REQUEST_TIMEOUT) + .setSocketTimeout(HTTP_SOCKET_TIMEOUT) + .build(); + this.webhooks = alarmRulesWatcher.getSlackSettings().getWebhooks(); Review comment: Don't extract this into a member variable, otherwise, the dynamic configuration mechanism won't work at line 69 ########## File path: oap-server/server-alarm-plugin/src/main/java/org/apache/skywalking/oap/server/core/alarm/provider/RulesReader.java ########## @@ -104,6 +108,24 @@ public Rules readRules() { rules.setGrpchookSetting(grpcAlarmSetting); } + + Map slacks = (Map) yamlData.get("slackHooks"); + if (slacks != null) { + SlackSettings slackSettings = new SlackSettings(); + Object textTemplate = slacks.getOrDefault("textTemplate", ""); + if (textTemplate != null) { Review comment: `slacks.getOrDefault` won't produces `null` ---------------------------------------------------------------- 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]
