-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ready command to profilecli (#3497)
- Loading branch information
1 parent
6ce2da6
commit e19b112
Showing
2 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |