Feat: Update the logger config to show the parent_id

This commit is contained in:
Manendra Pal Singh
2026-01-28 21:29:37 +05:30
parent a42159ddb4
commit 8948479dea
9 changed files with 104 additions and 13 deletions

View File

@@ -11,6 +11,7 @@ import (
"sync"
"time"
"github.com/beckn-one/beckn-onix/pkg/model"
"gopkg.in/yaml.v2"
"github.com/beckn-one/beckn-onix/core/module"
@@ -52,7 +53,7 @@ var runFunc = run
func main() {
// Define and parse command-line flags.
flag.StringVar(&configPath, "config", "../../config/onix/adapter.yaml", "Path to the configuration file")
flag.StringVar(&configPath, "config", "config/onix/adapter.yaml", "Path to the configuration file")
flag.Parse()
// Use custom log for initial setup messages.
@@ -153,6 +154,9 @@ func run(ctx context.Context, configPath string) error {
return fmt.Errorf("failed to initialize logger: %w", err)
}
//to add the parent_id in the context value so it get passed to the logs
ctx = addParentIdCtx(ctx, cfg)
// Initialize plugin manager.
log.Infof(ctx, "Initializing plugin manager")
mgr, closer, err := newManagerFunc(ctx, cfg.PluginManager)
@@ -220,3 +224,35 @@ func shutdown(ctx context.Context, httpServer *http.Server, wg *sync.WaitGroup,
}
}()
}
func addParentIdCtx(ctx context.Context, config *Config) context.Context {
var parentID string
var podName string
if p := os.Getenv("POD_NAME"); p != "" {
log.Infof(ctx, "Adding POD name: %s", p)
podName = p
} else {
log.Info(ctx, "POD_NAME environment variable not set falling back to hostname")
if hostname, err := os.Hostname(); err == nil {
log.Infof(ctx, "Setting POD name as hostname: %s", hostname)
podName = hostname
} else {
log.Info(ctx, "failed to get POD name")
}
}
for _, m := range config.Modules {
if m.Handler.Role != "" && m.Handler.SubscriberID != "" {
parentID = string(m.Handler.Role) + ":" + m.Handler.SubscriberID + ":" + podName
break
}
}
if parentID != "" {
ctx = context.WithValue(ctx, model.ContextKeyParentID, parentID)
} else {
log.Warnf(ctx, "Failed to find parent ID in config please add the role and subscriber_id in the handler config ")
}
return ctx
}