-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collect analytics for voyager usages #133
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package app | ||
|
||
import ( | ||
"strings" | ||
|
||
aci "github.com/appscode/k8s-addons/api" | ||
"github.com/appscode/k8s-addons/pkg/events" | ||
"github.com/appscode/k8s-addons/pkg/stash" | ||
acw "github.com/appscode/k8s-addons/pkg/watcher" | ||
"github.com/appscode/log" | ||
"github.com/appscode/voyager/pkg/analytics" | ||
"github.com/appscode/voyager/pkg/controller/certificates" | ||
ingresscontroller "github.com/appscode/voyager/pkg/controller/ingress" | ||
kapi "k8s.io/kubernetes/pkg/api" | ||
|
@@ -102,12 +105,15 @@ func (w *Watcher) Dispatch(e *events.Event) error { | |
certController := certificates.NewController(w.Client, w.AppsCodeExtensionClient) | ||
certController.Handle(e) | ||
} | ||
sendAnalytics(e, err) | ||
return err | ||
case events.Certificate: | ||
var err error | ||
if e.EventType.IsAdded() || e.EventType.IsUpdated() { | ||
certController := certificates.NewController(w.Client, w.AppsCodeExtensionClient) | ||
certController.Handle(e) | ||
err = certController.Handle(e) | ||
} | ||
sendAnalytics(e, err) | ||
case events.Service: | ||
if e.EventType.IsAdded() || e.EventType.IsDeleted() { | ||
return ingresscontroller.UpgradeAllEngress( | ||
|
@@ -152,3 +158,31 @@ func (w *Watcher) Certificate() { | |
|
||
go certificates.NewCertificateSyncer(w.Client, w.AppsCodeExtensionClient).RunSync() | ||
} | ||
|
||
func sendAnalytics(e *events.Event, err error) { | ||
switch e.ResourceType { | ||
case events.Ingress, events.ExtendedIngress, events.Certificate: | ||
if e.EventType.IsAdded() || e.EventType.IsDeleted() { | ||
// without converting to specific type for each resource, we | ||
// extract group version from resource SelfLink. This guaranties | ||
// having GroupVersion. | ||
gvk := e.MetaData.GetSelfLink() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you figure this out from ApiGroup & Name? Do we need to parse selfLink? |
||
if len(gvk) > 0 { | ||
gvk = strings.TrimPrefix(gvk, "/apis/") | ||
idx := strings.Index(gvk, "/namespace") | ||
if idx > 0 { | ||
gvk = gvk[0:idx] | ||
} | ||
if len(gvk) > 0 && !strings.HasSuffix(gvk, "/") { | ||
gvk += "/" | ||
} | ||
} | ||
|
||
label := "success" | ||
if err != nil { | ||
label = "fail" | ||
} | ||
go analytics.Send(gvk+e.ResourceType.String(), strings.ToLower(e.EventType.String()), label) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package analytics | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
|
||
"github.com/appscode/log" | ||
ga "github.com/jpillora/go-ogle-analytics" | ||
) | ||
|
||
const ( | ||
trackingID = "UA-62096468-13" | ||
) | ||
|
||
var ( | ||
mu sync.Mutex | ||
client *ga.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 | ||
} | ||
|
||
// error is ignored intentionally. we try to send event to GA in a best effort approach. | ||
c.Send(e) | ||
} | ||
|
||
func mustNewClient() *ga.Client { | ||
client, err := ga.NewClient(trackingID) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
return client | ||
} | ||
|
||
func VoyagerStarted() { | ||
send(ga.NewEvent("operator", "started")) | ||
} | ||
|
||
func VoyagerStopped() { | ||
send(ga.NewEvent("operator", "stopped")) | ||
} | ||
|
||
func Send(category, action string, label ...string) { | ||
ev := ga.NewEvent(category, action) | ||
if len(label) > 0 { | ||
ev.Label(strings.Join(label, ",")) | ||
} | ||
send(ev) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use EnableAnalytics here instead of true directly.