Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed Jan 22, 2025
1 parent a6a49a7 commit a575d60
Show file tree
Hide file tree
Showing 28 changed files with 414 additions and 207 deletions.
6 changes: 3 additions & 3 deletions internal/component/database_observability/mysql/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ func (c *Component) Run(ctx context.Context) error {
func (c *Component) getBaseTarget() (discovery.Target, error) {
data, err := c.opts.GetServiceData(http_service.ServiceName)
if err != nil {
return nil, fmt.Errorf("failed to get HTTP information: %w", err)
return discovery.NewEmptyTarget(), fmt.Errorf("failed to get HTTP information: %w", err)
}
httpData := data.(http_service.Data)

return discovery.Target{
return discovery.NewTargetFromMap(map[string]string{
model.AddressLabel: httpData.MemoryListenAddr,
model.SchemeLabel: "http",
model.MetricsPathLabel: path.Join(httpData.HTTPPathForComponent(c.opts.ID), "metrics"),
"instance": c.instanceKey(),
"job": database_observability.JobName,
}, nil
}), nil
}

func (c *Component) Update(args component.Arguments) error {
Expand Down
53 changes: 1 addition & 52 deletions internal/component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,17 @@ package discovery
import (
"context"
"fmt"
"slices"
"sort"
"strings"
"sync"
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/labels"

"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
// component.
type Target map[string]string

// Labels converts Target into a set of sorted labels.
func (t Target) Labels() labels.Labels {
var lset labels.Labels
for k, v := range t {
lset = append(lset, labels.Label{Name: k, Value: v})
}
sort.Sort(lset)
return lset
}

func (t Target) NonMetaLabels() labels.Labels {
var lset labels.Labels
for k, v := range t {
if !strings.HasPrefix(k, model.MetaLabelPrefix) {
lset = append(lset, labels.Label{Name: k, Value: v})
}
}
sort.Sort(lset)
return lset
}

func (t Target) SpecificLabels(lbls []string) labels.Labels {
var lset labels.Labels
for k, v := range t {
if slices.Contains(lbls, k) {
lset = append(lset, labels.Label{Name: k, Value: v})
}
}
sort.Sort(lset)
return lset
}

// Exports holds values which are exported by all discovery components.
type Exports struct {
Targets []Target `alloy:"targets,attr"`
Expand Down Expand Up @@ -269,17 +228,7 @@ func toAlloyTargets(cache map[string]*targetgroup.Group) []Target {

for _, group := range cache {
for _, target := range group.Targets {
tLabels := make(map[string]string, len(group.Labels)+len(target))

// first add the group labels, and then the
// target labels, so that target labels take precedence.
for k, v := range group.Labels {
tLabels[string(k)] = string(v)
}
for k, v := range target {
tLabels[string(k)] = string(v)
}
allTargets = append(allTargets, tLabels)
allTargets = append(allTargets, NewTargetFromOwnAndSharedLabelSets(target, group.Labels))
}
}
return allTargets
Expand Down
46 changes: 23 additions & 23 deletions internal/component/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ var updateTestCases = []discovererUpdateTestCase{
{Source: "test", Labels: model.LabelSet{"test_key": "value"}, Targets: []model.LabelSet{{"foo": "bar"}}},
},
expectedInitialExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
},
updatedTargets: []*targetgroup.Group{
{Source: "test", Labels: model.LabelSet{"test_key_2": "value"}, Targets: []model.LabelSet{{"baz": "bux"}}},
},
expectedUpdatedExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial re-published on shutdown
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated export
},
expectedFinalExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial re-published on shutdown
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated export
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated re-published on shutdown
},
},
{
Expand Down Expand Up @@ -86,15 +86,15 @@ var updateTestCases = []discovererUpdateTestCase{
{Source: "test", Labels: model.LabelSet{"test_key_2": "value"}, Targets: []model.LabelSet{{"baz": "bux"}}},
},
expectedUpdatedExports: []component.Exports{
Exports{Targets: []Target{}}, // Initial publish
Exports{Targets: []Target{}}, // Initial re-published on shutdown
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated export.
Exports{Targets: []Target{}}, // Initial publish
Exports{Targets: []Target{}}, // Initial re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated export.
},
expectedFinalExports: []component.Exports{
Exports{Targets: []Target{}}, // Initial publish
Exports{Targets: []Target{}}, // Initial re-published on shutdown
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated export.
Exports{Targets: []Target{{"test_key_2": "value", "baz": "bux"}}}, // Updated export re-published on shutdown.
Exports{Targets: []Target{}}, // Initial publish
Exports{Targets: []Target{}}, // Initial re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated export.
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"test_key_2": "value", "baz": "bux"})}}, // Updated export re-published on shutdown.
},
},
{
Expand All @@ -103,19 +103,19 @@ var updateTestCases = []discovererUpdateTestCase{
{Source: "test", Labels: model.LabelSet{"test_key": "value"}, Targets: []model.LabelSet{{"foo": "bar"}}},
},
expectedInitialExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
},
updatedTargets: nil,
expectedUpdatedExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial re-published on shutdown
Exports{Targets: []Target{}}, // Updated export should publish empty!
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial re-published on shutdown
Exports{Targets: []Target{}}, // Updated export should publish empty!
},
expectedFinalExports: []component.Exports{
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial export
Exports{Targets: []Target{{"foo": "bar", "test_key": "value"}}}, // Initial re-published on shutdown
Exports{Targets: []Target{}}, // Updated export should publish empty!
Exports{Targets: []Target{}}, // Updated re-published on shutdown
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial export
Exports{Targets: []Target{NewTargetFromMap(map[string]string{"foo": "bar", "test_key": "value"})}}, // Initial re-published on shutdown
Exports{Targets: []Target{}}, // Updated export should publish empty!
Exports{Targets: []Target{}}, // Updated re-published on shutdown
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/component/discovery/distributed_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ func BenchmarkDistributedTargets(b *testing.B) {
}

func mkTarget(kv ...string) Target {
target := make(Target)
target := make(map[string]string)
for i := 0; i < len(kv); i += 2 {
target[kv[i]] = kv[i+1]
}
return target
return NewTargetFromMap(target)
}

func testDistTargets(lookupMap map[shard.Key][]peer.Peer) *DistributedTargets {
Expand Down
26 changes: 4 additions & 22 deletions internal/component/discovery/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"sync"

"github.com/prometheus/prometheus/model/relabel"

"github.com/grafana/alloy/internal/component"
alloy_relabel "github.com/grafana/alloy/internal/component/common/relabel"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/featuregate"
"github.com/grafana/alloy/internal/service/livedebugging"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
)

func init() {
Expand Down Expand Up @@ -92,10 +92,10 @@ func (c *Component) Update(args component.Arguments) error {
c.rcs = relabelConfigs

for _, t := range newArgs.Targets {
lset := componentMapToPromLabels(t)
lset := t.Labels()
relabelled, keep := relabel.Process(lset, relabelConfigs...)
if keep {
targets = append(targets, promLabelsToComponent(relabelled))
targets = append(targets, discovery.NewTargetFromModelLabels(relabelled))
}
componentID := livedebugging.ComponentID(c.opts.ID)
if c.debugDataPublisher.IsActive(componentID) {
Expand All @@ -112,21 +112,3 @@ func (c *Component) Update(args component.Arguments) error {
}

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

func componentMapToPromLabels(ls discovery.Target) labels.Labels {
res := make([]labels.Label, 0, len(ls))
for k, v := range ls {
res = append(res, labels.Label{Name: k, Value: v})
}

return res
}

func promLabelsToComponent(ls labels.Labels) discovery.Target {
res := make(map[string]string, len(ls))
for _, l := range ls {
res[l.Name] = l.Value
}

return res
}
5 changes: 3 additions & 2 deletions internal/component/discovery/relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

alloy_relabel "github.com/grafana/alloy/internal/component/common/relabel"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/component/discovery/relabel"
"github.com/grafana/alloy/internal/runtime/componenttest"
"github.com/grafana/alloy/syntax"
"github.com/stretchr/testify/require"
)

func TestRelabelConfigApplication(t *testing.T) {
Expand Down Expand Up @@ -56,7 +57,7 @@ rule {
}
`
expectedOutput := []discovery.Target{
map[string]string{"__address__": "localhost", "app": "backend", "destination": "localhost/one", "meta_bar": "bar", "meta_foo": "foo", "name": "one"},
discovery.NewTargetFromMap(map[string]string{"__address__": "localhost", "app": "backend", "destination": "localhost/one", "meta_bar": "bar", "meta_foo": "foo", "name": "one"}),
}

var args relabel.Arguments
Expand Down
Loading

0 comments on commit a575d60

Please sign in to comment.