From 54e44999ab8c618364b827c5142b1018e86a66bb Mon Sep 17 00:00:00 2001 From: mnishhhD11 Date: Fri, 27 Jun 2025 11:29:42 +0530 Subject: [PATCH 1/3] fetch logs for traceId --- cmd/logs/logs.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ definition-1.json | 0 main.go | 1 + prov-dev-1.json | 0 4 files changed, 60 insertions(+) create mode 100644 cmd/logs/logs.go create mode 100644 definition-1.json create mode 100644 prov-dev-1.json diff --git a/cmd/logs/logs.go b/cmd/logs/logs.go new file mode 100644 index 00000000..4e87c4a8 --- /dev/null +++ b/cmd/logs/logs.go @@ -0,0 +1,59 @@ +package logs + +import ( + "context" + "fmt" + "github.com/dream11/odin/cmd" + "github.com/dream11/odin/internal/service" + "github.com/dream11/odin/pkg/constant" + logs "github.com/dream11/odin/proto/gen/go/dream11/od/logs/v1" + "github.com/spf13/cobra" +) + +var traceId string +var logsClient = service.Logs{} + +var logsCmd = &cobra.Command{ + Use: "logs", + Short: "Fetch logs", + Long: "Fetch logs", + Run: func(cmd *cobra.Command, args []string) { + execute(cmd) + }, +} + +func init() { + logsCmd.Flags().StringVar(&traceId, "traceid", "", "Trace Id to fetch logs") + cmd.RootCmd.AddCommand(logsCmd) +} + +func execute(cmd *cobra.Command) { + ctx := cmd.Context() + + contextWithTrace := context.WithValue(ctx, constant.TraceIDKey, traceId) + + streamCtx, cancelFunction := context.WithCancel(context.Background()) + defer cancelFunction() + + var searchAfterParams []int64 + var err error + fmt.Printf("Fetching logs for traceId: %s\n", traceId) + for { + select { + case <-streamCtx.Done(): + return + default: + // Get logs with retry on error + follow := true + searchAfterParams, err = logsClient.GetLogs(&contextWithTrace, &logs.GetLogsRequest{ + TraceId: traceId, + Follow: &follow, + SearchAfterParams: searchAfterParams, + }) + if err != nil { + continue + } + } + } + +} diff --git a/definition-1.json b/definition-1.json new file mode 100644 index 00000000..e69de29b diff --git a/main.go b/main.go index 6f3fb032..4efab5ad 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( _ "github.com/dream11/odin/cmd/deploy" _ "github.com/dream11/odin/cmd/describe" _ "github.com/dream11/odin/cmd/list" + _ "github.com/dream11/odin/cmd/logs" _ "github.com/dream11/odin/cmd/operate" _ "github.com/dream11/odin/cmd/release" _ "github.com/dream11/odin/cmd/set" diff --git a/prov-dev-1.json b/prov-dev-1.json new file mode 100644 index 00000000..e69de29b From 4719803f528c05a2a3f68567ef8e5996425f2370 Mon Sep 17 00:00:00 2001 From: mnishhhD11 Date: Fri, 27 Jun 2025 11:32:47 +0530 Subject: [PATCH 2/3] nit --- definition-1.json | 0 prov-dev-1.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 definition-1.json delete mode 100644 prov-dev-1.json diff --git a/definition-1.json b/definition-1.json deleted file mode 100644 index e69de29b..00000000 diff --git a/prov-dev-1.json b/prov-dev-1.json deleted file mode 100644 index e69de29b..00000000 From df783a621d423ab0c8acd2c6efe3e09c1a6db5ad Mon Sep 17 00:00:00 2001 From: mnishhhD11 Date: Fri, 27 Jun 2025 11:35:56 +0530 Subject: [PATCH 3/3] nit --- cmd/logs/logs.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/logs/logs.go b/cmd/logs/logs.go index 4e87c4a8..dd8a25dd 100644 --- a/cmd/logs/logs.go +++ b/cmd/logs/logs.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var traceId string +var traceID string var logsClient = service.Logs{} var logsCmd = &cobra.Command{ @@ -23,21 +23,21 @@ var logsCmd = &cobra.Command{ } func init() { - logsCmd.Flags().StringVar(&traceId, "traceid", "", "Trace Id to fetch logs") + logsCmd.Flags().StringVar(&traceID, "traceid", "", "Trace Id to fetch logs") cmd.RootCmd.AddCommand(logsCmd) } func execute(cmd *cobra.Command) { ctx := cmd.Context() - contextWithTrace := context.WithValue(ctx, constant.TraceIDKey, traceId) + contextWithTrace := context.WithValue(ctx, constant.TraceIDKey, traceID) streamCtx, cancelFunction := context.WithCancel(context.Background()) defer cancelFunction() var searchAfterParams []int64 var err error - fmt.Printf("Fetching logs for traceId: %s\n", traceId) + fmt.Printf("Fetching logs for traceID: %s\n", traceID) for { select { case <-streamCtx.Done(): @@ -46,7 +46,7 @@ func execute(cmd *cobra.Command) { // Get logs with retry on error follow := true searchAfterParams, err = logsClient.GetLogs(&contextWithTrace, &logs.GetLogsRequest{ - TraceId: traceId, + TraceId: traceID, Follow: &follow, SearchAfterParams: searchAfterParams, })