Skip to content

chore: print error message when sending webhook failed #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/site/content/zh/latest/tasks/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,11 @@ proxies:

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

## Webhook

有些场景下,需要定时向服务器发送请求,这时可以使用 Webhook。当前支持的协议包括:

* HTTP
* Syslog

> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
11 changes: 11 additions & 0 deletions docs/site/content/zh/latest/tasks/mock/webhook-syslog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
webhooks:
- timer: 3s
name: syslog
request:
protocol: syslog
path: 192.168.123.58:5140
body: |
{
"level": "{{ randEnum "FATAL" "ERROR" "WARNING" "INFO" }}",
"message": "{{ randAlphaNum 10 }}"
}
41 changes: 38 additions & 3 deletions pkg/mock/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"net"
"net/http"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -507,23 +508,57 @@ func (s *inMemoryServer) startWebhook(webhook *Webhook) (err error) {
}

func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error) {
client := http.DefaultClient
rawParams := make(map[string]string, len(wh.Param))
paramKeys := make([]string, 0, len(wh.Param))
for k, v := range wh.Param {
paramKeys = append(paramKeys, k)
rawParams[k] = v
}
sort.Strings(paramKeys)

for _, k := range paramKeys {
v, vErr := render.Render("mock webhook server param", wh.Param[k], wh)
if vErr == nil {
wh.Param[k] = v
}
}

var payload io.Reader
payload, err = render.RenderAsReader("mock webhook server payload", wh.Request.Body, wh)
if err != nil {
err = fmt.Errorf("error when render payload: %w", err)
return
}
wh.Param = rawParams

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

switch wh.Request.Protocol {
case "syslog":
err = sendSyslogWebhookRequest(ctx, wh, api, payload)
default:
err = sendHTTPWebhookRequest(ctx, wh, api, payload)
}
return
}

func sendSyslogWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
var conn net.Conn
if conn, err = net.Dial("udp", api); err == nil {
_, err = io.Copy(conn, payload)
}
return
}

func sendHTTPWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
method := util.EmptyThenDefault(wh.Request.Method, http.MethodPost)
client := http.DefaultClient

var bearerToken string
bearerToken, err = getBearerToken(ctx, wh.Request)
if err != nil {
Expand All @@ -550,7 +585,7 @@ func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error
memLogger.Info("send webhook request", "api", api)
resp, err := client.Do(req)
if err != nil {
err = fmt.Errorf("error when sending webhook")
err = fmt.Errorf("error when sending webhook: %v", err)
} else {
if resp.StatusCode != http.StatusOK {
memLogger.Info("unexpected status", "code", resp.StatusCode)
Expand Down
16 changes: 9 additions & 7 deletions pkg/mock/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type Item struct {
}

type Request struct {
Path string `yaml:"path" json:"path"`
Method string `yaml:"method" json:"method"`
Header map[string]string `yaml:"header" json:"header"`
Body string `yaml:"body" json:"body"`
Protocol string `yaml:"protocol" json:"protocol"`
Path string `yaml:"path" json:"path"`
Method string `yaml:"method" json:"method"`
Header map[string]string `yaml:"header" json:"header"`
Body string `yaml:"body" json:"body"`
}

type RequestWithAuth struct {
Expand All @@ -51,9 +52,10 @@ type Response struct {
}

type Webhook struct {
Name string `yaml:"name" json:"name"`
Timer string `yaml:"timer" json:"timer"`
Request RequestWithAuth `yaml:"request" json:"request"`
Name string `yaml:"name" json:"name"`
Timer string `yaml:"timer" json:"timer"`
Param map[string]string `yaml:"param" json:"param"`
Request RequestWithAuth `yaml:"request" json:"request"`
}

type Proxy struct {
Expand Down
Loading