Skip to content

Commit

Permalink
add max_cache_size (#6026)
Browse files Browse the repository at this point in the history
* add max_cache_size

* add max_cache_size to converter

* fix tests
  • Loading branch information
mattdurham authored Dec 27, 2023
1 parent 5e9df54 commit d4ba510
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Main (unreleased)
- Allows for pulling metrics at the Azure subscription level instead of resource by resource
- Disable dimension validation by default to reduce the number of exporter instances needed for full dimension coverage

- Add `max_cache_size` to `prometheus.relabel` to allow configurability instead of hard coded 100,000. (@mattdurham)

### Bugfixes

- Update `pyroscope.ebpf` to fix a logical bug causing to profile to many kthreads instead of regular processes https://github.com/grafana/pyroscope/pull/2778 (@korniltsev)
Expand Down
20 changes: 14 additions & 6 deletions component/prometheus/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,23 @@ type Arguments struct {
MetricRelabelConfigs []*flow_relabel.Config `river:"rule,block,optional"`

// Cache size to use for LRU cache.
//CacheSize int `river:"cache_size,attr,optional"`
CacheSize int `river:"max_cache_size,attr,optional"`
}

// SetToDefault implements river.Defaulter.
/*func (arg *Arguments) SetToDefault() {
func (arg *Arguments) SetToDefault() {
*arg = Arguments{
CacheSize: 500_000,
CacheSize: 100_000,
}
}*/
}

// Validate implements river.Validator.
func (arg *Arguments) Validate() error {
if arg.CacheSize <= 0 {
return fmt.Errorf("max_cache_size must be greater than 0 and is %d", arg.CacheSize)
}
return nil
}

// Exports holds values which are exported by the prometheus.relabel component.
type Exports struct {
Expand Down Expand Up @@ -88,7 +96,7 @@ var (

// New creates a new prometheus.relabel component.
func New(o component.Options, args Arguments) (*Component, error) {
cache, err := lru.New[uint64, *labelAndID](100_000)
cache, err := lru.New[uint64, *labelAndID](args.CacheSize)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -210,7 +218,7 @@ func (c *Component) Update(args component.Arguments) error {
defer c.mut.Unlock()

newArgs := args.(Arguments)
c.clearCache(100_000)
c.clearCache(newArgs.CacheSize)
c.mrc = flow_relabel.ComponentToPromRelabelConfigs(newArgs.MetricRelabelConfigs)
c.fanout.UpdateChildren(newArgs.ForwardTo)

Expand Down
14 changes: 13 additions & 1 deletion component/prometheus/relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@ func TestUpdateReset(t *testing.T) {
relabeller.relabel(0, lbls)
require.True(t, relabeller.cache.Len() == 1)
_ = relabeller.Update(Arguments{
CacheSize: 100000,
MetricRelabelConfigs: []*flow_relabel.Config{},
})
require.True(t, relabeller.cache.Len() == 0)
}

func TestValidator(t *testing.T) {
args := Arguments{CacheSize: 0}
err := args.Validate()
require.Error(t, err)

args.CacheSize = 1
err = args.Validate()
require.NoError(t, err)
}

func TestNil(t *testing.T) {
ls := labelstore.New(nil, prom.DefaultRegisterer)
fanout := prometheus.NewInterceptor(nil, ls, prometheus.WithAppendHook(func(ref storage.SeriesRef, _ labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {
Expand All @@ -72,6 +83,7 @@ func TestNil(t *testing.T) {
Action: "drop",
},
},
CacheSize: 100000,
})
require.NotNil(t, relabeller)
require.NoError(t, err)
Expand Down Expand Up @@ -129,7 +141,6 @@ func BenchmarkCache(b *testing.B) {

lbls := labels.FromStrings("__address__", "localhost")
app := entry.Appender(context.Background())

for i := 0; i < b.N; i++ {
app.Append(0, lbls, time.Now().UnixMilli(), 0)
}
Expand Down Expand Up @@ -161,6 +172,7 @@ func generateRelabel(t *testing.T) *Component {
Action: "replace",
},
},
CacheSize: 100_000,
})
require.NotNil(t, relabeller)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions converter/internal/prometheusconvert/component/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func toRelabelArguments(relabelConfigs []*prom_relabel.Config, forwardTo []stora
return &relabel.Arguments{
ForwardTo: forwardTo,
MetricRelabelConfigs: ToFlowRelabelConfigs(relabelConfigs),
CacheSize: 100_000,
}
}

Expand Down
3 changes: 2 additions & 1 deletion docs/sources/flow/reference/components/prometheus.relabel.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The following arguments are supported:
Name | Type | Description | Default | Required
---- | ---- | ----------- | ------- | --------
`forward_to` | `list(MetricsReceiver)` | Where the metrics should be forwarded to, after relabeling takes place. | | yes
`max_cache_size` | `int` | The maximum number of elements to hold in the relabeling cache. | 100,000 | no

## Blocks

Expand Down Expand Up @@ -187,4 +188,4 @@ connection work correctly. Refer to the linked documentation for more details.

{{% /admonition %}}

<!-- END GENERATED COMPATIBLE COMPONENTS -->
<!-- END GENERATED COMPATIBLE COMPONENTS -->

0 comments on commit d4ba510

Please sign in to comment.