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: trace #266

Merged
merged 7 commits into from
Dec 24, 2024
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/briandowns/spinner v1.23.0
github.com/google/uuid v1.3.0
github.com/olekukonko/tablewriter v0.0.5
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ github.com/google/s2a-go v0.1.0/go.mod h1:OJpEgntRZo8ugHpF9hkoLJbS5dSI20XZeXJ9JV
github.com/google/s2a-go v0.1.3/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
Expand Down
7 changes: 6 additions & 1 deletion internal/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/dream11/odin/pkg/constant"
"strings"

"github.com/dream11/odin/pkg/config"
Expand All @@ -17,6 +18,9 @@ import (

func grpcClient(ctx *context.Context) (*grpc.ClientConn, *context.Context, error) {
appConfig := config.GetConfig()
traceID := util.GenerateTraceID()
log.Debugf("Generated trace ID: %s", traceID)

if appConfig.BackendAddress == "" {
log.Fatal("Cannot create grpc client: Backend address is empty in config! Run `odin configure` to set backend address")
}
Expand All @@ -37,12 +41,13 @@ func grpcClient(ctx *context.Context) (*grpc.ClientConn, *context.Context, error
opts = append(opts, grpc.WithTransportCredentials(cred))
}

contextWithTrace := context.WithValue(*ctx, constant.TraceIDKey, traceID)
conn, err := grpc.Dial(appConfig.BackendAddress, opts...)

if err != nil {
return nil, nil, err
}
// Enrich context with authorisation metadata
requestCtx := metadata.AppendToOutgoingContext(*ctx, "Authorization", fmt.Sprintf("Bearer %s", appConfig.AccessToken))
requestCtx := metadata.AppendToOutgoingContext(contextWithTrace, "Authorization", fmt.Sprintf("Bearer %s", appConfig.AccessToken), string(constant.TraceIDKey), traceID)
return conn, &requestCtx, nil
}
3 changes: 3 additions & 0 deletions internal/service/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (e *Component) OperateComponent(ctx *context.Context, request *serviceProto
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand All @@ -64,6 +65,7 @@ func (e *Component) ListComponentType(ctx *context.Context, request *component.L
client := component.NewComponentServiceClient(conn)
response, err := client.ListComponentType(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand All @@ -79,6 +81,7 @@ func (e *Component) DescribeComponentType(ctx *context.Context, request *compone
client := component.NewComponentServiceClient(conn)
response, err := client.DescribeComponentType(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/service/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package service

import (
"context"
"github.com/dream11/odin/pkg/constant"
log "github.com/sirupsen/logrus"

auth "github.com/dream11/odin/proto/gen/go/dream11/od/auth/v1"
)
Expand All @@ -18,6 +20,7 @@ func (c *Configure) GetUserToken(ctx *context.Context, request *auth.GetUserToke
client := auth.NewAuthServiceClient(conn)
response, err := client.GetUserToken(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand Down
5 changes: 5 additions & 0 deletions internal/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (e *Environment) ListEnvironments(ctx *context.Context, request *environmen
client := environment.NewEnvironmentServiceClient(conn)
response, err := client.ListEnvironment(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand Down Expand Up @@ -56,6 +57,7 @@ func (e *Environment) CreateEnvironment(ctx *context.Context, request *environme
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand Down Expand Up @@ -96,6 +98,7 @@ func (e *Environment) DeleteEnvironment(ctx *context.Context, request *environme
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand All @@ -119,6 +122,7 @@ func (e *Environment) UpdateEnvironment(ctx *context.Context, request *environme
response, err := client.UpdateEnvironment(*requestCtx, request)

if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand All @@ -136,6 +140,7 @@ func (e *Environment) DescribeEnvironment(ctx *context.Context, request *environ
response, err := client.DescribeEnvironment(*requestCtx, request)

if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand Down
7 changes: 7 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}

Expand Down Expand Up @@ -85,6 +86,7 @@ func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.D
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}

Expand Down Expand Up @@ -129,6 +131,7 @@ func (e *Service) DeployReleasedService(ctx *context.Context, request *servicePr
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}

Expand Down Expand Up @@ -175,6 +178,7 @@ func (e *Service) UndeployService(ctx *context.Context, request *serviceProto.Un
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand Down Expand Up @@ -217,6 +221,7 @@ func (e *Service) OperateService(ctx *context.Context, request *serviceProto.Ope
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand Down Expand Up @@ -270,6 +275,7 @@ func (e *Service) ReleaseService(ctx *context.Context, request *serviceProto.Rel
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return err
}
if response != nil {
Expand Down Expand Up @@ -316,6 +322,7 @@ func (e *Service) DescribeService(ctx *context.Context, request *serviceProto.De
client := serviceProto.NewServiceServiceClient(conn)
response, err := client.DescribeService(*requestCtx, request)
if err != nil {
log.Errorf("TraceID: %s", (*requestCtx).Value(constant.TraceIDKey))
return nil, err
}

Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import (
_ "github.com/dream11/odin/cmd/undeploy"
_ "github.com/dream11/odin/cmd/update"
_ "github.com/dream11/odin/internal/ui"
"github.com/sirupsen/logrus"
)

func main() {
// Configure Logger to fix timestamp format
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05", // Custom format
FullTimestamp: true,
})
cmd.Execute()
}
6 changes: 6 additions & 0 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package constant

import "time"

// TraceID is the type for trace ID
type TraceID string

const (
// TEXT type output format
TEXT = "text"
Expand All @@ -18,4 +21,7 @@ const (

// SpinnerDelay Defines spinner delay
SpinnerDelay = 100 * time.Millisecond

// TraceIDKey is the key used to store traceID in context
TraceIDKey TraceID = "trace-id"
)
6 changes: 6 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"github.com/google/uuid"
"net"
"strings"

Expand Down Expand Up @@ -51,3 +52,8 @@ func GenerateResponseMessageComponentSpecific(response *v1.ServiceResponse, comp
}
return message
}

// GenerateTraceID generates a trace id
func GenerateTraceID() string {
return uuid.New().String()
}
Loading