Skip to content

hackathon: add OpenTelemetry tracing #1262

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ LOCAL_VOLUME_ARGS := -v$(CURDIR):/src:delegated -v $(GOPATH):/go:delegated
GOPATH_WD_OVERRIDES := -w /src -e GOPATH=/go
IMAGE_BUILD_FLAGS := -e CGO_ENABLED=0 -e GOOS=linux -e GOARCH=${GOARCH}
BUILD_FLAGS := CGO_ENABLED=0 GOOS=linux GOARCH=${GOARCH}
BUILD_CMD := go build -trimpath -ldflags="-X github.com/stackrox/scanner/pkg/version.Version=$(TAG)" -o image/scanner/bin/scanner ./cmd/clair
BUILD_CMD := go build -trimpath -buildvcs=false -ldflags="-X github.com/stackrox/scanner/pkg/version.Version=$(TAG)" -o image/scanner/bin/scanner ./cmd/clair
NODESCAN_BUILD_CMD := go build -trimpath -o tools/bin/local-nodescanner ./tools/local-nodescanner

#####################################################################
Expand Down Expand Up @@ -479,7 +479,7 @@ local-nodescanner:
$(BUILD_FLAGS) $(NODESCAN_BUILD_CMD)

.PHONY: local-nodescanner-build-dockerized
local-nodescanner-build-dockerized:
local-nodescanner-build-dockerized:
@echo "+ $@"
ifdef CI
docker container create --name builder $(BUILD_IMAGE) $(NODESCAN_BUILD_CMD)
Expand Down
9 changes: 9 additions & 0 deletions api/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
log "github.com/sirupsen/logrus"
"github.com/stackrox/scanner/pkg/features"
"github.com/stackrox/scanner/pkg/mtls"
"github.com/stackrox/scanner/pkg/observability/tracing"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand Down Expand Up @@ -151,6 +154,9 @@ func WithDefaultInterceptors() ConfigOpts {
slimModeUnaryServerInterceptor(),
grpcprometheus.UnaryServerInterceptor,
}
if features.Tracing.Enabled() {
cfg.UnaryInterceptors = append(cfg.UnaryInterceptors, tracing.UnaryServerInterceptor())
}
}
}

Expand All @@ -175,6 +181,9 @@ func (a *apiImpl) Register(services ...APIService) {
func (a *apiImpl) muxer(localConn *grpc.ClientConn) http.Handler {
mux := http.NewServeMux()
for route, handler := range a.config.CustomRoutes {
if features.Tracing.Enabled() {
handler = otelhttp.NewHandler(handler, route)
}
mux.Handle(route, handler)
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ import (
"github.com/stackrox/scanner/pkg/clairify/metrics"
"github.com/stackrox/scanner/pkg/clairify/server"
"github.com/stackrox/scanner/pkg/env"
"github.com/stackrox/scanner/pkg/features"
"github.com/stackrox/scanner/pkg/formatter"
"github.com/stackrox/scanner/pkg/ioutils"
"github.com/stackrox/scanner/pkg/observability/tracing"
"github.com/stackrox/scanner/pkg/repo2cpe"
"github.com/stackrox/scanner/pkg/updater"
"github.com/stackrox/scanner/pkg/version"
Expand Down Expand Up @@ -179,6 +181,11 @@ func Boot(config *Config, slimMode bool) {
metricsServ := metrics.NewHTTPServer(config.API)
go metricsServ.RunForever()

if features.Tracing.Enabled() {
tracing.Singleton().Start(tracing.ScannerResource())
defer tracing.Singleton().Stop()
}

serv := server.New(fmt.Sprintf(":%d", config.API.HTTPSPort), db)
go api.RunClairify(serv)

Expand Down
16 changes: 15 additions & 1 deletion database/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/stackrox/scanner/database/metrics"
"github.com/stackrox/scanner/database/pgsql/migrations"
"github.com/stackrox/scanner/pkg/commonerr"
"go.nhat.io/otelsql"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -132,8 +134,20 @@ func openDatabase(registrableComponentConfig database.RegistrableComponentConfig
}
}

// Register OpenTelemetry tracer.
driverName, err := otelsql.Register("postgres",
otelsql.AllowRoot(),
otelsql.TraceQueryWithoutArgs(),
otelsql.TraceRowsClose(),
otelsql.TraceRowsAffected(),
otelsql.WithSystem(semconv.DBSystemPostgreSQL),
)
if err != nil {
return nil, errors.Wrap(err, "could not register OpenTelemetry database tracer")
}

// Open database.
pg.DB, err = sql.Open("postgres", src)
pg.DB, err = sql.Open(driverName, src)
if err != nil {
pg.Close()
return nil, fmt.Errorf("pgsql: could not open database: %v", err)
Expand Down
18 changes: 16 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ require (
github.com/stackrox/rox v0.0.0-20210914215712-9ac265932e28
github.com/stretchr/testify v1.8.4
go.etcd.io/bbolt v1.3.7
go.nhat.io/otelsql v0.12.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.44.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.44.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.18.0
go.opentelemetry.io/otel/sdk v1.18.0
go.uber.org/goleak v1.2.1
go.uber.org/ratelimit v0.3.0
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
golang.org/x/sys v0.12.0
google.golang.org/api v0.140.0
google.golang.org/grpc v1.57.0
google.golang.org/grpc v1.58.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -65,6 +71,7 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/cfssl v1.6.3 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
Expand All @@ -76,8 +83,10 @@ require (
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/facebookincubator/flog v0.0.0-20190930132826-d2511d0ce33c // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gofrs/uuid v4.3.1+incompatible // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand All @@ -88,6 +97,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
Expand All @@ -107,7 +117,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tkuchiki/go-timezone v0.2.2 // indirect
Expand All @@ -118,6 +128,10 @@ require (
github.com/zmap/zcrypto v0.0.0-20220402174210-599ec18ecbac // indirect
github.com/zmap/zlint/v3 v3.4.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
Expand Down
Loading