Skip to content

Commit 230504a

Browse files
committed
Merge branch 'main' of ssh://github.com/moshebe/gtrace
2 parents 7997b82 + a341492 commit 230504a

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ gtrace get --project production-a,production-b 5e26a889fa12da351beee9ea16ce0a65
6565

6666
Format trace spans by a specific template:
6767
```shell
68-
gtrace format --input /tmp/trace.json --template "{{ .Name }}, {{ .Duration }}"
68+
gtrace format -f /tmp/trace.json --template "{{ .Name }}, {{ .Duration }}"
6969
```
7070

7171
Query traces by multiple filters from the last 3 hours:

internal/cli/cli.go

-7
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,3 @@ func read(path string) ([]byte, error) {
3838
}
3939
return os.ReadFile(path)
4040
}
41-
42-
func writer(path string) (io.WriteCloser, error) {
43-
if path == "-" {
44-
return os.Stdout, nil
45-
}
46-
return os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0660)
47-
}

internal/cli/format.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/moshebe/gtrace/pkg/span"
78
"github.com/urfave/cli/v2"
@@ -11,9 +12,9 @@ import (
1112

1213
var formatAction = func(c *cli.Context) error {
1314
format := c.String("template")
14-
input, output := c.String("input"), c.String("output")
15+
file := c.String("file")
1516

16-
in, err := read(input)
17+
in, err := read(file)
1718
if err != nil {
1819
return err
1920
}
@@ -24,13 +25,7 @@ var formatAction = func(c *cli.Context) error {
2425
return fmt.Errorf("unmarshal trace: %w", err)
2526
}
2627

27-
out, err := writer(output)
28-
if err != nil {
29-
return err
30-
}
31-
defer func() { _ = out.Close() }()
32-
33-
return span.Format(trace.Spans, format, out)
28+
return span.Format(trace.Spans, format, os.Stdout)
3429
}
3530

3631
var FormatCommand = &cli.Command{
@@ -41,17 +36,11 @@ var FormatCommand = &cli.Command{
4136
Action: formatAction,
4237
Flags: []cli.Flag{
4338
&cli.PathFlag{
44-
Name: "input",
45-
Aliases: []string{"i", "in"},
39+
Name: "file",
40+
Aliases: []string{"f"},
4641
Value: "-",
4742
Usage: "input file path. '-' means stdin",
4843
},
49-
&cli.PathFlag{
50-
Name: "output",
51-
Aliases: []string{"o", "out"},
52-
Value: "-",
53-
Usage: "output file path. '-' means stdout",
54-
},
5544
&cli.StringFlag{
5645
Name: "template",
5746
Value: "{{ .Name }} ({{ .Start }} - took {{ .Duration }})\n{{ if .Labels }}\t{{ .Labels }}\n{{ end }}",

internal/cli/get.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
var getAction = func(c *cli.Context) error {
1515
id := c.Args().First()
16-
output := c.Path("output")
1716
projects := stringSlice(c, "project")
1817

1918
if len(projects) == 0 {
@@ -23,12 +22,6 @@ var getAction = func(c *cli.Context) error {
2322
return fmt.Errorf("missing trace id")
2423
}
2524

26-
out, err := writer(output)
27-
if err != nil {
28-
return err
29-
}
30-
defer func() { _ = out.Close() }()
31-
3225
ctx, cancel := context.WithTimeout(c.Context, time.Minute)
3326
defer cancel()
3427

@@ -54,10 +47,7 @@ var getAction = func(c *cli.Context) error {
5447
return fmt.Errorf("marshal trace: %w", err)
5548
}
5649

57-
_, err = out.Write(traceJSON)
58-
if err != nil {
59-
return fmt.Errorf("write trace: %w", err)
60-
}
50+
fmt.Println(string(traceJSON))
6151

6252
return nil
6353
}
@@ -74,12 +64,6 @@ var GetCommand = &cli.Command{
7464
Aliases: []string{"p"},
7565
Usage: "the Google Cloud project ID to use for this invocation. values can be set multiple times or separated by comma",
7666
},
77-
&cli.PathFlag{
78-
Name: "output",
79-
Aliases: []string{"o", "out"},
80-
Value: "-",
81-
Usage: "output file path. '-' means stdout",
82-
},
8367
&cli.BoolFlag{
8468
Name: "pretty",
8569
Usage: "prettify JSON output",

internal/cli/list.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package cli
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
6-
"os"
7-
"text/tabwriter"
87

98
"github.com/moshebe/gtrace/pkg/span"
109
"github.com/moshebe/gtrace/pkg/tracer"
1110
"github.com/urfave/cli/v2"
1211
"google.golang.org/genproto/googleapis/devtools/cloudtrace/v1"
1312
)
1413

14+
type listResult struct {
15+
Span string `json:"name"`
16+
Traces []string `json:"traces"`
17+
}
18+
1519
var listAction = func(c *cli.Context) error {
1620
if !c.IsSet("project") {
1721
return fmt.Errorf("missing project")
@@ -58,18 +62,25 @@ var listAction = func(c *cli.Context) error {
5862

5963
rootSpans := span.ListRootSpans(traces)
6064

61-
w := tabwriter.NewWriter(os.Stdout, 0, 150, 10, '\t', tabwriter.AlignRight)
65+
results := make([]listResult, 0, len(rootSpans))
6266
for name, ids := range rootSpans {
63-
if len(ids) > 1 {
64-
_, err = fmt.Fprintf(w, "%s\t\t[%v + %d more...]\n", name, ids[0], len(ids)-1)
65-
} else {
66-
_, err = fmt.Fprintf(w, "%s\t\t%v\n", name, ids)
67-
}
68-
if err != nil {
69-
return err
70-
}
67+
results = append(results, listResult{
68+
Span: name,
69+
Traces: ids,
70+
})
71+
}
72+
73+
var output []byte
74+
if c.Bool("pretty") {
75+
output, err = json.MarshalIndent(results, "", "\t")
76+
} else {
77+
output, err = json.Marshal(results)
78+
}
79+
if err != nil {
80+
return fmt.Errorf("marshal results: %w", err)
7181
}
72-
_ = w.Flush()
82+
83+
fmt.Println(string(output))
7384

7485
return nil
7586
}
@@ -109,5 +120,9 @@ var ListCommand = &cli.Command{
109120
Layout: "2006-01-02T15:04:05",
110121
Usage: "end of the time interval (inclusive) during which the trace data was collected from the application",
111122
},
123+
&cli.BoolFlag{
124+
Name: "pretty",
125+
Usage: "prettify JSON output",
126+
},
112127
},
113128
}

0 commit comments

Comments
 (0)