diff --git a/sdk/instrumentation/database/sql/sql.go b/sdk/instrumentation/database/sql/sql.go index 8bdd7d7b..ed62d08f 100644 --- a/sdk/instrumentation/database/sql/sql.go +++ b/sdk/instrumentation/database/sql/sql.go @@ -11,6 +11,8 @@ import ( "github.com/ngrok/sqlmw" "reflect" + + internalconfig "github.com/hypertrace/goagent/sdk/internal/config" ) var regMu sync.Mutex @@ -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} @@ -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 { diff --git a/sdk/instrumentation/google.golang.org/grpc/client.go b/sdk/instrumentation/google.golang.org/grpc/client.go index 474c07e5..ecc89a88 100644 --- a/sdk/instrumentation/google.golang.org/grpc/client.go +++ b/sdk/instrumentation/google.golang.org/grpc/client.go @@ -14,6 +14,11 @@ 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", } @@ -21,7 +26,7 @@ func WrapUnaryClientInterceptor(delegateInterceptor grpc.UnaryClientInterceptor, 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 diff --git a/sdk/instrumentation/google.golang.org/grpc/server.go b/sdk/instrumentation/google.golang.org/grpc/server.go index 3123c60e..13b4dccf 100644 --- a/sdk/instrumentation/google.golang.org/grpc/server.go +++ b/sdk/instrumentation/google.golang.org/grpc/server.go @@ -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", } @@ -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), ) } } @@ -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", } @@ -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(), } } diff --git a/sdk/instrumentation/net/http/handler.go b/sdk/instrumentation/net/http/handler.go index ad96b180..4619eecf 100644 --- a/sdk/instrumentation/net/http/handler.go +++ b/sdk/instrumentation/net/http/handler.go @@ -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 @@ -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) { diff --git a/sdk/instrumentation/net/http/transport.go b/sdk/instrumentation/net/http/transport.go index 161785a7..de9b3d6f 100644 --- a/sdk/instrumentation/net/http/transport.go +++ b/sdk/instrumentation/net/http/transport.go @@ -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()} }