-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from yoyofx/dev
Dev
- Loading branch information
Showing
16 changed files
with
272 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package XLog | ||
|
||
import ( | ||
"fmt" | ||
logrus "github.com/sirupsen/logrus" | ||
"os" | ||
"time" | ||
) | ||
|
||
type LogrusLogger struct { | ||
logger *logrus.Logger | ||
dateFormat string | ||
class string | ||
} | ||
|
||
func NewLogger() ILogger { | ||
logger := logrus.New() | ||
return &LogrusLogger{logger: logger, dateFormat: LoggerDefaultDateFormat} | ||
} | ||
|
||
func GetClassLogger(class string) ILogger { | ||
logger := logrus.New() | ||
return &LogrusLogger{logger: logger, class: class, dateFormat: LoggerDefaultDateFormat} | ||
} | ||
|
||
func (log *LogrusLogger) log(level LogLevel, fiedls map[string]interface{}, format string, a ...interface{}) *logrus.Entry { | ||
hostName, _ := os.Hostname() | ||
message := format | ||
message = fmt.Sprintf(format, a...) | ||
start := time.Now() | ||
|
||
fieldsMap := make(map[string]interface{}) | ||
if fiedls != nil { | ||
fieldsMap = fiedls | ||
} | ||
fieldsMap["StartTime"] = start.Format(log.dateFormat) | ||
fieldsMap["Level"] = LevelString[level] | ||
fieldsMap["Class"] = log.class | ||
fieldsMap["Host"] = hostName | ||
fieldsMap["Message"] = message | ||
|
||
return logrus.WithFields(fieldsMap) | ||
} | ||
|
||
func (l LogrusLogger) Debug(format string, a ...interface{}) { | ||
panic("implement me") | ||
} | ||
|
||
func (l LogrusLogger) Info(format string, a ...interface{}) { | ||
panic("implement me") | ||
} | ||
|
||
func (l LogrusLogger) Warning(format string, a ...interface{}) { | ||
panic("implement me") | ||
} | ||
|
||
func (l LogrusLogger) Error(format string, a ...interface{}) { | ||
panic("implement me") | ||
} | ||
|
||
func (l LogrusLogger) SetCustomLogFormat(logFormatterFunc func(logInfo LogInfo) string) { | ||
panic("implement me") | ||
} | ||
|
||
func (l LogrusLogger) SetDateFormat(format string) { | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ type LogInfo struct { | |
Class string | ||
Host string | ||
Message string | ||
Extend map[string]interface{} | ||
} |
35 changes: 35 additions & 0 deletions
35
Examples/GrafanaAlertWebHook/WechatRequests/GrafanaAlertRequest.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package WechatRequests | ||
|
||
import "strconv" | ||
|
||
type GrafanaAlertRequest struct { | ||
PanelID int `json:"panelId" gorm:"column:panelId"` | ||
DashboardID int `json:"dashboardId" gorm:"column:dashboardId"` | ||
ImageUrl string `json:"imageUrl" gorm:"column:imageUrl"` | ||
RuleName string `json:"ruleName" gorm:"column:ruleName"` | ||
State string `json:"state" gorm:"column:state"` | ||
Message string `json:"message" gorm:"column:message"` | ||
RuleID int `json:"ruleId" gorm:"column:ruleId"` | ||
Title string `json:"title" gorm:"column:title"` | ||
RuleUrl string `json:"ruleUrl" gorm:"column:ruleUrl"` | ||
OrgID int `json:"orgId" gorm:"column:orgId"` | ||
|
||
EvalMatches []struct { | ||
Metric string `json:"metric" gorm:"column:metric"` | ||
Value int `json:"value" gorm:"column:value"` | ||
Tags struct{} `json:"tags" gorm:"column:tags"` | ||
} `json:"evalMatches" gorm:"column:evalMatches"` | ||
|
||
Tags map[string]string `json:"tags" gorm:"column:tags"` | ||
} | ||
|
||
func (request GrafanaAlertRequest) GetMetricValue() string { | ||
if len(request.EvalMatches) > 0 { | ||
return strconv.Itoa(request.EvalMatches[0].Value) | ||
} | ||
return "0" | ||
} | ||
|
||
func (request GrafanaAlertRequest) GetTag() string { | ||
return request.Tags["alert"] | ||
} |
8 changes: 8 additions & 0 deletions
8
Examples/GrafanaAlertWebHook/WechatRequests/MarkdownMessage.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package WechatRequests | ||
|
||
type MarkdownMessage struct { | ||
Markdown struct { | ||
Content string `json:"content" gorm:"column:content"` | ||
} `json:"markdown" gorm:"column:markdown"` | ||
Msgtype string `json:"msgtype" gorm:"column:msgtype"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package WechatRequests | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/yoyofx/yoyogo/Abstractions" | ||
"github.com/yoyofx/yoyogo/Abstractions/XLog" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func postWechatMessage(sendUrl, msg string) string { | ||
client := &http.Client{} | ||
req, _ := http.NewRequest("POST", sendUrl, bytes.NewBuffer([]byte(msg))) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("charset", "UTF-8") | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer resp.Body.Close() | ||
fmt.Println("response Status:", resp.Status) | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
strBody := string(body) | ||
fmt.Println("response Body:", strBody) | ||
return strBody | ||
} | ||
|
||
func SendTxtMessage(request GrafanaAlertRequest, config Abstractions.IConfiguration) string { | ||
tag := request.GetTag() | ||
logger := XLog.GetXLogger("wechat") | ||
js, _ := json.Marshal(request) | ||
logger.Info(string(js)) | ||
if tag == "" { | ||
logger.Info("no send") | ||
return "" | ||
} | ||
sendUrl := config.Get("alert.webhook_url").(string) | ||
linkUrl := config.Get(fmt.Sprintf("alert.%s.link_url", tag)).(string) | ||
var message *MarkdownMessage | ||
if request.State == "alerting" && len(request.EvalMatches) > 0 { | ||
message = &MarkdownMessage{ | ||
Markdown: struct { | ||
Content string `json:"content" gorm:"column:content"` | ||
}{ | ||
Content: request.RuleName + ",请相关同事注意。\n" + | ||
" > [报警次数]:<font color=\"warning\">" + request.GetMetricValue() + "次</font>" + "\n" + | ||
" > [报警明细](" + linkUrl + ")\n", | ||
}, | ||
Msgtype: "markdown", | ||
} | ||
} | ||
msg, _ := json.Marshal(message) | ||
msgStr := string(msg) | ||
logger.Info("send message:%s", msgStr) | ||
|
||
return postWechatMessage(sendUrl, msgStr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
yoyogo: | ||
application: | ||
name: Alert | ||
metadata: "" | ||
server: | ||
type: "fasthttp" | ||
address: ":8089" | ||
max_request_size: 2096157 | ||
|
||
# alert通过 tag识别节点值 | ||
alert: | ||
webhook_url: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=014f19d9-929e-4349-9b0f-ee57d868da9e | ||
jishi: | ||
link_url: http://jcenter-main.easypass.cn/jiankong/d/trpHG7FGk/che-hou-ye-wu-ri-zhi-cha-xun?orgId=1&from=now-1h&to=now&var-app=jishi*&var-level=error&var-host=All&var-msg=* | ||
znbk: | ||
link_url: http://jcenter-main.easypass.cn/jiankong/d/trpHG7FGk/che-hou-ye-wu-ri-zhi-cha-xun?orgId=1&from=now-1h&to=now&var-app=zhineng*&var-level=error&var-host=All&var-msg=* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"GrafanaAlertWebHook/WechatRequests" | ||
"github.com/yoyofx/yoyogo/Abstractions" | ||
"github.com/yoyofx/yoyogo/WebFramework/Context" | ||
) | ||
|
||
func PostAlert(ctx *Context.HttpContext) { | ||
var request WechatRequests.GrafanaAlertRequest | ||
_ = ctx.Bind(&request) | ||
var config Abstractions.IConfiguration | ||
_ = ctx.RequiredServices.GetService(&config) | ||
|
||
ctx.JSON(200, Context.H{ | ||
"Message": WechatRequests.SendTxtMessage(request, config), | ||
}) | ||
|
||
//var message WechatRequests.MarkdownMessage | ||
//if request.State == "alerting" && len(request.EvalMatches) > 0 { | ||
// message = WechatRequests.MarkdownMessage{ | ||
// Markdown: struct { | ||
// Content string `json:"content" gorm:"column:content"` | ||
// }{ | ||
// Content: request.RuleName + ",请相关同事注意。\n" + | ||
// " > [报警次数]:<font color=\"warning\">" + strconv.Itoa(request.EvalMatches[0].Value) + "次</font>" + "\n" + | ||
// " > [报警明细](http://jcenter-main.easypass.cn/jiankong/d/trpHG7FGk/che-hou-ye-wu-ri-zhi-cha-xun?orgId=1&from=now-1h&to=now&var-app=jishi*&var-level=error&var-host=All&var-msg=*)\n", | ||
// }, | ||
// Msgtype: "markdown", | ||
// } | ||
// //msg, _ := json.Marshal(message) | ||
// //sendUrl := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=efaebe93-7b21-4bc3-888f-260744f397ac" | ||
// //ctx.JSON(200, Context.H{ | ||
// // "Message": postWechatMessage(sendUrl,string(msg)), | ||
// //}) | ||
// ctx.JSON(200, message) | ||
//} | ||
//msg, _ := json.Marshal(message) | ||
// | ||
//ctx.JSON(200,Context.H{ | ||
// "Message":postWechatMessage(string(msg)) , | ||
//}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module GrafanaAlertWebHook | ||
|
||
go 1.15 | ||
|
||
require github.com/yoyofx/yoyogo v0.0.0 | ||
|
||
replace ( | ||
github.com/yoyofx/yoyogo => ../../ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/yoyofx/yoyogo/Abstractions" | ||
"github.com/yoyofx/yoyogo/WebFramework" | ||
"github.com/yoyofx/yoyogo/WebFramework/Router" | ||
) | ||
|
||
func main() { | ||
configuration := Abstractions.NewConfigurationBuilder().AddYamlFile("config").Build() | ||
// --profile=prod , to read , config.yml | ||
YoyoGo.NewWebHostBuilder(). | ||
UseConfiguration(configuration). | ||
Configure(func(app *YoyoGo.WebApplicationBuilder) { | ||
app.UseEndpoints(func(router Router.IRouterBuilder) { | ||
router.POST("/alert", PostAlert) | ||
}) | ||
}).Build().Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters