storj/satellite/console/consoleweb/consoleapi/analytics.go

112 lines
2.9 KiB
Go
Raw Normal View History

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleapi
import (
"encoding/json"
"io"
"net/http"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/private/web"
"storj.io/storj/satellite/analytics"
"storj.io/storj/satellite/console"
)
satellite/.../consoleapi: Respond with 401 on unauth req (#4781) Respond with the appropriate HTTP status code when a request to the analytics trigger event handler receive an authorized request. A part of fixing the response status code this will stop to log these response with ERROR level in our satellite logs. Example of error message found in our satellite logs: { "insertId": "0ljf1cfn4xroxfd6", "jsonPayload": { "N": "console:endpoint", "T": "2022-05-06T13:31:35.415Z", "errorVerbose": "unauthorized: http: named cookie not present\n\tstorj.io/storj/satellite/console.GetAuth:72\n\tstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered:60\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1:488\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tgithub.com/gorilla/mux.(*Router).ServeHTTP:210\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:495\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tnet/http.serverHandler.ServeHTTP:2879\n\tnet/http.(*conn).serve:1930", "L": "ERROR", "error": "unauthorized: http: named cookie not present", "message": "unauthorized: http: named cookie not present", "code": 500, "S": "storj.io/storj/satellite/console/consoleweb/consoleapi.serveCustomJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:37\nstorj.io/storj/satellite/console/consoleweb/consoleapi.serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:23\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:75\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:62\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:488\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210\nstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:495\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1930", "M": "returning error to client" }, "resource": { "type": "k8s_container", "labels": { "location": "us-central1", "pod_name": "us-central1-satellite-api-77c47f5c5-dzrpj", "project_id": "storj-prod", "namespace_name": "satellite", "container_name": "satellite", "cluster_name": "us-central1-gke-manatee" } }, "timestamp": "2022-05-06T13:31:35.416050390Z", "severity": "ERROR", "labels": { "k8s-pod/version": "v3", "k8s-pod/app": "us-central1-satellite-api", "compute.googleapis.com/resource_name": "gke-us-central1-gke--terraform-202110-97ff1891-t0fv", "k8s-pod/service": "api", "k8s-pod/pod-template-hash": "77c47f5c5" }, "logName": "projects/storj-prod/logs/stderr", "receiveTimestamp": "2022-05-06T13:31:37.419991630Z" } Change-Id: I7cfcfb500b7878c59b1d259683c92e8963e2dc3f Co-authored-by: Stefan Benten <mail@stefan-benten.de>
2022-05-08 11:35:42 +01:00
// ErrAnalyticsAPI - console analytics api error type.
var ErrAnalyticsAPI = errs.Class("consoleapi analytics")
// Analytics is an api controller that exposes analytics related functionality.
type Analytics struct {
log *zap.Logger
service *console.Service
analytics *analytics.Service
}
// NewAnalytics is a constructor for api analytics controller.
func NewAnalytics(log *zap.Logger, service *console.Service, a *analytics.Service) *Analytics {
return &Analytics{
log: log,
service: service,
analytics: a,
}
}
type eventTriggeredBody struct {
EventName string `json:"eventName"`
Link string `json:"link"`
ErrorEventSource string `json:"errorEventSource"`
Props map[string]string `json:"props"`
}
type pageVisitBody struct {
PageName string `json:"pageName"`
}
// EventTriggered tracks the occurrence of an arbitrary event on the client.
func (a *Analytics) EventTriggered(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
body, err := io.ReadAll(r.Body)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
}
var et eventTriggeredBody
err = json.Unmarshal(body, &et)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
}
user, err := console.GetUser(ctx)
if err != nil {
satellite/.../consoleapi: Respond with 401 on unauth req (#4781) Respond with the appropriate HTTP status code when a request to the analytics trigger event handler receive an authorized request. A part of fixing the response status code this will stop to log these response with ERROR level in our satellite logs. Example of error message found in our satellite logs: { "insertId": "0ljf1cfn4xroxfd6", "jsonPayload": { "N": "console:endpoint", "T": "2022-05-06T13:31:35.415Z", "errorVerbose": "unauthorized: http: named cookie not present\n\tstorj.io/storj/satellite/console.GetAuth:72\n\tstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered:60\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1:488\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tgithub.com/gorilla/mux.(*Router).ServeHTTP:210\n\tstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1:495\n\tnet/http.HandlerFunc.ServeHTTP:2047\n\tnet/http.serverHandler.ServeHTTP:2879\n\tnet/http.(*conn).serve:1930", "L": "ERROR", "error": "unauthorized: http: named cookie not present", "message": "unauthorized: http: named cookie not present", "code": 500, "S": "storj.io/storj/satellite/console/consoleweb/consoleapi.serveCustomJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:37\nstorj.io/storj/satellite/console/consoleweb/consoleapi.serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/common.go:23\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).serveJSONError\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:75\nstorj.io/storj/satellite/console/consoleweb/consoleapi.(*Analytics).EventTriggered\n\t/go/src/storj.io/storj/satellite/console/consoleweb/consoleapi/analytics.go:62\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nstorj.io/storj/satellite/console/consoleweb.(*Server).withAuth.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:488\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210\nstorj.io/storj/satellite/console/consoleweb.(*Server).withRequest.func1\n\t/go/src/storj.io/storj/satellite/console/consoleweb/server.go:495\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2047\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1930", "M": "returning error to client" }, "resource": { "type": "k8s_container", "labels": { "location": "us-central1", "pod_name": "us-central1-satellite-api-77c47f5c5-dzrpj", "project_id": "storj-prod", "namespace_name": "satellite", "container_name": "satellite", "cluster_name": "us-central1-gke-manatee" } }, "timestamp": "2022-05-06T13:31:35.416050390Z", "severity": "ERROR", "labels": { "k8s-pod/version": "v3", "k8s-pod/app": "us-central1-satellite-api", "compute.googleapis.com/resource_name": "gke-us-central1-gke--terraform-202110-97ff1891-t0fv", "k8s-pod/service": "api", "k8s-pod/pod-template-hash": "77c47f5c5" }, "logName": "projects/storj-prod/logs/stderr", "receiveTimestamp": "2022-05-06T13:31:37.419991630Z" } Change-Id: I7cfcfb500b7878c59b1d259683c92e8963e2dc3f Co-authored-by: Stefan Benten <mail@stefan-benten.de>
2022-05-08 11:35:42 +01:00
a.serveJSONError(w, http.StatusUnauthorized, err)
return
}
if et.ErrorEventSource != "" {
a.analytics.TrackErrorEvent(user.ID, user.Email, et.ErrorEventSource)
} else if et.Link != "" {
a.analytics.TrackLinkEvent(et.EventName, user.ID, user.Email, et.Link)
} else {
a.analytics.TrackEvent(et.EventName, user.ID, user.Email, et.Props)
}
w.WriteHeader(http.StatusOK)
}
// PageEventTriggered tracks the occurrence of an arbitrary page visit event on the client.
func (a *Analytics) PageEventTriggered(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
body, err := io.ReadAll(r.Body)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
}
var pv pageVisitBody
err = json.Unmarshal(body, &pv)
if err != nil {
a.serveJSONError(w, http.StatusInternalServerError, err)
}
user, err := console.GetUser(ctx)
if err != nil {
a.serveJSONError(w, http.StatusUnauthorized, err)
return
}
a.analytics.PageVisitEvent(pv.PageName, user.ID, user.Email)
w.WriteHeader(http.StatusOK)
}
// serveJSONError writes JSON error to response output stream.
func (a *Analytics) serveJSONError(w http.ResponseWriter, status int, err error) {
web.ServeJSONError(a.log, w, status, err)
}