Skip to content

Commit

Permalink
telemetry: Update telemetry identification (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Dec 4, 2017
1 parent d3838f7 commit 84bcd68
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Config) GetLogger() *logrus.Logger {

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

return c.metrics
Expand Down
3 changes: 2 additions & 1 deletion docs/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ the target architecture (amd64, darwin, ...), and the number of CPUs available o

## Identification

The running instance is identified using an unique identifier which is set every time ORY Hydra starts. The identifier
To identify an installation and group together clusters, we create a SHA-512 hash of the Issuer URL for identification.
Additionally, each running instance is identified using an unique identifier which is set every time ORY Hydra starts. The identifier
is a Universally Unique Identifier (V4) and is thus a cryptographically safe random string. Identification is triggered
when the instance has been running for more than 15 minutes.

Expand Down
70 changes: 54 additions & 16 deletions metrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import (
"sync"
"time"

"github.com/pborman/uuid"
"github.com/segmentio/analytics-go"
"github.com/urfave/negroni"
//"github.com/ory/hydra/cmd"
"crypto/sha512"
"encoding/base64"
"strings"

"github.com/ory/hydra/pkg"
"github.com/pborman/uuid"
"github.com/sirupsen/logrus"
)

Expand All @@ -36,21 +40,38 @@ type MetricsManager struct {
buildVersion string
buildHash string
buildTime string
issuerURL string
databaseURL string
internalID string
}

func shouldCommit(issuerURL string, databaseURL string) bool {
return !(databaseURL == "" || databaseURL == "memory" || issuerURL == "" || strings.Contains(issuerURL, "localhost"))
}

func identify(issuerURL string) string {
hash := sha512.New()
hash.Write([]byte(issuerURL))
return base64.URLEncoding.EncodeToString(hash.Sum(nil))
}

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

mm := &MetricsManager{
Snapshot: &Snapshot{
MemorySnapshot: &MemorySnapshot{},
ID: uuid.New(),
ID: identify(issuerURL),
Metrics: newMetrics(),
HTTPMetrics: newHttpMetrics(),
Paths: map[string]*PathMetrics{},
start: time.Now(),
},
Segment: analytics.New("JYilhx5zP8wrzfykUinXrSUbo5cRA3aA"),
Logger: l,
internalID: uuid.New(),
Segment: analytics.New("h8dRH3kVCWKkIFWydBmWsyYHR4M0u0vr"),
Logger: l,
issuerURL: issuerURL,
databaseURL: databaseURL,
}
return mm
}
Expand All @@ -61,6 +82,11 @@ const (
)

func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
if !shouldCommit(sw.issuerURL, sw.databaseURL) {
sw.Logger.Info("Detected local environment, skipping telemetry commit")
return
}

time.Sleep(defaultWait)
if err := pkg.Retry(sw.Logger, time.Minute*2, defaultWait, func() error {
return sw.Segment.Identify(&analytics.Identify{
Expand All @@ -73,6 +99,7 @@ func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
"version": version,
"hash": hash,
"buildTime": buildTime,
"instanceId": sw.internalID,
},
Context: map[string]interface{}{
"ip": "0.0.0.0",
Expand All @@ -85,12 +112,17 @@ func (sw *MetricsManager) RegisterSegment(version, hash, buildTime string) {
}

func (sw *MetricsManager) TickKeepAlive() {
if !shouldCommit(sw.issuerURL, sw.databaseURL) {
sw.Logger.Info("Detected local environment, skipping telemetry commit")
return
}

time.Sleep(defaultWait)
for {
if err := sw.Segment.Track(&analytics.Track{
Event: "keep-alive",
AnonymousId: sw.ID,
Properties: map[string]interface{}{},
Properties: map[string]interface{}{"instanceId": sw.internalID},
Context: map[string]interface{}{"ip": "0.0.0.0"},
}); err != nil {
sw.Logger.WithError(err).Debug("Could not send telemetry keep alive")
Expand All @@ -101,23 +133,29 @@ func (sw *MetricsManager) TickKeepAlive() {
}

func (sw *MetricsManager) CommitTelemetry() {
if !shouldCommit(sw.issuerURL, sw.databaseURL) {
sw.Logger.Info("Detected local environment, skipping telemetry commit")
return
}

for {
time.Sleep(defaultWait)
sw.Update()
if err := sw.Segment.Track(&analytics.Track{
Event: "telemetry",
AnonymousId: sw.ID,
Properties: map[string]interface{}{
"upTime": sw.UpTime,
"requests": sw.Requests,
"responses": sw.Responses,
"paths": sw.Paths,
"methods": sw.Methods,
"sizes": sw.Sizes,
"status": sw.Status,
"latencies": sw.Latencies,
"raw": sw,
"memory": sw.MemorySnapshot,
"upTime": sw.UpTime,
"requests": sw.Requests,
"responses": sw.Responses,
"paths": sw.Paths,
"methods": sw.Methods,
"sizes": sw.Sizes,
"status": sw.Status,
"latencies": sw.Latencies,
"raw": sw,
"memory": sw.MemorySnapshot,
"instanceId": sw.internalID,
},
Context: map[string]interface{}{
"ip": "0.0.0.0",
Expand Down
4 changes: 2 additions & 2 deletions metrics/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

func TestMiddleware(t *testing.T) {
rand.Seed(time.Now().Unix())
mw := metrics.NewMetricsManager(logrus.StandardLogger())
mw := metrics.NewMetricsManager("", "", logrus.StandardLogger())
n := negroni.New()
r := httprouter.New()

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestMiddleware(t *testing.T) {

func TestRacyMiddleware(t *testing.T) {
rand.Seed(time.Now().Unix())
mw := metrics.NewMetricsManager(logrus.StandardLogger())
mw := metrics.NewMetricsManager("", "", logrus.StandardLogger())
n := negroni.New()
r := httprouter.New()

Expand Down

0 comments on commit 84bcd68

Please sign in to comment.