Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ccfos/nightingale
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Nov 15, 2023
2 parents ee5322f + 60a2e0c commit f201b12
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 64 deletions.
1 change: 1 addition & 0 deletions fe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ GOPATH=$(go env GOPATH)
GOPATH=${GOPATH:-/home/runner/go}

# Embed files into a go binary
# go install github.com/rakyll/statik
if ! $GOPATH/bin/statik -src=./pub -dest=./front; then
echo "failed to embed files into a go binary!"
exit 4
Expand Down
75 changes: 43 additions & 32 deletions pkg/prom/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net/http"
"strings"

"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
Expand Down Expand Up @@ -48,47 +49,57 @@ func (w WriterType) Write(items []prompb.TimeSeries, headers ...map[string]strin
}

func (w WriterType) Post(req []byte, headers ...map[string]string) error {
httpReq, err := http.NewRequest("POST", w.Opts.Url, bytes.NewReader(req))
if err != nil {
logger.Warningf("create remote write request got error: %s", err.Error())
return err
}
urls := strings.Split(w.Opts.Url, ",")
var err error
var httpReq *http.Request

for _, url := range urls {
httpReq, err = http.NewRequest("POST", url, bytes.NewReader(req))
if err != nil {
logger.Warningf("create remote write:%s request got error: %s", url, err.Error())
continue
}

httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("User-Agent", "n9e")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("User-Agent", "n9e")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")

if len(headers) > 0 {
for k, v := range headers[0] {
httpReq.Header.Set(k, v)
if len(headers) > 0 {
for k, v := range headers[0] {
httpReq.Header.Set(k, v)
}
}
}

if w.Opts.BasicAuthUser != "" {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}
if w.Opts.BasicAuthUser != "" {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}

headerCount := len(w.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(w.Opts.Headers); i += 2 {
httpReq.Header.Add(w.Opts.Headers[i], w.Opts.Headers[i+1])
if w.Opts.Headers[i] == "Host" {
httpReq.Host = w.Opts.Headers[i+1]
headerCount := len(w.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(w.Opts.Headers); i += 2 {
httpReq.Header.Add(w.Opts.Headers[i], w.Opts.Headers[i+1])
if w.Opts.Headers[i] == "Host" {
httpReq.Host = w.Opts.Headers[i+1]
}
}
}
}

resp, body, err := w.Client.Do(context.Background(), httpReq)
if err != nil {
logger.Warningf("push data with remote write:%s request got error: %v, response body: %s", w.Opts.Url, err, string(body))
return err
}
resp, body, e := w.Client.Do(context.Background(), httpReq)
if e != nil {
logger.Warningf("push data with remote write:%s request got error: %v, response body: %s", url, e, string(body))
err = e
continue
}

if resp.StatusCode >= 400 {
err = fmt.Errorf("push data with remote write:%s request got status code: %v, response body: %s", url, resp.StatusCode, string(body))
logger.Warning(err)
continue
}

if resp.StatusCode >= 400 {
err = fmt.Errorf("push data with remote write:%s request got status code: %v, response body: %s", w.Opts.Url, resp.StatusCode, string(body))
return err
break
}

return nil
return err
}
74 changes: 42 additions & 32 deletions pushgw/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -79,49 +80,58 @@ func (w WriterType) Write(key string, items []prompb.TimeSeries, headers ...map[
}

func (w WriterType) Post(req []byte, headers ...map[string]string) error {
httpReq, err := http.NewRequest("POST", w.Opts.Url, bytes.NewReader(req))
if err != nil {
logger.Warningf("create remote write request got error: %s", err.Error())
return err
}
urls := strings.Split(w.Opts.Url, ",")
var err error
var httpReq *http.Request
for _, url := range urls {
httpReq, err = http.NewRequest("POST", url, bytes.NewReader(req))
if err != nil {
logger.Warningf("create remote write:%s request got error: %s", url, err.Error())
continue
}

httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("User-Agent", "n9e")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("User-Agent", "n9e")
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")

if len(headers) > 0 {
for k, v := range headers[0] {
httpReq.Header.Set(k, v)
if len(headers) > 0 {
for k, v := range headers[0] {
httpReq.Header.Set(k, v)
}
}
}

if w.Opts.BasicAuthUser != "" {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}
if w.Opts.BasicAuthUser != "" {
httpReq.SetBasicAuth(w.Opts.BasicAuthUser, w.Opts.BasicAuthPass)
}

headerCount := len(w.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(w.Opts.Headers); i += 2 {
httpReq.Header.Add(w.Opts.Headers[i], w.Opts.Headers[i+1])
if w.Opts.Headers[i] == "Host" {
httpReq.Host = w.Opts.Headers[i+1]
headerCount := len(w.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(w.Opts.Headers); i += 2 {
httpReq.Header.Add(w.Opts.Headers[i], w.Opts.Headers[i+1])
if w.Opts.Headers[i] == "Host" {
httpReq.Host = w.Opts.Headers[i+1]
}
}
}
}

resp, body, err := w.Client.Do(context.Background(), httpReq)
if err != nil {
logger.Warningf("push data with remote write request got error: %v, response body: %s", err, string(body))
return err
}
resp, body, e := w.Client.Do(context.Background(), httpReq)
if e != nil {
logger.Warningf("push data with remote write:%s request got error: %v, response body: %s", url, e, string(body))
err = e
continue
}

if resp.StatusCode >= 400 {
err = fmt.Errorf("push data with remote write request got status code: %v, response body: %s", resp.StatusCode, string(body))
return err
if resp.StatusCode >= 400 {
err = fmt.Errorf("push data with remote write:%s request got status code: %v, response body: %s", url, resp.StatusCode, string(body))
logger.Warning(err)
continue
}

break
}

return nil
return err
}

type WritersType struct {
Expand Down

0 comments on commit f201b12

Please sign in to comment.