Skip to content

Commit

Permalink
feat: add trace id in request header
Browse files Browse the repository at this point in the history
Signed-off-by: surajgour-d11 <[email protected]>
  • Loading branch information
surajgour-d11 committed Dec 9, 2024
1 parent 3391b95 commit 8ad4b94
Show file tree
Hide file tree
Showing 22 changed files with 103 additions and 48 deletions.
4 changes: 3 additions & 1 deletion cmd/create/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func init() {

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
// Validate accounts parameter
if err := validateAccounts(accounts); err != nil {
log.Fatal("Invalid accounts parameter: ", err)
Expand All @@ -70,9 +71,10 @@ func execute(cmd *cobra.Command) {
EnvName: envName,
Accounts: util.SplitProviderAccount(accounts),
ProvisioningType: provisioningType,
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to create environment ", err)
}
}
5 changes: 4 additions & 1 deletion cmd/delete/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package delete

import (
"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/util"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,11 +35,13 @@ func init() {

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
err := environmentClient.DeleteEnvironment(&ctx, &environment.DeleteEnvironmentRequest{
EnvName: name,
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to delete environment ", err)
}
}
4 changes: 3 additions & 1 deletion cmd/deploy/service-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deploy

import (
"encoding/json"
"github.com/dream11/odin/pkg/util"
"os"

"github.com/dream11/odin/pkg/config"
Expand Down Expand Up @@ -36,6 +37,7 @@ func executeDeployServiceSet(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)

ctx := cmd.Context()
traceId := util.GenerateTraceId()
if serviceSetName == "" && provisioningFile == "" {
log.Fatal("Please provide either --name or --file.")
}
Expand All @@ -61,7 +63,7 @@ func executeDeployServiceSet(cmd *cobra.Command) {
deployServiceSetRequest.Name = serviceSetName
}

err := serviceClient.DeployServiceSet(&ctx, &deployServiceSetRequest)
err := serviceClient.DeployServiceSet(&ctx, &deployServiceSetRequest, traceId)
if err != nil {
log.Fatal("Failed to deploy service ", err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/deploy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deploy

import (
"encoding/json"
"github.com/dream11/odin/pkg/util"
"os"

"github.com/dream11/odin/internal/service"
Expand Down Expand Up @@ -43,6 +44,7 @@ func init() {
func execute(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)
ctx := cmd.Context()
traceId := util.GenerateTraceId()
if (serviceName == "" && serviceVersion == "") && (definitionFile != "" && provisioningFile != "") {
definitionData, err := os.ReadFile(definitionFile)
if err != nil {
Expand All @@ -69,9 +71,10 @@ func execute(cmd *cobra.Command) {
EnvName: env,
ServiceDefinition: &definitionProto,
ProvisioningConfig: provisioningProto,
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to deploy service ", err)
}
} else if (serviceName != "" && serviceVersion != "") && (definitionFile == "" && provisioningFile == "") {
Expand All @@ -82,9 +85,10 @@ func execute(cmd *cobra.Command) {
ServiceName: serviceName,
ServiceVersion: serviceVersion,
},
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to deploy service ", err)
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion cmd/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package describe
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"

serviceBackend "github.com/dream11/odin/internal/service"
comp "github.com/dream11/odin/proto/gen/go/dream11/od/component/v1"
Expand Down Expand Up @@ -45,12 +46,14 @@ func executeDescribeComponentType(cmd *cobra.Command) {
"version": componentVersion,
}
ctx := cmd.Context()
traceId := util.GenerateTraceId()
response, err := componentClient.DescribeComponentType(&ctx, &comp.DescribeComponentTypeRequest{
ComponentType: componentName,
Params: params,
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to describe service: ", err)
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/describe/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package describe
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
Expand Down Expand Up @@ -36,6 +37,7 @@ func init() {

func executeEnv(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
params := map[string]string{}

if serviceName != "" {
Expand All @@ -47,9 +49,10 @@ func executeEnv(cmd *cobra.Command) {
response, err := environmentClient.DescribeEnvironment(&ctx, &environment.DescribeEnvironmentRequest{
Params: params,
EnvName: name,
})
}, traceId)

if err != nil {
log.Info("TraceId: ", traceId)
log.Fatal("Failed to describe environment ", err)
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/describe/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package describe
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"
"strconv"

serviceBackend "github.com/dream11/odin/internal/service"
Expand Down Expand Up @@ -57,11 +58,12 @@ func execute(cmd *cobra.Command) {
}

ctx := cmd.Context()
traceId := util.GenerateTraceId()
response, err := serviceClient.DescribeService(&ctx, &service.DescribeServiceRequest{
ServiceName: serviceName,
Version: serviceVersion,
Params: params,
})
}, traceId)

if err != nil {
log.Fatal("Failed to describe service: ", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/list/componentType.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
Expand Down Expand Up @@ -36,6 +37,7 @@ func init() {

func componentExecute(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
params := make(map[string]string)

// Add non-empty parameters to the map
Expand All @@ -49,7 +51,7 @@ func componentExecute(cmd *cobra.Command) {
// Make the API call with the populated parameters
response, err := componentTypeClient.ListComponentType(&ctx, &component.ListComponentTypeRequest{
Params: params,
})
}, traceId)

if err != nil {
log.Fatal("Failed to list component types ", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/list/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"
"strconv"

"github.com/dream11/odin/internal/service"
Expand Down Expand Up @@ -41,13 +42,14 @@ func init() {

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
response, err := environmentClient.ListEnvironments(&ctx, &environment.ListEnvironmentRequest{
Params: map[string]string{
"name": name,
"account": account,
"provisioningType": provisioningType,
"displayAll": strconv.FormatBool(displayAll)},
})
}, traceId)

if err != nil {
log.Fatal("Failed to list environments ", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/list/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"encoding/json"
"fmt"
"github.com/dream11/odin/pkg/util"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
Expand Down Expand Up @@ -36,11 +37,12 @@ func init() {

func listService(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()
response, err := serviceClient.ListService(&ctx, &serviceProto.ListServiceRequest{
Name: serviceName,
Version: version,
Tags: tags,
})
}, traceId)

if err != nil {
log.Fatal("Failed to list services ", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/operate/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operate

import (
"encoding/json"
"github.com/dream11/odin/pkg/util"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/config"
Expand Down Expand Up @@ -54,6 +55,7 @@ func execute(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)

ctx := cmd.Context()
traceId := util.GenerateTraceId()
//validate the variables
var optionsData map[string]interface{}

Expand Down Expand Up @@ -89,7 +91,7 @@ func execute(cmd *cobra.Command) {
IsComponentOperation: true,
Operation: operation,
Config: config,
})
}, traceId)

if err != nil {
log.Fatal("Failed to operate on component", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/operate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operate

import (
"encoding/json"
"github.com/dream11/odin/pkg/util"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/config"
Expand Down Expand Up @@ -44,6 +45,7 @@ func executeOperateService(cmd *cobra.Command) {
env = config.EnsureEnvPresent(env)

ctx := cmd.Context()
traceId := util.GenerateTraceId()
//validate the variables
var optionsData map[string]interface{}

Expand Down Expand Up @@ -78,7 +80,7 @@ func executeOperateService(cmd *cobra.Command) {
IsComponentOperation: false,
Operation: operation,
Config: config,
})
}, traceId)

if err != nil {
log.Fatal("Failed to operate on service", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/release/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package release

import (
"encoding/json"
"github.com/dream11/odin/pkg/util"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -38,6 +39,7 @@ func init() {

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
traceId := util.GenerateTraceId()

var err error

Expand Down Expand Up @@ -102,7 +104,7 @@ func execute(cmd *cobra.Command) {
}
serviceReleaseRequest.ProvisioningConfigs = provisioningConfigMap
serviceReleaseRequest.ServiceDefinition = &definitionProto
err = serviceClient.ReleaseService(&ctx, &serviceReleaseRequest)
err = serviceClient.ReleaseService(&ctx, &serviceReleaseRequest, traceId)
if err != nil {
log.Fatal("Failed to release service ", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/undeploy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package undeploy
import (
"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/config"
"github.com/dream11/odin/pkg/util"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,11 +40,12 @@ func execute(cmd *cobra.Command) {
envName = config.EnsureEnvPresent(envName)

ctx := cmd.Context()
traceId := util.GenerateTraceId()

err := serviceClient.UndeployService(&ctx, &serviceProto.UndeployServiceRequest{
EnvName: envName,
ServiceName: name,
})
}, traceId)

if err != nil {
log.Fatal("Failed to undeploy service ", err)
Expand Down
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
10 changes: 8 additions & 2 deletions internal/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import (
"google.golang.org/grpc/metadata"
)

func grpcClient(ctx *context.Context) (*grpc.ClientConn, *context.Context, error) {
func grpcClient(ctx *context.Context, traceIdOptional ...string) (*grpc.ClientConn, *context.Context, error) {
appConfig := config.GetConfig()

traceId := ""
if len(traceIdOptional) > 0 {
traceId = traceIdOptional[0]
}

if appConfig.BackendAddress == "" {
log.Fatal("Cannot create grpc client: Backend address is empty in config! Run `odin configure` to set backend address")
}
Expand All @@ -43,6 +49,6 @@ func grpcClient(ctx *context.Context) (*grpc.ClientConn, *context.Context, error
return nil, nil, err
}
// Enrich context with authorisation metadata
requestCtx := metadata.AppendToOutgoingContext(*ctx, "Authorization", fmt.Sprintf("Bearer %s", appConfig.AccessToken))
requestCtx := metadata.AppendToOutgoingContext(*ctx, "Authorization", fmt.Sprintf("Bearer %s", appConfig.AccessToken), "TraceId", traceId)
return conn, &requestCtx, nil
}
Loading

0 comments on commit 8ad4b94

Please sign in to comment.