diff --git a/pkg/query-service/app/server.go b/pkg/query-service/app/server.go index b79b7d011f..498d8b02f3 100644 --- a/pkg/query-service/app/server.go +++ b/pkg/query-service/app/server.go @@ -1,8 +1,11 @@ package app import ( + "bytes" "context" + "encoding/json" "fmt" + "io/ioutil" "net" "net/http" _ "net/http/pprof" // http profiler @@ -235,15 +238,62 @@ func (lrw *loggingResponseWriter) Flush() { lrw.ResponseWriter.(http.Flusher).Flush() } +func extractDashboardMetaData(path string, r *http.Request) (map[string]interface{}, bool) { + pathToExtractBodyFrom := "/api/v2/metrics/query_range" + var requestBody map[string]interface{} + data := map[string]interface{}{} + + if path == pathToExtractBodyFrom && (r.Method == "POST") { + bodyBytes, _ := ioutil.ReadAll(r.Body) + r.Body.Close() // must close + r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) + + json.Unmarshal(bodyBytes, &requestBody) + + } else { + return nil, false + } + + compositeMetricQuery, compositeMetricQueryExists := requestBody["compositeMetricQuery"] + compositeMetricQueryMap := compositeMetricQuery.(map[string]interface{}) + if compositeMetricQueryExists { + queryType, queryTypeExists := compositeMetricQueryMap["queryType"] + if queryTypeExists { + data["queryType"] = queryType + } + panelType, panelTypeExists := compositeMetricQueryMap["panelType"] + if panelTypeExists { + data["panelType"] = panelType + } + } + + datasource, datasourceExists := requestBody["dataSource"] + if datasourceExists { + data["datasource"] = datasource + } + + telemetry.GetInstance().SendEvent(telemetry.TELEMETRY_EVENT_DASHBOARDS_METADATA, data, false) + + return data, true +} + func (s *Server) analyticsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { route := mux.CurrentRoute(r) path, _ := route.GetPathTemplate() + dashboardMetadata, metadataExists := extractDashboardMetaData(path, r) + lrw := NewLoggingResponseWriter(w) next.ServeHTTP(lrw, r) data := map[string]interface{}{"path": path, "statusCode": lrw.statusCode} + if metadataExists { + for key, value := range dashboardMetadata { + data[key] = value + } + } + if telemetry.GetInstance().IsSampled() { if _, ok := telemetry.IgnoredPaths()[path]; !ok { telemetry.GetInstance().SendEvent(telemetry.TELEMETRY_EVENT_PATH, data) diff --git a/pkg/query-service/telemetry/telemetry.go b/pkg/query-service/telemetry/telemetry.go index e90207475a..0c1d014c1f 100644 --- a/pkg/query-service/telemetry/telemetry.go +++ b/pkg/query-service/telemetry/telemetry.go @@ -34,6 +34,7 @@ const ( TELEMETRY_EVENT_LANGUAGE = "Language" TELEMETRY_EVENT_LOGS_FILTERS = "Logs Filters" TELEMETRY_EVENT_DISTRIBUTED = "Distributed" + TELEMETRY_EVENT_DASHBOARDS_METADATA = "Dashboards Metadata" ) const api_key = "4Gmoa4ixJAUHx2BpJxsjwA1bEfnwEeRz" @@ -213,7 +214,12 @@ func (a *Telemetry) checkEvents(event string) bool { return sendEvent } -func (a *Telemetry) SendEvent(event string, data map[string]interface{}) { +func (a *Telemetry) SendEvent(event string, data map[string]interface{}, opts ...bool) { + + rateLimitFlag := true + if len(opts) > 0 { + rateLimitFlag = opts[0] + } if !a.isTelemetryEnabled() { return @@ -224,10 +230,12 @@ func (a *Telemetry) SendEvent(event string, data map[string]interface{}) { return } - if a.rateLimits[event] < RATE_LIMIT_VALUE { - a.rateLimits[event] += 1 - } else { - return + if rateLimitFlag { + if a.rateLimits[event] < RATE_LIMIT_VALUE { + a.rateLimits[event] += 1 + } else { + return + } } // zap.S().Info(data)