Skip to content

Commit

Permalink
feat: add authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
fungaren committed Dec 4, 2024
1 parent 9d7c770 commit abe7ba5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ This is a live graph from [metrics.sr.ht](https://metrics.sr.ht)
## Running the daemon

```
$ go build -o chartsrv main.go
$ ./chartsrv https://prometheus.example.org
$ go run main.go https://prometheus.example.org
Listening on :8142
```

Expand Down
30 changes: 23 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"image/color"
"io"
"log"
"math"
"net/http"
Expand Down Expand Up @@ -56,15 +57,24 @@ func Query(q string, start time.Time, end time.Time, step int) ([]PromResult, er
body.Set("start", fmt.Sprintf("%d", start.Unix()))
body.Set("end", fmt.Sprintf("%d", end.Unix()))
body.Set("step", fmt.Sprintf("%d", step))
resp, err := http.Post(fmt.Sprintf("%s/api/v1/query_range", upstream),
"application/x-www-form-urlencoded", strings.NewReader(body.Encode()))
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/query_range", upstream), strings.NewReader(body.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
auth := os.Getenv("AUTH")
if auth != "" {
req.Header.Set("Authorization", auth)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return nil, fmt.Errorf("Received %d response from upstream", resp.StatusCode)
data, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("received %d response from upstream: %s", resp.StatusCode, string(data))
}

var data PromResponse
Expand All @@ -78,7 +88,7 @@ func Query(q string, start time.Time, end time.Time, step int) ([]PromResult, er
}

if len(data.Data.Result) == 0 {
return nil, fmt.Errorf("No data")
return nil, fmt.Errorf("no data")
}

var results []PromResult
Expand Down Expand Up @@ -223,7 +233,13 @@ func registerExtension(router chi.Router, extension string, mime string) {
}
p.Legend.Top = true

sums := make([]float64, len(data[0].Values))
maxLen := 0
for _, item := range data {
if len(item.Values) > maxLen {
maxLen = len(item.Values)
}
}
sums := make([]float64, maxLen)

plotters := make([]plot.Plotter, len(data))
var nextColor int
Expand All @@ -236,8 +252,8 @@ func registerExtension(router chi.Router, extension string, mime string) {
value += sums[j]
}
points = append(points, plotter.XY{
float64(d.Time.Unix()),
value,
X: float64(d.Time.Unix()),
Y: value,
})
sums[j] += d.Value
}
Expand Down

0 comments on commit abe7ba5

Please sign in to comment.