|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/bufbuild/connect-go" |
| 14 | + "github.com/go-kit/log/level" |
| 15 | + gprofile "github.com/google/pprof/profile" |
| 16 | + "github.com/grafana/dskit/runutil" |
| 17 | + "github.com/k0kubun/pp/v3" |
| 18 | + "github.com/klauspost/compress/gzip" |
| 19 | + "github.com/mattn/go-isatty" |
| 20 | + "github.com/pkg/errors" |
| 21 | + "github.com/prometheus/common/model" |
| 22 | + "gopkg.in/alecthomas/kingpin.v2" |
| 23 | + |
| 24 | + querierv1 "github.com/grafana/phlare/api/gen/proto/go/querier/v1" |
| 25 | + "github.com/grafana/phlare/api/gen/proto/go/querier/v1/querierv1connect" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + outputConsole = "console" |
| 30 | + outputRaw = "raw" |
| 31 | + outputPprof = "pprof=" |
| 32 | +) |
| 33 | + |
| 34 | +func parseTime(s string) (time.Time, error) { |
| 35 | + if s == "" { |
| 36 | + return time.Time{}, fmt.Errorf("empty time") |
| 37 | + } |
| 38 | + t, err := time.Parse(time.RFC3339, s) |
| 39 | + if err == nil { |
| 40 | + return t, nil |
| 41 | + } |
| 42 | + |
| 43 | + // try if it is a relative time |
| 44 | + d, rerr := parseRelativeTime(s) |
| 45 | + if rerr == nil { |
| 46 | + return time.Now().Add(-d), nil |
| 47 | + } |
| 48 | + |
| 49 | + // if not return first error |
| 50 | + return time.Time{}, err |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +func parseRelativeTime(s string) (time.Duration, error) { |
| 55 | + s = strings.TrimSpace(s) |
| 56 | + if s == "now" { |
| 57 | + return 0, nil |
| 58 | + } |
| 59 | + s = strings.TrimPrefix(s, "now-") |
| 60 | + |
| 61 | + d, err := model.ParseDuration(s) |
| 62 | + if err != nil { |
| 63 | + return 0, err |
| 64 | + } |
| 65 | + return time.Duration(d), nil |
| 66 | +} |
| 67 | + |
| 68 | +type queryParams struct { |
| 69 | + URL string |
| 70 | + From string |
| 71 | + To string |
| 72 | + ProfileType string |
| 73 | + Query string |
| 74 | +} |
| 75 | + |
| 76 | +func (p *queryParams) parseFromTo() (from time.Time, to time.Time, err error) { |
| 77 | + from, err = parseTime(p.From) |
| 78 | + if err != nil { |
| 79 | + return time.Time{}, time.Time{}, errors.Wrap(err, "failed to parse from") |
| 80 | + } |
| 81 | + to, err = parseTime(p.To) |
| 82 | + if err != nil { |
| 83 | + return time.Time{}, time.Time{}, errors.Wrap(err, "failed to parse to") |
| 84 | + } |
| 85 | + |
| 86 | + if to.Before(from) { |
| 87 | + return time.Time{}, time.Time{}, errors.Wrap(err, "from cannot be after") |
| 88 | + } |
| 89 | + |
| 90 | + return from, to, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (p *queryParams) client() querierv1connect.QuerierServiceClient { |
| 94 | + return querierv1connect.NewQuerierServiceClient( |
| 95 | + http.DefaultClient, |
| 96 | + p.URL, |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +type flagger interface { |
| 101 | + Flag(name, help string) *kingpin.FlagClause |
| 102 | +} |
| 103 | + |
| 104 | +func addQueryParams(queryCmd flagger) *queryParams { |
| 105 | + params := &queryParams{} |
| 106 | + queryCmd.Flag("url", "URL of the profile store.").Default("http://localhost:4100").StringVar(¶ms.URL) |
| 107 | + queryCmd.Flag("from", "Beginning of the query.").Default("now-1h").StringVar(¶ms.From) |
| 108 | + queryCmd.Flag("to", "End of the query.").Default("now").StringVar(¶ms.To) |
| 109 | + queryCmd.Flag("profile-type", "Profile type to query.").Default("process_cpu:cpu:nanoseconds:cpu:nanoseconds").StringVar(¶ms.ProfileType) |
| 110 | + queryCmd.Flag("query", "Label selector to query.").Default("{}").StringVar(¶ms.Query) |
| 111 | + return params |
| 112 | +} |
| 113 | + |
| 114 | +func queryMerge(ctx context.Context, params *queryParams, outputFlag string) (err error) { |
| 115 | + from, to, err := params.parseFromTo() |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + level.Info(logger).Log("msg", "query aggregated profile from profile store", "url", params.URL, "from", from, "to", to, "query", params.Query, "type", params.ProfileType) |
| 121 | + |
| 122 | + qc := params.client() |
| 123 | + |
| 124 | + resp, err := qc.SelectMergeProfile(ctx, connect.NewRequest(&querierv1.SelectMergeProfileRequest{ |
| 125 | + ProfileTypeID: params.ProfileType, |
| 126 | + Start: from.UnixMilli(), |
| 127 | + End: to.UnixMilli(), |
| 128 | + LabelSelector: params.Query, |
| 129 | + })) |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + return errors.Wrap(err, "failed to query") |
| 133 | + } |
| 134 | + |
| 135 | + mypp := pp.New() |
| 136 | + mypp.SetColoringEnabled(isatty.IsTerminal(os.Stdout.Fd())) |
| 137 | + mypp.SetExportedOnly(true) |
| 138 | + |
| 139 | + if outputFlag == outputConsole { |
| 140 | + buf, err := resp.Msg.MarshalVT() |
| 141 | + if err != nil { |
| 142 | + return errors.Wrap(err, "failed to marshal protobuf") |
| 143 | + } |
| 144 | + |
| 145 | + p, err := gprofile.Parse(bytes.NewReader(buf)) |
| 146 | + if err != nil { |
| 147 | + return errors.Wrap(err, "failed to parse profile") |
| 148 | + } |
| 149 | + |
| 150 | + fmt.Fprintln(output(ctx), p.String()) |
| 151 | + return nil |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + if outputFlag == outputRaw { |
| 156 | + mypp.Print(resp.Msg) |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + if strings.HasPrefix(outputFlag, outputPprof) { |
| 161 | + filePath := strings.TrimPrefix(outputFlag, outputPprof) |
| 162 | + if filePath == "" { |
| 163 | + return errors.New("no file path specified after pprof=") |
| 164 | + } |
| 165 | + buf, err := resp.Msg.MarshalVT() |
| 166 | + if err != nil { |
| 167 | + return errors.Wrap(err, "failed to marshal protobuf") |
| 168 | + } |
| 169 | + |
| 170 | + // open new file, fail when the file already exists |
| 171 | + f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644) |
| 172 | + if err != nil { |
| 173 | + return errors.Wrap(err, "failed to create pprof file") |
| 174 | + } |
| 175 | + defer runutil.CloseWithErrCapture(&err, f, "failed to close pprof file") |
| 176 | + |
| 177 | + gzipWriter := gzip.NewWriter(f) |
| 178 | + defer runutil.CloseWithErrCapture(&err, gzipWriter, "failed to close pprof gzip writer") |
| 179 | + |
| 180 | + if _, err := io.Copy(gzipWriter, bytes.NewReader(buf)); err != nil { |
| 181 | + return errors.Wrap(err, "failed to write pprof") |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | + } |
| 186 | + |
| 187 | + return errors.Errorf("unknown output %s", outputFlag) |
| 188 | +} |
0 commit comments