Skip to content

Commit

Permalink
Add command usage tracking (#1456)
Browse files Browse the repository at this point in the history
* Add command usage tracking

* Add comment, remove test mode

* Rename optout env variable

* Update CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
marians authored Oct 28, 2024
1 parent ef5ba35 commit 400231a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

## [Unreleased]

### Added

- Basic usage tracking data is now collected for every command execution. This should help us maintain and develop the tool. Set the `KUBECTL_GS_TELEMETRY_OPTOUT` environment variable to an arbitrary value to disable this. Data is submitted to [TelemetryDeck](https://telemetrydeck.com/) in the EU. More details are available in our [docs](https://docs.giantswarm.io/vintage/use-the-api/kubectl-gs/telemetry/).

## [4.2.0] - 2024-10-15

### Changed
Expand Down
37 changes: 34 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"context"
"fmt"
"io"
"log"
"os"

"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/telemetrydeck-go"
"github.com/spf13/afero"
"github.com/spf13/cobra"

Expand All @@ -26,6 +29,9 @@ const (
Get more information at https://docs.giantswarm.io/use-the-api/kubectl-gs/
`
telemetrydeckAppID = "4539763B-A291-4835-B832-9BEB80CA7039"

telemetryOptOutVariable = "KUBECTL_GS_TELEMETRY_OPTOUT"
)

type Config struct {
Expand Down Expand Up @@ -66,9 +72,34 @@ func New(config Config) (*cobra.Command, error) {
}

c := &cobra.Command{
Use: name,
Short: description,
Long: description,
Use: name,
Short: description,
Long: description,

// Called for every subcommand execution
// to track command usage.
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if os.Getenv(telemetryOptOutVariable) != "" {
return
}

logger := log.New(os.Stdout, "", 0) // to bring telemetry errors to the surface
tdClient, err := telemetrydeck.NewClient(telemetrydeckAppID,
telemetrydeck.WithLogger(logger),
)
if err != nil {
log.Printf("error creating telemetrydeck client: %s", err)
} else {
err = tdClient.SendSignal(context.Background(), "GiantSwarm.command", map[string]interface{}{
"appVersion": project.Version(),
"command": cmd.CommandPath(),
})
if err != nil {
log.Printf("error sending telemetrydeck signal: %s", err)
}
}
},

RunE: r.Run,
PersistentPostRunE: r.PersistentPostRun,
SilenceUsage: true,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/giantswarm/micrologger v1.1.1
github.com/giantswarm/organization-operator v1.6.4
github.com/giantswarm/release-operator/v4 v4.2.0
github.com/giantswarm/telemetrydeck-go v0.0.0-20241011124412-94d192a4aad6
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ github.com/giantswarm/organization-operator v1.6.4 h1:t0G0hoB7TfeGzRnr58NIwO6LAm
github.com/giantswarm/organization-operator v1.6.4/go.mod h1:Rw9gG/ReI5YfvoC85F9H5wH2aWguUR7JY/NfTjZC2Jc=
github.com/giantswarm/release-operator/v4 v4.2.0 h1:8aU8V3BlF/sKmYG1MgcPGXs8Q/cOlZfhgK4/oBfMPbg=
github.com/giantswarm/release-operator/v4 v4.2.0/go.mod h1:/ew0vh4BnfJqOddNTcm7iAHvm7g4MBp5C+pxi+H4ydk=
github.com/giantswarm/telemetrydeck-go v0.0.0-20241011124412-94d192a4aad6 h1:IHUooOg3MXokQgrIyrX8TCrYT4SqA3S4OIRA7TYHG0Y=
github.com/giantswarm/telemetrydeck-go v0.0.0-20241011124412-94d192a4aad6/go.mod h1:BitlH7nTrQmWvVEtnAa8pNM+FjI0+9S7JA9tl1PVvzo=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E=
Expand Down

0 comments on commit 400231a

Please sign in to comment.