Skip to content

Commit

Permalink
disable livedebugging by default and add a config block to enable it.…
Browse files Browse the repository at this point in the history
… The stability is also updated to experimental
  • Loading branch information
wildum committed Jun 10, 2024
1 parent 17aaf44 commit a809977
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
40 changes: 40 additions & 0 deletions docs/sources/reference/config-blocks/livedebugging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
canonical: https://grafana.com/docs/alloy/latest/reference/config-blocks/livedebugging/
description: Learn about the livedebugging configuration block
menuTitle: livedebugging
title: livedebugging block
---

<span class="badge docs-labels__stage docs-labels__item">Experimental</span>

# livedebugging block

{{< docs/shared lookup="stability/experimental.md" source="alloy" version="<ALLOY_VERSION>" >}}

`livedebugging` is an optional configuration block that enables the [live debugging feature][debug] which streams real-time data from your components directly to {{< param "PRODUCT_NAME" >}}'s UI.

By default, [live debugging][debug] is disabled and must be explicitly enabled through this configuration block to make the debugging data visible in the UI.

{{< admonition type="note" >}}
Enabling this feature provides detailed insights into the data flowing through your pipelines via {{< param "PRODUCT_NAME" >}}'s API.
To ensure that your data remains secure while live debugging is enabled, consider configuring TLS in the [http block][].
{{< /admonition >}}

## Example

```alloy
livedebugging {
enabled = true
}
```

## Arguments

The following arguments are supported:

| Name | Type | Description | Default | Required |
| --------- | ------ | ----------------------------------- | ------- | -------- |
| `enabled` | `bool` | Enables the live debugging feature. | `false` | no |

[debug]: ../../../tasks/debug/
[http block]: ../http/
17 changes: 15 additions & 2 deletions internal/service/livedebugging/livedebugging.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type liveDebugging struct {
loadMut sync.RWMutex
callbacks map[ComponentID]map[CallbackID]func(string)
host service.Host
enabled bool
}

var _ CallbackManager = &liveDebugging{}
Expand All @@ -47,8 +48,10 @@ func NewLiveDebugging() *liveDebugging {
func (s *liveDebugging) Publish(componentID ComponentID, data string) {
s.loadMut.RLock()
defer s.loadMut.RUnlock()
for _, callback := range s.callbacks[componentID] {
callback(data)
if s.enabled {
for _, callback := range s.callbacks[componentID] {
callback(data)
}
}
}

Expand All @@ -63,6 +66,10 @@ func (s *liveDebugging) AddCallback(callbackID CallbackID, componentID Component
s.loadMut.Lock()
defer s.loadMut.Unlock()

if !s.enabled {
return fmt.Errorf("the live debugging service is disabled. Check the documentation to find out how to enable it")
}

if s.host == nil {
return fmt.Errorf("the live debugging service is not ready yet")
}
Expand Down Expand Up @@ -94,3 +101,9 @@ func (s *liveDebugging) SetServiceHost(h service.Host) {
defer s.loadMut.Unlock()
s.host = h
}

func (s *liveDebugging) SetEnabled(enabled bool) {
s.loadMut.Lock()
defer s.loadMut.Unlock()
s.enabled = enabled
}
11 changes: 11 additions & 0 deletions internal/service/livedebugging/livedebugging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ func TestAddCallback(t *testing.T) {
callback := func(data string) {}

err := livedebugging.AddCallback(callbackID, "fake.liveDebugging", callback)
require.ErrorContains(t, err, "the live debugging service is disabled. Check the documentation to find out how to enable it")

livedebugging.SetEnabled(true)

err = livedebugging.AddCallback(callbackID, "fake.liveDebugging", callback)
require.ErrorContains(t, err, "the live debugging service is not ready yet")

setupServiceHost(livedebugging)

err = livedebugging.AddCallback(callbackID, "not found", callback)
require.ErrorContains(t, err, "component not found")

Expand Down Expand Up @@ -49,6 +55,10 @@ func TestStream(t *testing.T) {

livedebugging.Publish(componentID, "test data")
require.Equal(t, "test data", receivedData)

livedebugging.SetEnabled(false)
livedebugging.Publish(componentID, "new test data")
require.Equal(t, "test data", receivedData) // not updated because the feature is disabled
}

func TestStreamEmpty(t *testing.T) {
Expand Down Expand Up @@ -122,6 +132,7 @@ func setupServiceHost(liveDebugging *liveDebugging) {
},
}
liveDebugging.SetServiceHost(host)
liveDebugging.SetEnabled(true)
}

type fakeInfo struct {
Expand Down
15 changes: 10 additions & 5 deletions internal/service/livedebugging/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package livedebugging

import (
"context"
"fmt"

"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/service"
Expand All @@ -24,6 +23,10 @@ func New() *Service {
}
}

type Arguments struct {
Enabled bool `alloy:"enabled,attr,optional"`
}

// Data implements service.Service.
// It returns the liveDebugging for the components to stream.
func (s *Service) Data() any {
Expand All @@ -34,9 +37,9 @@ func (s *Service) Data() any {
func (*Service) Definition() service.Definition {
return service.Definition{
Name: ServiceName,
ConfigType: nil, // livedebugging does not accept configuration
ConfigType: Arguments{},
DependsOn: []string{},
Stability: featuregate.StabilityGenerallyAvailable,
Stability: featuregate.StabilityExperimental,
}
}

Expand All @@ -48,6 +51,8 @@ func (s *Service) Run(ctx context.Context, host service.Host) error {
}

// Update implements service.Service.
func (*Service) Update(_ any) error {
return fmt.Errorf("livedebugging service does not support configuration")
func (s *Service) Update(args any) error {
newArgs := args.(Arguments)
s.liveDebugging.SetEnabled(newArgs.Enabled)
return nil
}

0 comments on commit a809977

Please sign in to comment.