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

Add otel instrumentation to http/grpc/sql libraries. #2440

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions cmd/guaccollect/cmd/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"github.com/guacsec/guac/pkg/handler/collector"
"github.com/guacsec/guac/pkg/handler/collector/deps_dev"
"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -53,6 +54,8 @@
publishToQueue bool
// sets artificial latency on the deps.dev collector (default to nil)
addedLatency *time.Duration
// enable otel
enableOtel bool
}

var depsDevCmd = &cobra.Command{
Expand Down Expand Up @@ -90,13 +93,24 @@
viper.GetInt("prometheus-port"),
viper.GetBool("publish-to-queue"),
viper.GetString("deps-dev-latency"),
viper.GetBool("enable-otel"),
args,
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
_ = cmd.Help()
os.Exit(1)
}

var shutdown func(context.Context) error = func(context.Context) error { return nil }
if opts.enableOtel {
var err error
shutdown, err = metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
}

// Register collector
depsDevCollector, err := deps_dev.NewDepsCollector(ctx, opts.dataSource, opts.poll, opts.retrieveDependencies, 30*time.Second, opts.addedLatency)
if err != nil {
Expand All @@ -117,6 +131,7 @@
}

initializeNATsandCollector(ctx, opts.pubsubAddr, opts.blobAddr, opts.publishToQueue)
shutdown(ctx)

Check failure on line 134 in cmd/guaccollect/cmd/deps_dev.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be via a defer as soon as we create it? Right now (at least for this function) it doesn't seem to make a difference but to future proof when we add some code path that returns early

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in a number of cases (this one) we are not using special timing on shutdown and a defer would be better.

Other cases we should call shutdown when we get the C-c signal but before we call the context cancel function. I'll look through and see which should be change and which are more sensitive.

I think the main thing for the shutdown is to flush any metrics that are recorded but not sent (the send happens in batches). This will be more applicable in some of the guacone commands that use the instrumented clients but do no poll.

},
}

Expand All @@ -133,6 +148,7 @@
prometheusPort int,
pubToQueue bool,
addedLatencyStr string,
enableOtel bool,
args []string,
) (depsDevOptions, error) {
var opts depsDevOptions
Expand All @@ -143,6 +159,7 @@
opts.enablePrometheus = enablePrometheus
opts.prometheusPort = prometheusPort
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

if addedLatencyStr != "" {
addedLatency, err := time.ParseDuration(addedLatencyStr)
Expand Down
16 changes: 16 additions & 0 deletions cmd/guaccollect/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
csubclient "github.com/guacsec/guac/pkg/collectsub/client"
"github.com/guacsec/guac/pkg/collectsub/datasource/csubsource"
"github.com/guacsec/guac/pkg/collectsub/datasource/inmemsource"
"github.com/guacsec/guac/pkg/metrics"

"github.com/guacsec/guac/pkg/cli"

Expand Down Expand Up @@ -62,6 +63,8 @@
ownerRepoName string
// enable/disable message publish to queue
publishToQueue bool
// enable otel
enableOtel bool
}

var githubCmd = &cobra.Command{
Expand Down Expand Up @@ -102,13 +105,23 @@
viper.GetBool("use-csub"),
viper.GetBool("service-poll"),
viper.GetBool("publish-to-queue"),
viper.GetBool("enable-otel"),
args)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
_ = cmd.Help()
os.Exit(1)
}

var shutdown func(context.Context) error = func(context.Context) error { return nil }
if opts.enableOtel {
var err error
shutdown, err = metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
}

// GITHUB_TOKEN is the default token name
ghc, err := githubclient.NewGithubClient(ctx, os.Getenv("GITHUB_TOKEN"))
if err != nil {
Expand Down Expand Up @@ -154,6 +167,7 @@
}

initializeNATsandCollector(ctx, opts.pubsubAddr, opts.blobAddr, opts.publishToQueue)
shutdown(ctx)

Check failure on line 170 in cmd/guaccollect/cmd/github.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
},
}

Expand All @@ -169,6 +183,7 @@
useCsub,
poll bool,
pubToQueue bool,
enableOtel bool,
args []string,
) (githubOptions, error) {
var opts githubOptions
Expand All @@ -179,6 +194,7 @@
opts.sbomName = sbomName
opts.workflowFileName = workflowFileName
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

if useCsub {
csubOpts, err := csubclient.ValidateCsubClientFlags(csubAddr, csubTls, csubTlsSkipVerify)
Expand Down
20 changes: 19 additions & 1 deletion cmd/guaccollect/cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"github.com/guacsec/guac/pkg/certifier/components/root_package"
"github.com/guacsec/guac/pkg/cli"
"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -58,6 +59,8 @@
// last time the scan was done in hours, if not set it will return
// all packages to check
lastScan *int
// enable otel
enableOtel bool
}

var cdCmd = &cobra.Command{
Expand Down Expand Up @@ -90,6 +93,7 @@
viper.GetString("certifier-latency"),
viper.GetInt("certifier-batch-size"),
viper.GetInt("last-scan"),
viper.GetBool("enable-otel"),
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
Expand All @@ -100,6 +104,15 @@
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

var shutdown func(context.Context) error = func(context.Context) error { return nil }
if opts.enableOtel {
var err error
shutdown, err = metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
}

if err := certify.RegisterCertifier(clearlydefined.NewClearlyDefinedCertifier, certifier.CertifierClearlyDefined); err != nil {
logger.Fatalf("unable to register certifier: %v", err)
}
Expand All @@ -115,6 +128,7 @@
}

initializeNATsandCertifier(ctx, opts.blobAddr, opts.pubsubAddr, opts.poll, opts.publishToQueue, opts.interval, packageQueryFunc())
shutdown(ctx)

Check failure on line 131 in cmd/guaccollect/cmd/license.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
},
}

