Skip to content

Commit

Permalink
history: add comparison support to trace
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Feb 6, 2025
1 parent 7f3d6cd commit 3fd4ec0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
39 changes: 24 additions & 15 deletions commands/history/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
)

type traceOptions struct {
builder string
ref string
containerName string
addr string
builder string
ref string
addr string
compare string
}

func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, []byte, error) {
Expand Down Expand Up @@ -175,39 +175,48 @@ func runTrace(ctx context.Context, dockerCli command.Cli, opts traceOptions) err
}
}

traceid, data, err := loadTrace(ctx, opts.ref, nodes)
traceID, data, err := loadTrace(ctx, opts.ref, nodes)
if err != nil {
return err
}
srv := jaegerui.NewServer(jaegerui.Config{})
if err := srv.AddTrace(traceID, bytes.NewReader(data)); err != nil {
return err
}
url := "/trace/" + traceID

if opts.compare != "" {
traceIDcomp, data, err := loadTrace(ctx, opts.compare, nodes)
if err != nil {
return errors.Wrapf(err, "failed to load trace for %s", opts.compare)
}
if err := srv.AddTrace(traceIDcomp, bytes.NewReader(data)); err != nil {
return err
}
url = "/trace/" + traceIDcomp + "..." + traceID
}

var term bool
if _, err := console.ConsoleFromFile(os.Stdout); err == nil {
term = true
}

if !term {
if !term && opts.compare == "" {
fmt.Fprintln(dockerCli.Out(), string(data))
return nil
}

srv := jaegerui.NewServer(jaegerui.Config{})

if err := srv.AddTrace(traceid, bytes.NewReader(data)); err != nil {
return err
}

ln, err := net.Listen("tcp", opts.addr)
if err != nil {
return err
}

url := "http://" + ln.Addr().String() + "/trace/" + traceid

go func() {
time.Sleep(100 * time.Millisecond)
browser.OpenURL(url)
}()

url = "http://" + ln.Addr().String() + url
fmt.Fprintf(dockerCli.Err(), "Trace available at %s\n", url)

go func() {
Expand Down Expand Up @@ -244,8 +253,8 @@ func traceCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
}

flags := cmd.Flags()
flags.StringVar(&options.containerName, "container", "", "Container name")
flags.StringVar(&options.addr, "addr", "127.0.0.1:0", "Address to bind the UI server")
flags.StringVar(&options.compare, "compare", "", "Compare with another build reference")

return cmd
}
15 changes: 8 additions & 7 deletions docs/reference/buildx_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Commands to work on build records

### Subcommands

| Name | Description |
|:---------------------------------------|:-------------------------------|
| [`inspect`](buildx_history_inspect.md) | Inspect a build |
| [`logs`](buildx_history_logs.md) | Print the logs of a build |
| [`ls`](buildx_history_ls.md) | List build records |
| [`open`](buildx_history_open.md) | Open a build in Docker Desktop |
| [`rm`](buildx_history_rm.md) | Remove build records |
| Name | Description |
|:---------------------------------------|:-----------------------------------------------|
| [`inspect`](buildx_history_inspect.md) | Inspect a build |
| [`logs`](buildx_history_logs.md) | Print the logs of a build |
| [`ls`](buildx_history_ls.md) | List build records |
| [`open`](buildx_history_open.md) | Open a build in Docker Desktop |
| [`rm`](buildx_history_rm.md) | Remove build records |
| [`trace`](buildx_history_trace.md) | Show the OpenTelemetry trace of a build record |


### Options
Expand Down
17 changes: 17 additions & 0 deletions docs/reference/buildx_history_trace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# docker buildx history trace

<!---MARKER_GEN_START-->
Show the OpenTelemetry trace of a build record

### Options

| Name | Type | Default | Description |
|:----------------|:---------|:--------------|:-----------------------------------------|
| `--addr` | `string` | `127.0.0.1:0` | Address to bind the UI server |
| `--builder` | `string` | | Override the configured builder instance |
| `--compare` | `string` | | Compare with another build reference |
| `-D`, `--debug` | `bool` | | Enable debug logging |


<!---MARKER_GEN_END-->

0 comments on commit 3fd4ec0

Please sign in to comment.