Add simple anonymous usage metrics (#1167)

This change reports total function call count to an analytics url once a day.

It's done behind the existing analytics flag.
This commit is contained in:
Soam Vasani
2019-04-30 15:48:04 -07:00
committed by GitHub
parent 9a3cc517d0
commit 23f3585245
5 changed files with 82 additions and 0 deletions
@@ -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"
@@ -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"
+61
View File
@@ -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))
}
}
+5
View File
@@ -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
+2
View File
@@ -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)