Skip to content
Snippets Groups Projects
Commit 57c00ab4 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Merge branch 'fix/PRXS-2060-FixMissingClientID' into 'master'

Исправлено отсутствие client_id в записи лога в поле callerId

See merge request perxis/perxis-go!180
parents e6509d32 e2fcb132
No related branches found
No related tags found
No related merge requests found
......@@ -77,7 +77,7 @@ func TestExample(t *testing.T) {
logger.Info("Item created",
logzap.Event(items.EventCreateItem),
logzap.Object(item),
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, item.SpaceID),
logzap.Tags("tag1", "tag2", "tag3"),
)
......@@ -85,7 +85,7 @@ func TestExample(t *testing.T) {
logger.Warn("Item updated",
logzap.Event(items.EventUpdateItem),
logzap.Object(item),
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, item.SpaceID),
logzap.Attr(map[string]map[string]any{"title": {"old": "old title", "new": "new title"}}),
)
}
......
......@@ -25,8 +25,12 @@ func LoggingMiddleware(logger *zap.Logger) Middleware {
}
func (m *loggingMiddleware) Create(ctx context.Context, collection *collections.Collection) (created *collections.Collection, err error) {
var spaceID string
if collection != nil {
spaceID = collection.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(collections.EventCollectionCreate),
)
......@@ -42,7 +46,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections.
func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId string, collectionId string) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Event(collections.EventCollectionDelete),
logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)),
)
......@@ -59,7 +63,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId st
func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, options ...*collections.GetOptions) (collection *collections.Collection, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
collection, err = m.next.Get(ctx, spaceId, envId, collectionId, options...)
......@@ -73,7 +77,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin
func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId string, filter *collections.Filter) (collections []*collections.Collection, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
collections, err = m.next.List(ctx, spaceId, envId, filter)
......@@ -87,7 +91,7 @@ func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId stri
func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId string, collectionId string, schema *schema.Schema) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Event(collections.EventCollectionSetSchema),
logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)),
)
......@@ -104,7 +108,7 @@ func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId
func (m *loggingMiddleware) SetState(ctx context.Context, spaceId string, envId string, collectionId string, state *collections.StateInfo) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
err = m.next.SetState(ctx, spaceId, envId, collectionId, state)
......@@ -118,8 +122,12 @@ func (m *loggingMiddleware) SetState(ctx context.Context, spaceId string, envId
}
func (m *loggingMiddleware) Update(ctx context.Context, coll *collections.Collection) (err error) {
var spaceID string
if coll != nil {
spaceID = coll.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(collections.EventCollectionUpdate),
logzap.Object(coll),
)
......
......@@ -26,7 +26,7 @@ func LoggingMiddleware(logger *zap.Logger) Middleware {
func (m *loggingMiddleware) Aggregate(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregateOptions) (result map[string]interface{}, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
result, err = m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
......@@ -40,7 +40,7 @@ func (m *loggingMiddleware) Aggregate(ctx context.Context, spaceId string, envId
func (m *loggingMiddleware) AggregatePublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
result, err = m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
......@@ -53,8 +53,12 @@ func (m *loggingMiddleware) AggregatePublished(ctx context.Context, spaceId stri
}
func (m *loggingMiddleware) Archive(ctx context.Context, item *items.Item, options ...*items.ArchiveOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventArchiveItem),
logzap.Object(item),
)
......@@ -70,8 +74,12 @@ func (m *loggingMiddleware) Archive(ctx context.Context, item *items.Item, optio
}
func (m *loggingMiddleware) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventCreateItem),
)
......@@ -86,8 +94,12 @@ func (m *loggingMiddleware) Create(ctx context.Context, item *items.Item, opts .
}
func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, options ...*items.DeleteOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventDeleteItem),
logzap.Object(item),
)
......@@ -104,7 +116,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, option
func (m *loggingMiddleware) Find(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
items, total, err = m.next.Find(ctx, spaceId, envId, collectionId, filter, options...)
......@@ -119,7 +131,7 @@ func (m *loggingMiddleware) Find(ctx context.Context, spaceId string, envId stri
func (m *loggingMiddleware) FindArchived(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindArchivedOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
items, total, err = m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...)
......@@ -133,7 +145,7 @@ func (m *loggingMiddleware) FindArchived(ctx context.Context, spaceId string, en
func (m *loggingMiddleware) FindPublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
)
items, total, err = m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
......@@ -147,7 +159,7 @@ func (m *loggingMiddleware) FindPublished(ctx context.Context, spaceId string, e
func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
......@@ -162,7 +174,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin
func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
......@@ -177,7 +189,7 @@ func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, en
func (m *loggingMiddleware) GetRevision(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, revisionId string, options ...*items.GetRevisionOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
......@@ -191,8 +203,12 @@ func (m *loggingMiddleware) GetRevision(ctx context.Context, spaceId string, env
}
func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, opts ...*items.IntrospectOptions) (itm *items.Item, sch *schema.Schema, err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Object(item),
)
......@@ -207,7 +223,7 @@ func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, op
func (m *loggingMiddleware) ListRevisions(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.ListRevisionsOptions) (items []*items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceId),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
......@@ -221,8 +237,12 @@ func (m *loggingMiddleware) ListRevisions(ctx context.Context, spaceId string, e
}
func (m *loggingMiddleware) Publish(ctx context.Context, item *items.Item, options ...*items.PublishOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventPublishItem),
logzap.Object(item),
)
......@@ -238,8 +258,12 @@ func (m *loggingMiddleware) Publish(ctx context.Context, item *items.Item, optio
}
func (m *loggingMiddleware) Unarchive(ctx context.Context, item *items.Item, options ...*items.UnarchiveOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventUnarchiveItem),
logzap.Object(item),
)
......@@ -255,8 +279,12 @@ func (m *loggingMiddleware) Unarchive(ctx context.Context, item *items.Item, opt
}
func (m *loggingMiddleware) Undelete(ctx context.Context, item *items.Item, options ...*items.UndeleteOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventUndeleteItem),
logzap.Object(item),
)
......@@ -272,8 +300,12 @@ func (m *loggingMiddleware) Undelete(ctx context.Context, item *items.Item, opti
}
func (m *loggingMiddleware) Unpublish(ctx context.Context, item *items.Item, options ...*items.UnpublishOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventUnpublishItem),
logzap.Object(item),
)
......@@ -289,8 +321,12 @@ func (m *loggingMiddleware) Unpublish(ctx context.Context, item *items.Item, opt
}
func (m *loggingMiddleware) Update(ctx context.Context, item *items.Item, options ...*items.UpdateOptions) (err error) {
var spaceID string
if item != nil {
spaceID = item.SpaceID
}
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.CallerFromContext(ctx, spaceID),
logzap.Event(items.EventUpdateItem),
logzap.Object(item),
)
......
......@@ -55,8 +55,14 @@ func Caller(v any) zap.Field {
}
// CallerFromContext извлекает auth.Principal из контекста и устанавливает его в качестве "вызывающего" в формате Object.
func CallerFromContext(ctx context.Context) zap.Field {
return Caller(auth.GetPrincipal(ctx))
// Вторым параметром передается идентификатор пространства, который требуется, если вызывающий является auth.SpaceAccessor.
// Если вызывающий не связан с пространством, следует передать пустую строку.
func CallerFromContext(ctx context.Context, spaceID string) zap.Field {
principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok && spaceID != "" {
principal = accessor.Space(spaceID)
}
return Caller(principal)
}
func Attr(attr any) zap.Field {
......
......@@ -167,8 +167,8 @@ func TestCallerFromContext(t *testing.T) {
field zap.Field
want zap.Field
}{
{name: "ok", field: CallerFromContext(ctx), want: zap.Reflect("caller", oid)},
{name: "invalid", field: CallerFromContext(context.TODO()), want: zap.Reflect("caller", (*id.ObjectId)(nil))},
{name: "ok", field: CallerFromContext(ctx, ""), want: zap.Reflect("caller", oid)},
{name: "invalid", field: CallerFromContext(context.TODO(), ""), want: zap.Reflect("caller", (*id.ObjectId)(nil))},
}
for _, tc := range tests {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment