Skip to content

Commit

Permalink
metrics: Improves naming of traits (#803)
Browse files Browse the repository at this point in the history
Closes #802
  • Loading branch information
arekkas authored Mar 10, 2018
1 parent 7594eb4 commit dd06073
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func RunHost(c *config.Config) func(cmd *cobra.Command, args []string) {

if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok && os.Getenv("DISABLE_TELEMETRY") != "1" {
metrics := c.GetMetrics()
go metrics.RegisterSegment(c.BuildVersion, c.BuildHash, c.BuildTime)
go metrics.RegisterSegment()
go metrics.CommitMemoryStatistics()
n.Use(metrics)
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *Config) GetLogger() *logrus.Logger {

func (c *Config) GetMetrics() *metrics.MetricsManager {
if c.metrics == nil {
c.metrics = metrics.NewMetricsManager(c.Issuer, c.DatabaseURL, c.GetLogger())
c.metrics = metrics.NewMetricsManager(c.Issuer, c.DatabaseURL, c.GetLogger(), c.BuildVersion, c.BuildHash, c.BuildTime)
}

return c.metrics
Expand Down
28 changes: 14 additions & 14 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ type MemoryStatistics struct {

func (ms *MemoryStatistics) ToMap() map[string]interface{} {
return map[string]interface{}{
"alloc": ms.Alloc,
"totalAlloc": ms.TotalAlloc,
"sys": ms.Sys,
"lookups": ms.Lookups,
"mallocs": ms.Mallocs,
"frees": ms.Frees,
"heapAlloc": ms.HeapAlloc,
"heapSys": ms.HeapSys,
"heapIdle": ms.HeapIdle,
"heapInuse": ms.HeapInuse,
"heapReleased": ms.HeapReleased,
"heapObjects": ms.HeapObjects,
"numGC": ms.NumGC,
"nonInteraction": 1,
"memoryAlloc": ms.Alloc,
"memoryTotalAlloc": ms.TotalAlloc,
"memorySys": ms.Sys,
"memoryLookups": ms.Lookups,
"memoryMallocs": ms.Mallocs,
"memoryFrees": ms.Frees,
"memoryHeapAlloc": ms.HeapAlloc,
"memoryHeapSys": ms.HeapSys,
"memoryHeapIdle": ms.HeapIdle,
"memoryHeapInuse": ms.HeapInuse,
"memoryHeapReleased": ms.HeapReleased,
"memoryHeapObjects": ms.HeapObjects,
"memoryNumGC": ms.NumGC,
"nonInteraction": 1,
}
}

Expand Down
80 changes: 52 additions & 28 deletions metrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ func shouldCommit(issuerURL string, databaseURL string) bool {
return !(databaseURL == "" || databaseURL == "memory" || issuerURL == "" || strings.Contains(issuerURL, "localhost"))
}

func hash(value string) string {
func generateID(issuerURL string, databaseURL string) string {
if !shouldCommit(issuerURL, databaseURL) {
return "local"
}

hash := sha256.New()
hash.Write([]byte(value))
hash.Write([]byte(issuerURL))
return hex.EncodeToString(hash.Sum(nil))
}

func NewMetricsManager(issuerURL string, databaseURL string, l logrus.FieldLogger) *MetricsManager {
func NewMetricsManager(issuerURL string, databaseURL string, l logrus.FieldLogger, version, hash, buildTime string) *MetricsManager {
l.Info("Setting up telemetry - for more information please visit https://ory.gitbooks.io/hydra/content/telemetry.html")

segment, err := analytics.NewWithConfig("h8dRH3kVCWKkIFWydBmWsyYHR4M0u0vr", analytics.Config{
Expand All @@ -83,15 +87,17 @@ func NewMetricsManager(issuerURL string, databaseURL string, l logrus.FieldLogge
issuerURL: issuerURL,
databaseURL: databaseURL,
MemoryStatistics: &MemoryStatistics{},
ID: hash(issuerURL),
ID: generateID(issuerURL, databaseURL),
start: time.Now().UTC(),
shouldCommit: shouldCommit(issuerURL, databaseURL),
salt: uuid.New(),
//shouldCommit: shouldCommit(issuerURL, databaseURL),
shouldCommit: true,
salt: uuid.New(),
BuildTime: buildTime, BuildVersion: version, BuildHash: hash,
}
return mm
}

func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
func (sw *MetricsManager) RegisterSegment() {
sw.Lock()
defer sw.Unlock()

Expand All @@ -104,14 +110,14 @@ func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
return sw.Segment.Enqueue(analytics.Identify{
UserId: sw.ID,
Traits: analytics.NewTraits().
Set("goarch", runtime.GOARCH).
Set("goos", runtime.GOOS).
Set("numCpu", runtime.NumCPU()).
Set("runtimeGoarch", runtime.GOARCH).
Set("runtimeGoos", runtime.GOOS).
Set("runtimeNumCpu", runtime.NumCPU()).
Set("runtimeVersion", runtime.Version()).
Set("version", version).
Set("hash", hash).
Set("buildTime", buildTime).
Set("instanceId", sw.InstanceID),
Set("buildVersion", sw.BuildVersion).
Set("buildHash", sw.BuildHash).
Set("buildTime", sw.BuildTime).
Set("instance", sw.InstanceID),
Context: &analytics.Context{
IP: net.IPv4(0, 0, 0, 0),
},
Expand All @@ -131,14 +137,23 @@ func (sw *MetricsManager) CommitMemoryStatistics() {
for {
sw.MemoryStatistics.Update()
if err := sw.Segment.Enqueue(analytics.Track{
UserId: sw.ID,
Event: "stats.memory",
Properties: analytics.Properties(sw.MemoryStatistics.ToMap()),
Context: &analytics.Context{IP: net.IPv4(0, 0, 0, 0)},
UserId: sw.ID,
Event: "stats.memory",
Properties: analytics.Properties(sw.MemoryStatistics.ToMap()).
Set("runtimeGoarch", runtime.GOARCH).
Set("runtimeGoos", runtime.GOOS).
Set("runtimeNumCpu", runtime.NumCPU()).
Set("runtimeVersion", runtime.Version()).
Set("buildVersion", sw.BuildVersion).
Set("buildHash", sw.BuildHash).
Set("buildTime", sw.BuildTime).
Set("instance", sw.InstanceID).
Set("nonInteraction", 1),
Context: &analytics.Context{IP: net.IPv4(0, 0, 0, 0)},
}); err != nil {
sw.Logger.WithError(err).Debug("Could not commit anonymized telemetry data")
} else {
sw.Logger.Debug("Telemetry data transmitted")
sw.Logger.Debug("Transmitted anonymized memory usage statistics")
}
time.Sleep(time.Hour)
}
Expand Down Expand Up @@ -167,21 +182,30 @@ func (sw *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, nex
status := res.Status()
size := res.Size()

sw.Segment.Enqueue(analytics.Page{
if err := sw.Segment.Enqueue(analytics.Page{
UserId: sw.ID,
Name: path,
Properties: analytics.
NewProperties().
SetURL(scheme+"//"+sw.ID+path+"?"+query).
SetPath(path).
SetName(path).
Set("status", status).
Set("size", size).
Set("latency", latency).
Set("instance", sw.InstanceID).
Set("method", r.Method),
Set("requestMethod", r.Method).
Set("requestStatus", status).
Set("requestSize", size).
Set("requestLatency", latency).
Set("runtimeGoarch", runtime.GOARCH).
Set("runtimeGoos", runtime.GOOS).
Set("runtimeNumCpu", runtime.NumCPU()).
Set("runtimeVersion", runtime.Version()).
Set("buildVersion", sw.BuildVersion).
Set("buildHash", sw.BuildHash).
Set("buildTime", sw.BuildTime).
Set("instance", sw.InstanceID),
Context: &analytics.Context{IP: net.IPv4(0, 0, 0, 0)},
})
}); err != nil {
sw.Logger.WithError(err).Debug("Unable to queue analytics")
}
}

func anonymizePath(path string, salt string) string {
Expand Down Expand Up @@ -211,7 +235,7 @@ func anonymizePath(path string, salt string) string {
if len(path) == len(p) && path[:len(p)] == strings.ToLower(p) {
return p
} else if len(path) > len(p) && path[:len(p)+1] == strings.ToLower(p)+"/" {
return path[:len(p)] + "/" + hash(path[len(p):]+"|"+salt)
return path[:len(p)] + "/" + generateID(path[len(p):]+"|"+salt, "should-commit")
}
}

Expand All @@ -222,7 +246,7 @@ func anonymizeQuery(query url.Values, salt string) string {
for _, q := range query {
for i, s := range q {
if s != "" {
s = hash(s + "|" + salt)
s = generateID(s+"|"+salt, "should-commit")
q[i] = s
}
}
Expand Down

0 comments on commit dd06073

Please sign in to comment.