Skip to content

Commit

Permalink
Add ready command to profilecli (#3497)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhuhta authored Aug 20, 2024
1 parent 6ce2da6 commit e19b112
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
18 changes: 15 additions & 3 deletions cmd/profilecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func main() {
bucketWebCmd := bucketCmd.Command("web", "Run the web tool for visualizing blocks in object-store buckets.")
bucketWebParams := addBucketWebToolParams(bucketWebCmd)

readyCmd := app.Command("ready", "Check Pyroscope health.")
readyParams := addReadyParams(readyCmd)

// parse command line arguments
parsedCmd := kingpin.MustParse(app.Parse(os.Args[1:]))

Expand Down Expand Up @@ -151,17 +154,26 @@ func main() {
if err := blocksCompact(ctx, cfg.blocks.compact.src, cfg.blocks.compact.dst, cfg.blocks.compact.shards); err != nil {
os.Exit(checkError(err))
}
case readyCmd.FullCommand():
if err := ready(ctx, readyParams); err != nil {
os.Exit(checkError(err))
}
default:
level.Error(logger).Log("msg", "unknown command", "cmd", parsedCmd)
}
}

func checkError(err error) int {
if err != nil {
switch err {
case nil:
return 0
case notReadyErr:
// The reason for the failed ready is already logged, so just exit with
// an error code.
default:
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}
return 0
return 1
}

type contextKey uint8
Expand Down
55 changes: 55 additions & 0 deletions cmd/profilecli/ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"fmt"
"io"
"net/http"

"github.com/go-kit/log/level"
"github.com/pkg/errors"
)

var (
// Occurs when Pyroscope is not ready.
notReadyErr = errors.New("not ready")
)

type readyParams struct {
*phlareClient
}

func addReadyParams(cmd commander) *readyParams {
params := &readyParams{}
params.phlareClient = addPhlareClient(cmd)

return params
}

func ready(ctx context.Context, params *readyParams) error {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/ready", params.URL), nil)
if err != nil {
return err
}
req = req.WithContext(ctx)

client := params.phlareClient.httpClient()
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

bytes, err := io.ReadAll(res.Body)
if err != nil {
return err
}

if res.StatusCode != http.StatusOK {
level.Error(logger).Log("msg", "not ready", "status", res.Status, "body", string(bytes))
return notReadyErr
}

level.Info(logger).Log("msg", "ready")
return nil
}

0 comments on commit e19b112

Please sign in to comment.