diff --git a/docs/sources/reference/cli/run.md b/docs/sources/reference/cli/run.md index d572700bfb..4cbe811690 100644 --- a/docs/sources/reference/cli/run.md +++ b/docs/sources/reference/cli/run.md @@ -38,7 +38,6 @@ The following flags are supported: * `--server.http.memory-addr`: Address to listen for [in-memory HTTP traffic][] on (default `alloy.internal:12345`). * `--server.http.listen-addr`: Address to listen for HTTP traffic on (default `127.0.0.1:12345`). * `--server.http.ui-path-prefix`: Base path where the UI is exposed (default `/`). -* `--server.http.live-debugging-buffer-stream-size`: Buffer stream size used for buffering the live debugging entries (default `1000`) * `--storage.path`: Base directory where components can store data (default `data-alloy/`). * `--disable-reporting`: Disable [data collection][] (default `false`). * `--disable-support-bundle`: Disable [support bundle][] endpoint (default `false`). diff --git a/docs/sources/reference/config-blocks/livedebugging.md b/docs/sources/reference/config-blocks/livedebugging.md index 4587d95117..33563a5119 100644 --- a/docs/sources/reference/config-blocks/livedebugging.md +++ b/docs/sources/reference/config-blocks/livedebugging.md @@ -30,8 +30,9 @@ livedebugging { The following arguments are supported: -| Name | Type | Description | Default | Required | -| --------- | ------ | ----------------------------------- | ------- | -------- | -| `enabled` | `bool` | Enables the live debugging feature. | `false` | no | +| Name | Type | Description | Default | Required | +| -------------------- | ----- | --------------------------------------------------------------- | ------- | -------- | +| `enabled` | `bool`| Enables the live debugging feature. | `false` | no | +| `buffer_stream_size` | `int` | Buffer stream size used for buffering the live debugging entries | 1000 | no | [debug]: ../../../troubleshoot/debug/ diff --git a/internal/alloycli/cmd_run.go b/internal/alloycli/cmd_run.go index e89abfe577..af8575dda3 100644 --- a/internal/alloycli/cmd_run.go +++ b/internal/alloycli/cmd_run.go @@ -52,19 +52,18 @@ import ( func runCommand() *cobra.Command { r := &alloyRun{ - inMemoryAddr: "alloy.internal:12345", - httpListenAddr: "127.0.0.1:12345", - storagePath: "data-alloy/", - minStability: featuregate.StabilityGenerallyAvailable, - uiPrefix: "/", - disableReporting: false, - enablePprof: true, - configFormat: "alloy", - clusterAdvInterfaces: advertise.DefaultInterfaces, - clusterMaxJoinPeers: 5, - clusterRejoinInterval: 60 * time.Second, - disableSupportBundle: false, - liveDebuggingBufferStreamSize: 1000, + inMemoryAddr: "alloy.internal:12345", + httpListenAddr: "127.0.0.1:12345", + storagePath: "data-alloy/", + minStability: featuregate.StabilityGenerallyAvailable, + uiPrefix: "/", + disableReporting: false, + enablePprof: true, + configFormat: "alloy", + clusterAdvInterfaces: advertise.DefaultInterfaces, + clusterMaxJoinPeers: 5, + clusterRejoinInterval: 60 * time.Second, + disableSupportBundle: false, } cmd := &cobra.Command{ @@ -114,8 +113,6 @@ depending on the nature of the reload error. BoolVar(&r.enablePprof, "server.http.enable-pprof", r.enablePprof, "Enable /debug/pprof profiling endpoints.") cmd.Flags(). BoolVar(&r.disableSupportBundle, "server.http.disable-support-bundle", r.disableSupportBundle, "Disable /-/support support bundle retrieval.") - cmd.Flags(). - IntVar(&r.liveDebuggingBufferStreamSize, "server.http.live-debugging-buffer-stream-size", r.liveDebuggingBufferStreamSize, "Buffer stream size used for buffering the live debugging entries") // Cluster flags cmd.Flags(). @@ -164,33 +161,32 @@ depending on the nature of the reload error. } type alloyRun struct { - inMemoryAddr string - httpListenAddr string - storagePath string - minStability featuregate.Stability - uiPrefix string - enablePprof bool - disableReporting bool - clusterEnabled bool - clusterNodeName string - clusterAdvAddr string - clusterJoinAddr string - clusterDiscoverPeers string - clusterAdvInterfaces []string - clusterRejoinInterval time.Duration - clusterMaxJoinPeers int - clusterName string - clusterEnableTLS bool - clusterTLSCAPath string - clusterTLSCertPath string - clusterTLSKeyPath string - clusterTLSServerName string - configFormat string - configBypassConversionErrors bool - configExtraArgs string - enableCommunityComps bool - disableSupportBundle bool - liveDebuggingBufferStreamSize int + inMemoryAddr string + httpListenAddr string + storagePath string + minStability featuregate.Stability + uiPrefix string + enablePprof bool + disableReporting bool + clusterEnabled bool + clusterNodeName string + clusterAdvAddr string + clusterJoinAddr string + clusterDiscoverPeers string + clusterAdvInterfaces []string + clusterRejoinInterval time.Duration + clusterMaxJoinPeers int + clusterName string + clusterEnableTLS bool + clusterTLSCAPath string + clusterTLSCertPath string + clusterTLSKeyPath string + clusterTLSServerName string + configFormat string + configBypassConversionErrors bool + configExtraArgs string + enableCommunityComps bool + disableSupportBundle bool } func (fr *alloyRun) Run(cmd *cobra.Command, configPath string) error { @@ -320,9 +316,9 @@ func (fr *alloyRun) Run(cmd *cobra.Command, configPath string) error { liveDebuggingService := livedebugging.New() uiService := uiservice.New(uiservice.Options{ - UIPrefix: fr.uiPrefix, - CallbackManager: liveDebuggingService.Data().(livedebugging.CallbackManager), - LiveDebuggingBufferStreamSize: fr.liveDebuggingBufferStreamSize, + UIPrefix: fr.uiPrefix, + CallbackManager: liveDebuggingService.Data().(livedebugging.CallbackManager), + LiveDebuggingConfig: liveDebuggingService.Data().(livedebugging.ConfigViewer), }) otelService := otel_service.New(l) diff --git a/internal/service/livedebugging/livedebugging.go b/internal/service/livedebugging/livedebugging.go index 39bcec0805..1a38757f1a 100644 --- a/internal/service/livedebugging/livedebugging.go +++ b/internal/service/livedebugging/livedebugging.go @@ -28,15 +28,23 @@ type DebugDataPublisher interface { IsActive(componentID ComponentID) bool } +// ConfigViewer is used by components to access the configs of the live debugging +type ConfigViewer interface { + // returns the buffer stream size used by the UI service + GetBufferStreamSize() int +} + type liveDebugging struct { - loadMut sync.RWMutex - callbacks map[ComponentID]map[CallbackID]func(string) - host service.Host - enabled bool + loadMut sync.RWMutex + callbacks map[ComponentID]map[CallbackID]func(string) + host service.Host + enabled bool + bufferStreamSize int } var _ CallbackManager = &liveDebugging{} var _ DebugDataPublisher = &liveDebugging{} +var _ ConfigViewer = &liveDebugging{} // NewLiveDebugging creates a new instance of liveDebugging. func NewLiveDebugging() *liveDebugging { @@ -131,3 +139,15 @@ func (s *liveDebugging) SetEnabled(enabled bool) { defer s.loadMut.Unlock() s.enabled = enabled } + +func (s *liveDebugging) SetBufferStreamSize(size int) { + s.loadMut.Lock() + defer s.loadMut.Unlock() + s.bufferStreamSize = size +} + +func (s *liveDebugging) GetBufferStreamSize() int { + s.loadMut.RLock() + defer s.loadMut.RUnlock() + return s.bufferStreamSize +} diff --git a/internal/service/livedebugging/service.go b/internal/service/livedebugging/service.go index 75e0c08cd7..78f8a3e6f5 100644 --- a/internal/service/livedebugging/service.go +++ b/internal/service/livedebugging/service.go @@ -24,7 +24,16 @@ func New() *Service { } type Arguments struct { - Enabled bool `alloy:"enabled,attr,optional"` + Enabled bool `alloy:"enabled,attr,optional"` + BufferStreamSize int `alloy:"buffer_stream_size,attr,optional"` +} + +// SetToDefault implements syntax.Defaulter. +func (args *Arguments) SetToDefault() { + *args = Arguments{ + Enabled: false, + BufferStreamSize: 1000, + } } // Data implements service.Service. @@ -54,5 +63,6 @@ func (s *Service) Run(ctx context.Context, host service.Host) error { func (s *Service) Update(args any) error { newArgs := args.(Arguments) s.liveDebugging.SetEnabled(newArgs.Enabled) + s.liveDebugging.SetBufferStreamSize(newArgs.BufferStreamSize) return nil } diff --git a/internal/service/ui/ui.go b/internal/service/ui/ui.go index 2683c1d878..38364f56ef 100644 --- a/internal/service/ui/ui.go +++ b/internal/service/ui/ui.go @@ -23,9 +23,9 @@ const ServiceName = "ui" // Options are used to configure the UI service. Options are constant for the // lifetime of the UI service. type Options struct { - UIPrefix string // Path prefix to host the UI at. - CallbackManager livedebugging.CallbackManager // CallbackManager is used for live debugging in the UI. - LiveDebuggingBufferStreamSize int // Buffer size for the live debugging stream channel + UIPrefix string // Path prefix to host the UI at. + CallbackManager livedebugging.CallbackManager // CallbackManager is used for live debugging in the UI. + LiveDebuggingConfig livedebugging.ConfigViewer // ConfigViewer is used to access the configs of liveDebugging } // Service implements the UI service. @@ -79,7 +79,7 @@ func (s *Service) Data() any { func (s *Service) ServiceHandler(host service.Host) (base string, handler http.Handler) { r := mux.NewRouter() - fa := api.NewAlloyAPI(host, s.opts.CallbackManager, s.opts.LiveDebuggingBufferStreamSize) + fa := api.NewAlloyAPI(host, s.opts.CallbackManager, s.opts.LiveDebuggingConfig) fa.RegisterRoutes(path.Join(s.opts.UIPrefix, "/api/v0/web"), r) ui.RegisterRoutes(s.opts.UIPrefix, r) diff --git a/internal/web/api/api.go b/internal/web/api/api.go index 8e365491e5..3283fc9518 100644 --- a/internal/web/api/api.go +++ b/internal/web/api/api.go @@ -25,14 +25,14 @@ import ( // AlloyAPI is a wrapper around the component API. type AlloyAPI struct { - alloy service.Host - CallbackManager livedebugging.CallbackManager - liveDebuggingBufferStreamSize int + alloy service.Host + CallbackManager livedebugging.CallbackManager + LiveDebuggingConfig livedebugging.ConfigViewer } // NewAlloyAPI instantiates a new Alloy API. -func NewAlloyAPI(alloy service.Host, CallbackManager livedebugging.CallbackManager, liveDebuggingBufferStreamSize int) *AlloyAPI { - return &AlloyAPI{alloy: alloy, CallbackManager: CallbackManager, liveDebuggingBufferStreamSize: liveDebuggingBufferStreamSize} +func NewAlloyAPI(alloy service.Host, CallbackManager livedebugging.CallbackManager, LiveDebuggingConfig livedebugging.ConfigViewer) *AlloyAPI { + return &AlloyAPI{alloy: alloy, CallbackManager: CallbackManager, LiveDebuggingConfig: LiveDebuggingConfig} } // RegisterRoutes registers all the API's routes. @@ -51,7 +51,7 @@ func (a *AlloyAPI) RegisterRoutes(urlPrefix string, r *mux.Router) { r.Handle(path.Join(urlPrefix, "/remotecfg/components/{id:.+}"), httputil.CompressionHandler{Handler: getComponentHandlerRemoteCfg(a.alloy)}) r.Handle(path.Join(urlPrefix, "/peers"), httputil.CompressionHandler{Handler: getClusteringPeersHandler(a.alloy)}) - r.Handle(path.Join(urlPrefix, "/debug/{id:.+}"), liveDebugging(a.alloy, a.CallbackManager, a.liveDebuggingBufferStreamSize)) + r.Handle(path.Join(urlPrefix, "/debug/{id:.+}"), liveDebugging(a.alloy, a.CallbackManager, a.LiveDebuggingConfig)) } func listComponentsHandler(host service.Host) http.HandlerFunc { @@ -167,12 +167,12 @@ func getClusteringPeersHandler(host service.Host) http.HandlerFunc { } } -func liveDebugging(_ service.Host, callbackManager livedebugging.CallbackManager, liveDebuggingBufferStreamSize int) http.HandlerFunc { +func liveDebugging(_ service.Host, callbackManager livedebugging.CallbackManager, LiveDebuggingConfig livedebugging.ConfigViewer) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) componentID := livedebugging.ComponentID(vars["id"]) - dataCh := make(chan string, liveDebuggingBufferStreamSize) + dataCh := make(chan string, LiveDebuggingConfig.GetBufferStreamSize()) ctx := r.Context() sampleProb := setSampleProb(w, r.URL.Query().Get("sampleProb"))