Skip to content

Commit

Permalink
wip - squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed Jan 30, 2025
1 parent 0bb74cc commit b39c1f9
Show file tree
Hide file tree
Showing 79 changed files with 1,617 additions and 500 deletions.
6 changes: 3 additions & 3 deletions internal/component/beyla/ebpf/beyla_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,17 @@ func (c *Component) Update(args component.Arguments) error {
func (c *Component) baseTarget() (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.EmptyTarget, 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": defaultInstance(),
"job": "beyla",
}, nil
}), nil
}

func (c *Component) reportUnhealthy(err error) {
Expand Down
112 changes: 112 additions & 0 deletions internal/component/common/relabel/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package relabel

// TODO(thampiotr): write comment
// TODO(thampiotr): port the test

import (
"crypto/md5"
"encoding/binary"
"fmt"
"strings"

"github.com/prometheus/common/model"
)

type LabelBuilder interface {
Get(label string) string
// TODO(thampiotr): test that Set and Del can be called while iterating.
Range(f func(label string, value string))
Set(label string, val string)
Del(ns ...string)
}

// ProcessBuilder is like Process, but the caller passes a labels.Builder
// containing the initial set of labels, which is mutated by the rules.
func ProcessBuilder(lb LabelBuilder, cfgs ...*Config) (keep bool) {
for _, cfg := range cfgs {
keep = doRelabel(cfg, lb)
if !keep {
return false
}
}
return true
}

func doRelabel(cfg *Config, lb LabelBuilder) (keep bool) {
var va [16]string
values := va[:0]
if len(cfg.SourceLabels) > cap(values) {
values = make([]string, 0, len(cfg.SourceLabels))
}
for _, ln := range cfg.SourceLabels {
values = append(values, lb.Get(string(ln)))
}
val := strings.Join(values, cfg.Separator)

switch cfg.Action {
case Drop:
if cfg.Regex.MatchString(val) {
return false
}
case Keep:
if !cfg.Regex.MatchString(val) {
return false
}
case DropEqual:
if lb.Get(cfg.TargetLabel) == val {
return false
}
case KeepEqual:
if lb.Get(cfg.TargetLabel) != val {
return false
}
case Replace:
indexes := cfg.Regex.FindStringSubmatchIndex(val)
// If there is no match no replacement must take place.
if indexes == nil {
break
}
target := model.LabelName(cfg.Regex.ExpandString([]byte{}, cfg.TargetLabel, val, indexes))
if !target.IsValid() {
break
}
res := cfg.Regex.ExpandString([]byte{}, cfg.Replacement, val, indexes)
if len(res) == 0 {
lb.Del(string(target))
break
}
lb.Set(string(target), string(res))
case Lowercase:
lb.Set(cfg.TargetLabel, strings.ToLower(val))
case Uppercase:
lb.Set(cfg.TargetLabel, strings.ToUpper(val))
case HashMod:
hash := md5.Sum([]byte(val))
// Use only the last 8 bytes of the hash to give the same result as earlier versions of this code.
mod := binary.BigEndian.Uint64(hash[8:]) % cfg.Modulus
lb.Set(cfg.TargetLabel, fmt.Sprintf("%d", mod))
case LabelMap:
lb.Range(func(name, value string) {
if cfg.Regex.MatchString(name) {
res := cfg.Regex.ReplaceAllString(name, cfg.Replacement)
lb.Set(res, value)
}
})
case LabelDrop:
lb.Range(func(name, value string) {
if cfg.Regex.MatchString(name) {
lb.Del(name)
}
})
case LabelKeep:
lb.Range(func(name, value string) {
if !cfg.Regex.MatchString(name) {
lb.Del(value)
}
})
default:
panic(fmt.Errorf("relabel: unknown relabel action type %q", cfg.Action))
}

return true
}
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 @@ -165,17 +165,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.EmptyTarget, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 30 38546436 ns/op 64963036 B/op 241805 allocs/op
Benchmark_Targets_TypicalPipeline-8 28 38647682 ns/op 64953672 B/op 241803 allocs/op
Benchmark_Targets_TypicalPipeline-8 28 38550351 ns/op 64967889 B/op 241822 allocs/op
Benchmark_Targets_TypicalPipeline-8 30 38803608 ns/op 64960262 B/op 241807 allocs/op
Benchmark_Targets_TypicalPipeline-8 27 38605718 ns/op 64965312 B/op 241814 allocs/op
Benchmark_Targets_TypicalPipeline-8 28 38427967 ns/op 64958167 B/op 241812 allocs/op
Benchmark_Targets_TypicalPipeline-8 28 38815302 ns/op 64961392 B/op 241810 allocs/op
Benchmark_Targets_TypicalPipeline-8 28 39033807 ns/op 64964683 B/op 241812 allocs/op
Benchmark_Targets_TypicalPipeline-8 26 39444696 ns/op 64963500 B/op 241818 allocs/op
Benchmark_Targets_TypicalPipeline-8 26 38543861 ns/op 64961057 B/op 241808 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 23.807s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 24 45995441 ns/op 66573585 B/op 241368 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 44810203 ns/op 66532546 B/op 241380 allocs/op
Benchmark_Targets_TypicalPipeline-8 24 44766747 ns/op 66531254 B/op 241373 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 45150108 ns/op 66530506 B/op 241367 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 47782998 ns/op 66576992 B/op 241376 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 47254332 ns/op 66576284 B/op 241378 allocs/op
Benchmark_Targets_TypicalPipeline-8 24 44567898 ns/op 66536935 B/op 241394 allocs/op
Benchmark_Targets_TypicalPipeline-8 24 44572594 ns/op 66574589 B/op 241369 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 44610702 ns/op 66532897 B/op 241375 allocs/op
Benchmark_Targets_TypicalPipeline-8 25 44945075 ns/op 66575368 B/op 241376 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 23.817s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 72 15278984 ns/op 20085046 B/op 60435 allocs/op
Benchmark_Targets_TypicalPipeline-8 75 14940728 ns/op 20085236 B/op 60436 allocs/op
Benchmark_Targets_TypicalPipeline-8 75 15123676 ns/op 20085305 B/op 60437 allocs/op
Benchmark_Targets_TypicalPipeline-8 74 15007377 ns/op 20085855 B/op 60439 allocs/op
Benchmark_Targets_TypicalPipeline-8 75 15170948 ns/op 20085227 B/op 60436 allocs/op
Benchmark_Targets_TypicalPipeline-8 75 15316376 ns/op 20084762 B/op 60435 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 13.170s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 48 22740573 ns/op 21467362 B/op 100933 allocs/op
Benchmark_Targets_TypicalPipeline-8 45 23141183 ns/op 21466235 B/op 100932 allocs/op
Benchmark_Targets_TypicalPipeline-8 48 24125997 ns/op 21466766 B/op 100936 allocs/op
Benchmark_Targets_TypicalPipeline-8 46 22152172 ns/op 21469613 B/op 100944 allocs/op
Benchmark_Targets_TypicalPipeline-8 46 26163378 ns/op 21466312 B/op 100928 allocs/op
Benchmark_Targets_TypicalPipeline-8 38 41765575 ns/op 21539059 B/op 100944 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 8.729s
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 33 35633138 ns/op 10840681 B/op 80539 allocs/op
Benchmark_Targets_TypicalPipeline-8 34 32671718 ns/op 10840274 B/op 80535 allocs/op
Benchmark_Targets_TypicalPipeline-8 37 33207543 ns/op 10927221 B/op 80541 allocs/op
Benchmark_Targets_TypicalPipeline-8 34 33105222 ns/op 10840144 B/op 80529 allocs/op
Benchmark_Targets_TypicalPipeline-8 36 32274262 ns/op 10927934 B/op 80541 allocs/op
Benchmark_Targets_TypicalPipeline-8 37 32763682 ns/op 10840785 B/op 80536 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 8.826s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
goos: darwin
goarch: arm64
pkg: github.com/grafana/alloy/internal/component/discovery
cpu: Apple M2
Benchmark_Targets_TypicalPipeline-8 40 29411972 ns/op 10927567 B/op 80537 allocs/op
Benchmark_Targets_TypicalPipeline-8 40 29447266 ns/op 10927386 B/op 80537 allocs/op
Benchmark_Targets_TypicalPipeline-8 40 29141625 ns/op 10840319 B/op 80536 allocs/op
Benchmark_Targets_TypicalPipeline-8 40 29083859 ns/op 10840952 B/op 80541 allocs/op
Benchmark_Targets_TypicalPipeline-8 40 36991749 ns/op 10927696 B/op 80541 allocs/op
Benchmark_Targets_TypicalPipeline-8 39 29443703 ns/op 10927869 B/op 80538 allocs/op
PASS
ok github.com/grafana/alloy/internal/component/discovery 9.926s
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, NewTargetFromSpecificAndBaseLabelSet(target, group.Labels))
}
}
return allTargets
Expand Down
Loading

0 comments on commit b39c1f9

Please sign in to comment.