From d12f845f05d8eedc8604498dd82365c929799a10 Mon Sep 17 00:00:00 2001 From: davidvader Date: Thu, 12 Sep 2024 10:50:14 -0500 Subject: [PATCH] fix: conditionally apply scm tracing --- cmd/vela-server/scm.go | 4 +++- cmd/vela-server/server.go | 2 +- scm/github/github.go | 17 +++++++++++------ scm/github/opts.go | 11 +++++++++++ scm/setup.go | 4 ++++ tracing/config.go | 21 +++++++++++++-------- 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/cmd/vela-server/scm.go b/cmd/vela-server/scm.go index c8c441442..7124761f0 100644 --- a/cmd/vela-server/scm.go +++ b/cmd/vela-server/scm.go @@ -7,10 +7,11 @@ import ( "github.com/urfave/cli/v2" "github.com/go-vela/server/scm" + "github.com/go-vela/server/tracing" ) // helper function to setup the scm from the CLI arguments. -func setupSCM(c *cli.Context) (scm.Service, error) { +func setupSCM(c *cli.Context, tc *tracing.Client) (scm.Service, error) { logrus.Debug("creating scm client from CLI configuration") // scm configuration @@ -24,6 +25,7 @@ func setupSCM(c *cli.Context) (scm.Service, error) { StatusContext: c.String("scm.context"), WebUIAddress: c.String("webui-addr"), Scopes: c.StringSlice("scm.scopes"), + Tracing: tc, } // setup the scm diff --git a/cmd/vela-server/server.go b/cmd/vela-server/server.go index 5e251c7c2..7733b9c15 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -108,7 +108,7 @@ func server(c *cli.Context) error { return err } - scm, err := setupSCM(c) + scm, err := setupSCM(c, tc) if err != nil { return err } diff --git a/scm/github/github.go b/scm/github/github.go index ac84e3a73..851556664 100644 --- a/scm/github/github.go +++ b/scm/github/github.go @@ -13,6 +13,8 @@ import ( "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "golang.org/x/oauth2" + + "github.com/go-vela/server/tracing" ) const ( @@ -53,6 +55,7 @@ type client struct { config *config OAuth *oauth2.Config AuthReq *github.AuthorizationRequest + Tracing *tracing.Client // https://pkg.go.dev/github.com/sirupsen/logrus#Entry Logger *logrus.Entry } @@ -158,12 +161,14 @@ func (c *client) newClientToken(ctx context.Context, token string) *github.Clien // } // } - tc.Transport = otelhttp.NewTransport( - tc.Transport, - otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { - return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutSubSpans()) - }), - ) + if c.Tracing.Config.EnableTracing { + tc.Transport = otelhttp.NewTransport( + tc.Transport, + otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { + return otelhttptrace.NewClientTrace(ctx, otelhttptrace.WithoutSubSpans()) + }), + ) + } // create the GitHub client from the OAuth client github := github.NewClient(tc) diff --git a/scm/github/opts.go b/scm/github/opts.go index df6d46506..bb7385827 100644 --- a/scm/github/opts.go +++ b/scm/github/opts.go @@ -5,6 +5,8 @@ package github import ( "fmt" "strings" + + "github.com/go-vela/server/tracing" ) // ClientOpt represents a configuration option to initialize the scm client for GitHub. @@ -149,3 +151,12 @@ func WithScopes(scopes []string) ClientOpt { return nil } } + +// WithTracing sets the shared tracing config in the scm client for GitHub. +func WithTracing(tracing *tracing.Client) ClientOpt { + return func(e *client) error { + e.Tracing = tracing + + return nil + } +} diff --git a/scm/setup.go b/scm/setup.go index 3b4082f7f..c32a0cf66 100644 --- a/scm/setup.go +++ b/scm/setup.go @@ -9,6 +9,7 @@ import ( "github.com/sirupsen/logrus" "github.com/go-vela/server/scm/github" + "github.com/go-vela/server/tracing" "github.com/go-vela/types/constants" ) @@ -36,6 +37,8 @@ type Setup struct { WebUIAddress string // specifies the OAuth scopes to use for the scm client Scopes []string + // specifies OTel tracing configurations + Tracing *tracing.Client } // Github creates and returns a Vela service capable of @@ -55,6 +58,7 @@ func (s *Setup) Github() (Service, error) { github.WithStatusContext(s.StatusContext), github.WithWebUIAddress(s.WebUIAddress), github.WithScopes(s.Scopes), + github.WithTracing(s.Tracing), ) } diff --git a/tracing/config.go b/tracing/config.go index e51fda72c..7cbb7b431 100644 --- a/tracing/config.go +++ b/tracing/config.go @@ -66,16 +66,21 @@ func FromCLIContext(c *cli.Context) (*Client, error) { m := keyValueSliceToMap(c.StringSlice("tracing.resource.env_attributes"), os.Getenv) maps.Copy(cfg.ResourceAttributes, m) - // initialize the tracer provider and assign it to the client - tracer, err := initTracer(c.Context, cfg) - if err != nil { - return nil, err + client := &Client{ + Config: cfg, } - return &Client{ - Config: cfg, - TracerProvider: tracer, - }, nil + if cfg.EnableTracing { + // initialize the tracer provider and assign it to the client + tracer, err := initTracer(c.Context, cfg) + if err != nil { + return nil, err + } + + client.TracerProvider = tracer + } + + return client, nil } // keyValueSliceToMap converts a slice of key=value strings to a map of key to value using the supplied map function.