moved the logger creation to the utils package to make them available to all packages without recreating or passing them

This commit is contained in:
pb 2025-04-15 11:40:44 +02:00
parent 04d6001fec
commit 31580f1905
3 changed files with 33 additions and 3 deletions

View File

@ -67,7 +67,7 @@ func main() {
conf.GetConfig().Logs, conf.GetConfig().Logs,
) )
logger = logs.CreateLogger("oc-monitord") logger = u.GetLogger()
logger.Debug().Msg("Loki URL : " + conf.GetConfig().LokiURL) logger.Debug().Msg("Loki URL : " + conf.GetConfig().LokiURL)
logger.Debug().Msg("Workflow executed : " + conf.GetConfig().ExecutionID) logger.Debug().Msg("Workflow executed : " + conf.GetConfig().ExecutionID)
@ -124,13 +124,16 @@ func executeInside(execID string, ns string, argo_file_path string, stepMax int)
t, err := tools2.NewService(conf.GetConfig().Mode) t, err := tools2.NewService(conf.GetConfig().Mode)
if err != nil { if err != nil {
logger.Error().Msg("Could not create KubernetesTool") logger.Error().Msg("Could not create KubernetesTool")
return
} }
name, err := t.CreateArgoWorkflow(argo_file_path, ns) name, err := t.CreateArgoWorkflow(argo_file_path, ns)
if err != nil { if err != nil {
logger.Error().Msg("Could not create argo workflow : " + err.Error()) logger.Error().Msg("Could not create argo workflow : " + err.Error())
return
} else { } else {
split := strings.Split(argo_file_path, "_") argoLogs := models.NewArgoLogs(workflowName, "argo", stepMax)
argoLogs := models.NewArgoLogs(split[0], "argo", stepMax)
argoLogs.StartStepRecording(argoLogs.NewWatch(), wf_logger) argoLogs.StartStepRecording(argoLogs.NewWatch(), wf_logger)
err := t.LogWorkflow(execID, ns, name, argo_file_path, stepMax, argoLogs.NewWatch(), argoLogs.NewWatch(), argoLogs, []string{}, logWorkflow) err := t.LogWorkflow(execID, ns, name, argo_file_path, stepMax, argoLogs.NewWatch(), argoLogs.NewWatch(), argoLogs, []string{}, logWorkflow)
if err != nil { if err != nil {

View File

@ -66,6 +66,8 @@ func (k *KubernetesTools) LogWorkflow(execID string, namespace string, workflowN
return errors.New("Could not retrieve workflow ID from execution ID " + execID) return errors.New("Could not retrieve workflow ID from execution ID " + execID)
} }
if exec.State == enum.DRAFT || exec.State == enum.FAILURE || exec.State == enum.SUCCESS { if exec.State == enum.DRAFT || exec.State == enum.FAILURE || exec.State == enum.SUCCESS {
l := utils.GetWFLogger("")
l.Error().Msg("The execution's state doesn't meet requirement, state is : " + exec.State.String())
return nil return nil
} }
k.logWorkflow(namespace, workflowName, argoFilePath, stepMax, current_watch, previous_watch, argoLogs, seen, logFunc) k.logWorkflow(namespace, workflowName, argoFilePath, stepMax, current_watch, previous_watch, argoLogs, seen, logFunc)
@ -76,6 +78,7 @@ func (k *KubernetesTools) logWorkflow(namespace string, workflowName string, arg
seen []string, seen []string,
logFunc func(argoFilePath string, stepMax int, pipe io.ReadCloser, current_watch *models.ArgoWatch, previous_watch *models.ArgoWatch, argoLogs *models.ArgoLogs, seen []string, wg *sync.WaitGroup)) error { logFunc func(argoFilePath string, stepMax int, pipe io.ReadCloser, current_watch *models.ArgoWatch, previous_watch *models.ArgoWatch, argoLogs *models.ArgoLogs, seen []string, wg *sync.WaitGroup)) error {
// List pods related to the Argo workflow // List pods related to the Argo workflow
fmt.Println("\n!!!!!!!! !!!!!!!!!! !!!!!!!! &&&& & STARTING LOG\n\n")
labelSelector := fmt.Sprintf("workflows.argoproj.io/workflow=%s", workflowName) labelSelector := fmt.Sprintf("workflows.argoproj.io/workflow=%s", workflowName)
for retries := 0; retries < 10; retries++ { // Retry for up to ~20 seconds for retries := 0; retries < 10; retries++ { // Retry for up to ~20 seconds
// List workflow pods // List workflow pods

View File

@ -2,11 +2,21 @@ package utils
import ( import (
"oc-monitord/conf" "oc-monitord/conf"
"sync"
oclib "cloud.o-forge.io/core/oc-lib" oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution" "cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"github.com/rs/zerolog"
) )
var (
logger zerolog.Logger
wf_logger zerolog.Logger
pods_logger zerolog.Logger
onceLogger sync.Once
onceWF sync.Once
)
func GetExecution(exec_id string) *workflow_execution.WorkflowExecution { func GetExecution(exec_id string) *workflow_execution.WorkflowExecution {
res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", conf.GetConfig().PeerID, []string{}, nil).LoadOne(exec_id) res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", conf.GetConfig().PeerID, []string{}, nil).LoadOne(exec_id)
if res.Code != 200 { if res.Code != 200 {
@ -16,3 +26,17 @@ func GetExecution(exec_id string) *workflow_execution.WorkflowExecution {
} }
return res.ToWorkflowExecution() return res.ToWorkflowExecution()
} }
func GetLogger() zerolog.Logger {
onceLogger.Do(func(){
logger = logs.CreateLogger("oc-monitord")
})
return logger
}
func GetWFLogger(workflowName string) zerolog.Logger {
onceWF.Do(func(){
wf_logger = logger.With().Str("argo_name", workflowName).Str("workflow_id", conf.GetConfig().WorkflowID).Str("workflow_execution_id", conf.GetConfig().ExecutionID).Logger()
})
return wf_logger
}