From 43a5fb577306177e2e62f7ea40b34aaba5bc3ef3 Mon Sep 17 00:00:00 2001 From: Joana Hrotko Date: Tue, 19 Mar 2024 15:31:50 +0000 Subject: [PATCH] imrpove telemetry Signed-off-by: Joana Hrotko --- cmd/formatter/shortcut.go | 11 +++--- internal/tracing/attributes.go | 30 ++++++--------- internal/tracing/keyboard_metrics.go | 57 ++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 internal/tracing/keyboard_metrics.go diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index 21a62430a05..d78edd029c7 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -132,10 +132,7 @@ func NewKeyboardManager(isDockerDesktopActive, isWatchConfigured bool, km.signalChannel = sc - km.metrics = tracing.KeyboardMetrics{ - EnabledViewDockerDesktop: isDockerDesktopActive, - HasWatchConfig: isWatchConfigured, - } + km.metrics = tracing.NewKeyboardMetrics(isDockerDesktopActive, isWatchConfigured) KeyboardManager = &km @@ -237,7 +234,7 @@ func (lk *LogKeyboard) openDockerDesktop(project *types.Project) { if !lk.IsDockerDesktopActive { return } - lk.metrics.ActivateViewDockerDesktop = true + lk.metrics.RegisterCommand(tracing.GUI) link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name) err := open.Run(link) if err != nil { @@ -276,7 +273,9 @@ func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Cont case 'v': lk.openDockerDesktop(project) case 'w': - lk.metrics.ActivateWatch = true + if !lk.Watch.isWatching() { + lk.metrics.RegisterCommand(tracing.WATCH) + } lk.StartWatch(ctx, project, options) } switch key := event.Key; key { diff --git a/internal/tracing/attributes.go b/internal/tracing/attributes.go index 622f5f8b40c..04da316d1f9 100644 --- a/internal/tracing/attributes.go +++ b/internal/tracing/attributes.go @@ -42,13 +42,6 @@ type Metrics struct { CountIncludesRemote int } -type KeyboardMetrics struct { - EnabledViewDockerDesktop bool - HasWatchConfig bool - ActivateViewDockerDesktop bool - ActivateWatch bool -} - func (s SpanOptions) SpanStartOptions() []trace.SpanStartOption { out := make([]trace.SpanStartOption, len(s)) for i := range s { @@ -142,18 +135,6 @@ func ServiceOptions(service types.ServiceConfig) SpanOptions { } } -func KeyboardOptions(metrics KeyboardMetrics) SpanOptions { - attrs := []attribute.KeyValue{ - attribute.Bool("view.enabled", metrics.EnabledViewDockerDesktop), - attribute.Bool("view.activated", metrics.ActivateViewDockerDesktop), - attribute.Bool("watch.activated", metrics.ActivateWatch), - attribute.Bool("watch.config", metrics.HasWatchConfig), - } - return []trace.SpanStartEventOption{ - trace.WithAttributes(attrs...), - } -} - // ContainerOptions returns common attributes from a Moby container. // // For convenience, it's returned as a SpanOptions object to allow it to be @@ -175,6 +156,17 @@ func ContainerOptions(container moby.Container) SpanOptions { } } +func KeyboardOptions(metrics KeyboardMetrics) SpanOptions { + attrs := []attribute.KeyValue{ + attribute.Bool("navmenu.enabled", metrics.enabled), + attribute.StringSlice("navmenu.command", CommandSliceToString(metrics.command)), + attribute.StringSlice("navmenu.command_available", CommandSliceToString(metrics.commandAvailable)), + } + return []trace.SpanStartEventOption{ + trace.WithAttributes(attrs...), + } +} + func keys[T any](m map[string]T) []string { out := make([]string, 0, len(m)) for k := range m { diff --git a/internal/tracing/keyboard_metrics.go b/internal/tracing/keyboard_metrics.go new file mode 100644 index 00000000000..7ece56119e3 --- /dev/null +++ b/internal/tracing/keyboard_metrics.go @@ -0,0 +1,57 @@ +/* + Copyright 2024 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package tracing + +type KeyboardMetrics struct { + enabled bool + command []Command + commandAvailable []Command +} + +type Command string + +const ( + GUI Command = "gui" + WATCH Command = "watch" + DEBUG Command = "debug" +) + +func NewKeyboardMetrics(IsDockerDesktopActive, isWatchConfigured bool) KeyboardMetrics { + metrics := KeyboardMetrics{ + enabled: true, + commandAvailable: []Command{}, + } + if IsDockerDesktopActive { + metrics.commandAvailable = append(metrics.commandAvailable, GUI) + } + if isWatchConfigured { + metrics.commandAvailable = append(metrics.commandAvailable, WATCH) + } + return metrics +} + +func (metrics *KeyboardMetrics) RegisterCommand(command Command) { + metrics.command = append(metrics.command, command) +} + +func CommandSliceToString(lst []Command) []string { + result := []string{} + for _, c := range lst { + result = append(result, string(c)) + } + return result +}