05d9c7940d
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>
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package consoleapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
|
|
"storj.io/storj/satellite/analytics"
|
|
"storj.io/storj/satellite/console"
|
|
)
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// 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 := ioutil.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)
|
|
}
|
|
|
|
auth, err := console.GetAuth(ctx)
|
|
if err != nil {
|
|
a.serveJSONError(w, http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
if et.Link != "" {
|
|
a.analytics.TrackLinkEvent(et.EventName, auth.User.ID, auth.User.Email, et.Link)
|
|
} else {
|
|
a.analytics.TrackEvent(et.EventName, auth.User.ID, auth.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) {
|
|
serveJSONError(a.log, w, status, err)
|
|
}
|