mirror of
https://github.com/hanzoai/traces.git
synced 2026-10-30 18:20:41 +00:00
* tempo-cli: support relative time strings (now, now-1h) for start/end args Add support for relative time strings across all CLI commands that accept start/end arguments, matching Grafana's time string format. example: now-1h, now, now-30m, etc. Affected commands: - query api search - query api search-tags - query api search-tag-values - query api metrics query-range - query search Help text previously said "ISO8601 format" or showed a ISO 8601 format but the code was parsing with `time.RFC3339` (2006-01-02T15:04:05Z07:00). ISO 8601 is a broad standard that allows many representations including timestamps without timezone offsets. RFC 3339 is a strict profile of ISO 8601 that always requires a timezone offset (Z or +HH:MM). Help text is update to say RFC3339 with an example. We now accept a uniform time format across all commands: either RFC3339 absolute timestamps or relative time strings. query search: previously accepted timestamps without timezone (2024-03-15T10:30:00) using the layout "2006-01-02T15:04:05". This is valid ISO 8601 but not valid RFC 3339. These timestamps are now rejected. Users must include a timezone offset (e.g. 2024-03-15T10:30:00Z) or use relative time (e.g. now-1h). * update docs * Add CHANGELOG.md * trim spaces before parsing and add tests
123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"path"
|
|
|
|
"github.com/grafana/dskit/user"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/grafana/tempo/pkg/httpclient"
|
|
"github.com/grafana/tempo/pkg/tempopb"
|
|
)
|
|
|
|
type querySearchTagValuesCmd struct {
|
|
HostPort string `arg:"" help:"tempo host and port. scheme and path will be provided based on query type. e.g. localhost:3200"`
|
|
Tag string `arg:"" help:"tag name"`
|
|
Start string `arg:"" optional:"" help:"start time in RFC3339 (e.g. 2006-01-02T15:04:05Z07:00) or relative (e.g. now-1h) format"`
|
|
End string `arg:"" optional:"" help:"end time in RFC3339 (e.g. 2006-01-02T15:04:05Z07:00) or relative (e.g. now) format"`
|
|
|
|
Query string `help:"TraceQL query to filter attribute results by"`
|
|
OrgID string `help:"optional orgID"`
|
|
UseGRPC bool `help:"stream search results over GRPC"`
|
|
PathPrefix string `help:"string to prefix all http paths with"`
|
|
Secure bool `help:"use https or grpc with TLS"`
|
|
}
|
|
|
|
func (cmd *querySearchTagValuesCmd) Run(_ *globalOptions) error {
|
|
var start, end int64
|
|
|
|
if cmd.Start != "" {
|
|
startDate, err := parseTime(cmd.Start)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
start = startDate.Unix()
|
|
}
|
|
|
|
if cmd.End != "" {
|
|
endDate, err := parseTime(cmd.End)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
end = endDate.Unix()
|
|
}
|
|
|
|
if cmd.UseGRPC {
|
|
return cmd.searchGRPC(start, end)
|
|
}
|
|
return cmd.searchHTTP(start, end)
|
|
}
|
|
|
|
// nolint: goconst // goconst wants us to make http:// a const
|
|
func (cmd *querySearchTagValuesCmd) searchHTTP(start, end int64) error {
|
|
if cmd.PathPrefix != "" {
|
|
cmd.HostPort = path.Join(cmd.HostPort, cmd.PathPrefix)
|
|
}
|
|
client := httpclient.New(httpScheme(cmd.Secure)+"://"+cmd.HostPort, cmd.OrgID)
|
|
|
|
var tags *tempopb.SearchTagValuesV2Response
|
|
var err error
|
|
if start != 0 || end != 0 {
|
|
tags, err = client.SearchTagValuesV2WithRange(cmd.Tag, cmd.Query, start, end)
|
|
} else {
|
|
tags, err = client.SearchTagValuesV2(cmd.Tag, cmd.Query)
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return printAsJSON(tags)
|
|
}
|
|
|
|
func (cmd *querySearchTagValuesCmd) searchGRPC(start, end int64) error {
|
|
ctx := user.InjectOrgID(context.Background(), cmd.OrgID)
|
|
ctx, err := user.InjectIntoGRPCRequest(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
creds, err := grpcTransportCredentials(cmd.Secure)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
clientConn, err := grpc.NewClient(cmd.HostPort, creds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := tempopb.NewStreamingQuerierClient(clientConn)
|
|
|
|
tagsRequest := &tempopb.SearchTagValuesRequest{
|
|
TagName: cmd.Tag,
|
|
Start: uint32(start),
|
|
End: uint32(end),
|
|
Query: cmd.Query,
|
|
}
|
|
|
|
resp, err := client.SearchTagValuesV2(ctx, tagsRequest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for {
|
|
searchResp, err := resp.Recv()
|
|
if searchResp != nil {
|
|
err = printAsJSON(searchResp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|