commands: adds metrics associated with the debugger

Add metrics associated with the debugger that are reported through the
metrics writer. This adds a few attributes that are only added when a
debugger is used with either the `debug` command or `dap` command.

At the moment, these metrics show up the exact same as a build and we
can't identify if something is using `dap` or `debug` since they use the
same code path.

This also adds a new available metric that can be utilized by plugins to
report additional information. The metrics will check if an environment
variable `BUILDX_DAP_USER_AGENT` is sent and that will get included in
the metrics if they are enabled.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2026-01-28 14:05:11 -06:00
parent 694bd9ad16
commit 3e0e9333c2
3 changed files with 38 additions and 4 deletions
+15 -4
View File
@@ -239,10 +239,12 @@ const (
commandOptionsHash = attribute.Key("command.options.hash")
driverNameAttribute = attribute.Key("driver.name")
driverTypeAttribute = attribute.Key("driver.type")
debuggerName = attribute.Key("debugger.name")
debuggerUserAgent = attribute.Key("debugger.user.agent")
)
func buildMetricAttributes(dockerCli command.Cli, driverType string, options *buildOptions) attribute.Set {
return attribute.NewSet(
func buildMetricAttributes(dockerCli command.Cli, driverType string, options *buildOptions, debugger debuggerOptions) attribute.Set {
kvs := []attribute.KeyValue{
commandNameAttribute.String("build"),
attribute.Stringer(string(commandOptionsHash), &buildOptionsHash{
buildOptions: options,
@@ -250,7 +252,16 @@ func buildMetricAttributes(dockerCli command.Cli, driverType string, options *bu
}),
driverNameAttribute.String(options.builder),
driverTypeAttribute.String(driverType),
)
}
if debugger != nil {
d := debugger.Info()
kvs = append(kvs,
debuggerName.String(d.Name),
debuggerUserAgent.String(d.UserAgent),
)
}
return attribute.NewSet(kvs...)
}
// buildOptionsHash computes a hash for the buildOptions when the String method is invoked.
@@ -331,7 +342,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, debugOpts debuggerOpti
}
driverType := b.Driver
attributes := buildMetricAttributes(dockerCli, driverType, &options)
attributes := buildMetricAttributes(dockerCli, driverType, &options, debugOpts)
ctx2, cancel := context.WithCancelCause(context.TODO())
defer func() { cancel(errors.WithStack(context.Canceled)) }()
+11
View File
@@ -18,6 +18,10 @@ import (
"github.com/spf13/cobra"
)
const (
dapEnvUserAgent = "BUILDX_DAP_USER_AGENT"
)
func dapCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options dapOptions
cmd := &cobra.Command{
@@ -51,6 +55,13 @@ func (d *dapOptions) New(in ioset.In) (debuggerInstance, error) {
}, nil
}
func (d *dapOptions) Info() debuggerInfo {
return debuggerInfo{
Name: "dap",
UserAgent: os.Getenv(dapEnvUserAgent),
}
}
type LaunchConfig struct {
Dockerfile string `json:"dockerfile,omitempty"`
ContextPath string `json:"contextPath,omitempty"`
+12
View File
@@ -26,9 +26,15 @@ type debugOptions struct {
OnFlag string
}
type debuggerInfo struct {
Name string
UserAgent string
}
// debuggerOptions will start a debuggerOptions instance.
type debuggerOptions interface {
New(in ioset.In) (debuggerInstance, error)
Info() debuggerInfo
}
// debuggerInstance is an instance of a Debugger that has been started.
@@ -71,6 +77,12 @@ func (d *debugOptions) New(in ioset.In) (debuggerInstance, error) {
}, nil
}
func (d *debugOptions) Info() debuggerInfo {
return debuggerInfo{
Name: "debug",
}
}
type monitorDebuggerInstance struct {
cfg *build.InvokeConfig
in io.ReadCloser