Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live debugging #999

Merged
merged 18 commits into from
Jun 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
disable livedebugging by default and add a config block to enable it.…
… The stability is also updated to experimental
  • Loading branch information
wildum committed Jun 10, 2024
commit a80997788a15aa8d3043eff5735d1b55723913ea
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
@@ -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{}
@@ -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)
}
}
}

@@ -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")
}
@@ -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
@@ -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")

@@ -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) {
@@ -122,6 +132,7 @@ func setupServiceHost(liveDebugging *liveDebugging) {
},
}
liveDebugging.SetServiceHost(host)
liveDebugging.SetEnabled(true)
}

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

import (
"context"
"fmt"

"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/service"
@@ -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 {
@@ -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,
}
}

@@ -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
}
Loading