Skip to content

Commit e137f36

Browse files
committed
fix: do not use subcommands as main command in metric
1 parent 5d3c190 commit e137f36

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pkg/metrics/otel.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,22 @@ func newMeterProvider(res *resource.Resource) *metric.MeterProvider {
5252
return meterProvider
5353
}
5454

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) {
5656
commandHistogram, _ := provider.Meter(naisCliPrefixName).Int64Histogram(
5757
naisCliPrefixName+"_command_usage",
5858
m.WithUnit("1"),
5959
m.WithDescription("Usage frequency of command flags"))
6060

61+
validCommands := map[string]bool{}
62+
for _, command := range mainCommands {
63+
validCommands[command.Name] = true
64+
}
65+
6166
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:], "_")))
6671
}
6772
}
6873
commandHistogram.Record(ctx, 1, m.WithAttributes(attributes...))
@@ -107,7 +112,7 @@ func CollectCommandHistogram(commands []*cli.Command) {
107112
provider := newMeterProvider(res)
108113

109114
// 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)
111116
provider.Shutdown(ctx)
112117
}
113118

0 commit comments

Comments
 (0)