mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-12 17:39:03 +08:00
chore: added sampling in analytics
This commit is contained in:
parent
4875652ecb
commit
99c0c97c1e
@ -232,10 +232,11 @@ func (s *Server) analyticsMiddleware(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(lrw, r)
|
next.ServeHTTP(lrw, r)
|
||||||
|
|
||||||
data := map[string]interface{}{"path": path, "statusCode": lrw.statusCode}
|
data := map[string]interface{}{"path": path, "statusCode": lrw.statusCode}
|
||||||
|
if telemetry.GetInstance().IsSampled() {
|
||||||
if _, ok := telemetry.IgnoredPaths()[path]; !ok {
|
if _, ok := telemetry.IgnoredPaths()[path]; !ok {
|
||||||
telemetry.GetInstance().SendEvent(telemetry.TELEMETRY_EVENT_PATH, data)
|
telemetry.GetInstance().SendEvent(telemetry.TELEMETRY_EVENT_PATH, data)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package telemetry
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -23,6 +24,7 @@ const (
|
|||||||
TELEMETRY_EVENT_NUMBER_OF_SERVICES = "Number of Services"
|
TELEMETRY_EVENT_NUMBER_OF_SERVICES = "Number of Services"
|
||||||
TELEMETRY_EVENT_HEART_BEAT = "Heart Beat"
|
TELEMETRY_EVENT_HEART_BEAT = "Heart Beat"
|
||||||
TELEMETRY_EVENT_ORG_SETTINGS = "Org Settings"
|
TELEMETRY_EVENT_ORG_SETTINGS = "Org Settings"
|
||||||
|
DEFAULT_SAMPLING = 0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
const api_key = "4Gmoa4ixJAUHx2BpJxsjwA1bEfnwEeRz"
|
const api_key = "4Gmoa4ixJAUHx2BpJxsjwA1bEfnwEeRz"
|
||||||
@ -35,6 +37,18 @@ const HEART_BEAT_DURATION = 6 * time.Hour
|
|||||||
var telemetry *Telemetry
|
var telemetry *Telemetry
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
|
|
||||||
|
func (a *Telemetry) IsSampled() bool {
|
||||||
|
|
||||||
|
number := a.minRandInt + rand.Intn(a.maxRandInt-a.minRandInt)
|
||||||
|
|
||||||
|
if (number % a.maxRandInt) == 0 {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
type Telemetry struct {
|
type Telemetry struct {
|
||||||
operator analytics.Client
|
operator analytics.Client
|
||||||
ipAddress string
|
ipAddress string
|
||||||
@ -43,6 +57,8 @@ type Telemetry struct {
|
|||||||
distinctId string
|
distinctId string
|
||||||
reader interfaces.Reader
|
reader interfaces.Reader
|
||||||
companyDomain string
|
companyDomain string
|
||||||
|
minRandInt int
|
||||||
|
maxRandInt int
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTelemetry() {
|
func createTelemetry() {
|
||||||
@ -50,6 +66,10 @@ func createTelemetry() {
|
|||||||
operator: analytics.New(api_key),
|
operator: analytics.New(api_key),
|
||||||
ipAddress: getOutboundIP(),
|
ipAddress: getOutboundIP(),
|
||||||
}
|
}
|
||||||
|
telemetry.minRandInt = 0
|
||||||
|
telemetry.maxRandInt = int(1 / DEFAULT_SAMPLING)
|
||||||
|
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
data := map[string]interface{}{}
|
data := map[string]interface{}{}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user