Skip to content

Commit

Permalink
Add analytics (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mir Shahriar authored and tamalsaha committed Jun 13, 2017
1 parent 50bca20 commit 9accfcb
Show file tree
Hide file tree
Showing 92 changed files with 10,684 additions and 176 deletions.
19 changes: 16 additions & 3 deletions cmd/searchlight/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
_ "github.com/appscode/searchlight/api/install"
acs "github.com/appscode/searchlight/client/clientset"
_ "github.com/appscode/searchlight/client/clientset/fake"
"github.com/appscode/searchlight/pkg/analytics"
"github.com/appscode/searchlight/pkg/client/icinga"
acw "github.com/appscode/searchlight/pkg/watcher"
"github.com/k8sdb/apimachinery/pkg/docker"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
Expand All @@ -30,6 +32,8 @@ var (

kubeClient clientset.Interface
extClient acs.ExtensionInterface

enableAnalytics bool = true
)

func NewCmdRun() *cobra.Command {
Expand All @@ -48,6 +52,9 @@ func NewCmdRun() *cobra.Command {

cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.")

// Analytics flags
cmd.Flags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical event to Google Analytics")

return cmd
}

Expand All @@ -61,9 +68,10 @@ func run() {
extClient = acs.NewForConfigOrDie(config)

w := &acw.Watcher{
KubeClient: kubeClient,
ExtClient: extClient,
SyncPeriod: time.Minute * 2,
KubeClient: kubeClient,
ExtClient: extClient,
EnableAnalytics: enableAnalytics,
SyncPeriod: time.Minute * 2,
}
if icingaSecretName == "" {
log.Fatalln("Missing icinga secret")
Expand All @@ -77,6 +85,11 @@ func run() {
log.Infoln("Starting Searchlight operator...")
go w.Run()

if enableAnalytics {
analytics.Enable()
}
analytics.SendEvent(docker.ImageOperator, "started", Version)

http.Handle("/metrics", promhttp.Handler())
log.Infoln("Listening on", address)
log.Fatal(http.ListenAndServe(address, nil))
Expand Down
31 changes: 26 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import:
version: v1.2.1
- package: github.com/Unknwon/com
version: master
- package: github.com/Sirupsen/logrus
version: v0.10.0
- package: github.com/jpillora/go-ogle-analytics
version: master
testImport:
- package: github.com/stretchr/testify
version: v1.1.4
56 changes: 56 additions & 0 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package analytics

import (
"sync"

"github.com/appscode/log"
ga "github.com/jpillora/go-ogle-analytics"
)

const (
id = "UA-62096468-14"
)

var (
mu sync.Mutex
client *ga.Client
)

func mustNewClient() *ga.Client {
client, err := ga.NewClient(id)
if err != nil {
log.Fatalln(err)
}
return client
}

func Enable() {
mu.Lock()
defer mu.Unlock()
client = mustNewClient()
}

func Disable() {
mu.Lock()
defer mu.Unlock()
client = nil
}

func send(e *ga.Event) {
mu.Lock()
c := client
mu.Unlock()

if c == nil {
return
}
c.Send(e)
}

func SendEvent(category string, action, label string) {
event := ga.NewEvent(category, action)
if label != "" {
event = event.Label(label)
}
send(event)
}
Loading

0 comments on commit 9accfcb

Please sign in to comment.