Skip to content

[db] Setup go db tracing #16706

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

Merged
merged 8 commits into from
Mar 10, 2023
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
19 changes: 15 additions & 4 deletions components/gitpod-db/go/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/plugin/opentelemetry/tracing"
)

type ConnectionParams struct {
Expand All @@ -41,7 +42,7 @@ func ConnectionParamsFromEnv() ConnectionParams {
func Connect(p ConnectionParams) (*gorm.DB, error) {
loc, err := time.LoadLocation("UTC")
if err != nil {
return nil, fmt.Errorf("Failed to load UT location: %w", err)
return nil, fmt.Errorf("Failed to load UTC location: %w", err)
}
cfg := driver_mysql.Config{
User: p.User,
Expand All @@ -57,7 +58,7 @@ func Connect(p ConnectionParams) (*gorm.DB, error) {
if p.CaCert != "" {
rootCertPool := x509.NewCertPool()
if ok := rootCertPool.AppendCertsFromPEM([]byte(p.CaCert)); !ok {
log.Fatal("Failed to append custom DB CA cert.")
return nil, fmt.Errorf("failed to append custom certificate for database connection")
}

tlsConfigName := "custom"
Expand All @@ -66,13 +67,13 @@ func Connect(p ConnectionParams) (*gorm.DB, error) {
MinVersion: tls.VersionTLS12, // semgrep finding: set lower boundary to exclude insecure TLS1.0
})
if err != nil {
return nil, fmt.Errorf("Failed to register custom DB CA cert: %w", err)
return nil, fmt.Errorf("failed to register custom DB CA cert: %w", err)
}
cfg.TLSConfig = tlsConfigName
}

// refer to https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
return gorm.Open(mysql.Open(cfg.FormatDSN()), &gorm.Config{
conn, err := gorm.Open(mysql.Open(cfg.FormatDSN()), &gorm.Config{
Logger: logger.New(log.Log, logger.Config{
SlowThreshold: 200 * time.Millisecond,
Colorful: false,
Expand All @@ -89,4 +90,14 @@ func Connect(p ConnectionParams) (*gorm.DB, error) {
})(),
}),
})
if err != nil {
return nil, fmt.Errorf("failed to open db connection: %w", err)
}

err = conn.Use(tracing.NewPlugin())
if err != nil {
return nil, fmt.Errorf("failed to setup db tracing: %w", err)
}

return conn, nil
}
6 changes: 6 additions & 0 deletions components/gitpod-db/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ require (
gorm.io/datatypes v1.0.7
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.1
gorm.io/plugin/opentelemetry v0.1.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
Expand Down
16 changes: 15 additions & 1 deletion components/gitpod-db/go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions components/public-api-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ require (
require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
gorm.io/driver/mysql v1.4.4 // indirect
gorm.io/plugin/opentelemetry v0.1.1 // indirect
)

require (
github.com/alicebob/miniredis/v2 v2.30.0
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/configcat/go-sdk/v7 v7.6.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
Expand Down Expand Up @@ -79,7 +85,7 @@ require (
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/zitadel/logging v0.3.4 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
Expand Down
25 changes: 19 additions & 6 deletions components/public-api-server/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
Expand All @@ -44,7 +46,10 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/atomic v1.6.0 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.3.0 // indirect
Expand All @@ -55,6 +60,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/datatypes v1.0.7 // indirect
gorm.io/driver/mysql v1.4.4 // indirect
gorm.io/plugin/opentelemetry v0.1.1 // indirect
)

replace github.com/gitpod-io/gitpod/components/gitpod-db/go => ../gitpod-db/go // leeway
Expand Down
Loading