Skip to content

Commit

Permalink
Add otel instrumentation to http/grpc/sql libraries. (#2440)
Browse files Browse the repository at this point in the history
* Add otel instrumentation to http/grpc/sql libraries.

Also add cli arg to the clis that use those libraries to enable exporting
metrics and traces. Docs under pkg/metrics. Default to off and also change
prometheus default to off.

Signed-off-by: Jeff Mendoza <jlm@jlm.name>

* Update shutdown for otel on cli commands

Signed-off-by: Jeff Mendoza <jlm@jlm.name>

---------

Signed-off-by: Jeff Mendoza <jlm@jlm.name>
  • Loading branch information
jeffmendoza authored Jan 16, 2025
1 parent 3a1318b commit 6ad13e9
Show file tree
Hide file tree
Showing 25 changed files with 500 additions and 57 deletions.
19 changes: 19 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 @@ import (
"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 @@ type depsDevOptions struct {
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,27 @@ you have access to read and write to the respective blob store.`,
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)
}

if opts.enableOtel {
shutdown, err := metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
defer func() {
if err := shutdown(ctx); err != nil {
logger.Errorf("Error on Otel shutdown: %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 Down Expand Up @@ -133,6 +150,7 @@ func validateDepsDevFlags(
prometheusPort int,
pubToQueue bool,
addedLatencyStr string,
enableOtel bool,
args []string,
) (depsDevOptions, error) {
var opts depsDevOptions
Expand All @@ -143,6 +161,7 @@ func validateDepsDevFlags(
opts.enablePrometheus = enablePrometheus
opts.prometheusPort = prometheusPort
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

if addedLatencyStr != "" {
addedLatency, err := time.ParseDuration(addedLatencyStr)
Expand Down
18 changes: 18 additions & 0 deletions cmd/guaccollect/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
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 @@ type githubOptions struct {
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,26 @@ you have access to read and write to the respective blob store.`,
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)
}

if opts.enableOtel {
shutdown, err := metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
defer func() {
if err := shutdown(ctx); err != nil {
logger.Errorf("Error on Otel shutdown: %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 @@ -169,6 +185,7 @@ func validateGithubFlags(
useCsub,
poll bool,
pubToQueue bool,
enableOtel bool,
args []string,
) (githubOptions, error) {
var opts githubOptions
Expand All @@ -179,6 +196,7 @@ func validateGithubFlags(
opts.sbomName = sbomName
opts.workflowFileName = workflowFileName
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

if useCsub {
csubOpts, err := csubclient.ValidateCsubClientFlags(csubAddr, csubTls, csubTlsSkipVerify)
Expand Down
22 changes: 21 additions & 1 deletion cmd/guaccollect/cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"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 @@ type cdOptions struct {
// 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 @@ you have access to read and write to the respective blob store.`,
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,18 @@ you have access to read and write to the respective blob store.`,
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

if opts.enableOtel {
shutdown, err := metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
defer func() {
if err := shutdown(ctx); err != nil {
logger.Errorf("Error on Otel shutdown: %v", err)
}
}()
}

if err := certify.RegisterCertifier(clearlydefined.NewClearlyDefinedCertifier, certifier.CertifierClearlyDefined); err != nil {
logger.Fatalf("unable to register certifier: %v", err)
}
Expand Down Expand Up @@ -134,7 +150,10 @@ func validateCDFlags(
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 +163,7 @@ func validateCDFlags(
opts.blobAddr = blobAddr
opts.poll = poll
opts.publishToQueue = pubToQueue
opts.enableOtel = enableOtel

i, err := time.ParseDuration(interval)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions cmd/guaccollect/cmd/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"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 @@ type osvOptions struct {
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 @@ you have access to read and write to the respective blob store.`,
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,18 @@ you have access to read and write to the respective blob store.`,
ctx := logging.WithLogger(context.Background())
logger := logging.FromContext(ctx)

if opts.enableOtel {
shutdown, err := metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}
defer func() {
if err := shutdown(ctx); err != nil {
logger.Errorf("Error on Otel shutdown: %v", err)
}
}()
}

if err := certify.RegisterCertifier(func() certifier.Certifier {
certifierOpts := []osv.CertifierOpts{}
if opts.addVulnMetadata {
Expand Down Expand Up @@ -146,6 +162,7 @@ func validateOSVFlags(
certifierLatencyStr string,
batchSize int, lastScan int,
addVulnMetadata bool,
enableOtel bool,
) (osvOptions, error) {
var opts osvOptions

Expand All @@ -156,6 +173,7 @@ func validateOSVFlags(
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
34 changes: 25 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,21 @@ func startServer(cmd *cobra.Command) {
srvHandler = srv
}

if flags.enableOtel {
shutdown, err := metrics.SetupOTelSDK(ctx)
if err != nil {
logger.Fatalf("Error setting up Otel: %v", err)
}

srvHandler = otelhttp.NewHandler(srvHandler, "/")

defer func() {
if err := shutdown(ctx); err != nil {
logger.Errorf("Error on Otel shutdown: %v", err)
}
}()
}

if flags.tracegql {
tracer := &debug.Tracer{}
srv.Use(tracer)
Expand Down
Loading

0 comments on commit 6ad13e9

Please sign in to comment.