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

Optimise Prometheus targets handling #2474

Merged
merged 5 commits into from
Feb 13, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Main (unreleased)
- Added table columns parsing (@cristiagreco)
- Add enable/disable collector configurability to `database_observability.mysql`. This removes the `query_samples_enabled` argument, now configurable via enable/disable collector. (@fridgepoet)

- Improved memory and CPU performance of Prometheus pipelines by changing the underlying implementation of targets (@thampiotr)

### Bugfixes

- Fix log rotation for Windows in `loki.source.file` by refactoring the component to use the runner pkg. This should also reduce CPU consumption when tailing a lot of files in a dynamic environment. (@wildum)
Expand Down
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
11 changes: 11 additions & 0 deletions internal/component/common/relabel/label_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package relabel

// LabelBuilder is an interface that can be used to change labels with relabel logic.
type LabelBuilder interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows us to use our own label builder with prometheus relabel logic. We may be able to contribute this upstream as it doesn't seem harmful.

// Get returns given label value. If label is not present, an empty string is returned.
Get(label string) string
Range(f func(label string, value string))
// Set will set given label to given value. Setting to empty value is equivalent to deleting this label.
Set(label string, val string)
Del(ns ...string)
}
118 changes: 117 additions & 1 deletion internal/component/common/relabel/relabel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// NOTE: this file is copied from Prometheus codebase and adapted to work correctly with Alloy types.
// For backwards compatibility purposes, the behaviour implemented here should not be changed.

// 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 +168,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 +211,97 @@ func (rc *Config) Validate() error {
return nil
}

// ProcessBuilder should be called with lb LabelBuilder containing the initial set of labels,
// which are then modified following the configured rules using builder's methods such as Set and Del.
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