diff --git a/bootstrap.go b/bootstrap.go index 2ed50fae..251e0b32 100644 --- a/bootstrap.go +++ b/bootstrap.go @@ -25,7 +25,7 @@ const ( // StartDatadog parallel process to collect data for Datadog. // connectionType flag related to Datadog connection type, it supports HTTP or socket - values will be used from config.DatadogParameters func StartDatadog(cfg config.DatadogParameters, connectionType ConnectionType) error { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return nil } diff --git a/docs/index.md b/docs/index.md index 9e3f2c00..1a7b11ff 100644 --- a/docs/index.md +++ b/docs/index.md @@ -22,11 +22,11 @@ simple metric service that allows sending `Incr`, `Gauge`, and `Count`. In order for `go-datadog-lib` to send data to Datadog your service/application must be configured correctly. -Setting the environment variable `DD_ENABLE` to a `false` or any other value +Setting the environment variable `DD_DISABLE` to a `true` or any other value that [`strconv#ParseBool`](https://pkg.go.dev/strconv#ParseBool) can parse to -`false` with out returning an error. If `DD_ENABLE` is undefined or a value +`true` with out returning an error. If `DD_DISABLE` is undefined or a value that [`strconv#ParseBool`](https://pkg.go.dev/strconv#ParseBool) can parse to -`true` or returns an error the library will be enabled. This is done to ensure +`false` or returns an error the library will be enabled. This is done to ensure that the library is not disabled in production by accident. ### 1. Setup container diff --git a/internal/env.go b/internal/env.go index a2e4309c..e7e53b48 100644 --- a/internal/env.go +++ b/internal/env.go @@ -20,12 +20,14 @@ const ( DatadogEnableExtraProfiling = "DD_ENABLE_EXTRA_PROFILING" // DatadogEnable is the environment variable key for whether to enable the Datadog integration. DatadogEnable = "DD_ENABLE" + // DatadogDisable is the environment variable key for whether to disable the Datadog integration. + DatadogDisable = "DD_DISABLE" ) // IsDatadogConfigured checks some common environment-variables to determine if // the service is configured to use Datadog. func IsDatadogConfigured() bool { - if !IsDatadogEnabled() { + if IsDatadogDisabled() { return false } if val := os.Getenv(DatadogEnvironment); val != "" { @@ -40,14 +42,14 @@ func IsDatadogConfigured() bool { return false } -// IsDatadogEnabled checks if the Datadog integration is enabled. The -// environment variable DD_ENABLE is checked. If the variable is missing the -// Datadog integration is assumed to be enabled. -func IsDatadogEnabled() bool { - valStr := os.Getenv(DatadogEnable) +// IsDatadogDisabled checks if the Datadog integration is disabled. The +// environment variable DD_DISABLE is checked. If the variable is missing or +// cannot be parsed to a bool the Datadog integration is assumed to be enabled. +func IsDatadogDisabled() bool { + valStr := os.Getenv(DatadogDisable) val, err := strconv.ParseBool(valStr) if err != nil { - return true + return false } return val } diff --git a/internal/env_test.go b/internal/env_test.go index 0893eab2..7e22530f 100644 --- a/internal/env_test.go +++ b/internal/env_test.go @@ -10,33 +10,33 @@ import ( "github.com/coopnorge/go-datadog-lib/v2/internal" ) -func TestIsDatadogEnabledFlagNotSet(t *testing.T) { - t.Setenv(internal.DatadogEnable, "") - os.Unsetenv(internal.DatadogEnable) +func TestIsDatadogDisabledFlagNotSet(t *testing.T) { + t.Setenv(internal.DatadogDisable, "") + os.Unsetenv(internal.DatadogDisable) - assert.True(t, internal.IsDatadogEnabled()) + assert.False(t, internal.IsDatadogDisabled()) } -func TestIsDatadogEnabledFlagEmpty(t *testing.T) { - t.Setenv(internal.DatadogEnable, "") +func TestIsDatadogDisabledFlagEmpty(t *testing.T) { + t.Setenv(internal.DatadogDisable, "") - assert.True(t, internal.IsDatadogEnabled()) + assert.False(t, internal.IsDatadogDisabled()) } -func TestIsDatadogEnabledFlagTrue(t *testing.T) { - t.Setenv(internal.DatadogEnable, strconv.FormatBool(true)) +func TestIsDatadogDisabledFlagTrue(t *testing.T) { + t.Setenv(internal.DatadogDisable, strconv.FormatBool(true)) - assert.True(t, internal.IsDatadogEnabled()) + assert.True(t, internal.IsDatadogDisabled()) } -func TestIsDatadogEnabledFlagFalse(t *testing.T) { - t.Setenv(internal.DatadogEnable, strconv.FormatBool(false)) +func TestIsDatadogDisabledFlagFalse(t *testing.T) { + t.Setenv(internal.DatadogDisable, strconv.FormatBool(false)) - assert.False(t, internal.IsDatadogEnabled()) + assert.False(t, internal.IsDatadogDisabled()) } -func TestIsDatadogEnabledNonBoolValue(t *testing.T) { - t.Setenv(internal.DatadogEnable, "Hello") +func TestIsDatadogDisabledNonBoolValue(t *testing.T) { + t.Setenv(internal.DatadogDisable, "Hello") - assert.True(t, internal.IsDatadogEnabled()) + assert.False(t, internal.IsDatadogDisabled()) } diff --git a/middleware/echo/echo.go b/middleware/echo/echo.go index cd9afcc3..a3602b88 100644 --- a/middleware/echo/echo.go +++ b/middleware/echo/echo.go @@ -8,7 +8,7 @@ import ( // TraceServerMiddleware for Datadog Log Integration, middleware will create span that can be used from context func TraceServerMiddleware() echo.MiddlewareFunc { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpMiddlewareFunc() } diff --git a/middleware/gorm/gorm.go b/middleware/gorm/gorm.go index fb01446f..381b1d41 100644 --- a/middleware/gorm/gorm.go +++ b/middleware/gorm/gorm.go @@ -11,7 +11,7 @@ import ( // NewORM returns a new gorm DB instance. // Create a dialector by calling e.g. https://pkg.go.dev/gorm.io/driver/mysql#New func NewORM(dialector gorm.Dialector, gormCfg *gorm.Config, options ...Option) (*gorm.DB, error) { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return gorm.Open(dialector, gormCfg) } diff --git a/middleware/grpc/client.go b/middleware/grpc/client.go index 86dd2296..1210cffc 100644 --- a/middleware/grpc/client.go +++ b/middleware/grpc/client.go @@ -13,7 +13,7 @@ import ( // // Deprecated: Use UnaryClientInterceptor instead. This function will be removed in a later version. func TraceUnaryClientInterceptor() grpc.UnaryClientInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpUnaryClientInterceptor() } @@ -23,7 +23,7 @@ func TraceUnaryClientInterceptor() grpc.UnaryClientInterceptor { // UnaryClientInterceptor create a client-interceptor to automatically create child-spans, and append to gRPC metadata. // UnaryServerInterceptor returns a middleware that creates datadog-spans on outgoing requests, and adds them to the request's gRPC-metadata. func UnaryClientInterceptor(options ...Option) grpc.UnaryClientInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpUnaryClientInterceptor() } diff --git a/middleware/grpc/server.go b/middleware/grpc/server.go index 763f3855..ec0a3418 100644 --- a/middleware/grpc/server.go +++ b/middleware/grpc/server.go @@ -12,7 +12,7 @@ import ( // // Deprecated: Use UnaryServerInterceptor instead. This function will be removed in a later version. func TraceUnaryServerInterceptor() grpc.UnaryServerInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpUnaryServerInterceptor() } @@ -23,7 +23,7 @@ func TraceUnaryServerInterceptor() grpc.UnaryServerInterceptor { // // Deprecated: Use StreamServerInterceptor instead. This function will be removed in a later version. func TraceStreamServerInterceptor() grpc.StreamServerInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpStreamServerInterceptor() } @@ -32,7 +32,7 @@ func TraceStreamServerInterceptor() grpc.StreamServerInterceptor { // UnaryServerInterceptor returns a middleware that creates datadog-spans on incoming requests, and stores them in the requests' context. func UnaryServerInterceptor(options ...Option) grpc.UnaryServerInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpUnaryServerInterceptor() } opts := convertOptions(options...) @@ -41,7 +41,7 @@ func UnaryServerInterceptor(options ...Option) grpc.UnaryServerInterceptor { // StreamServerInterceptor returns a middleware that creates datadog-spans on incoming requests, and stores them in the requests' context. func StreamServerInterceptor(options ...Option) grpc.StreamServerInterceptor { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return noOpStreamServerInterceptor() } opts := convertOptions(options...) diff --git a/middleware/http/client.go b/middleware/http/client.go index 50f359b6..a76b29f0 100644 --- a/middleware/http/client.go +++ b/middleware/http/client.go @@ -17,7 +17,7 @@ func WrapClient(client *http.Client) *http.Client { // AddTracingToClient wraps the net/http.Client to automatically create child-spans, and append to HTTP Headers. func AddTracingToClient(client *http.Client, options ...Option) *http.Client { - if !internal.IsDatadogEnabled() { + if internal.IsDatadogDisabled() { return client } opts := convertClientOptions(options...)