Skip to content

Commit

Permalink
Add livedebugging support for discovery components (#2309)
Browse files Browse the repository at this point in the history
* Add livedebugging support for discovery components

* Add live debugging for discovery.process

* Simplifying docs

* Update docs/sources/troubleshoot/debug.md

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>

* typo fix

* Fix test failures

---------

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
  • Loading branch information
ravishankar15 and clayton-cornell authored Jan 7, 2025
1 parent 11d2d05 commit 0afec17
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Main (unreleased)
- Live Debugging button should appear in UI only for supported components (@ravishankar15)
- Add three new stdlib functions to_base64, from_URLbase64 and to_URLbase64 (@ravishankar15)
- Add `ignore_older_than` option for local.file_match (@ravishankar15)
- Add livedebugging support for discovery components (@ravishankar15)
- Add livedebugging support for `discover.relabel` (@ravishankar15)
- Performance optimization for live debugging feature (@ravishankar15)

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/troubleshoot/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Supported components:
* `otelcol.processor.*`
* `otelcol.receiver.*`
* `prometheus.relabel`
* `discovery.relabel`
* `discovery.*`
{{< /admonition >}}

## Debug using the UI
Expand Down
22 changes: 20 additions & 2 deletions internal/component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discovery

import (
"context"
"fmt"
"slices"
"sort"
"strings"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/runtime/logging/level"
"github.com/grafana/alloy/internal/service/livedebugging"
)

// Target refers to a singular discovered endpoint found by a discovery
Expand Down Expand Up @@ -74,16 +76,26 @@ type Component struct {
latestDisc DiscovererWithMetrics
newDiscoverer chan struct{}

creator Creator
creator Creator
debugDataPublisher livedebugging.DebugDataPublisher
}

var _ component.Component = (*Component)(nil)
var _ component.LiveDebugging = (*Component)(nil)

// New creates a discovery component given arguments and a concrete Discovery implementation function.
func New(o component.Options, args component.Arguments, creator Creator) (*Component, error) {
debugDataPublisher, err := o.GetServiceData(livedebugging.ServiceName)
if err != nil {
return nil, err
}

c := &Component{
opts: o,
creator: creator,
// buffered to avoid deadlock from the first immediate update
newDiscoverer: make(chan struct{}, 1),
newDiscoverer: make(chan struct{}, 1),
debugDataPublisher: debugDataPublisher.(livedebugging.DebugDataPublisher),
}
return c, c.Update(args)
}
Expand Down Expand Up @@ -224,6 +236,10 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) {
allTargets = append(allTargets, labels)
}
}
componentID := livedebugging.ComponentID(c.opts.ID)
if c.debugDataPublisher.IsActive(componentID) {
c.debugDataPublisher.Publish(componentID, fmt.Sprintf("%s", allTargets))
}
c.opts.OnStateChange(Exports{Targets: allTargets})
}

Expand Down Expand Up @@ -257,3 +273,5 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) {
}
}
}

func (c *Component) LiveDebugging(_ int) {}
33 changes: 23 additions & 10 deletions internal/component/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discovery

import (
"context"
"fmt"
"os"
"sync"
"testing"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/service/livedebugging"
)

// discovererUpdateTestCase is a test case for testing discovery updates. A discovery component is created and the
Expand Down Expand Up @@ -128,17 +130,28 @@ func TestDiscoveryUpdates(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var publishedExports []component.Exports
publishedExportsMut := sync.Mutex{}
comp := &Component{
opts: component.Options{
ID: "discovery.test",
OnStateChange: func(e component.Exports) {
publishedExportsMut.Lock()
defer publishedExportsMut.Unlock()
publishedExports = append(publishedExports, e)
},
Logger: log.NewLogfmtLogger(os.Stdout),
opts := component.Options{
ID: "discovery.test",
OnStateChange: func(e component.Exports) {
publishedExportsMut.Lock()
defer publishedExportsMut.Unlock()
publishedExports = append(publishedExports, e)
},
Logger: log.NewLogfmtLogger(os.Stdout),
GetServiceData: func(name string) (interface{}, error) {
switch name {
case livedebugging.ServiceName:
return livedebugging.NewLiveDebugging(), nil
default:
return nil, fmt.Errorf("service %q does not exist", name)
}
},
newDiscoverer: make(chan struct{}, 1),
}
debugDataPublisher, _ := opts.GetServiceData(livedebugging.ServiceName)
comp := &Component{
opts: opts,
newDiscoverer: make(chan struct{}, 1),
debugDataPublisher: debugDataPublisher.(livedebugging.DebugDataPublisher),
}

discoverer := newFakeDiscoverer()
Expand Down
10 changes: 10 additions & 0 deletions internal/component/discovery/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/service/livedebugging"
"github.com/grafana/alloy/syntax"
)

Expand Down Expand Up @@ -95,6 +97,14 @@ func TestComponent(t *testing.T) {
cancel()
},
Registerer: prometheus.NewRegistry(),
GetServiceData: func(name string) (interface{}, error) {
switch name {
case livedebugging.ServiceName:
return livedebugging.NewLiveDebugging(), nil
default:
return nil, fmt.Errorf("service %q does not exist", name)
}
},
},
Arguments{
RefreshInterval: time.Second,
Expand Down
30 changes: 26 additions & 4 deletions internal/component/discovery/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ package process

import (
"context"
"fmt"
"time"

"github.com/go-kit/log"
"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/service/livedebugging"
)

func init() {
Expand All @@ -26,24 +28,37 @@ func init() {
}

func New(opts component.Options, args Arguments) (*Component, error) {
debugDataPublisher, err := opts.GetServiceData(livedebugging.ServiceName)
if err != nil {
return nil, err
}

c := &Component{
l: opts.Logger,
onStateChange: opts.OnStateChange,
argsUpdates: make(chan Arguments),
args: args,
opts: opts,
l: opts.Logger,
onStateChange: opts.OnStateChange,
argsUpdates: make(chan Arguments),
args: args,
debugDataPublisher: debugDataPublisher.(livedebugging.DebugDataPublisher),
}

return c, nil
}

type Component struct {
opts component.Options
l log.Logger
onStateChange func(e component.Exports)
processes []discovery.Target
argsUpdates chan Arguments
args Arguments

debugDataPublisher livedebugging.DebugDataPublisher
}

var _ component.Component = (*Component)(nil)
var _ component.LiveDebugging = (*Component)(nil)

func (c *Component) Run(ctx context.Context) error {
doDiscover := func() error {
processes, err := discover(c.l, &c.args.DiscoverConfig)
Expand All @@ -53,6 +68,11 @@ func (c *Component) Run(ctx context.Context) error {
c.processes = convertProcesses(processes)
c.changed()

componentID := livedebugging.ComponentID(c.opts.ID)
if c.debugDataPublisher.IsActive(componentID) {
c.debugDataPublisher.Publish(componentID, fmt.Sprintf("%s", c.processes))
}

return nil
}
if err := doDiscover(); err != nil {
Expand Down Expand Up @@ -88,3 +108,5 @@ func (c *Component) changed() {
Targets: join(c.processes, c.args.Join),
})
}

func (c *Component) LiveDebugging(_ int) {}

0 comments on commit 0afec17

Please sign in to comment.