Skip to content

Commit

Permalink
feat(telemetry): add cluster-fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 4, 2024
1 parent 07b8cf4 commit 9636b05
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Migrate to our custom telemetry endpoint. ([@palkan][])

## 1.5.3 (2024-08-28)

- Fix potential deadlocks in Redis pub/sub on reconnect. ([@palkan][])
Expand Down
83 changes: 83 additions & 0 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package telemetry
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"maps"
"net/http"
"os"
"runtime"
"slices"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -212,6 +216,7 @@ func (t *Tracker) bootProperties() map[string]interface{} {

props.Set("version", version.Version())
props.Set("os", runtime.GOOS)
props.Set("cluster-fingerprint", t.clusterFingerprint())

return props
}
Expand All @@ -232,6 +237,7 @@ func (t *Tracker) appProperties() map[string]interface{} {
}

props.Set("deploy", guessPlatform())
props.Set("cluster-fingerprint", t.clusterFingerprint())

// Features
props.Set("has-secret", t.config.Secret != "")
Expand Down Expand Up @@ -303,3 +309,80 @@ func guessPlatform() string {

return ""
}

// Try to generate a unique cluster fingerprint to identify events
// from different instances of the same cluster.
func (t *Tracker) clusterFingerprint() string {
platformID := platformServiceID()

if platformID != "" {
return generateDigest(platformID)
}

return generateDigest(
// Explicitly set env vars
anycableEnvVarsList(),
// Command line arguments
anycableCLIArgs(),
)
}

func platformServiceID() string {
if id, ok := os.LookupEnv("FLY_APP_NAME"); ok {
return id
}

if id, ok := os.LookupEnv("HEROKU_APP_ID"); ok {
return id
}

if id, ok := os.LookupEnv("RENDER_SERVICE_ID"); ok {
return id
}

if id, ok := os.LookupEnv("HATCHBOX_APP_NAME"); ok {
return id
}

if id, ok := os.LookupEnv("K_SERVICE"); ok {
return id
}

return ""
}

func generateDigest(parts ...string) string {
h := sha256.New()

for _, part := range parts {
if part != "" {
h.Write([]byte(part))
}
}

return fmt.Sprintf("%x", h.Sum(nil))
}

// Return a sorted list of AnyCable environment variables.
func anycableEnvVarsList() string {
pairs := os.Environ()
vars := []string{}

for _, pair := range pairs {
if strings.HasPrefix(pair, "ANYCABLE") {
vars = append(vars, pair)
}
}

slices.Sort(vars)

return strings.Join(vars, ",")
}

// Return a sorted list of AnyCable CLI arguments.
func anycableCLIArgs() string {
args := os.Args[1:]
slices.Sort(args)

return strings.Join(args, ",")
}

0 comments on commit 9636b05

Please sign in to comment.