Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init customized info #381

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/check_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func runCheckConstructionCmd(_ *cobra.Command, _ []string) error {

g, ctx := errgroup.WithContext(ctx)
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)

g.Go(func() error {
return constructionTester.StartPeriodicLogger(ctx)
Expand Down
1 change: 1 addition & 0 deletions cmd/check_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {

g, ctx := errgroup.WithContext(ctx)
ctx = logger.AddRequestUUIDToContext(ctx, Config.RequestUUID)
ctx = logger.AddInfoMetaDataToContext(ctx, Config.InfoMetaData)

g.Go(func() error {
return dataTester.StartPeriodicLogger(ctx)
Expand Down
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
tableSize int64
requestUUID string
statusPort uint
InfoMetaData string

// Config is the populated *configuration.Configuration from
// the configurationFile. If none is provided, this is set
Expand Down Expand Up @@ -289,6 +290,13 @@ default values.`,
"requestUUID configures the requestUUID in logs, which aims to enable search logs by requestUUID",
)

checkDataCmd.Flags().StringVar(
&InfoMetaData,
"info-metadata",
"",
"metadata configures the metadata which aims to show in logs",
)

checkDataCmd.Flags().UintVar(
&statusPort,
"status-port",
Expand Down Expand Up @@ -441,6 +449,10 @@ func initConfig() {
Config.Construction.StatusPort = statusPort
}
}

if len(InfoMetaData) != 0 {
Config.InfoMetaData = InfoMetaData
}
}

func ensureDataDirectoryExists() {
Expand Down
4 changes: 4 additions & 0 deletions configuration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ type Configuration struct {
// then this value must be true.
CoinSupported bool `json:"coin_supported"`

// InfoMetaData is a map of key:value
// which aims to show in the log for search
InfoMetaData string `json:"info_metadata,omitempty"`

Construction *ConstructionConfiguration `json:"construction"`
Data *DataConfiguration `json:"data"`
Perf *CheckPerfConfiguration `json:"perf"`
Expand Down
77 changes: 76 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"os"
"path"
"strings"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -43,6 +44,8 @@ type contextKey int
const (
RequestUUID contextKey = iota

InfoMetaDataKey contextKey = iota

// blockStreamFile contains the stream of processed
// blocks and whether they were added or removed.
blockStreamFile = "blocks.txt"
Expand Down Expand Up @@ -83,6 +86,7 @@ type Logger struct {
logBalanceChanges bool
logReconciliation bool
logRequestUUID string
logInfoMetaData map[string]string

lastStatsMessage string
lastProgressMessage string
Expand All @@ -100,6 +104,7 @@ func NewLogger(
checkType CheckType,
network *types.NetworkIdentifier,
logRequestUUID string,
logInfoMetaData map[string]string,
fields ...zap.Field,
) (*Logger, error) {
zapLogger, err := buildZapLogger(checkType, network, logRequestUUID, fields...)
Expand All @@ -113,6 +118,7 @@ func NewLogger(
logBalanceChanges: logBalanceChanges,
logReconciliation: logReconciliation,
logRequestUUID: logRequestUUID,
logInfoMetaData: logInfoMetaData,
zapLogger: zapLogger,
}, nil
}
Expand Down Expand Up @@ -161,6 +167,7 @@ func (l *Logger) LogDataStatus(ctx context.Context, status *results.CheckDataSta
)

statsMessage = AddRequestUUID(statsMessage, l.logRequestUUID)
statsMessage = AddInfoMetaData(statsMessage, l.logInfoMetaData)

// Don't print out the same stats message twice.
if statsMessage == l.lastStatsMessage {
Expand All @@ -187,6 +194,7 @@ func (l *Logger) LogDataStatus(ctx context.Context, status *results.CheckDataSta
)

progressMessage = AddRequestUUID(progressMessage, l.logRequestUUID)
progressMessage = AddInfoMetaData(progressMessage, l.logInfoMetaData)

// Don't print out the same progress message twice.
if progressMessage == l.lastProgressMessage {
Expand Down Expand Up @@ -216,7 +224,8 @@ func (l *Logger) LogConstructionStatus(
}

statsMessage = AddRequestUUID(statsMessage, l.logRequestUUID)

statsMessage = AddInfoMetaData(statsMessage, l.logInfoMetaData)

l.lastStatsMessage = statsMessage
color.Cyan(statsMessage)
}
Expand All @@ -232,6 +241,7 @@ func LogMemoryStats(ctx context.Context) {
memUsage.GarbageCollections,
)
statsMessage = AddRequestUUIDFromContext(ctx, statsMessage)
statsMessage = AddInfoMetaDataFromContext(ctx, statsMessage)
color.Cyan(statsMessage)
}

Expand Down Expand Up @@ -267,6 +277,7 @@ func (l *Logger) AddBlockStream(
block.ParentBlockIdentifier.Hash,
)
blockString = AddRequestUUID(blockString, l.logRequestUUID)
blockString = AddInfoMetaData(blockString, l.logInfoMetaData)
color.Cyan(blockString)
if _, err := f.WriteString(blockString); err != nil {
return fmt.Errorf("failed to write block string %s: %w", blockString, err)
Expand Down Expand Up @@ -305,6 +316,7 @@ func (l *Logger) RemoveBlockStream(
block.Hash,
)
blockString = AddRequestUUID(blockString, l.logRequestUUID)
blockString = AddInfoMetaData(blockString, l.logInfoMetaData)
color.Cyan(blockString)
_, err = f.WriteString(blockString)
if err != nil {
Expand Down Expand Up @@ -347,6 +359,7 @@ func (l *Logger) TransactionStream(
block.BlockIdentifier.Hash,
)
transactionString = AddRequestUUID(transactionString, l.logRequestUUID)
transactionString = AddInfoMetaData(transactionString, l.logInfoMetaData)
color.Cyan(transactionString)
_, err = f.WriteString(transactionString)
if err != nil {
Expand Down Expand Up @@ -383,6 +396,7 @@ func (l *Logger) TransactionStream(
*op.Status,
)
transactionOperationString = AddRequestUUID(transactionOperationString, l.logRequestUUID)
transactionOperationString = AddInfoMetaData(transactionOperationString, l.logInfoMetaData)
color.Cyan(transactionOperationString)
_, err = f.WriteString(transactionOperationString)
if err != nil {
Expand Down Expand Up @@ -429,6 +443,7 @@ func (l *Logger) BalanceStream(
balanceChange.Block.Hash,
)
balanceLog = AddRequestUUID(balanceLog, l.logRequestUUID)
balanceLog = AddInfoMetaData(balanceLog, l.logInfoMetaData)
color.Cyan(balanceLog)
if _, err := f.WriteString(fmt.Sprintf("%s\n", balanceLog)); err != nil {
err = fmt.Errorf("failed to write balance log %s: %w", balanceLog, err)
Expand Down Expand Up @@ -473,6 +488,7 @@ func (l *Logger) ReconcileSuccessStream(
block.Index,
)
reconciledLog = AddRequestUUID(reconciledLog, l.logRequestUUID)
reconciledLog = AddInfoMetaData(reconciledLog, l.logInfoMetaData)
color.Cyan(reconciledLog)

reconciliationSuccessString := fmt.Sprintf(
Expand All @@ -485,6 +501,7 @@ func (l *Logger) ReconcileSuccessStream(
block.Hash,
)
reconciliationSuccessString = AddRequestUUID(reconciliationSuccessString, l.logRequestUUID)
reconciliationSuccessString = AddInfoMetaData(reconciliationSuccessString, l.logInfoMetaData)
color.Cyan(reconciliationSuccessString)

_, err = f.WriteString(reconciliationSuccessString)
Expand Down Expand Up @@ -558,6 +575,7 @@ func (l *Logger) ReconcileFailureStream(
liveBalance,
)
reconciliationFailureString = AddRequestUUID(reconciliationFailureString, l.logRequestUUID)
reconciliationFailureString = AddInfoMetaData(reconciliationFailureString, l.logInfoMetaData)
color.Cyan(reconciliationFailureString)
_, err = f.WriteString(reconciliationFailureString)
if err != nil {
Expand Down Expand Up @@ -649,3 +667,60 @@ func requestUUIDFromContext(ctx context.Context) string {
return ""
}
}

// Add InfoMetaData k-v pairs to the tip
func AddInfoMetaDataFromContext(ctx context.Context, msg string) string {
logInfoMetaData := InfoMetaDataFromContext(ctx)
if len(logInfoMetaData) != 0 {
for k, v := range logInfoMetaData {
msg = fmt.Sprintf("%s, %s: %s", msg, k, v)
}
}
return msg
}

// Add InfoMetaData k-v pairs to the tip
func AddInfoMetaData(msg string, logInfoMetaData map[string]string) string {
if len(logInfoMetaData) != 0 {
for k, v := range logInfoMetaData {
msg = fmt.Sprintf("%s, %s: %s", msg, k, v)
}
}
return msg
}

// AddInfoMetaDataToContext will add InfoMetaData to the context, and return the new context
func AddInfoMetaDataToContext(ctx context.Context, InfoMetaData string) context.Context {
return context.WithValue(ctx, InfoMetaDataKey, InfoMetaData)
}

// InfoMetaDataFromContext is used to extract InfoMetaData from a context
func InfoMetaDataFromContext(ctx context.Context) map[string]string {
var metadata string
switch v := ctx.Value(InfoMetaDataKey).(type) {
case string:
metadata = v
default:
metadata = ""
}
return ConvertStringToMap(metadata)
}

// ConvertStringToMap is used to convert a string to map by split , and ;
func ConvertStringToMap(metadata string) map[string]string {
InfoMetaDataMap := make(map[string]string)
if len(metadata) == 0 {
return InfoMetaDataMap
}
pairs := strings.Split(metadata, ",")
for _, pair := range pairs {
kv := strings.Split(pair, ":")
if(len(kv) != 2) {
log := fmt.Sprintf("the %s from %s could be transfer to key value pair", pair, metadata)
color.Yellow(log)
} else {
InfoMetaDataMap[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
}
return InfoMetaDataMap
}
2 changes: 2 additions & 0 deletions pkg/tester/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func InitializeConstruction(
}

counterStorage := modules.NewCounterStorage(localStore)
logInfoMetaData := logger.ConvertStringToMap(config.InfoMetaData)
logger, err := logger.NewLogger(
dataPath,
false,
Expand All @@ -124,6 +125,7 @@ func InitializeConstruction(
logger.Construction,
network,
config.RequestUUID,
logInfoMetaData,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize logger with error: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/tester/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func InitializeData(
blockStorage := modules.NewBlockStorage(localStore, config.SerialBlockWorkers)
balanceStorage := modules.NewBalanceStorage(localStore)

logInfoMetaData := logger.ConvertStringToMap(config.InfoMetaData)
logger, err := logger.NewLogger(
dataPath,
config.Data.LogBlocks,
Expand All @@ -241,6 +242,7 @@ func InitializeData(
logger.Data,
network,
config.RequestUUID,
logInfoMetaData,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize logger with error: %w", err)
Expand Down Expand Up @@ -1089,6 +1091,7 @@ func (t *DataTester) recursiveOpSearch(
logger.Data,
t.network,
EmptyRequestUUID,
nil,
)

if err != nil {
Expand Down