add userlog tracing (#6772)
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
@@ -4,12 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/cs3org/reva/v2/pkg/ctx"
|
||||
revactx "github.com/cs3org/reva/v2/pkg/ctx"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/roles"
|
||||
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
|
||||
settings "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
)
|
||||
|
||||
// HeaderAcceptLanguage is the header where the client can set the locale
|
||||
@@ -22,19 +22,25 @@ func (ul *UserlogService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// HandleGetEvents is the GET handler for events
|
||||
func (ul *UserlogService) HandleGetEvents(w http.ResponseWriter, r *http.Request) {
|
||||
u, ok := revactx.ContextGetUser(r.Context())
|
||||
ctx, span := tracer.Start(r.Context(), "HandleGetEvents")
|
||||
defer span.End()
|
||||
u, ok := revactx.ContextGetUser(ctx)
|
||||
if !ok {
|
||||
ul.log.Error().Int("returned statuscode", http.StatusUnauthorized).Msg("user unauthorized")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
evs, err := ul.GetEvents(r.Context(), u.GetId().GetOpaqueId())
|
||||
evs, err := ul.GetEvents(ctx, u.GetId().GetOpaqueId())
|
||||
if err != nil {
|
||||
ul.log.Error().Err(err).Int("returned statuscode", http.StatusInternalServerError).Msg("get events failed")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
span.SetAttributes(attribute.KeyValue{
|
||||
Key: "events",
|
||||
Value: attribute.IntValue(len(evs)),
|
||||
})
|
||||
|
||||
conv := ul.getConverter(r.Header.Get(HeaderAcceptLanguage))
|
||||
|
||||
@@ -61,7 +67,7 @@ func (ul *UserlogService) HandleGetEvents(w http.ResponseWriter, r *http.Request
|
||||
resp.OCS.Data = append(resp.OCS.Data, noti)
|
||||
}
|
||||
|
||||
glevs, err := ul.GetGlobalEvents()
|
||||
glevs, err := ul.GetGlobalEvents(ctx)
|
||||
if err != nil {
|
||||
ul.log.Error().Err(err).Int("returned statuscode", http.StatusInternalServerError).Msg("get global events failed")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -85,7 +91,7 @@ func (ul *UserlogService) HandleGetEvents(w http.ResponseWriter, r *http.Request
|
||||
|
||||
// HandleSSE is the GET handler for events
|
||||
func (ul *UserlogService) HandleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
u, ok := ctx.ContextGetUser(r.Context())
|
||||
u, ok := revactx.ContextGetUser(r.Context())
|
||||
if !ok {
|
||||
ul.log.Error().Msg("sse: no user in context")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -119,7 +125,7 @@ func (ul *UserlogService) HandlePostGlobalEvent(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
|
||||
if err := ul.StoreGlobalEvent(req.Type, req.Data); err != nil {
|
||||
if err := ul.StoreGlobalEvent(r.Context(), req.Type, req.Data); err != nil {
|
||||
ul.log.Error().Err(err).Msg("post: error storing global event")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -137,7 +143,7 @@ func (ul *UserlogService) HandleDeleteGlobalEvent(w http.ResponseWriter, r *http
|
||||
return
|
||||
}
|
||||
|
||||
if err := ul.DeleteGlobalEvents(req.IDs); err != nil {
|
||||
if err := ul.DeleteGlobalEvents(r.Context(), req.IDs); err != nil {
|
||||
ul.log.Error().Err(err).Int("returned statuscode", http.StatusInternalServerError).Msg("delete events failed")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -29,9 +29,17 @@ import (
|
||||
"github.com/r3labs/sse/v2"
|
||||
micrometadata "go-micro.dev/v4/metadata"
|
||||
"go-micro.dev/v4/store"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
var tracer trace.Tracer
|
||||
|
||||
func init() {
|
||||
tracer = otel.Tracer("github.com/owncloud/ocis/services/userlog/pkg/service")
|
||||
}
|
||||
|
||||
// UserlogService is the service responsible for user activities
|
||||
type UserlogService struct {
|
||||
log log.Logger
|
||||
@@ -43,7 +51,6 @@ type UserlogService struct {
|
||||
valueClient settingssvc.ValueService
|
||||
sse *sse.Server
|
||||
registeredEvents map[string]events.Unmarshaller
|
||||
translationPath string
|
||||
}
|
||||
|
||||
// NewUserlogService returns an EventHistory service
|
||||
@@ -54,7 +61,7 @@ func NewUserlogService(opts ...Option) (*UserlogService, error) {
|
||||
}
|
||||
|
||||
if o.Consumer == nil || o.Store == nil {
|
||||
return nil, fmt.Errorf("Need non nil consumer (%v) and store (%v) to work properly", o.Consumer, o.Store)
|
||||
return nil, fmt.Errorf("need non nil consumer (%v) and store (%v) to work properly", o.Consumer, o.Store)
|
||||
}
|
||||
|
||||
ch, err := events.Consume(o.Consumer, "userlog", o.RegisteredEvents...)
|
||||
@@ -191,6 +198,8 @@ func (ul *UserlogService) MemorizeEvents(ch <-chan events.Event) {
|
||||
|
||||
// GetEvents allows retrieving events from the eventhistory by userid
|
||||
func (ul *UserlogService) GetEvents(ctx context.Context, userid string) ([]*ehmsg.Event, error) {
|
||||
ctx, span := tracer.Start(ctx, "GetEvents")
|
||||
defer span.End()
|
||||
rec, err := ul.store.Read(userid)
|
||||
if err != nil && err != store.ErrNotFound {
|
||||
ul.log.Error().Err(err).Str("userid", userid).Msg("failed to read record from store")
|
||||
@@ -246,7 +255,9 @@ func (ul *UserlogService) DeleteEvents(userid string, evids []string) error {
|
||||
}
|
||||
|
||||
// StoreGlobalEvent will store a global event that will be returned with each `GetEvents` request
|
||||
func (ul *UserlogService) StoreGlobalEvent(typ string, data map[string]string) error {
|
||||
func (ul *UserlogService) StoreGlobalEvent(ctx context.Context, typ string, data map[string]string) error {
|
||||
ctx, span := tracer.Start(ctx, "StoreGlobalEvent")
|
||||
defer span.End()
|
||||
switch typ {
|
||||
default:
|
||||
return fmt.Errorf("unknown event type: %s", typ)
|
||||
@@ -277,7 +288,7 @@ func (ul *UserlogService) StoreGlobalEvent(typ string, data map[string]string) e
|
||||
return err
|
||||
}
|
||||
|
||||
return ul.alterGlobalEvents(func(evs map[string]json.RawMessage) error {
|
||||
return ul.alterGlobalEvents(ctx, func(evs map[string]json.RawMessage) error {
|
||||
evs[typ] = b
|
||||
return nil
|
||||
})
|
||||
@@ -286,7 +297,9 @@ func (ul *UserlogService) StoreGlobalEvent(typ string, data map[string]string) e
|
||||
}
|
||||
|
||||
// GetGlobalEvents will return all global events
|
||||
func (ul *UserlogService) GetGlobalEvents() (map[string]json.RawMessage, error) {
|
||||
func (ul *UserlogService) GetGlobalEvents(ctx context.Context) (map[string]json.RawMessage, error) {
|
||||
_, span := tracer.Start(ctx, "GetGlobalEvents")
|
||||
defer span.End()
|
||||
out := make(map[string]json.RawMessage)
|
||||
|
||||
recs, err := ul.store.Read(_globalEventsKey)
|
||||
@@ -304,8 +317,10 @@ func (ul *UserlogService) GetGlobalEvents() (map[string]json.RawMessage, error)
|
||||
}
|
||||
|
||||
// DeleteGlobalEvents will delete the specified event
|
||||
func (ul *UserlogService) DeleteGlobalEvents(evnames []string) error {
|
||||
return ul.alterGlobalEvents(func(evs map[string]json.RawMessage) error {
|
||||
func (ul *UserlogService) DeleteGlobalEvents(ctx context.Context, evnames []string) error {
|
||||
_, span := tracer.Start(ctx, "DeleteGlobalEvents")
|
||||
defer span.End()
|
||||
return ul.alterGlobalEvents(ctx, func(evs map[string]json.RawMessage) error {
|
||||
for _, name := range evnames {
|
||||
delete(evs, name)
|
||||
}
|
||||
@@ -390,8 +405,10 @@ func (ul *UserlogService) alterUserEventList(userid string, alter func([]string)
|
||||
})
|
||||
}
|
||||
|
||||
func (ul *UserlogService) alterGlobalEvents(alter func(map[string]json.RawMessage) error) error {
|
||||
evs, err := ul.GetGlobalEvents()
|
||||
func (ul *UserlogService) alterGlobalEvents(ctx context.Context, alter func(map[string]json.RawMessage) error) error {
|
||||
_, span := tracer.Start(ctx, "alterGlobalEvents")
|
||||
defer span.End()
|
||||
evs, err := ul.GetGlobalEvents(ctx)
|
||||
if err != nil && err != store.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
@@ -587,7 +604,7 @@ func getSpace(ctx context.Context, spaceID string, gatewaySelector pool.Selectab
|
||||
}
|
||||
|
||||
if res.GetStatus().GetCode() != rpc.Code_CODE_OK {
|
||||
return nil, fmt.Errorf("Error while getting space: (%v) %s", res.GetStatus().GetCode(), res.GetStatus().GetMessage())
|
||||
return nil, fmt.Errorf("error while getting space: (%v) %s", res.GetStatus().GetCode(), res.GetStatus().GetMessage())
|
||||
}
|
||||
|
||||
if len(res.StorageSpaces) == 0 {
|
||||
@@ -647,7 +664,7 @@ func getResource(ctx context.Context, resourceid *storageprovider.ResourceId, ga
|
||||
}
|
||||
|
||||
if res.GetStatus().GetCode() != rpc.Code_CODE_OK {
|
||||
return nil, fmt.Errorf("Unexpected status code while getting space: %v", res.GetStatus().GetCode())
|
||||
return nil, fmt.Errorf("unexpected status code while getting space: %v", res.GetStatus().GetCode())
|
||||
}
|
||||
|
||||
return res.GetInfo(), nil
|
||||
|
||||
Reference in New Issue
Block a user