Skip to content

Commit 1093622

Browse files
authored
chore: print error message when sending webhook failed (#770)
* chore: print error message when sending webhook failed * add param into webhook * render webhook param as template * render the webhook param in order * add syslog support in webhook mock --------- Co-authored-by: rick <[email protected]>
1 parent 77b3e73 commit 1093622

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

docs/site/content/zh/latest/tasks/mock.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,11 @@ proxies:
196196

197197
当前代理支持 HTTP 和 TCP 协议,上面的例子中代理了 MySQL 的 `33060` 端口。
198198

199+
## Webhook
200+
201+
有些场景下,需要定时向服务器发送请求,这时可以使用 Webhook。当前支持的协议包括:
202+
203+
* HTTP
204+
* Syslog
205+
199206
> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
webhooks:
2+
- timer: 3s
3+
name: syslog
4+
request:
5+
protocol: syslog
6+
path: 192.168.123.58:5140
7+
body: |
8+
{
9+
"level": "{{ randEnum "FATAL" "ERROR" "WARNING" "INFO" }}",
10+
"message": "{{ randAlphaNum 10 }}"
11+
}

pkg/mock/in_memory.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"net"
2626
"net/http"
27+
"sort"
2728
"strings"
2829
"sync"
2930
"time"
@@ -507,23 +508,57 @@ func (s *inMemoryServer) startWebhook(webhook *Webhook) (err error) {
507508
}
508509

509510
func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error) {
510-
client := http.DefaultClient
511+
rawParams := make(map[string]string, len(wh.Param))
512+
paramKeys := make([]string, 0, len(wh.Param))
513+
for k, v := range wh.Param {
514+
paramKeys = append(paramKeys, k)
515+
rawParams[k] = v
516+
}
517+
sort.Strings(paramKeys)
518+
519+
for _, k := range paramKeys {
520+
v, vErr := render.Render("mock webhook server param", wh.Param[k], wh)
521+
if vErr == nil {
522+
wh.Param[k] = v
523+
}
524+
}
511525

512526
var payload io.Reader
513527
payload, err = render.RenderAsReader("mock webhook server payload", wh.Request.Body, wh)
514528
if err != nil {
515529
err = fmt.Errorf("error when render payload: %w", err)
516530
return
517531
}
532+
wh.Param = rawParams
518533

519-
method := util.EmptyThenDefault(wh.Request.Method, http.MethodPost)
520534
var api string
521535
api, err = render.Render("webhook request api", wh.Request.Path, objCtx)
522536
if err != nil {
523537
err = fmt.Errorf("error when render api: %w, template: %s", err, wh.Request.Path)
524538
return
525539
}
526540

541+
switch wh.Request.Protocol {
542+
case "syslog":
543+
err = sendSyslogWebhookRequest(ctx, wh, api, payload)
544+
default:
545+
err = sendHTTPWebhookRequest(ctx, wh, api, payload)
546+
}
547+
return
548+
}
549+
550+
func sendSyslogWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
551+
var conn net.Conn
552+
if conn, err = net.Dial("udp", api); err == nil {
553+
_, err = io.Copy(conn, payload)
554+
}
555+
return
556+
}
557+
558+
func sendHTTPWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
559+
method := util.EmptyThenDefault(wh.Request.Method, http.MethodPost)
560+
client := http.DefaultClient
561+
527562
var bearerToken string
528563
bearerToken, err = getBearerToken(ctx, wh.Request)
529564
if err != nil {
@@ -550,7 +585,7 @@ func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error
550585
memLogger.Info("send webhook request", "api", api)
551586
resp, err := client.Do(req)
552587
if err != nil {
553-
err = fmt.Errorf("error when sending webhook")
588+
err = fmt.Errorf("error when sending webhook: %v", err)
554589
} else {
555590
if resp.StatusCode != http.StatusOK {
556591
memLogger.Info("unexpected status", "code", resp.StatusCode)

pkg/mock/types.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ type Item struct {
2929
}
3030

3131
type Request struct {
32-
Path string `yaml:"path" json:"path"`
33-
Method string `yaml:"method" json:"method"`
34-
Header map[string]string `yaml:"header" json:"header"`
35-
Body string `yaml:"body" json:"body"`
32+
Protocol string `yaml:"protocol" json:"protocol"`
33+
Path string `yaml:"path" json:"path"`
34+
Method string `yaml:"method" json:"method"`
35+
Header map[string]string `yaml:"header" json:"header"`
36+
Body string `yaml:"body" json:"body"`
3637
}
3738

3839
type RequestWithAuth struct {
@@ -51,9 +52,10 @@ type Response struct {
5152
}
5253

5354
type Webhook struct {
54-
Name string `yaml:"name" json:"name"`
55-
Timer string `yaml:"timer" json:"timer"`
56-
Request RequestWithAuth `yaml:"request" json:"request"`
55+
Name string `yaml:"name" json:"name"`
56+
Timer string `yaml:"timer" json:"timer"`
57+
Param map[string]string `yaml:"param" json:"param"`
58+
Request RequestWithAuth `yaml:"request" json:"request"`
5759
}
5860

5961
type Proxy struct {

0 commit comments

Comments
 (0)