Skip to content

Commit

Permalink
Refactor to disable instead of enable
Browse files Browse the repository at this point in the history
  • Loading branch information
nhhagen committed Apr 2, 2024
1 parent 81d27c9 commit ba73ff6
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions internal/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
32 changes: 16 additions & 16 deletions internal/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion middleware/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion middleware/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions middleware/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
}

Expand Down
8 changes: 4 additions & 4 deletions middleware/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
}

Expand All @@ -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...)
Expand All @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion middleware/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit ba73ff6

Please sign in to comment.