Skip to content

Commit 3e0e933

Browse files
committed
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>
1 parent 694bd9a commit 3e0e933

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

commands/build.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,29 @@ const (
239239
commandOptionsHash = attribute.Key("command.options.hash")
240240
driverNameAttribute = attribute.Key("driver.name")
241241
driverTypeAttribute = attribute.Key("driver.type")
242+
debuggerName = attribute.Key("debugger.name")
243+
debuggerUserAgent = attribute.Key("debugger.user.agent")
242244
)
243245

244-
func buildMetricAttributes(dockerCli command.Cli, driverType string, options *buildOptions) attribute.Set {
245-
return attribute.NewSet(
246+
func buildMetricAttributes(dockerCli command.Cli, driverType string, options *buildOptions, debugger debuggerOptions) attribute.Set {
247+
kvs := []attribute.KeyValue{
246248
commandNameAttribute.String("build"),
247249
attribute.Stringer(string(commandOptionsHash), &buildOptionsHash{
248250
buildOptions: options,
249251
cfg: confutil.NewConfig(dockerCli),
250252
}),
251253
driverNameAttribute.String(options.builder),
252254
driverTypeAttribute.String(driverType),
253-
)
255+
}
256+
257+
if debugger != nil {
258+
d := debugger.Info()
259+
kvs = append(kvs,
260+
debuggerName.String(d.Name),
261+
debuggerUserAgent.String(d.UserAgent),
262+
)
263+
}
264+
return attribute.NewSet(kvs...)
254265
}
255266

256267
// 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
331342
}
332343
driverType := b.Driver
333344

334-
attributes := buildMetricAttributes(dockerCli, driverType, &options)
345+
attributes := buildMetricAttributes(dockerCli, driverType, &options, debugOpts)
335346

336347
ctx2, cancel := context.WithCancelCause(context.TODO())
337348
defer func() { cancel(errors.WithStack(context.Canceled)) }()

commands/dap.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"github.com/spf13/cobra"
1919
)
2020

21+
const (
22+
dapEnvUserAgent = "BUILDX_DAP_USER_AGENT"
23+
)
24+
2125
func dapCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
2226
var options dapOptions
2327
cmd := &cobra.Command{
@@ -51,6 +55,13 @@ func (d *dapOptions) New(in ioset.In) (debuggerInstance, error) {
5155
}, nil
5256
}
5357

58+
func (d *dapOptions) Info() debuggerInfo {
59+
return debuggerInfo{
60+
Name: "dap",
61+
UserAgent: os.Getenv(dapEnvUserAgent),
62+
}
63+
}
64+
5465
type LaunchConfig struct {
5566
Dockerfile string `json:"dockerfile,omitempty"`
5667
ContextPath string `json:"contextPath,omitempty"`

commands/debug.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ type debugOptions struct {
2626
OnFlag string
2727
}
2828

29+
type debuggerInfo struct {
30+
Name string
31+
UserAgent string
32+
}
33+
2934
// debuggerOptions will start a debuggerOptions instance.
3035
type debuggerOptions interface {
3136
New(in ioset.In) (debuggerInstance, error)
37+
Info() debuggerInfo
3238
}
3339

3440
// debuggerInstance is an instance of a Debugger that has been started.
@@ -71,6 +77,12 @@ func (d *debugOptions) New(in ioset.In) (debuggerInstance, error) {
7177
}, nil
7278
}
7379

80+
func (d *debugOptions) Info() debuggerInfo {
81+
return debuggerInfo{
82+
Name: "debug",
83+
}
84+
}
85+
7486
type monitorDebuggerInstance struct {
7587
cfg *build.InvokeConfig
7688
in io.ReadCloser

0 commit comments

Comments
 (0)