Skip to content

Commit e19b112

Browse files
authored
Add ready command to profilecli (#3497)
1 parent 6ce2da6 commit e19b112

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

cmd/profilecli/main.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func main() {
8989
bucketWebCmd := bucketCmd.Command("web", "Run the web tool for visualizing blocks in object-store buckets.")
9090
bucketWebParams := addBucketWebToolParams(bucketWebCmd)
9191

92+
readyCmd := app.Command("ready", "Check Pyroscope health.")
93+
readyParams := addReadyParams(readyCmd)
94+
9295
// parse command line arguments
9396
parsedCmd := kingpin.MustParse(app.Parse(os.Args[1:]))
9497

@@ -151,17 +154,26 @@ func main() {
151154
if err := blocksCompact(ctx, cfg.blocks.compact.src, cfg.blocks.compact.dst, cfg.blocks.compact.shards); err != nil {
152155
os.Exit(checkError(err))
153156
}
157+
case readyCmd.FullCommand():
158+
if err := ready(ctx, readyParams); err != nil {
159+
os.Exit(checkError(err))
160+
}
154161
default:
155162
level.Error(logger).Log("msg", "unknown command", "cmd", parsedCmd)
156163
}
157164
}
158165

159166
func checkError(err error) int {
160-
if err != nil {
167+
switch err {
168+
case nil:
169+
return 0
170+
case notReadyErr:
171+
// The reason for the failed ready is already logged, so just exit with
172+
// an error code.
173+
default:
161174
fmt.Fprintf(os.Stderr, "error: %v\n", err)
162-
return 1
163175
}
164-
return 0
176+
return 1
165177
}
166178

167179
type contextKey uint8

cmd/profilecli/ready.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
9+
"github.com/go-kit/log/level"
10+
"github.com/pkg/errors"
11+
)
12+
13+
var (
14+
// Occurs when Pyroscope is not ready.
15+
notReadyErr = errors.New("not ready")
16+
)
17+
18+
type readyParams struct {
19+
*phlareClient
20+
}
21+
22+
func addReadyParams(cmd commander) *readyParams {
23+
params := &readyParams{}
24+
params.phlareClient = addPhlareClient(cmd)
25+
26+
return params
27+
}
28+
29+
func ready(ctx context.Context, params *readyParams) error {
30+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/ready", params.URL), nil)
31+
if err != nil {
32+
return err
33+
}
34+
req = req.WithContext(ctx)
35+
36+
client := params.phlareClient.httpClient()
37+
res, err := client.Do(req)
38+
if err != nil {
39+
return err
40+
}
41+
defer res.Body.Close()
42+
43+
bytes, err := io.ReadAll(res.Body)
44+
if err != nil {
45+
return err
46+
}
47+
48+
if res.StatusCode != http.StatusOK {
49+
level.Error(logger).Log("msg", "not ready", "status", res.Status, "body", string(bytes))
50+
return notReadyErr
51+
}
52+
53+
level.Info(logger).Log("msg", "ready")
54+
return nil
55+
}

0 commit comments

Comments
 (0)