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

WIP: Optimise Prometheus targets handling #2474

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
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
9 changes: 9 additions & 0 deletions internal/component/common/relabel/label_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package relabel

// LabelBuilder is an interface that can be used to change labels with relabel logic.
type LabelBuilder interface {
Get(label string) string
Range(f func(label string, value string))
Set(label string, val string)
Del(ns ...string)
}
117 changes: 116 additions & 1 deletion internal/component/common/relabel/relabel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
// NOTE: this is adapted from Prometheus codebase to work correctly with Alloy types.

// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package relabel

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

"github.com/grafana/regexp"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -149,7 +167,13 @@ func (rc *Config) Validate() error {
if (rc.Action == Replace || rc.Action == HashMod || rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && rc.TargetLabel == "" {
return fmt.Errorf("relabel configuration for %s action requires 'target_label' value", rc.Action)
}
if (rc.Action == Replace || rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && !relabelTarget.MatchString(rc.TargetLabel) {
if rc.Action == Replace && !strings.Contains(rc.TargetLabel, "$") && !model.LabelName(rc.TargetLabel).IsValid() {
return fmt.Errorf("%q is invalid 'target_label' for %s action", rc.TargetLabel, rc.Action)
}
if rc.Action == Replace && strings.Contains(rc.TargetLabel, "$") && !relabelTarget.MatchString(rc.TargetLabel) {
return fmt.Errorf("%q is invalid 'target_label' for %s action", rc.TargetLabel, rc.Action)
}
if (rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && !model.LabelName(rc.TargetLabel).IsValid() {
return fmt.Errorf("%q is invalid 'target_label' for %s action", rc.TargetLabel, rc.Action)
}
if (rc.Action == Lowercase || rc.Action == Uppercase || rc.Action == KeepEqual || rc.Action == DropEqual) && rc.Replacement != DefaultRelabelConfig.Replacement {
Expand Down Expand Up @@ -186,6 +210,97 @@ func (rc *Config) Validate() error {
return nil
}

// 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(name)
}
})
default:
panic(fmt.Errorf("relabel: unknown relabel action type %q", cfg.Action))
}

return true
}

// ComponentToPromRelabelConfigs bridges the Component-based configuration of
// relabeling steps to the Prometheus implementation.
func ComponentToPromRelabelConfigs(rcs []*Config) []*relabel.Config {
Expand Down
Loading
Loading