Skip to content

Commit

Permalink
imrpove telemetry
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <joana.hrotko@docker.com>
  • Loading branch information
jhrotko committed Mar 19, 2024
1 parent 13a1940 commit 63fb90c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
11 changes: 5 additions & 6 deletions cmd/formatter/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Check warning on line 279 in cmd/formatter/shortcut.go

View check run for this annotation

Codecov / codecov/patch

cmd/formatter/shortcut.go#L271-L279

Added lines #L271 - L279 were not covered by tests
}
switch key := event.Key; key {
Expand Down
30 changes: 11 additions & 19 deletions internal/tracing/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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...),
}

Check warning on line 167 in internal/tracing/attributes.go

View check run for this annotation

Codecov / codecov/patch

internal/tracing/attributes.go#L159-L167

Added lines #L159 - L167 were not covered by tests
}

func keys[T any](m map[string]T) []string {
out := make([]string, 0, len(m))
for k := range m {
Expand Down
57 changes: 57 additions & 0 deletions internal/tracing/keyboard_metrics.go
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 44 in internal/tracing/keyboard_metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/tracing/keyboard_metrics.go#L33-L44

Added lines #L33 - L44 were not covered by tests
}

func (metrics *KeyboardMetrics) RegisterCommand(command Command) {
metrics.command = append(metrics.command, command)

Check warning on line 48 in internal/tracing/keyboard_metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/tracing/keyboard_metrics.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}

func CommandSliceToString(lst []Command) []string {
result := []string{}
for _, c := range lst {
result = append(result, string(c))
}
return result

Check warning on line 56 in internal/tracing/keyboard_metrics.go

View check run for this annotation

Codecov / codecov/patch

internal/tracing/keyboard_metrics.go#L51-L56

Added lines #L51 - L56 were not covered by tests
}

0 comments on commit 63fb90c

Please sign in to comment.