@@ -52,17 +52,22 @@ func newMeterProvider(res *resource.Resource) *metric.MeterProvider {
52
52
return meterProvider
53
53
}
54
54
55
- func recordCommandUsage (ctx context.Context , provider * metric.MeterProvider , flags []string ) {
55
+ func recordCommandUsage (ctx context.Context , provider * metric.MeterProvider , allCommands []string , mainCommands [] * cli. Command ) {
56
56
commandHistogram , _ := provider .Meter (naisCliPrefixName ).Int64Histogram (
57
57
naisCliPrefixName + "_command_usage" ,
58
58
m .WithUnit ("1" ),
59
59
m .WithDescription ("Usage frequency of command flags" ))
60
60
61
+ validCommands := map [string ]bool {}
62
+ for _ , command := range mainCommands {
63
+ validCommands [command .Name ] = true
64
+ }
65
+
61
66
attributes := make ([]attribute.KeyValue , 0 )
62
- if len (flags ) > 0 {
63
- attributes = append (attributes , attribute .String ("command" , flags [0 ]))
64
- if len (flags ) > 1 {
65
- attributes = append (attributes , attribute .String ("subcommand" , strings .Join (flags [1 :], "_" )))
67
+ if len (allCommands ) > 0 && validCommands [ allCommands [ 0 ]] {
68
+ attributes = append (attributes , attribute .String ("command" , allCommands [0 ]))
69
+ if len (allCommands ) > 1 {
70
+ attributes = append (attributes , attribute .String ("subcommand" , strings .Join (allCommands [1 :], "_" )))
66
71
}
67
72
}
68
73
commandHistogram .Record (ctx , 1 , m .WithAttributes (attributes ... ))
@@ -107,7 +112,7 @@ func CollectCommandHistogram(commands []*cli.Command) {
107
112
provider := newMeterProvider (res )
108
113
109
114
// Record usages of subcommands that are exactly in the list of args we have, nothing else
110
- recordCommandUsage (ctx , provider , intersection (os .Args , validSubcommands ))
115
+ recordCommandUsage (ctx , provider , intersection (os .Args , validSubcommands ), commands )
111
116
provider .Shutdown (ctx )
112
117
}
113
118
0 commit comments