Skip to content

Commit 1d8a5ae

Browse files
committed
history: add UI view to traces
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
1 parent 9b7410b commit 1d8a5ae

File tree

16 files changed

+466
-8
lines changed

16 files changed

+466
-8
lines changed

commands/history/trace.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
package history
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
7+
"fmt"
68
"io"
7-
"log"
9+
"net"
10+
"os"
811
"slices"
912
"time"
1013

14+
"github.com/containerd/console"
1115
"github.com/containerd/containerd/v2/core/content/proxy"
1216
"github.com/docker/buildx/builder"
1317
"github.com/docker/buildx/util/cobrautil/completion"
1418
"github.com/docker/buildx/util/otelutil"
19+
"github.com/docker/buildx/util/otelutil/jaeger"
1520
"github.com/docker/cli/cli/command"
1621
controlapi "github.com/moby/buildkit/api/services/control"
1722
"github.com/opencontainers/go-digest"
1823
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
24+
"github.com/pkg/browser"
1925
"github.com/pkg/errors"
2026
"github.com/spf13/cobra"
27+
jaegerui "github.com/tonistiigi/jaeger-ui-rest"
2128
)
2229

2330
type traceOptions struct {
2431
builder string
2532
ref string
2633
containerName string
34+
addr string
2735
}
2836

2937
func runTrace(ctx context.Context, dockerCli command.Cli, opts traceOptions) error {
@@ -104,8 +112,6 @@ func runTrace(ctx context.Context, dockerCli command.Cli, opts traceOptions) err
104112
}
105113
}
106114

107-
log.Printf("trace %+v", rec.Trace)
108-
109115
c, err := rec.node.Driver.Client(ctx)
110116
if err != nil {
111117
return err
@@ -127,12 +133,68 @@ func runTrace(ctx context.Context, dockerCli command.Cli, opts traceOptions) err
127133
return err
128134
}
129135

130-
// TODO: try to upload build to Jaeger UI
131-
jd := spans.JaegerData().Data
136+
wrapper := struct {
137+
Data []jaeger.Trace `json:"data"`
138+
}{
139+
Data: spans.JaegerData().Data,
140+
}
141+
142+
var term bool
143+
if _, err := console.ConsoleFromFile(os.Stdout); err == nil {
144+
term = true
145+
}
132146

133-
enc := json.NewEncoder(dockerCli.Out())
147+
if len(wrapper.Data) == 0 {
148+
return errors.New("no trace data")
149+
}
150+
151+
if !term {
152+
enc := json.NewEncoder(dockerCli.Out())
153+
enc.SetIndent("", " ")
154+
return enc.Encode(wrapper)
155+
}
156+
157+
srv := jaegerui.NewServer(jaegerui.Config{})
158+
159+
buf := &bytes.Buffer{}
160+
enc := json.NewEncoder(buf)
134161
enc.SetIndent("", " ")
135-
return enc.Encode(jd)
162+
if err := enc.Encode(wrapper); err != nil {
163+
return err
164+
}
165+
166+
if err := srv.AddTrace(string(wrapper.Data[0].TraceID), bytes.NewReader(buf.Bytes())); err != nil {
167+
return err
168+
}
169+
170+
ln, err := net.Listen("tcp", opts.addr)
171+
if err != nil {
172+
return err
173+
}
174+
175+
url := "http://" + ln.Addr().String() + "/trace/" + string(wrapper.Data[0].TraceID)
176+
177+
go func() {
178+
time.Sleep(100 * time.Millisecond)
179+
browser.OpenURL(url)
180+
}()
181+
182+
fmt.Fprintf(dockerCli.Err(), "Trace available at %s\n", url)
183+
184+
go func() {
185+
<-ctx.Done()
186+
ln.Close()
187+
}()
188+
189+
err = srv.Serve(ln)
190+
if err != nil {
191+
select {
192+
case <-ctx.Done():
193+
return nil
194+
default:
195+
}
196+
}
197+
return err
136198
}
137199

138200
func traceCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
@@ -154,6 +216,7 @@ func traceCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
154216

155217
flags := cmd.Flags()
156218
flags.StringVar(&options.containerName, "container", "", "Container name")
219+
flags.StringVar(&options.addr, "addr", "127.0.0.1:0", "Address to bind the UI server")
157220

158221
return cmd
159222
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/docker/buildx
22

3-
go 1.22.0
3+
go 1.23.1
44

55
require (
66
github.com/Masterminds/semver/v3 v3.2.1
@@ -46,6 +46,7 @@ require (
4646
github.com/stretchr/testify v1.10.0
4747
github.com/tonistiigi/fsutil v0.0.0-20250113203817-b14e27f4135a
4848
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4
49+
github.com/tonistiigi/jaeger-ui-rest v0.0.0-20250206065519-bb2801509720
4950
github.com/zclconf/go-cty v1.16.0
5051
go.opentelemetry.io/otel v1.31.0
5152
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ github.com/tonistiigi/fsutil v0.0.0-20250113203817-b14e27f4135a h1:EfGw4G0x/8qXW
447447
github.com/tonistiigi/fsutil v0.0.0-20250113203817-b14e27f4135a/go.mod h1:Dl/9oEjK7IqnjAm21Okx/XIxUCFJzvh+XdVHUlBwXTw=
448448
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4 h1:7I5c2Ig/5FgqkYOh/N87NzoyI9U15qUPXhDD8uCupv8=
449449
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4/go.mod h1:278M4p8WsNh3n4a1eqiFcV2FGk7wE5fwUpUom9mK9lE=
450+
github.com/tonistiigi/jaeger-ui-rest v0.0.0-20250206065519-bb2801509720 h1:feVEZxSTNtC2GrzidXn4tCwBrSp2MHWGRIwmWVGRQIg=
451+
github.com/tonistiigi/jaeger-ui-rest v0.0.0-20250206065519-bb2801509720/go.mod h1:MD2HGVy33cP0VcvmXMvAPiidkQHKz/04Ja9iYQnhZso=
450452
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea h1:SXhTLE6pb6eld/v/cCndK0AMpt1wiVFb/YYmqB3/QG0=
451453
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea/go.mod h1:WPnis/6cRcDZSUvVmezrxJPkiO87ThFYsoUiMwWNDJk=
452454
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab h1:H6aJ0yKQ0gF49Qb2z5hI1UHxSQt4JMyxebFR15KnApw=

vendor/github.com/tonistiigi/jaeger-ui-rest/Dockerfile

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tonistiigi/jaeger-ui-rest/config.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tonistiigi/jaeger-ui-rest/decompress/decompress.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tonistiigi/jaeger-ui-rest/docker-bake.hcl

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tonistiigi/jaeger-ui-rest/fs.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.79 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)