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

feat: add logs command #312

Open
wants to merge 1 commit into
base: 2.x.x
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions cmd/logs/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package logs

import (
"github.com/dream11/odin/cmd"
"github.com/dream11/odin/internal/service"
logs "github.com/dream11/odin/proto/gen/go/dream11/od/logs/v1"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var traceID string
var follow bool
var serviceName string
var componentName string
var logsClient = service.Logs{}

var logsCmd = &cobra.Command{
Use: "logs",
Short: "Get Logs",
Long: `Get logs for a trace id`,
Run: func(cmd *cobra.Command, args []string) {
execute(cmd)
},
}

func init() {
logsCmd.Flags().StringVarP(&traceID, "trace-id", "t", "", "trace id")
logsCmd.Flags().BoolVarP(&follow, "follow", "f", false, "follow logs")
logsCmd.Flags().StringVar(&serviceName, "service", "", "service name")
logsCmd.Flags().StringVar(&serviceName, "component", "", "component name")
cmd.RootCmd.AddCommand(logsCmd)
}

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
// Check traceid should be present
if traceID == "" {
logger.Error("TraceID is required")
err := cmd.Help()
if err != nil {
return
}
} else {
err := logsClient.GetLogs(&ctx, &logs.GetLogsRequest{
TraceId: traceID,
Follow: &follow,
ServiceName: &serviceName,
ComponentName: &componentName,
})
if err != nil {
logger.Fatal("Failed to get logs ", err)
}
}
}
36 changes: 36 additions & 0 deletions internal/service/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package service

import (
"context"
"fmt"

logs "github.com/dream11/odin/proto/gen/go/dream11/od/logs/v1"
)

// Logs performs operation on logs like get logs
type Logs struct{}

// GetLogs Get logs
func (l *Logs) GetLogs(ctx *context.Context, request *logs.GetLogsRequest) error {
// Get logs
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return err
}
client := logs.NewLogsServiceClient(conn)
stream, err := client.GetLogs(*requestCtx, request)
if err != nil {
return err
}
for {
response, err := stream.Recv()
if err != nil {
break
}
// Print logs
for _, logMessage := range response.Logs {
fmt.Print(logMessage.Message)
}
}
return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions proto/dream11/od/logs/v1/logs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
package dream11.od.logs.v1;

option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/logs/v1";

service LogsService {
rpc GetLogs(GetLogsRequest) returns (stream GetLogsResponse) {}
}

message GetLogsRequest {
string trace_id = 1;
optional bool follow = 2;
optional string service_name = 3;
optional string component_name = 4;
}

message GetLogsResponse {
repeated Log logs = 1;
}

message Log {
string message = 1;
string level = 2;
string timestamp = 3;
}
Loading