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

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
# Logs # Logs
.DS_Store .DS_Store
logs logs
.idea
*.log *.log
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*

View File

@@ -11,6 +11,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/beckn-one/beckn-onix/pkg/model"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/beckn-one/beckn-onix/core/module" "github.com/beckn-one/beckn-onix/core/module"
@@ -52,7 +53,7 @@ var runFunc = run
func main() { func main() {
// Define and parse command-line flags. // 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() flag.Parse()
// Use custom log for initial setup messages. // 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) 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. // Initialize plugin manager.
log.Infof(ctx, "Initializing plugin manager") log.Infof(ctx, "Initializing plugin manager")
mgr, closer, err := newManagerFunc(ctx, cfg.PluginManager) 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
}

View File

@@ -20,6 +20,11 @@ import (
type ctxKey any type ctxKey any
var requestID ctxKey = "requestID" var requestID ctxKey = "requestID"
var transaction_id ctxKey = "transactionID"
var message_id ctxKey = "messageID"
var subscriber_id ctxKey = "subscriberID"
var module_id ctxKey = "moduleID"
var parent_id ctxKey = "parentID"
const testLogFilePath = "./test_logs/test.log" const testLogFilePath = "./test_logs/test.log"
@@ -69,6 +74,7 @@ func setupLogger(t *testing.T, l level) string {
model.ContextKeyMsgID, model.ContextKeyMsgID,
model.ContextKeySubscriberID, model.ContextKeySubscriberID,
model.ContextKeyModuleID, model.ContextKeyModuleID,
model.ContextKeyParentID,
}, },
} }
@@ -103,16 +109,25 @@ func parseLogLine(t *testing.T, line string) map[string]interface{} {
func TestDebug(t *testing.T) { func TestDebug(t *testing.T) {
t.Helper() t.Helper()
logPath := setupLogger(t, DebugLevel) logPath := setupLogger(t, DebugLevel)
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345") ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "subscriber-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyTxnID, "trx-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyMsgID, "message-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyModuleID, "module-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyParentID, "parent-id-12345")
Debug(ctx, "Debug message") Debug(ctx, "Debug message")
lines := readLogFile(t, logPath) lines := readLogFile(t, logPath)
if len(lines) == 0 { if len(lines) == 0 {
t.Fatal("No logs were written.") t.Fatal("No logs were written.")
} }
expected := map[string]interface{}{ expected := map[string]interface{}{
"level": "debug", "level": "debug",
"subscriber_id": "12345", "transaction_id": "trx-id-12345",
"message": "Debug message", "message_id": "message-id-12345",
"subscriber_id": "subscriber-id-12345",
"module_id": "module-id-12345",
"parent_id": "parent-id-12345",
"message": "Debug message",
} }
var found bool var found bool
@@ -135,16 +150,24 @@ func TestDebug(t *testing.T) {
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
logPath := setupLogger(t, InfoLevel) logPath := setupLogger(t, InfoLevel)
ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "12345") ctx := context.WithValue(context.Background(), model.ContextKeySubscriberID, "subscriber-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyTxnID, "trx-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyMsgID, "message-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyModuleID, "module-id-12345")
ctx = context.WithValue(ctx, model.ContextKeyParentID, "parent-id-12345")
Info(ctx, "Info message") Info(ctx, "Info message")
lines := readLogFile(t, logPath) lines := readLogFile(t, logPath)
if len(lines) == 0 { if len(lines) == 0 {
t.Fatal("No logs were written.") t.Fatal("No logs were written.")
} }
expected := map[string]interface{}{ expected := map[string]interface{}{
"level": "info", "level": "info",
"subscriber_id": "12345", "transaction_id": "trx-id-12345",
"message": "Info message", "message_id": "message-id-12345",
"subscriber_id": "subscriber-id-12345",
"module_id": "module-id-12345",
"parent_id": "parent-id-12345",
"message": "Info message",
} }
var found bool var found bool
@@ -227,6 +250,12 @@ func TestError(t *testing.T) {
func TestRequest(t *testing.T) { func TestRequest(t *testing.T) {
logPath := setupLogger(t, InfoLevel) logPath := setupLogger(t, InfoLevel)
ctx := context.WithValue(context.Background(), requestID, "abc-123") ctx := context.WithValue(context.Background(), requestID, "abc-123")
ctx = context.WithValue(context.Background(), transaction_id, "transaction-id-123-")
ctx = context.WithValue(context.Background(), message_id, "message-id-123")
ctx = context.WithValue(context.Background(), subscriber_id, "subscriber-id-123")
ctx = context.WithValue(context.Background(), module_id, "module-id-123")
ctx = context.WithValue(context.Background(), parent_id, "parent-id-123")
req, _ := http.NewRequest("POST", "/api/test", bytes.NewBuffer([]byte(`{"key":"value"}`))) req, _ := http.NewRequest("POST", "/api/test", bytes.NewBuffer([]byte(`{"key":"value"}`)))
req.RemoteAddr = "127.0.0.1:8080" req.RemoteAddr = "127.0.0.1:8080"
Request(ctx, req, []byte(`{"key":"value"}`)) Request(ctx, req, []byte(`{"key":"value"}`))

View File

@@ -208,6 +208,7 @@ func TestParseContextKey_ValidKeys(t *testing.T) {
{"message_id", ContextKeyMsgID}, {"message_id", ContextKeyMsgID},
{"subscriber_id", ContextKeySubscriberID}, {"subscriber_id", ContextKeySubscriberID},
{"module_id", ContextKeyModuleID}, {"module_id", ContextKeyModuleID},
{"parent_id", ContextKeyParentID},
} }
for _, tt := range tests { for _, tt := range tests {

View File

@@ -53,6 +53,9 @@ const (
// ContextKeyModuleID is the context key for storing and retrieving the model ID from a request context. // ContextKeyModuleID is the context key for storing and retrieving the model ID from a request context.
ContextKeyModuleID ContextKey = "module_id" ContextKeyModuleID ContextKey = "module_id"
// ContextKeyParentID is the context key for storing and retrieving the parent ID from a request context
ContextKeyParentID ContextKey = "parent_id"
) )
var contextKeys = map[string]ContextKey{ var contextKeys = map[string]ContextKey{
@@ -60,6 +63,7 @@ var contextKeys = map[string]ContextKey{
"message_id": ContextKeyMsgID, "message_id": ContextKeyMsgID,
"subscriber_id": ContextKeySubscriberID, "subscriber_id": ContextKeySubscriberID,
"module_id": ContextKeyModuleID, "module_id": ContextKeyModuleID,
"parent_id": ContextKeyParentID,
} }
// ParseContextKey converts a string into a valid ContextKey. // ParseContextKey converts a string into a valid ContextKey.
@@ -100,6 +104,8 @@ const (
RoleGateway Role = "gateway" RoleGateway Role = "gateway"
// RoleRegistery represents the Registry that maintains network participant details. // RoleRegistery represents the Registry that maintains network participant details.
RoleRegistery Role = "registery" RoleRegistery Role = "registery"
// RoleCDS represents the CDS for that network
RoleCDS Role = "cds"
) )
var validRoles = map[Role]bool{ var validRoles = map[Role]bool{
@@ -107,6 +113,7 @@ var validRoles = map[Role]bool{
RoleBPP: true, RoleBPP: true,
RoleGateway: true, RoleGateway: true,
RoleRegistery: true, RoleRegistery: true,
RoleCDS: true,
} }
// UnmarshalYAML implements custom YAML unmarshalling for Role to ensure only valid values are accepted. // UnmarshalYAML implements custom YAML unmarshalling for Role to ensure only valid values are accepted.

View File

@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/beckn-one/beckn-onix/pkg/model"
"github.com/beckn-one/beckn-onix/pkg/plugin/implementation/reqpreprocessor" "github.com/beckn-one/beckn-onix/pkg/plugin/implementation/reqpreprocessor"
) )
@@ -18,6 +19,11 @@ func (p provider) New(ctx context.Context, c map[string]string) (func(http.Handl
if contextKeys, ok := c["contextKeys"]; ok { if contextKeys, ok := c["contextKeys"]; ok {
config.ContextKeys = strings.Split(contextKeys, ",") config.ContextKeys = strings.Split(contextKeys, ",")
} }
if v := ctx.Value(model.ContextKeyParentID); v != nil {
config.ParentID = v.(string)
}
return reqpreprocessor.NewPreProcessor(config) return reqpreprocessor.NewPreProcessor(config)
} }

View File

@@ -7,6 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/beckn-one/beckn-onix/pkg/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -92,7 +93,9 @@ func TestProviderNew(t *testing.T) {
}` }`
p := provider{} p := provider{}
middleware, err := p.New(context.Background(), tc.config) ctx := context.Background()
ctx = context.WithValue(ctx, model.ContextKeyParentID, "bap:bap-1:instanceID")
middleware, err := p.New(ctx, tc.config)
if tc.expectedError { if tc.expectedError {
assert.Error(t, err) assert.Error(t, err)
return return

View File

@@ -17,6 +17,7 @@ import (
type Config struct { type Config struct {
Role string Role string
ContextKeys []string ContextKeys []string
ParentID string
} }
const contextKey = "context" const contextKey = "context"
@@ -58,6 +59,11 @@ func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
log.Debugf(ctx, "adding subscriberId to request:%s, %v", model.ContextKeySubscriberID, subID) log.Debugf(ctx, "adding subscriberId to request:%s, %v", model.ContextKeySubscriberID, subID)
ctx = context.WithValue(ctx, model.ContextKeySubscriberID, subID) ctx = context.WithValue(ctx, model.ContextKeySubscriberID, subID)
} }
if cfg.ParentID != "" {
log.Debugf(ctx, "adding parentID to request:%s, %v", model.ContextKeyParentID, cfg.ParentID)
ctx = context.WithValue(ctx, model.ContextKeyParentID, cfg.ParentID)
}
for _, key := range cfg.ContextKeys { for _, key := range cfg.ContextKeys {
ctxKey, _ := model.ParseContextKey(key) ctxKey, _ := model.ParseContextKey(key)
if v, ok := reqContext[key]; ok { if v, ok := reqContext[key]; ok {

View File

@@ -22,7 +22,8 @@ func TestNewPreProcessorSuccessCases(t *testing.T) {
{ {
name: "BAP role with valid context", name: "BAP role with valid context",
config: &Config{ config: &Config{
Role: "bap", Role: "bap",
ParentID: "bap:bap-123",
}, },
requestBody: map[string]interface{}{ requestBody: map[string]interface{}{
"context": map[string]interface{}{ "context": map[string]interface{}{
@@ -38,7 +39,8 @@ func TestNewPreProcessorSuccessCases(t *testing.T) {
{ {
name: "BPP role with valid context", name: "BPP role with valid context",
config: &Config{ config: &Config{
Role: "bpp", Role: "bpp",
ParentID: "bap:bap-123",
}, },
requestBody: map[string]interface{}{ requestBody: map[string]interface{}{
"context": map[string]interface{}{ "context": map[string]interface{}{