Skip to content

Commit e08cfa6

Browse files
committed
add short timeout for pushgateway error reporting
1 parent 2d93cb4 commit e08cfa6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

internal/errorhandler/errorhandler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errorhandler
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/base64"
67
"fmt"
78
"io/ioutil"
@@ -67,7 +68,16 @@ func (e *Pushgateway) Status(status error, message string, v ...interface{}) {
6768
base64.StdEncoding.EncodeToString([]byte(e.Namespace)),
6869
base64.StdEncoding.EncodeToString([]byte(e.Release)),
6970
)
70-
resp, err := http.Post(url, "text", bytes.NewReader(buffer.Bytes()))
71+
72+
ctx, cancel := context.WithCancel(context.TODO())
73+
time.AfterFunc(2*time.Second, cancel)
74+
req, err := http.NewRequest("POST", url, bytes.NewReader(buffer.Bytes()))
75+
if err != nil {
76+
log.Printf("unable to create request for pushgateway: %s", err)
77+
}
78+
req.WithContext(ctx)
79+
80+
resp, err := http.DefaultClient.Do(req)
7181
if err != nil {
7282
log.Printf("unable to push result to pushgateway host %q: %s", e.PushGatewayURL, err)
7383
} else if resp.StatusCode >= 400 {

0 commit comments

Comments
 (0)