Skip to content

Commit

Permalink
fix: support for +Inf, -Inf and NaN values in trace by id endpoints
Browse files Browse the repository at this point in the history
'encoding/json' package can't handle +Inf, -Inf, NaN values and will
fail to Marshal the response from tracebyid endpoints if response has keys with
values of +Inf, -Inf or NaN.

gogo/protobuf/jsonpb package correctly handles these values so use that to
Marshal and print the response to stdout
  • Loading branch information
electron0zero committed Sep 27, 2024
1 parent f3e704d commit b70fa15
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cmd/tempo-cli/cmd-query-trace-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"os"

"github.com/gogo/protobuf/jsonpb"
"github.com/grafana/tempo/pkg/httpclient"
"github.com/grafana/tempo/pkg/tempopb"
)

type queryTraceIDCmd struct {
Expand All @@ -19,25 +21,34 @@ func (cmd *queryTraceIDCmd) Run(_ *globalOptions) error {
client := httpclient.New(cmd.APIEndpoint, cmd.OrgID)
// util.QueryTrace will only add orgID header if len(orgID) > 0

var trace *tempopb.Trace
var err error
// use v1 API if specified, we default to v2
if cmd.V1 {
trace, err := client.QueryTrace(cmd.TraceID)
trace, err = client.QueryTrace(cmd.TraceID)
if err != nil {
return err
}
return printAsJSON(trace)
}

traceResp, err := client.QueryTraceV2(cmd.TraceID)
if err != nil {
return err
}
// log the Message and trace field
trace = traceResp.Trace // only print the trace field to stdout
if traceResp.Message != "" {
// print message and status to stderr if there is one.
// allows users to get a clean trace on the stdout, and pipe it to a file or another commands.
_, _ = fmt.Fprintf(os.Stderr, "status: %s , message: %s\n", traceResp.Status, traceResp.Message)
}
// only print the trace field
return printAsJSON(traceResp.Trace)

// tracebyid endpoints are protobuf, we are using 'gogo/protobuf/jsonpb' to marshal the
// trace to json because 'encoding/json' package can't handle +Inf, -Inf, NaN
marshaller := &jsonpb.Marshaler{}
err = marshaller.Marshal(os.Stdout, trace)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to marshal trace: %v\n", err)
}

return nil
}

0 comments on commit b70fa15

Please sign in to comment.