diff --git a/charts/fission-all/templates/deployment.yaml b/charts/fission-all/templates/deployment.yaml index e256b20a..00f4b51b 100644 --- a/charts/fission-all/templates/deployment.yaml +++ b/charts/fission-all/templates/deployment.yaml @@ -209,6 +209,13 @@ spec: value: {{ .Values.debugEnv | quote }} - name: TRACING_SAMPLING_RATE value: {{ .Values.traceSamplingRate | default "0.5" | quote }} +{{ if .Values.analytics }} + - name: ANALYTICS_URL + value: "https://g.fission.sh/metrics" +{{ else if .Values.analyticsNonHelmInstall }} + - name: ANALYTICS_URL + value: "https://g.fission.sh/metrics" +{{ end }} readinessProbe: httpGet: path: "/router-healthz" diff --git a/charts/fission-core/templates/deployment.yaml b/charts/fission-core/templates/deployment.yaml index db7f21a9..6a8725d5 100644 --- a/charts/fission-core/templates/deployment.yaml +++ b/charts/fission-core/templates/deployment.yaml @@ -211,6 +211,13 @@ spec: value: {{ .Values.routerRoundTripSvcAddressUpdateTimeout | default 30 | quote }} - name: DEBUG_ENV value: {{ .Values.debugEnv | quote }} +{{ if .Values.analytics }} + - name: ANALYTICS_URL + value: "https://g.fission.sh/metrics" +{{ else if .Values.analyticsNonHelmInstall }} + - name: ANALYTICS_URL + value: "https://g.fission.sh/metrics" +{{ end }} readinessProbe: httpGet: path: "/router-healthz" diff --git a/router/analytics.go b/router/analytics.go new file mode 100644 index 00000000..09adb5a6 --- /dev/null +++ b/router/analytics.go @@ -0,0 +1,61 @@ +package router + +import ( + "bytes" + "encoding/json" + "net/http" + "os" + "sync/atomic" + "time" + + "github.com/dchest/uniuri" +) + +type ( + Analytics struct { + id string + url string + } + AnalyticsData struct { + id string + FunctionCallCount uint64 `json:"FunctionCallCount"` + } +) + +func MakeAnalytics(url string) *Analytics { + + if len(url) == 0 { + url = os.Getenv("ANALYTICS_URL") + if len(url) == 0 { + return nil + } + } + + a := &Analytics{ + url: url, + id: uniuri.NewLen(8), + } + go a.run() + return a +} + +func (a *Analytics) gatherData() *AnalyticsData { + return &AnalyticsData{ + FunctionCallCount: atomic.LoadUint64(&globalFunctionCallCount), + } +} + +func (a *Analytics) run() { + ticker := time.NewTicker(24 * time.Hour) + for range ticker.C { + msg := a.gatherData() + msg.id = a.id + + msgbytes, err := json.Marshal(*msg) + if err != nil { + continue + } + + _, _ = http.Post(a.url, "application/json", bytes.NewReader(msgbytes)) + } +} diff --git a/router/metrics.go b/router/metrics.go index 3733cd61..19b6ce72 100644 --- a/router/metrics.go +++ b/router/metrics.go @@ -2,11 +2,14 @@ package router import ( "fmt" + "sync/atomic" "time" "github.com/prometheus/client_golang/prometheus" ) +var globalFunctionCallCount uint64 + type ( // functionLabels is the set of metrics labels that relate to // functions. @@ -116,6 +119,8 @@ func labelsToStrings(f *functionLabels, h *httpLabels) []string { } func functionCallCompleted(f *functionLabels, h *httpLabels, overhead, duration time.Duration, respSize int64) { + atomic.AddUint64(&globalFunctionCallCount, 1) + l := labelsToStrings(f, h) // overhead: time from request ingress into router upto proxing into function pod diff --git a/router/router.go b/router/router.go index 52d65d90..8f6d5bce 100644 --- a/router/router.go +++ b/router/router.go @@ -94,6 +94,8 @@ func Start(logger *zap.Logger, port int, executorUrl string) { // setup a signal handler for SIGTERM fission.SetupStackTraceHandler() + _ = MakeAnalytics("") + fmap := makeFunctionServiceMap(logger, time.Minute) frmap := makeFunctionRecorderMap(logger, time.Minute)