From 2fc325930fc6cf6e1f7e412a12d6f07c69c62c4c Mon Sep 17 00:00:00 2001 From: Ravishankar Date: Sat, 21 Dec 2024 22:13:36 +0530 Subject: [PATCH] Add livedebugging support for discovery components --- CHANGELOG.md | 1 + docs/sources/troubleshoot/debug.md | 30 +++++++++++++++++++++++ internal/component/discovery/discovery.go | 22 +++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 685f799212..5536f02714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,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) - Upgrade `github.com/goccy/go-json` to v0.10.4, which reduces the memory consumption of an Alloy instance by 20MB. diff --git a/docs/sources/troubleshoot/debug.md b/docs/sources/troubleshoot/debug.md index 5b8518515d..7c7697f348 100644 --- a/docs/sources/troubleshoot/debug.md +++ b/docs/sources/troubleshoot/debug.md @@ -112,6 +112,36 @@ Supported components: * `prometheus.relabel` {{< /admonition >}} * `discovery.relabel` +* `discovery.azure` +* `discovery.consul` +* `discovery.consulagent` +* `discovery.digitalocean` +* `discovery.dns` +* `discovery.docker` +* `discovery.dockerswarm` +* `discovery.ec2` +* `discovery.eureka` +* `discovery.file` +* `discovery.gce` +* `discovery.hetzner` +* `discovery.http` +* `discovery.ionos` +* `discovery.kubelet` +* `discovery.kubernetes` +* `discovery.kuma` +* `discovery.lightsail` +* `discovery.linode` +* `discovery.marathon` +* `discovery.nerve` +* `discovery.nomad` +* `discovery.openstack` +* `discovery.ovhcloud` +* `discovery.process` +* `discovery.puppetdb` +* `discovery.scaleway` +* `discovery.serverset` +* `discovery.triton` +* `discovery.uyuni` ## Debug using the UI diff --git a/internal/component/discovery/discovery.go b/internal/component/discovery/discovery.go index 726b2a0552..f4904e56af 100644 --- a/internal/component/discovery/discovery.go +++ b/internal/component/discovery/discovery.go @@ -2,6 +2,7 @@ package discovery import ( "context" + "fmt" "slices" "sort" "strings" @@ -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 @@ -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) } @@ -224,6 +236,10 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) { allTargets = append(allTargets, labels) } } + componenentID := livedebugging.ComponentID(c.opts.ID) + if c.debugDataPublisher.IsActive(componenentID) { + c.debugDataPublisher.Publish(componenentID, fmt.Sprintf("%s", allTargets)) + } c.opts.OnStateChange(Exports{Targets: allTargets}) } @@ -257,3 +273,5 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) { } } } + +func (c *Component) LiveDebugging(_ int) {}