|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/go-faster/errors" |
| 13 | + "github.com/schollz/progressbar/v3" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/go-faster/oteldb/internal/promapi" |
| 17 | + "github.com/go-faster/oteldb/internal/promproxy" |
| 18 | +) |
| 19 | + |
| 20 | +type PromQL struct { |
| 21 | + Addr string |
| 22 | + Input string |
| 23 | + RequestTimeout time.Duration |
| 24 | + |
| 25 | + client *promapi.Client |
| 26 | +} |
| 27 | + |
| 28 | +func (p *PromQL) Setup() error { |
| 29 | + var err error |
| 30 | + p.client, err = promapi.NewClient(p.Addr) |
| 31 | + if err != nil { |
| 32 | + return errors.Wrap(err, "create client") |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +func toPrometheusTimestamp(t time.Time) promapi.PrometheusTimestamp { |
| 38 | + return promapi.PrometheusTimestamp(strconv.FormatInt(t.Unix(), 10)) |
| 39 | +} |
| 40 | + |
| 41 | +func (p *PromQL) sendRangeQuery(ctx context.Context, q promproxy.RangeQuery) error { |
| 42 | + if _, err := p.client.GetQueryRange(ctx, promapi.GetQueryRangeParams{ |
| 43 | + Query: q.Query, |
| 44 | + Step: strconv.Itoa(q.Step), |
| 45 | + Start: toPrometheusTimestamp(q.Start), |
| 46 | + End: toPrometheusTimestamp(q.End), |
| 47 | + }); err != nil { |
| 48 | + return errors.Wrap(err, "get query range") |
| 49 | + } |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func toOptPrometheusTimestamp(t promproxy.OptDateTime) promapi.OptPrometheusTimestamp { |
| 54 | + if !t.IsSet() { |
| 55 | + return promapi.OptPrometheusTimestamp{} |
| 56 | + } |
| 57 | + return promapi.NewOptPrometheusTimestamp(toPrometheusTimestamp(t.Value)) |
| 58 | +} |
| 59 | + |
| 60 | +func (p *PromQL) sendInstantQuery(ctx context.Context, q promproxy.InstantQuery) error { |
| 61 | + if _, err := p.client.GetQuery(ctx, promapi.GetQueryParams{ |
| 62 | + Query: q.Query, |
| 63 | + Time: toOptPrometheusTimestamp(q.Time), |
| 64 | + }); err != nil { |
| 65 | + return errors.Wrap(err, "get query") |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (p *PromQL) sendSeriesQuery(ctx context.Context, query promproxy.SeriesQuery) error { |
| 71 | + if _, err := p.client.GetSeries(ctx, promapi.GetSeriesParams{ |
| 72 | + Start: toOptPrometheusTimestamp(query.Start), |
| 73 | + End: toOptPrometheusTimestamp(query.End), |
| 74 | + Match: query.Matchers, |
| 75 | + }); err != nil { |
| 76 | + return errors.Wrap(err, "get series") |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (p *PromQL) send(ctx context.Context, q promproxy.Query) error { |
| 82 | + ctx, cancel := context.WithTimeout(ctx, p.RequestTimeout) |
| 83 | + defer cancel() |
| 84 | + switch q.Type { |
| 85 | + case promproxy.InstantQueryQuery: |
| 86 | + return p.sendInstantQuery(ctx, q.InstantQuery) |
| 87 | + case promproxy.RangeQueryQuery: |
| 88 | + return p.sendRangeQuery(ctx, q.RangeQuery) |
| 89 | + case promproxy.SeriesQueryQuery: |
| 90 | + return p.sendSeriesQuery(ctx, q.SeriesQuery) |
| 91 | + default: |
| 92 | + return errors.Errorf("unknown query type %q", q.Type) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (p *PromQL) each(ctx context.Context, fn func(ctx context.Context, q promproxy.Query) error) error { |
| 97 | + f, err := os.Open(p.Input) |
| 98 | + if err != nil { |
| 99 | + return errors.Wrap(err, "read") |
| 100 | + } |
| 101 | + defer func() { |
| 102 | + _ = f.Close() |
| 103 | + }() |
| 104 | + d := json.NewDecoder(f) |
| 105 | + for { |
| 106 | + var q promproxy.Query |
| 107 | + if err := d.Decode(&q); err != nil { |
| 108 | + if errors.Is(err, io.EOF) { |
| 109 | + break |
| 110 | + } |
| 111 | + return errors.Wrap(err, "decode query") |
| 112 | + } |
| 113 | + if err := fn(ctx, q); err != nil { |
| 114 | + return errors.Wrap(err, "callback") |
| 115 | + } |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (p *PromQL) Run(ctx context.Context) error { |
| 121 | + fmt.Println("sending", p.Input, "to", p.Addr) |
| 122 | + var total int64 |
| 123 | + if err := p.each(ctx, func(ctx context.Context, q promproxy.Query) error { |
| 124 | + total++ |
| 125 | + return nil |
| 126 | + }); err != nil { |
| 127 | + return errors.Wrap(err, "count total") |
| 128 | + } |
| 129 | + pb := progressbar.Default(total) |
| 130 | + start := time.Now() |
| 131 | + if err := p.each(ctx, func(ctx context.Context, q promproxy.Query) error { |
| 132 | + if err := p.send(ctx, q); err != nil { |
| 133 | + return errors.Wrap(err, "send") |
| 134 | + } |
| 135 | + if err := pb.Add(1); err != nil { |
| 136 | + return errors.Wrap(err, "update progress bar") |
| 137 | + } |
| 138 | + return nil |
| 139 | + }); err != nil { |
| 140 | + _ = pb.Exit() |
| 141 | + return errors.Wrap(err, "send") |
| 142 | + } |
| 143 | + if err := pb.Finish(); err != nil { |
| 144 | + return errors.Wrap(err, "finish progress bar") |
| 145 | + } |
| 146 | + fmt.Println("done in", time.Since(start).Round(time.Millisecond)) |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func newPromQLCommand() *cobra.Command { |
| 151 | + p := &PromQL{} |
| 152 | + cmd := &cobra.Command{ |
| 153 | + Use: "promql", |
| 154 | + Short: "Run promql queries", |
| 155 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 156 | + if err := p.Setup(); err != nil { |
| 157 | + return errors.Wrap(err, "setup") |
| 158 | + } |
| 159 | + return p.Run(cmd.Context()) |
| 160 | + }, |
| 161 | + } |
| 162 | + f := cmd.Flags() |
| 163 | + f.StringVar(&p.Addr, "addr", "http://localhost:9090", "Prometheus address") |
| 164 | + f.StringVarP(&p.Input, "input", "i", "queries.jsonl", "Input file") |
| 165 | + f.DurationVar(&p.RequestTimeout, "request-timeout", time.Second*10, "Request timeout") |
| 166 | + return cmd |
| 167 | +} |
0 commit comments