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: adds support for enabled/disabled when it comes to wrappers. #163

Open
wants to merge 3 commits 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
12 changes: 12 additions & 0 deletions sdk/instrumentation/database/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/ngrok/sqlmw"

"reflect"

internalconfig "github.com/hypertrace/goagent/sdk/internal/config"
)

var regMu sync.Mutex
Expand Down Expand Up @@ -190,6 +192,11 @@ func (w *dsnReadWrapper) parseDSNAttributes(dsn string) map[string]string {

// Wrap takes a SQL driver and wraps it with Hypertrace instrumentation.
func Wrap(d driver.Driver, startSpan sdk.StartSpan) driver.Driver {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return d
}

driverName := getDriverName(d)
in := &interceptor{startSpan: startSpan}
return &dsnReadWrapper{Driver: sqlmw.Driver(d, in), driverName: driverName, inDefaultAttributes: &in.defaultAttributes}
Expand All @@ -199,6 +206,11 @@ func Wrap(d driver.Driver, startSpan sdk.StartSpan) driver.Driver {
// identified by its driverName. On success it
// returns the generated driverName to use when calling hypersql.Open.
func Register(driverName string, startSpan sdk.StartSpan) (string, error) {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return driverName, nil
}

// retrieve the driver implementation we need to wrap with instrumentation
db, err := stdSQL.Open(driverName, "")
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion sdk/instrumentation/google.golang.org/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ import (
// WrapUnaryClientInterceptor returns an interceptor that records the request and response message's body
// and serialize it as JSON.
func WrapUnaryClientInterceptor(delegateInterceptor grpc.UnaryClientInterceptor, spanFromContext sdk.SpanFromContext) grpc.UnaryClientInterceptor {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return delegateInterceptor
}

defaultAttributes := map[string]string{
"rpc.system": "grpc",
}
if containerID, err := container.GetID(); err == nil {
defaultAttributes["container_id"] = containerID
}

dataCaptureConfig := internalconfig.GetConfig().GetDataCapture()
dataCaptureConfig := cfg.GetDataCapture()

return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
var header metadata.MD
Expand Down
14 changes: 12 additions & 2 deletions sdk/instrumentation/google.golang.org/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func WrapUnaryServerInterceptor(
spanFromContext sdk.SpanFromContext,
options *Options,
) grpc.UnaryServerInterceptor {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return delegateInterceptor
}

defaultAttributes := map[string]string{
"rpc.system": "grpc",
}
Expand All @@ -50,7 +55,7 @@ func WrapUnaryServerInterceptor(
ctx,
req,
info,
wrapHandler(info.FullMethod, handler, spanFromContext, defaultAttributes, internalconfig.GetConfig().GetDataCapture(), options),
wrapHandler(info.FullMethod, handler, spanFromContext, defaultAttributes, cfg.GetDataCapture(), options),
)
}
}
Expand Down Expand Up @@ -225,6 +230,11 @@ func (s *handler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Con
// WrapStatsHandler wraps an instrumented StatsHandler and returns a new one that records
// the request/response body and metadata.
func WrapStatsHandler(delegate stats.Handler, spanFromContext sdk.SpanFromContext) stats.Handler {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return delegate
}

defaultAttributes := map[string]string{
"rpc.system": "grpc",
}
Expand All @@ -236,7 +246,7 @@ func WrapStatsHandler(delegate stats.Handler, spanFromContext sdk.SpanFromContex
Handler: delegate,
spanFromContext: spanFromContext,
defaultAttributes: defaultAttributes,
dataCaptureConfig: internalconfig.GetConfig().GetDataCapture(),
dataCaptureConfig: cfg.GetDataCapture(),
}
}

Expand Down
7 changes: 6 additions & 1 deletion sdk/instrumentation/net/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type Options struct {
// WrapHandler wraps an uninstrumented handler (e.g. a handleFunc) and returns a new one
// that should be used as base to an instrumented handler
func WrapHandler(delegate http.Handler, spanFromContext sdk.SpanFromContext, options *Options) http.Handler {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return delegate
}

defaultAttributes := make(map[string]string)
if containerID, err := container.GetID(); err == nil {
defaultAttributes["container_id"] = containerID
Expand All @@ -37,7 +42,7 @@ func WrapHandler(delegate http.Handler, spanFromContext sdk.SpanFromContext, opt
if options != nil && options.Filter != nil {
f = options.Filter
}
return &handler{delegate, defaultAttributes, spanFromContext, internalconfig.GetConfig().GetDataCapture(), f}
return &handler{delegate, defaultAttributes, spanFromContext, cfg.GetDataCapture(), f}
}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 6 additions & 1 deletion sdk/instrumentation/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// WrapTransport returns a new http.RoundTripper that should be wrapped
// by an instrumented http.RoundTripper
func WrapTransport(delegate http.RoundTripper, spanFromContextRetriever sdk.SpanFromContext) http.RoundTripper {
cfg := internalconfig.GetConfig()
if cfg.Enabled != nil && !cfg.Enabled.Value {
return delegate
}

defaultAttributes := make(map[string]string)
if containerID, err := container.GetID(); err == nil {
defaultAttributes["container_id"] = containerID
}

return &roundTripper{delegate, defaultAttributes, spanFromContextRetriever, internalconfig.GetConfig().GetDataCapture()}
return &roundTripper{delegate, defaultAttributes, spanFromContextRetriever, cfg.GetDataCapture()}
}