visit.go 6.4 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package visit

import (
	"encoding/json"
黄孟柱 已提交
5 6
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
E
eoLinker API Management 已提交
7 8 9
	"strconv"
	"time"

黄孟柱 已提交
10
	node_common "github.com/eolinker/goku-api-gateway/goku-node/node-common"
E
eoLinker API Management 已提交
11

Y
Your Name 已提交
12
	redis_manager "github.com/eolinker/goku-api-gateway/common/redis-manager"
黄孟柱 已提交
13 14 15
	cmd2 "github.com/eolinker/goku-api-gateway/goku-node/cmd"
	gateway_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/gateway-manager"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
16 17 18 19 20 21 22 23 24
	jsoniter "github.com/json-iterator/go"
)

type alertInfo struct {
	AlertAddr       string `json:"alertAddr"`
	AlertPeriodType int    `json:"alertPeriodType"`
	ReceiveList     string `json:"receiveList"`
}

Y
Your Name 已提交
25 26
//UpdateProxyFailureCount 更新网关转发失败次数
func UpdateProxyFailureCount(apiInfo *entity.APIExtend,
E
eoLinker API Management 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	requestMethod string,
	proxyMethod string,
	headers map[string][]string,
	queryParams map[string][]string,
	formParams map[string][]string,
	responseStatus int,
	responseHeader map[string][]string,

	ctx *common.Context) (bool, string, error) {
	redisConn := redis_manager.GetConnection()
	var (
		alertAddress    string
		alertStatus     int
		alertPeriodType int
	)
	alertStatus = gateway_manager.GetAlertStatus()

	if alertStatus == 0 || apiInfo.AlertValve == 0 {
		return true, "", nil
	} else {
		info := gateway_manager.GetAlertInfo()
		alertJson := alertInfo{}
		err := json.Unmarshal([]byte(info), &alertJson)
		if err != nil {
			return false, err.Error(), err
		}

		clusterName := node_common.ClusterName()
		alertPeriodType = alertJson.AlertPeriodType
		alertAddress = alertJson.AlertAddr
		for {
			rep, err := redisConn.SetNX(clusterName+":lock", 1, 3*time.Second).Result()
			if err != nil {
				log.Info(err)
				return false, "", err
			}
			if rep {
				break
			}
		}

		// 获取当前告警时间信息
Y
Your Name 已提交
69
		redisKey := clusterName + ":gokuFailureCount:" + strconv.Itoa(apiInfo.APIID)
E
eoLinker API Management 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		// 获取key信息
		result, err := redisConn.Get(redisKey).Result()
		if err != nil {
			if err.Error() != "redis: nil" {
				log.Info(err)
				return false, "", err
			}
		}
		var expire int = 60
		if alertPeriodType == 1 {
			expire = 5 * 60
		} else if alertPeriodType == 2 {
			expire = 15 * 60
		} else if alertPeriodType == 3 {
			expire = 30 * 60
		} else if alertPeriodType == 4 {
			expire = 60 * 60
		}

		var json = jsoniter.ConfigCompatibleWithStandardLibrary
		headerList, err := json.Marshal(headers)
		if err != nil {
			log.Info(err)
			return false, "", err
		}
		queryParamList, err := json.Marshal(queryParams)
		if err != nil {
			log.Info(err)
			return false, "", err
		}
		formParamsList, err := json.Marshal(formParams)
		if err != nil {
			log.Info(err)
			return false, "", err
		}
		responseHeaderList, err := json.Marshal(responseHeader)
		if err != nil {
			log.Info(err)
			return false, "", err
		}

		var count int = 0

		if result == "" {
			_, err = redisConn.SetNX(redisKey, 1, time.Duration(expire)*time.Second).Result()
			if err != nil {
				log.Info(err)

				return false, "", err
			}
			count = 1
		} else {
			count, err = strconv.Atoi(result)
			if err != nil {
				log.Info(err)

				return false, "", err
			}
		}

		if count >= apiInfo.AlertValve {

Y
Your Name 已提交
132 133
			_, _, _ = cmd2.AddAlertMessage(apiInfo.APIID,
				apiInfo.APIName,
E
eoLinker API Management 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				apiInfo.RequestURL,
				ctx.FinalTargetServer(),
				apiInfo.TargetURL,
				requestMethod,
				proxyMethod,
				string(headerList),
				string(queryParamList),
				string(formParamsList),
				string(responseHeaderList),
				alertPeriodType,
				apiInfo.AlertValve,
				responseStatus,
				"true",
				ctx.StrategyId(),
				ctx.StrategyName(),
				ctx.RequestId())
			AlertLog(
				apiInfo.RequestURL,
				ctx.FinalTargetServer(),
				apiInfo.TargetURL,
				requestMethod,
				proxyMethod,
				string(headerList),
				string(queryParamList),
				string(formParamsList),
				string(responseHeaderList),
				responseStatus,
				ctx.StrategyId(),
				ctx.StrategyName(),
				ctx.RequestId())

			period := "1"
			if alertPeriodType == 1 {
				period = "5"
			} else if alertPeriodType == 2 {
				period = "15"
			} else if alertPeriodType == 3 {
				period = "30"
			} else if alertPeriodType == 4 {
				period = "60"
			}
			msg := "[Alert] GoKu Gateway failed to proxy requests for " + period + " minute " + strconv.Itoa(apiInfo.AlertValve) + " times"
Y
Your Name 已提交
176
			cmd2.SendRequestToAlertAddress(alertAddress, apiInfo.RequestURL, ctx.FinalTargetServer(), apiInfo.TargetURL, msg, apiInfo.APIName, apiInfo.APIID)
E
eoLinker API Management 已提交
177 178 179 180 181 182 183

			_, err = redisConn.Set(redisKey, 1, time.Duration(expire)*time.Second).Result()
			if err != nil {
				log.Info(err)
			}
		} else {
			_, _, _ = cmd2.AddAlertMessage(
Y
Your Name 已提交
184 185
				apiInfo.APIID,
				apiInfo.APIName,
E
eoLinker API Management 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
				apiInfo.RequestURL,
				ctx.FinalTargetServer(),
				apiInfo.TargetURL,
				requestMethod,
				proxyMethod,
				string(headerList),
				string(queryParamList),
				string(formParamsList),
				string(responseHeaderList),
				alertPeriodType,
				apiInfo.AlertValve,
				responseStatus,
				"false",
				ctx.StrategyId(),
				ctx.StrategyName(),
				ctx.RequestId())
			AlertLog(
				apiInfo.RequestURL,
				ctx.FinalTargetServer(),
				apiInfo.TargetURL,
				apiInfo.RequestMethod,
				apiInfo.TargetMethod,
				string(headerList),
				string(queryParamList),
				string(formParamsList),
				string(responseHeaderList),
				responseStatus,
				ctx.StrategyId(),
				ctx.StrategyName(),
				ctx.RequestId())
			if _, err := redisConn.Incr(redisKey).Result(); err != nil {

				return false, err.Error(), err
			}
		}
		redisConn.Del("lock")
		return true, "", nil
	}
}

Y
Your Name 已提交
226
//AlertLog 记录告警日志
E
eoLinker API Management 已提交
227
func AlertLog(requestURL, targetServer, targetURL, requestMethod, proxyMethod, headerList, queryParamList, formParamList, responseHeaderList string, responseStatus int, strategyID string, strategyName, requestID string) {
Y
Your Name 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241
	log.WithFields(log.Fields{
		"request_id":          requestID,
		"strategy_name":       strategyName,
		"strategy_id":         strategyID,
		"request_method":      requestMethod,
		"request_url":         requestURL,
		"target_method":       proxyMethod,
		"target_server":       targetServer,
		"target_url":          targetURL,
		"request_query":       queryParamList,
		"request_header":      headerList,
		"request_form_param":  formParamList,
		"response_statusCode": strconv.Itoa(responseStatus),
		"response_header":     responseHeaderList,
E
eoLinker API Management 已提交
242 243
	}).Warning("alert")

Y
Your Name 已提交
244
	//_= logutils.Log("log/alertLog", "alert", log.PeriodDay, logInfo)
E
eoLinker API Management 已提交
245 246 247

	return
}