|
5 | 5 | "github.com/urfave/cli/v2"
|
6 | 6 | "log"
|
7 | 7 | "os"
|
8 |
| - "strings" |
9 | 8 | "time"
|
10 | 9 |
|
11 | 10 | "go.opentelemetry.io/otel/attribute"
|
@@ -56,7 +55,15 @@ func recordCommandUsage(ctx context.Context, provider *metric.MeterProvider, fla
|
56 | 55 | naisCliPrefixName+"_command_usage",
|
57 | 56 | m.WithUnit("1"),
|
58 | 57 | m.WithDescription("Usage frequency of command flags"))
|
59 |
| - commandHistogram.Record(ctx, 1, m.WithAttributes(attribute.String("command", strings.Join(flags, "_")))) |
| 58 | + if flags != nil { |
| 59 | + commandHistogram.Record(ctx, 1, m.WithAttributes(attribute.String("command", flags[0]))) |
| 60 | + } |
| 61 | + for i, f := range flags { |
| 62 | + if i == 0 { |
| 63 | + continue |
| 64 | + } |
| 65 | + commandHistogram.Record(ctx, 1, m.WithAttributes(attribute.String("subcommand", f))) |
| 66 | + } |
60 | 67 | }
|
61 | 68 |
|
62 | 69 | // intersection
|
@@ -88,22 +95,23 @@ func intersection(list1, list2 []string) []string {
|
88 | 95 | }
|
89 | 96 |
|
90 | 97 | func CollectCommandHistogram(commands []*cli.Command) {
|
| 98 | + doNotTrack := os.Getenv("DO_NOT_TRACK") |
| 99 | + if doNotTrack == "1" { |
| 100 | + log.Default().Println("DO_NOT_TRACK is set, not collecting metrics") |
| 101 | + } |
| 102 | + |
91 | 103 | ctx := context.Background()
|
92 | 104 | var validSubcommands []string
|
93 | 105 | for _, command := range commands {
|
94 | 106 | gatherCommands(command, &validSubcommands)
|
95 | 107 | }
|
96 | 108 |
|
97 |
| - doNotTrack := os.Getenv("DO_NOT_TRACK") |
98 |
| - if doNotTrack == "1" { |
99 |
| - log.Default().Println("DO_NOT_TRACK is set, not collecting metrics") |
100 |
| - } |
101 |
| - |
102 | 109 | res, _ := newResource()
|
103 | 110 | provider := newMeterProvider(res)
|
104 |
| - defer provider.Shutdown(ctx) |
| 111 | + |
105 | 112 | // Record usages of subcommands that are exactly in the list of args we have, nothing else
|
106 | 113 | recordCommandUsage(ctx, provider, intersection(os.Args, validSubcommands))
|
| 114 | + provider.Shutdown(ctx) |
107 | 115 | }
|
108 | 116 |
|
109 | 117 | func gatherCommands(command *cli.Command, validSubcommands *[]string) {
|
|
0 commit comments