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