github.com/grafana/pyroscope@v1.18.0/cmd/profilecli/ready.go (about)

     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  }