Skip to content

Commit

Permalink
using enable debug in agent as atomic bool
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Jun 17, 2023
1 parent 7eddfc4 commit 402a65d
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 64 deletions.
10 changes: 7 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ type Agent struct {

// enterpriseAgent embeds fields that we only access in consul-enterprise builds
enterpriseAgent

enableDebug atomic.Bool
}

// New process the desired options and creates a new Agent.
Expand Down Expand Up @@ -598,6 +600,9 @@ func (a *Agent) Start(ctx context.Context) error {
// Overwrite the configuration.
a.config = c

a.enableDebug = atomic.Bool{}
a.enableDebug.Store(c.EnableDebug)

if err := a.tlsConfigurator.Update(a.config.TLS); err != nil {
return fmt.Errorf("Failed to load TLS configurations after applying auto-config settings: %w", err)
}
Expand Down Expand Up @@ -4157,6 +4162,8 @@ func (a *Agent) reloadConfig(autoReload bool) error {
}
}

a.enableDebug.Store(newCfg.EnableDebug)

return a.reloadConfigInternal(newCfg)
}

Expand Down Expand Up @@ -4291,9 +4298,6 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {

a.proxyConfig.SetUpdateRateLimit(newCfg.XDSUpdateRateLimit)

a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(newCfg.EnableDebug.Load())

return nil
}

Expand Down
4 changes: 1 addition & 3 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -6009,8 +6008,7 @@ func TestAgent_Monitor(t *testing.T) {
cancelCtx, cancelFunc := context.WithCancel(context.Background())
req = req.WithContext(cancelCtx)

a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
resp := httptest.NewRecorder()
handler := a.srv.handler()
go handler.ServeHTTP(resp, req)
Expand Down
18 changes: 1 addition & 17 deletions agent/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sort"
"strconv"
"strings"
"sync/atomic"
"time"

"github.com/armon/go-metrics/prometheus"
Expand Down Expand Up @@ -1011,7 +1010,7 @@ func (b *builder) build() (rt RuntimeConfig, err error) {
DiscoveryMaxStale: b.durationVal("discovery_max_stale", c.DiscoveryMaxStale),
EnableAgentTLSForChecks: boolVal(c.EnableAgentTLSForChecks),
EnableCentralServiceConfig: boolVal(c.EnableCentralServiceConfig),
EnableDebug: *atomicBoolVal(c.EnableDebug),
EnableDebug: c.EnableDebug,

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / dev-build / build

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-enums

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-enums

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / dev-build / build

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-386

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-amd64

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-arm

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-enums

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-enums

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / dev-build / build

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / dev-build / build

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint-32bit / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-386

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-amd64

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / lint / lint

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal) (typecheck)

Check failure on line 1013 in agent/config/builder.go

View workflow job for this annotation

GitHub Actions / build-arm

cannot use c.EnableDebug (variable of type *bool) as bool value in struct literal
EnableRemoteScriptChecks: enableRemoteScriptChecks,
EnableLocalScriptChecks: enableLocalScriptChecks,
EncryptKey: stringVal(c.EncryptKey),
Expand Down Expand Up @@ -1939,21 +1938,6 @@ func boolValWithDefault(v *bool, defaultVal bool) bool {
return *v
}

func atomicBool(v bool) *atomic.Bool {
atomicBool := atomic.Bool{}
atomicBool.Store(v)
return &atomicBool
}

func atomicBoolVal(v *bool) *atomic.Bool {
if v == nil {
return &atomic.Bool{}
}
atomicBool := atomic.Bool{}
atomicBool.Store(*v)
return &atomicBool
}

func boolVal(v *bool) bool {
if v == nil {
return false
Expand Down
3 changes: 1 addition & 2 deletions agent/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"reflect"
"strings"
"sync/atomic"
"time"

"github.com/hashicorp/go-uuid"
Expand Down Expand Up @@ -652,7 +651,7 @@ type RuntimeConfig struct {
// EnableDebug is used to enable various debugging features.
//
// hcl: enable_debug = (true|false)
EnableDebug atomic.Bool
EnableDebug bool

// EnableLocalScriptChecks controls whether health checks declared from the local
// config file which execute scripts are enabled. This includes regular script
Expand Down
11 changes: 3 additions & 8 deletions agent/config/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"reflect"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -325,8 +324,7 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
rt.DevMode = true
rt.DisableAnonymousSignature = true
rt.DisableKeyringFile = true
rt.EnableDebug = atomic.Bool{}
rt.EnableDebug.Store(true)
rt.EnableDebug = true
rt.Experiments = []string{"resource-apis"}
rt.UIConfig.Enabled = true
rt.LeaveOnTerm = false
Expand Down Expand Up @@ -5966,8 +5964,7 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
// The logstore settings from first file should not be overridden by a
// later file with nothing to say about logstores!
rt.RaftLogStoreConfig.Backend = consul.LogStoreBackendWAL
rt.EnableDebug = atomic.Bool{}
rt.EnableDebug.Store(true)
rt.EnableDebug = true
},
})
}
Expand Down Expand Up @@ -6070,8 +6067,6 @@ func TestLoad_FullConfig(t *testing.T) {
_, n, _ := net.ParseCIDR(s)
return n
}
atomicBoolTrue := atomic.Bool{}
atomicBoolTrue.Store(true)

defaultEntMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
nodeEntMeta := structs.NodeEnterpriseMetaInDefaultPartition()
Expand Down Expand Up @@ -6357,7 +6352,7 @@ func TestLoad_FullConfig(t *testing.T) {
DiscoveryMaxStale: 5 * time.Second,
EnableAgentTLSForChecks: true,
EnableCentralServiceConfig: false,
EnableDebug: *atomicBool(true),
EnableDebug: true,
EnableRemoteScriptChecks: true,
EnableLocalScriptChecks: true,
EncryptKey: "A4wELWqH",
Expand Down
2 changes: 1 addition & 1 deletion agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (s *HTTPHandlers) handler() http.Handler {
wrapper := func(resp http.ResponseWriter, req *http.Request) {

// If enableDebug or ACL enabled, register wrapped pprof handlers
if !s.agent.config.EnableDebug.Load() && s.checkACLDisabled() {
if !s.agent.enableDebug.Load() && s.checkACLDisabled() {
resp.WriteHeader(http.StatusNotFound)
return
}
Expand Down
8 changes: 3 additions & 5 deletions agent/http_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -145,8 +144,7 @@ func TestHTTPAPI_OptionMethod_OSS(t *testing.T) {
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
req, _ := http.NewRequest("OPTIONS", uri, nil)
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
allMethods := append([]string{"OPTIONS"}, methods...)

Expand Down Expand Up @@ -193,8 +191,8 @@ func TestHTTPAPI_AllowedNets_OSS(t *testing.T) {
req, _ := http.NewRequest(method, uri, nil)
req.RemoteAddr = "192.168.1.2:5555"
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug = true

a.srv.handler().ServeHTTP(resp, req)

require.Equal(t, http.StatusForbidden, resp.Code, "%s %s", method, path)
Expand Down
31 changes: 10 additions & 21 deletions agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -289,8 +288,7 @@ func TestSetupHTTPServer_HTTP2(t *testing.T) {
err = setupHTTPS(httpServer, noopConnState, time.Second)
require.NoError(t, err)

a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
srvHandler := a.srv.handler()
mux, ok := srvHandler.(*wrappedMux)
require.True(t, ok, "expected a *wrappedMux, got %T", handler)
Expand Down Expand Up @@ -486,8 +484,7 @@ func TestHTTPAPI_Ban_Nonprintable_Characters(t *testing.T) {
t.Fatal(err)
}
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
if got, want := resp.Code, http.StatusBadRequest; got != want {
t.Fatalf("bad response code got %d want %d", got, want)
Expand All @@ -511,8 +508,7 @@ func TestHTTPAPI_Allow_Nonprintable_Characters_With_Flag(t *testing.T) {
t.Fatal(err)
}
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
// Key doesn't actually exist so we should get 404
if got, want := resp.Code, http.StatusNotFound; got != want {
Expand Down Expand Up @@ -652,8 +648,7 @@ func requireHasHeadersSet(t *testing.T, a *TestAgent, path string) {

resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", path, nil)
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)

hdrs := resp.Header()
Expand Down Expand Up @@ -715,17 +710,15 @@ func TestAcceptEncodingGzip(t *testing.T) {
// negotiation, but since this call doesn't go through a real
// transport, the header has to be set manually
req.Header["Accept-Encoding"] = []string{"gzip"}
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
require.Equal(t, "", resp.Header().Get("Content-Encoding"))

resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/v1/kv/long", nil)
req.Header["Accept-Encoding"] = []string{"gzip"}
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
require.Equal(t, "gzip", resp.Header().Get("Content-Encoding"))
Expand Down Expand Up @@ -1081,8 +1074,7 @@ func TestHTTPServer_PProfHandlers_EnableDebug(t *testing.T) {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/debug/pprof/profile?seconds=1", nil)

a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
httpServer := &HTTPHandlers{agent: a.Agent}
httpServer.handler().ServeHTTP(resp, req)

Expand Down Expand Up @@ -1183,8 +1175,7 @@ func TestHTTPServer_PProfHandlers_ACLs(t *testing.T) {
t.Run(fmt.Sprintf("case %d (%#v)", i, c), func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s?token=%s", c.endpoint, c.token), nil)
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
assert.Equal(t, c.code, resp.Code)
})
Expand Down Expand Up @@ -1495,8 +1486,7 @@ func TestEnableWebUI(t *testing.T) {

req, _ := http.NewRequest("GET", "/ui/", nil)
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)

Expand Down Expand Up @@ -1526,8 +1516,7 @@ func TestEnableWebUI(t *testing.T) {
{
req, _ := http.NewRequest("GET", "/ui/", nil)
resp := httptest.NewRecorder()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
require.Contains(t, resp.Body.String(), `<!-- CONSUL_VERSION:`)
Expand Down
3 changes: 1 addition & 2 deletions agent/ui_endpoint_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func TestUIEndpoint_MetricsProxy_ACLDeny(t *testing.T) {
`, backendURL))
defer a.Shutdown()

a.config.EnableDebug = atomic.Bool{}
a.Config.EnableDebug.Store(true)
a.config.EnableDebug = true
h := a.srv.handler()

testrpc.WaitForLeader(t, a.RPC, "dc1")
Expand Down
3 changes: 1 addition & 2 deletions agent/ui_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2621,8 +2621,7 @@ func TestUIEndpoint_MetricsProxy(t *testing.T) {

// Now fetch the API handler to run requests against
h := a.srv.handler()
a.config.EnableDebug = atomic.Bool{}
a.config.EnableDebug.Store(true)
a.config.EnableDebug = true

req := httptest.NewRequest("GET", tc.path, nil)
rec := httptest.NewRecorder()
Expand Down

0 comments on commit 402a65d

Please sign in to comment.