Skip to content

Commit

Permalink
Reinstate analytics (#139)
Browse files Browse the repository at this point in the history
* add analytics original patch

* use gopkg.in to ensure we use v3 of analytics-go

* update modules for analytics-go
  • Loading branch information
nickatsegment authored and systemizer committed Oct 2, 2018
1 parent 28b12a8 commit 9d49cf8
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# This makefile is meant for humans

VERSION := $(shell git describe --tags --always --dirty="-dev")
LDFLAGS := -ldflags='-X "main.Version=$(VERSION)"'
ANALYTICS_WRITE_KEY ?=
LDFLAGS := -ldflags='-X "main.Version=$(VERSION)" -X "main.AnalyticsWriteKey=$(ANALYTICS_WRITE_KEY)"'

test:
GO111MODULE=on go test -v ./...
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ If it's preferred to not use any backend at all, set `CHAMBER_SECRET_BACKEND` to
This feature is experimental, and not currently meant for production work.


## Analytics

`chamber` includes some usage analytics code which Segment uses internally for tracking usage of internal tools. This analytics code is turned off by default, and can only be enabled via a linker flag at build time, which we do not set for public github releases.

## Releasing

To cut a new release, just push a tag named `v<semver>` where `<semver>` is a
Expand Down
13 changes: 13 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// deleteCmd represents the delete command
Expand All @@ -31,6 +32,18 @@ func delete(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "delete").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("key", key).
Set("backend", backend),
})
}
secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
Expand Down
13 changes: 13 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// execCmd represents the exec command
Expand Down Expand Up @@ -37,6 +38,18 @@ func execRun(cmd *cobra.Command, args []string) error {
dashIx := cmd.ArgsLenAtDash()
services, command, commandArgs := args[:dashIx], args[dashIx], args[dashIx+1:]

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "exec").
Set("chamber-version", chamberVersion).
Set("services", services).
Set("backend", backend),
})
}

env := environ(os.Environ())
secretStore, err := getSecretStore()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/magiconair/properties"
"github.com/pkg/errors"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

const doubleQuoteSpecialChars = "\\\n\r\"!$`"
Expand All @@ -39,6 +40,18 @@ func init() {
func runExport(cmd *cobra.Command, args []string) error {
var err error

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "export").
Set("chamber-version", chamberVersion).
Set("services", args).
Set("backend", backend),
})
}

secretStore, err := getSecretStore()
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// historyCmd represents the history command
Expand All @@ -34,6 +35,19 @@ func history(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "history").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("key", key).
Set("backend", backend),
})
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
Expand Down
13 changes: 13 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

var (
Expand Down Expand Up @@ -51,6 +52,18 @@ func importRun(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to decode input as json")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "import").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("backend", backend),
})
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
Expand Down
14 changes: 13 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// listCmd represents the list command
Expand All @@ -33,11 +34,22 @@ func list(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate service")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "list").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("backend", backend),
})
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
}

secrets, err := secretStore.List(service, withValues)
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
Expand Down
14 changes: 14 additions & 0 deletions cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

var (
Expand Down Expand Up @@ -41,6 +42,19 @@ func read(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "read").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("key", key).
Set("backend", backend),
})
}

secretStore, err := getSecretStore()
if err != nil {
return errors.Wrap(err, "Failed to get secret store")
Expand Down
45 changes: 41 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// Regex's used to validate service and key names
Expand All @@ -19,6 +20,12 @@ var (
verbose bool
numRetries int
chamberVersion string
backend string

analyticsEnabled bool
analyticsWriteKey string
analyticsClient analytics.Client
username string
)

const (
Expand All @@ -41,9 +48,11 @@ var Backends = []string{SSMBackend, S3Backend, NullBackend}

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "chamber",
Short: "CLI for storing secrets",
SilenceUsage: true,
Use: "chamber",
Short: "CLI for storing secrets",
SilenceUsage: true,
PersistentPreRun: prerun,
PersistentPostRun: postrun,
}

func init() {
Expand All @@ -53,8 +62,12 @@ func init() {

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(vers string) {
func Execute(vers string, writeKey string) {
chamberVersion = vers

analyticsWriteKey = writeKey
analyticsEnabled = analyticsWriteKey != ""

if cmd, err := RootCmd.ExecuteC(); err != nil {
if strings.Contains(err.Error(), "arg(s)") || strings.Contains(err.Error(), "usage") {
cmd.Usage()
Expand Down Expand Up @@ -102,3 +115,27 @@ func getSecretStore() (store.Store, error) {
}
return s, err
}

func prerun(cmd *cobra.Command, args []string) {
backend = strings.ToUpper(os.Getenv(BackendEnvVar))

if analyticsEnabled {
// set up analytics client
analyticsClient, _ = analytics.NewWithConfig(analyticsWriteKey, analytics.Config{
BatchSize: 1,
})

username = os.Getenv("USER")
analyticsClient.Enqueue(analytics.Identify{
UserId: username,
Traits: analytics.NewTraits().
Set("chamber-version", chamberVersion),
})
}
}

func postrun(cmd *cobra.Command, args []string) {
if analyticsEnabled && analyticsClient != nil {
analyticsClient.Close()
}
}
11 changes: 11 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

// versionCmd represents the version command
Expand All @@ -20,5 +21,15 @@ func init() {

func versionRun(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stdout, "chamber %s\n", chamberVersion)
if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "version").
Set("chamber-version", chamberVersion).
Set("backend", backend),
})
}
return nil
}
14 changes: 14 additions & 0 deletions cmd/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
analytics "gopkg.in/segmentio/analytics-go.v3"
)

var (
Expand Down Expand Up @@ -39,6 +40,19 @@ func write(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("command", "write").
Set("chamber-version", chamberVersion).
Set("service", service).
Set("backend", backend).
Set("key", key),
})
}

value := args[2]
if value == "-" {
// Read value from standard input
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ require (
github.com/magiconair/properties v1.8.0
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/stretchr/testify v1.2.2
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
gopkg.in/segmentio/analytics-go.v3 v3.0.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c h1:rsRTAcCR5CeNLkvgBVSjQoDGRRt6kggsE6XYBqCv2KQ=
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c/go.mod h1:kJ9mm9YmoWSkk+oQ+5Cj8DEoRCX2JT6As4kEtIIOp1M=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
gopkg.in/segmentio/analytics-go.v3 v3.0.0 h1:CyPSnB9Y3MhF/lL71ARYukL7Si55KADW9fGJFDEPG0E=
gopkg.in/segmentio/analytics-go.v3 v3.0.0/go.mod h1:4QqqlTlSSpVlWA9/9nDcPw+FkM2yv1NQoYjUbL9/JAw=
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import "github.com/segmentio/chamber/cmd"

var (
// This is updated by linker flags during build
Version = "dev"
Version = "dev"
AnalyticsWriteKey = ""
)

func main() {
cmd.Execute(Version)
cmd.Execute(Version, AnalyticsWriteKey)
}
Loading

0 comments on commit 9d49cf8

Please sign in to comment.