From 11c9df6f006a36b30545063284b9653125bd9ff7 Mon Sep 17 00:00:00 2001
From: Evgenii Stratonikov <evgeniy@nspcc.ru>
Date: Tue, 19 Jul 2022 14:27:12 +0300
Subject: [PATCH] [#1611] shard: Print shard ID in component logs

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
---
 pkg/local_object_storage/blobstor/blobstor.go     |  5 +++++
 pkg/local_object_storage/metabase/db.go           |  5 +++++
 pkg/local_object_storage/shard/id.go              | 11 +++++++++++
 pkg/local_object_storage/writecache/writecache.go |  6 ++++++
 4 files changed, 27 insertions(+)

diff --git a/pkg/local_object_storage/blobstor/blobstor.go b/pkg/local_object_storage/blobstor/blobstor.go
index 2a30279ff..4dbd52f5b 100644
--- a/pkg/local_object_storage/blobstor/blobstor.go
+++ b/pkg/local_object_storage/blobstor/blobstor.go
@@ -91,6 +91,11 @@ func New(opts ...Option) *BlobStor {
 	}
 }
 
+// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
+func (b *BlobStor) SetLogger(l *zap.Logger) {
+	b.log = l
+}
+
 // WithShallowDepth returns option to set the
 // depth of the object file subdirectory tree.
 //
diff --git a/pkg/local_object_storage/metabase/db.go b/pkg/local_object_storage/metabase/db.go
index 7f15af2a5..4e3ce1cf3 100644
--- a/pkg/local_object_storage/metabase/db.go
+++ b/pkg/local_object_storage/metabase/db.go
@@ -251,6 +251,11 @@ func bucketKeyHelper(hdr string, val string) []byte {
 	}
 }
 
+// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
+func (db *DB) SetLogger(l *zap.Logger) {
+	db.log = l
+}
+
 // WithLogger returns option to set logger of DB.
 func WithLogger(l *logger.Logger) Option {
 	return func(c *cfg) {
diff --git a/pkg/local_object_storage/shard/id.go b/pkg/local_object_storage/shard/id.go
index a7fdd56a9..a72184269 100644
--- a/pkg/local_object_storage/shard/id.go
+++ b/pkg/local_object_storage/shard/id.go
@@ -2,6 +2,7 @@ package shard
 
 import (
 	"github.com/mr-tron/base58"
+	"go.uber.org/zap"
 )
 
 // ID represents Shard identifier.
@@ -41,6 +42,16 @@ func (s *Shard) UpdateID() (err error) {
 	}
 	if len(id) != 0 {
 		s.info.ID = NewIDFromBytes(id)
+	}
+
+	s.log = s.log.With(zap.String("shard_id", s.info.ID.String()))
+	s.metaBase.SetLogger(s.log)
+	s.blobStor.SetLogger(s.log)
+	if s.hasWriteCache() {
+		s.writeCache.SetLogger(s.log)
+	}
+
+	if len(id) != 0 {
 		return nil
 	}
 	return s.metaBase.WriteShardID(*s.info.ID)
diff --git a/pkg/local_object_storage/writecache/writecache.go b/pkg/local_object_storage/writecache/writecache.go
index 52e0f8e81..29c848bde 100644
--- a/pkg/local_object_storage/writecache/writecache.go
+++ b/pkg/local_object_storage/writecache/writecache.go
@@ -24,6 +24,7 @@ type Cache interface {
 	Iterate(IterationPrm) error
 	Put(*object.Object) error
 	SetMode(Mode)
+	SetLogger(*zap.Logger)
 	DumpInfo() Info
 
 	Init() error
@@ -111,6 +112,11 @@ func New(opts ...Option) Cache {
 	return c
 }
 
+// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
+func (c *cache) SetLogger(l *zap.Logger) {
+	c.log = l
+}
+
 func (c *cache) DumpInfo() Info {
 	return Info{
 		Path: c.path,