Expand All @@ -134,7 +148,10 @@
poll bool,
pubToQueue bool,
certifierLatencyStr string,
batchSize int, lastScan int) (cdOptions, error) {
batchSize int,
lastScan int,
enableOtel bool,
) (cdOptions, error) {

var opts cdOptions

Expand All @@ -144,6 +161,7 @@
opts.blobAddr = blobAddr
opts.poll = poll
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

i, err := time.ParseDuration(interval)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/guaccollect/cmd/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"github.com/guacsec/guac/pkg/handler/collector"
"github.com/guacsec/guac/pkg/handler/processor"
"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -68,6 +69,8 @@
lastScan *int
// adds metadata to vulnerabities during collection
addVulnMetadata bool
// enable otel
enableOtel bool
}

var osvCmd = &cobra.Command{
Expand Down Expand Up @@ -101,6 +104,7 @@
viper.GetInt("certifier-batch-size"),
viper.GetInt("last-scan"),
viper.GetBool("add-vuln-metadata"),
viper.GetBool("enable-otel"),
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
Expand All @@ -111,6 +115,15 @@
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

var shutdown func(context.Context) error = func(context.Context) error { return nil }
if opts.enableOtel {
var err error
shutdown, err = metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
}

if err := certify.RegisterCertifier(func() certifier.Certifier {
certifierOpts := []osv.CertifierOpts{}
if opts.addVulnMetadata {
Expand All @@ -132,6 +145,7 @@
}

initializeNATsandCertifier(ctx, opts.blobAddr, opts.pubsubAddr, opts.poll, opts.publishToQueue, opts.interval, packageQueryFunc())
shutdown(ctx)

Check failure on line 148 in cmd/guaccollect/cmd/osv.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value is not checked (errcheck)
},
}

Expand All @@ -146,6 +160,7 @@
certifierLatencyStr string,
batchSize int, lastScan int,
addVulnMetadata bool,
enableOtel bool,
) (osvOptions, error) {
var opts osvOptions

Expand All @@ -156,6 +171,7 @@
opts.poll = poll
opts.publishToQueue = pubToQueue
opts.addVulnMetadata = addVulnMetadata
opts.enableOtel = enableOtel

i, err := time.ParseDuration(interval)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/guaccollect/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
"enable-prometheus",
"publish-to-queue",
"gql-addr",
"enable-otel",
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions cmd/guacgql/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var flags = struct {
tlsKeyFile string
debug bool
tracegql bool
enableOtel bool
}{}

var rootCmd = &cobra.Command{
Expand All @@ -48,6 +49,7 @@ var rootCmd = &cobra.Command{
flags.tlsKeyFile = viper.GetString("gql-tls-key-file")
flags.debug = viper.GetBool("gql-debug")
flags.tracegql = viper.GetBool("gql-trace")
flags.enableOtel = viper.GetBool("enable-otel")

startServer(cmd)
},
Expand All @@ -65,6 +67,7 @@ func init() {
"gql-backend",
"gql-trace",
"enable-prometheus",
"enable-otel",
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
Expand Down
30 changes: 21 additions & 9 deletions cmd/guacgql/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ import (
"syscall"
"time"

// import all known backends
_ "github.com/guacsec/guac/pkg/assembler/backends/neo4j"
_ "github.com/guacsec/guac/pkg/assembler/backends/neptune"
_ "github.com/guacsec/guac/pkg/assembler/backends/ent/backend"
_ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue"
_ "github.com/guacsec/guac/pkg/assembler/backends/arangodb"

"github.com/99designs/gqlgen/graphql/handler/debug"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

"github.com/guacsec/guac/pkg/assembler/backends"
// import all known backends
_ "github.com/guacsec/guac/pkg/assembler/backends/arangodb"
_ "github.com/guacsec/guac/pkg/assembler/backends/ent/backend"
_ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue"
_ "github.com/guacsec/guac/pkg/assembler/backends/neo4j"
_ "github.com/guacsec/guac/pkg/assembler/backends/neptune"
"github.com/guacsec/guac/pkg/assembler/server"
"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
"github.com/guacsec/guac/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func startServer(cmd *cobra.Command) {
Expand Down Expand Up @@ -80,6 +81,16 @@ func startServer(cmd *cobra.Command) {
srvHandler = srv
}

var shutdown func(context.Context) error = func(context.Context) error { return nil }
if flags.enableOtel {
var err error
shutdown, err = metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
srvHandler = otelhttp.NewHandler(srvHandler, "/")
}

if flags.tracegql {
tracer := &debug.Tracer{}
srv.Use(tracer)
Expand Down Expand Up @@ -115,6 +126,7 @@ func startServer(cmd *cobra.Command) {
ctx, cf := context.WithCancel(ctx)
go func() {
_ = server.Shutdown(ctx)
_ = shutdown(ctx)
done <- true
}()
select {
Expand Down
Loading
Loading