Skip to content

Commit

Permalink
fix: conditionally apply scm tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Sep 12, 2024
1 parent c604d5f commit d12f845
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
4 changes: 3 additions & 1 deletion cmd/vela-server/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 11 additions & 6 deletions scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions scm/github/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
4 changes: 4 additions & 0 deletions scm/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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),
)
}

Expand Down
21 changes: 13 additions & 8 deletions tracing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d12f845

Please sign in to comment.