diff --git a/cmd/otelq/README.md b/cmd/otelq/README.md new file mode 100644 index 0000000..31fd6c3 --- /dev/null +++ b/cmd/otelq/README.md @@ -0,0 +1,3 @@ +# Otelq + +Like `jq` or `yq`, but for OpenTelemetry data. diff --git a/cmd/otelq/main.go b/cmd/otelq/main.go new file mode 100644 index 0000000..f4332ce --- /dev/null +++ b/cmd/otelq/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "flag" + "fmt" + "io" + "log" + "os" + + tracev1 "go.opentelemetry.io/proto/otlp/trace/v1" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" +) + +var ( + file = flag.String("file", "", "OpenTelemetry proto file. if set, will not use stdin.") +) + +func main() { + flag.Parse() + + if *file != "" { + fmt.Fprintf(os.Stderr, "reading file %s\n", *file) + h := must(os.Open(*file)) + bs := must(io.ReadAll(h)) + + var td tracev1.TracesData + if err := proto.Unmarshal(bs, &td); err != nil { + log.Fatalln(err) + } + + jsonbs, err := protojson.Marshal(&td) + if err != nil { + log.Fatalln(err) + } + + fmt.Fprint(os.Stdout, string(jsonbs)) + } else { + fmt.Println("using stdin") + } +} + +func must[T any](v T, err error) T { + if err != nil { + log.Fatalln(err) + } + return v +} diff --git a/go.mod b/go.mod index 7aec69b..a86ed94 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module go.wperron.io/toolkit go 1.20 require ( + github.com/golang/protobuf v1.5.2 github.com/grafana/tempo v1.5.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/otel/sdk v1.14.0 @@ -14,7 +15,6 @@ require ( github.com/cenkalti/backoff/v4 v4.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect