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: otel tracing #899

Merged
merged 85 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
aa10d2d
init jaeger
JordanSussman Jun 28, 2023
4301fbe
wip: otel gin and gorm
plyr4 Jun 28, 2023
ced4353
wip: added context to ONLY CreateSchedule
plyr4 Jun 28, 2023
fe6b3eb
update docker compose
JordanSussman Jun 28, 2023
7752be5
attempt to mask gorm values
JordanSussman Jun 28, 2023
817dee1
Merge branch 'feat/otel-tracing' of github.com:go-vela/server into fe…
jbrockopp Jun 28, 2023
0799b91
chore: save work
jbrockopp Jun 28, 2023
453d5bc
fix github creds
JordanSussman Jun 28, 2023
52ce323
add back vault
JordanSussman Jun 28, 2023
208410b
make the serviceName as vela-server
JordanSussman Jun 28, 2023
efd0487
pass around the proper context
JordanSussman Jun 29, 2023
646f64b
cli flag and opt-in enable tracing
plyr4 Jun 29, 2023
e78ff24
Merge branch 'feat/otel-tracing' of github.com:go-vela/server into fe…
plyr4 Jun 29, 2023
6f86666
refactor: move code into tracing package
plyr4 Jun 29, 2023
2b7db12
tweak: order of vars and flags
plyr4 Jun 29, 2023
fedcee4
Merge branch 'main' into feat/otel-tracing
JordanSussman Jun 29, 2023
570d148
fix depends on
JordanSussman Jun 29, 2023
120a5e7
make linter happy
JordanSussman Jun 29, 2023
541025d
fix tests
JordanSussman Jun 29, 2023
4fe0b68
chore: merge with main
plyr4 Aug 16, 2023
b695599
enhance: add context to scm
plyr4 Sep 18, 2023
4d42413
chore: replace TODO with ctx
plyr4 Sep 18, 2023
a043735
chore: merge with main
plyr4 Sep 19, 2023
36a7b60
chore: merge with main
plyr4 Sep 19, 2023
304456f
Merge branch 'main' of github.com:go-vela/server into feat/otel-tracing
plyr4 Sep 19, 2023
d43a49d
chore: merge with main
plyr4 Sep 19, 2023
793f17d
chore: merge with main
plyr4 Aug 1, 2024
f597009
chore: rm launch.json
plyr4 Aug 1, 2024
d9da5a3
chore: headers and misc cleanup
plyr4 Aug 1, 2024
7b4e110
chore: golangci
plyr4 Aug 1, 2024
602c183
Merge branch 'main' of github.com:go-vela/server into feat/otel-tracing
plyr4 Aug 13, 2024
da924d5
enhance: supply parent context to all db calls
plyr4 Aug 13, 2024
ae40a65
fix: lint
plyr4 Aug 13, 2024
72636ba
Merge branch 'main' into enhance/db-ctx
plyr4 Aug 15, 2024
eba4507
Merge branch 'main' of github.com:go-vela/server into feat/otel-tracing
plyr4 Aug 19, 2024
e99b4ce
Merge branch 'main' into enhance/db-ctx
plyr4 Aug 19, 2024
15c8115
chore: merge with main
plyr4 Aug 20, 2024
19703bf
Merge branch 'enhance/db-ctx' of github.com:go-vela/server into feat/…
plyr4 Aug 20, 2024
d921b3e
chore: merge with enhance/db-ctx
plyr4 Aug 20, 2024
e590f23
enhance: simple head samplers for ratio and rate limit
plyr4 Aug 21, 2024
c22be46
Merge branch 'main' of github.com:go-vela/server into feat/otel-tracing
plyr4 Aug 21, 2024
3aa5b8d
enhance: customizable config, code cleanup, middleware
plyr4 Aug 21, 2024
868fa97
fix: lint
plyr4 Aug 21, 2024
02899ae
enhance: customizable resource and span attributes for restrictive co…
plyr4 Aug 26, 2024
14619d2
fix: min tls version
plyr4 Aug 26, 2024
3d273ce
trigger pipeline
plyr4 Aug 26, 2024
0397f5a
fix: compose options
plyr4 Aug 26, 2024
fcc8b23
fix: add disabled tracing config to db test
plyr4 Aug 26, 2024
bac7ab8
Merge branch 'main' into feat/otel-tracing
plyr4 Aug 26, 2024
fba0da2
Merge branch 'main' into feat/otel-tracing
plyr4 Aug 26, 2024
33bf135
fix: remove unused var
plyr4 Aug 26, 2024
50e82aa
Merge branch 'feat/otel-tracing' of github.com:go-vela/server into fe…
plyr4 Aug 26, 2024
093b912
Merge branch 'main' into feat/otel-tracing
plyr4 Sep 4, 2024
069c4d7
chore: go mod tidy
plyr4 Sep 4, 2024
85b67b1
chore: revert compose var
plyr4 Sep 4, 2024
37a6e9b
Merge branch 'main' into feat/otel-tracing
plyr4 Sep 5, 2024
0aa5de5
chore: remove bogus file
plyr4 Sep 5, 2024
938fe6b
Merge branch 'feat/otel-tracing' of github.com:go-vela/server into fe…
plyr4 Sep 5, 2024
e945cd8
Merge branch 'main' into feat/otel-tracing
KellyMerrick Sep 6, 2024
3cb9bb3
Merge branch 'main' into feat/otel-tracing
plyr4 Sep 9, 2024
ee8a03a
feat: tracing middleware tests
plyr4 Sep 9, 2024
cd973d5
chore: lint
plyr4 Sep 9, 2024
c95193b
Merge branch 'main' into feat/otel-tracing
plyr4 Sep 11, 2024
7b27836
enhance: configurable min-tls
plyr4 Sep 11, 2024
0525a14
fix: vela-server default service name
plyr4 Sep 11, 2024
00e22c4
chore: remove default env resource attrs
plyr4 Sep 11, 2024
9e8c026
Merge branch 'main' into feat/otel-tracing
ecrupper Sep 11, 2024
7e5ac7e
enhance: refactored attribute mapping
plyr4 Sep 11, 2024
ebc4427
Merge branch 'feat/otel-tracing' of github.com:go-vela/server into fe…
plyr4 Sep 11, 2024
2339384
chore: cleanup
plyr4 Sep 11, 2024
8379079
chore: lint
plyr4 Sep 11, 2024
e5f3a29
chore: cleanup
plyr4 Sep 11, 2024
f03fa06
chore: cleanup
plyr4 Sep 11, 2024
18ea2c4
chore: cleanup
plyr4 Sep 11, 2024
bb6b59a
fix: better url parsing
plyr4 Sep 11, 2024
2c4fe9e
enhance: docs links and flag clarification
plyr4 Sep 11, 2024
6db9549
enhance: more flag clarification
plyr4 Sep 11, 2024
c604d5f
Merge branch 'main' into feat/otel-tracing
wass3rw3rk Sep 12, 2024
d12f845
fix: conditionally apply scm tracing
plyr4 Sep 12, 2024
eb0d870
chore: tests
plyr4 Sep 12, 2024
7c98676
fix: do not cancel goroutine context used after http response closes
plyr4 Sep 12, 2024
f4a949d
fix: do not cancel goroutine context used after http response closes
plyr4 Sep 12, 2024
2139c9c
fix: avoid double responses
plyr4 Sep 12, 2024
da2bbee
chore: lint...
plyr4 Sep 12, 2024
1e20dd9
fix: scm test client
plyr4 Sep 12, 2024
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
4 changes: 2 additions & 2 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func CreateRepo(c *gin.Context) {
}).Infof("creating new repo %s", input.GetFullName())

// get repo information from the source
r, err := scm.FromContext(c).GetRepo(u, input)
r, err := scm.FromContext(c).GetRepo(c.Request.Context(), u, input)
if err != nil {
retErr := fmt.Errorf("unable to retrieve repo info for %s from source: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -256,7 +256,7 @@ func CreateRepo(c *gin.Context) {
// check if we should create the webhook
if c.Value("webhookvalidation").(bool) {
// send API call to create the webhook
h, _, err = scm.FromContext(c).Enable(u, r, h)
h, _, err = scm.FromContext(c).Enable(c.Request.Context(), u, r, h)
if err != nil {
retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/repo/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func DeleteRepo(c *gin.Context) {
}).Infof("deleting repo %s", r.GetFullName())

// send API call to remove the webhook
err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName())
err := scm.FromContext(c).Disable(c.Request.Context(), u, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err)

Expand Down
4 changes: 2 additions & 2 deletions api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RepairRepo(c *gin.Context) {
// check if we should create the webhook
if c.Value("webhookvalidation").(bool) {
// send API call to remove the webhook
err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName())
err := scm.FromContext(c).Disable(c.Request.Context(), u, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err)

Expand All @@ -87,7 +87,7 @@ func RepairRepo(c *gin.Context) {
}

// send API call to create the webhook
hook, _, err = scm.FromContext(c).Enable(u, r, hook)
hook, _, err = scm.FromContext(c).Enable(c.Request.Context(), u, r, hook)
if err != nil {
retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func CreateSchedule(c *gin.Context) {
s, _ = database.FromContext(c).GetScheduleForRepo(r, dbSchedule.GetName())
} else {
// send API call to create the schedule
err = database.FromContext(c).CreateSchedule(s)
err = database.FromContext(c).CreateSchedule(c.Request.Context(), s)
if err != nil {
retErr := fmt.Errorf("unable to create new schedule %s: %w", r.GetName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func SyncRepo(c *gin.Context) {
logger.Infof("syncing repo %s", r.GetFullName())

// retrieve repo from source code manager service
_, err := scm.FromContext(c).GetRepo(u, r)
_, err := scm.FromContext(c).GetRepo(c.Request.Context(), u, r)

// if there is an error retrieving repo, we know it is deleted: set to inactive
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func SyncReposForOrg(c *gin.Context) {

// iterate through captured repos and check if they are in GitHub
for _, repo := range repos {
_, err := scm.FromContext(c).GetRepo(u, repo)
_, err := scm.FromContext(c).GetRepo(c.Request.Context(), u, repo)
// if repo cannot be captured from GitHub, set to inactive in database
if err != nil {
repo.SetActive(false)
Expand Down
8 changes: 6 additions & 2 deletions api/user/get_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package user

import (
"fmt"
"go.opentelemetry.io/otel/trace"
JordanSussman marked this conversation as resolved.
Show resolved Hide resolved
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -41,20 +42,23 @@ import (
func GetSourceRepos(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
"user": u.GetName(),
"span_id": trace.SpanFromContext(ctx).SpanContext().SpanID(),
"trace_id": trace.SpanFromContext(ctx).SpanContext().TraceID(),
}).Infof("reading available SCM repos for user %s", u.GetName())

// variables to capture requested data
dbRepos := []*library.Repo{}
output := make(map[string][]library.Repo)

// send API call to capture the list of repos for the user
srcRepos, err := scm.FromContext(c).ListUserRepos(u)
srcRepos, err := scm.FromContext(c).ListUserRepos(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to get SCM repos for user %s: %w", u.GetName(), err)

Expand Down
4 changes: 4 additions & 0 deletions cmd/vela-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-vela/server/queue"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/secret"
"github.com/go-vela/server/tracing"
"github.com/go-vela/server/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -235,6 +236,9 @@ func main() {
// Add Source Flags
app.Flags = append(app.Flags, scm.Flags...)

// Add Tracing Flags
app.Flags = append(app.Flags, tracing.Flags...)

// set logrus to log in JSON format
logrus.SetFormatter(&logrus.JSONFormatter{})

Expand Down
29 changes: 28 additions & 1 deletion cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"github.com/go-vela/server/database"
"github.com/go-vela/server/router"
"github.com/go-vela/server/router/middleware"
"github.com/go-vela/server/tracing"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"golang.org/x/sync/errgroup"

"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -62,7 +64,29 @@ func server(c *cli.Context) error {
return err
}

database, err := database.FromCLIContext(c)
// create the tracing config
tc, err := tracing.New(c)
if err != nil {
return err
}

// opt-in for telemetry
tracingMiddleware := func(ctx *gin.Context) {}
if tc.EnableTracing {
// initialize tracing middleware
tracingMiddleware = otelgin.Middleware(c.String("tracing.service.name"),
otelgin.WithTracerProvider(tc.TracerProvider))

// shutdown tracing when server closes
defer func() {
err := tc.TracerProvider.Shutdown(context.Background())
if err != nil {
logrus.Errorf("unable to shutdown tracer provider: %v", err)
}
}()
}

database, err := database.FromCLIContext(c, tc)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,6 +132,9 @@ func server(c *cli.Context) error {
middleware.DefaultRepoEvents(c.StringSlice("default-repo-events")),
middleware.AllowlistSchedule(c.StringSlice("vela-schedule-allowlist")),
middleware.ScheduleFrequency(c.Duration("schedule-minimum-frequency")),

// tracing middle, which is an empty handler when tracing is disabled
tracingMiddleware,
)

addr, err := url.Parse(c.String("server-addr"))
Expand Down
6 changes: 5 additions & 1 deletion database/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package database
import (
"context"

"github.com/go-vela/server/tracing"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -30,6 +31,8 @@ func FromContext(c context.Context) Interface {
return nil
}

// assign as inflight property

return d
}

Expand All @@ -40,7 +43,7 @@ func ToContext(c Setter, d Interface) {
}

// FromCLIContext creates and returns a database engine from the urfave/cli context.
func FromCLIContext(c *cli.Context) (Interface, error) {
func FromCLIContext(c *cli.Context, tc *tracing.TracingConfig) (Interface, error) {
logrus.Debug("creating database engine from CLI configuration")

return New(
Expand All @@ -52,5 +55,6 @@ func FromCLIContext(c *cli.Context) (Interface, error) {
WithDriver(c.String("database.driver")),
WithEncryptionKey(c.String("database.encryption.key")),
WithSkipCreation(c.Bool("database.skip_creation")),
WithTracingConfig(tc),
)
}
2 changes: 1 addition & 1 deletion database/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestDatabase_FromCLIContext(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := FromCLIContext(test.context)
_, err := FromCLIContext(test.context, nil)

if test.failure {
if err == nil {
Expand Down
17 changes: 17 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"github.com/go-vela/server/database/step"
"github.com/go-vela/server/database/user"
"github.com/go-vela/server/database/worker"
"github.com/go-vela/server/tracing"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
"github.com/uptrace/opentelemetry-go-extra/otelgorm"

"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
Expand Down Expand Up @@ -56,6 +58,8 @@ type (
config *config
// sirupsen/logrus logger used in database functions
logger *logrus.Entry
// configurations related to telemetry/tracing
tracing *tracing.TracingConfig

build.BuildInterface
hook.HookInterface
Expand Down Expand Up @@ -129,6 +133,19 @@ func New(opts ...EngineOpt) (Interface, error) {
return nil, err
}

// initialize otel tracing if enabled
if e.tracing.EnableTracing {
otelPlugin := otelgorm.NewPlugin(
otelgorm.WithTracerProvider(e.tracing.TracerProvider),
otelgorm.WithoutQueryVariables(),
)

err := e.client.Use(otelPlugin)
if err != nil {
return nil, err
}
}

// set the maximum amount of time a connection may be reused
db.SetConnMaxLifetime(e.config.ConnectionLife)
// set the maximum number of connections in the idle connection pool
Expand Down
16 changes: 15 additions & 1 deletion database/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package database

import "time"
import (
"time"

"github.com/go-vela/server/tracing"
)

// EngineOpt represents a configuration option to initialize the database engine.
type EngineOpt func(*engine) error
Expand Down Expand Up @@ -88,3 +92,13 @@ func WithSkipCreation(skipCreation bool) EngineOpt {
return nil
}
}

// WithTracingConfig sets the shared tracing config in the database engine.
func WithTracingConfig(tracing *tracing.TracingConfig) EngineOpt {
return func(e *engine) error {
// set the tracing config
e.tracing = tracing

return nil
}
}
7 changes: 5 additions & 2 deletions database/schedule/count_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package schedule

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -46,12 +47,14 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
ctx := context.TODO()

err := _sqlite.CreateSchedule(ctx, _scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
err = _sqlite.CreateSchedule(ctx, _scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
7 changes: 5 additions & 2 deletions database/schedule/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package schedule

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -50,12 +51,14 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
ctx := context.TODO()

err := _sqlite.CreateSchedule(ctx, _scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
err = _sqlite.CreateSchedule(ctx, _scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
7 changes: 5 additions & 2 deletions database/schedule/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package schedule

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -44,12 +45,14 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSchedule(_scheduleOne)
ctx := context.TODO()

err := _sqlite.CreateSchedule(ctx, _scheduleOne)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}

err = _sqlite.CreateSchedule(_scheduleTwo)
err = _sqlite.CreateSchedule(ctx, _scheduleTwo)
if err != nil {
t.Errorf("unable to create test schedule for sqlite: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion database/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
package schedule

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)

// CreateSchedule creates a new schedule in the database.
func (e *engine) CreateSchedule(s *library.Schedule) error {
func (e *engine) CreateSchedule(ctx context.Context, s *library.Schedule) error {
e.logger.WithFields(logrus.Fields{
"schedule": s.GetName(),
"span_id": trace.SpanFromContext(ctx).SpanContext().SpanID(),
"trace_id": trace.SpanFromContext(ctx).SpanContext().TraceID(),
}).Tracef("creating schedule %s in the database", s.GetName())

// cast the library type to database type
Expand All @@ -29,6 +34,7 @@ func (e *engine) CreateSchedule(s *library.Schedule) error {

// send query to the database
return e.client.
WithContext(ctx).
Table(constants.TableSchedule).
Create(schedule).
Error
Expand Down
Loading