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

pkg/config: support seconds in pprof configuration #4623

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 1 addition & 15 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,6 @@ func (c *ScrapeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
unmarshalled.ProfilingConfig = defaults.ProfilingConfig
} else if unmarshalled.ProfilingConfig.PprofConfig == nil {
unmarshalled.ProfilingConfig.PprofConfig = defaults.ProfilingConfig.PprofConfig
} else {
// Merge unmarshalled config with defaults
for pt, pc := range defaults.ProfilingConfig.PprofConfig {
// nothing set yet so simply use the default
if unmarshalled.ProfilingConfig.PprofConfig[pt] == nil {
unmarshalled.ProfilingConfig.PprofConfig[pt] = pc
continue
}
if unmarshalled.ProfilingConfig.PprofConfig[pt].Enabled == nil {
unmarshalled.ProfilingConfig.PprofConfig[pt].Enabled = trueValue()
}
if unmarshalled.ProfilingConfig.PprofConfig[pt].Path == "" {
unmarshalled.ProfilingConfig.PprofConfig[pt].Path = pc.Path
}
}
}

// If path prefix is specified, add to PprofConfig path
Expand Down Expand Up @@ -295,6 +280,7 @@ type PprofProfilingConfig struct {
Path string `yaml:"path,omitempty"`
Delta bool `yaml:"delta,omitempty"`
KeepSampleType []SampleType `yaml:"keep_sample_type,omitempty"`
Seconds int `yaml:"seconds,omitempty"`
}

type SampleType struct {
Expand Down
60 changes: 25 additions & 35 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ scrape_configs:
- job_name: 'path-prefix-with-defaults'
profiling_config:
path_prefix: /test/prefix
- job_name: 'path-prefix-and-seconds'
profiling_config:
path_prefix: /test/prefix
pprof_config:
fgprof:
enabled: true
path: /debug/fgprof
seconds: 3
`

expected := &Config{
Expand All @@ -83,23 +91,6 @@ scrape_configs:
Enabled: trueValue(),
Path: "/parca/debug/pprof/allocs",
},
"block": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/debug/pprof/block",
},
"goroutine": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/debug/pprof/goroutine",
},
"mutex": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/debug/pprof/mutex",
},
"process_cpu": &PprofProfilingConfig{
Enabled: trueValue(),
Delta: true,
Path: "/debug/pprof/profile",
},
"fgprof": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/debug/fgprof",
Expand Down Expand Up @@ -131,23 +122,6 @@ scrape_configs:
Enabled: trueValue(),
Path: "/test/prefix/parca/debug/pprof/allocs",
},
"block": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/test/prefix/debug/pprof/block",
},
"goroutine": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/test/prefix/debug/pprof/goroutine",
},
"mutex": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/test/prefix/debug/pprof/mutex",
},
"process_cpu": &PprofProfilingConfig{
Enabled: trueValue(),
Delta: true,
Path: "/test/prefix/debug/pprof/profile",
},
"fgprof": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/test/prefix/debug/fgprof",
Expand Down Expand Up @@ -187,11 +161,27 @@ scrape_configs:
},
},
},
{
JobName: "path-prefix-and-seconds",
ScrapeInterval: model.Duration(10 * time.Second),
ScrapeTimeout: model.Duration(13 * time.Second),
Scheme: "http",
ProfilingConfig: &ProfilingConfig{
PprofPrefix: "/test/prefix",
PprofConfig: PprofConfig{
"fgprof": &PprofProfilingConfig{
Enabled: trueValue(),
Path: "/test/prefix/debug/fgprof",
Seconds: 3,
},
},
},
},
},
}
c, err := Load(complexYAML)
require.NoError(t, err)
require.Len(t, c.ScrapeConfigs, 4)
require.Len(t, c.ScrapeConfigs, 5)
require.Equal(t, expected, c)
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/scrape/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ func targetsFromGroup(tg *targetgroup.Group, cfg *config.ScrapeConfig, targets [
}

if pcfg, found := cfg.ProfilingConfig.PprofConfig[profType]; found && pcfg.Delta {
params.Add("seconds", strconv.Itoa(int(time.Duration(cfg.ScrapeInterval)/time.Second)))
// If Seconds is NOT set on the pprof configuration explicitly, we default to ScrapeInterval.
seconds := int(time.Duration(cfg.ScrapeInterval) / time.Second)
alperkokmen marked this conversation as resolved.
Show resolved Hide resolved
if pcfg.Seconds > 0 {
seconds = pcfg.Seconds
}
params.Add("seconds", strconv.Itoa(seconds))
}

targets = append(targets, NewTarget(lset, origLabels, params, keepSets[i]))
Expand Down
163 changes: 163 additions & 0 deletions pkg/scrape/target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2024 The Parca 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 scrape
alperkokmen marked this conversation as resolved.
Show resolved Hide resolved

import (
"sort"
"testing"
"time"

"github.com/parca-dev/parca/pkg/config"
alperkokmen marked this conversation as resolved.
Show resolved Hide resolved
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
)

func TestTargetsFromGroup(t *testing.T) {
testCases := []struct {
name string
tg *targetgroup.Group
cfg config.ScrapeConfig
targets Targets
lb *labels.Builder
expected Targets
err error
}{
{
name: "default-scrape-config",
tg: &targetgroup.Group{
Targets: []model.LabelSet{
{"__address__": "localhost:9090"},
},
Labels: model.LabelSet{},
},
cfg: config.DefaultScrapeConfig(),
lb: labels.NewBuilder(labels.EmptyLabels()),
expected: Targets{
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/allocs",
),
},
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/mutex",
),
},
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/block",
),
},
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/goroutine",
),
},
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/profile",
model.ParamLabelPrefix+"seconds", "10",
),
},
},
err: nil,
},
{
name: "custom-scrape-config-with-pprof-seconds",
tg: &targetgroup.Group{
Targets: []model.LabelSet{
{"__address__": "localhost:9090"},
},
Labels: model.LabelSet{},
},
cfg: config.ScrapeConfig{
ScrapeInterval: model.Duration(time.Minute * 10),
Scheme: "http",
ProfilingConfig: &config.ProfilingConfig{
PprofConfig: config.PprofConfig{
"pprofMemory": &config.PprofProfilingConfig{
Enabled: trueValue(),
Delta: true,
Path: "/debug/pprof/allocs",
Seconds: 10,
},
"pprofProcessCPU": &config.PprofProfilingConfig{
Enabled: trueValue(),
Delta: true,
Path: "/debug/pprof/profile",
Seconds: 30,
},
},
},
},
lb: labels.NewBuilder(labels.EmptyLabels()),
expected: Targets{
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/allocs",
model.ParamLabelPrefix+"seconds", "10",
),
},
{
labels: labels.FromStrings(
model.AddressLabel, "localhost:9090",
model.SchemeLabel, "http",
ProfilePath, "/debug/pprof/profile",
model.ParamLabelPrefix+"seconds", "30",
),
},
},
err: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
a, err := targetsFromGroup(tc.tg, &tc.cfg, tc.targets, tc.lb)
actual := Targets(a) // convert to slice type for convenience
if tc.err != nil && (err == nil || err.Error() != tc.err.Error()) {
t.Fatalf("unexpected error: %v", err)
return
}
if tc.err != nil && err != nil && err.Error() == tc.err.Error() {
return
}
require.Equal(t, len(tc.expected), len(actual), "unexpected number of targets")
sort.Sort(tc.expected)
sort.Sort(actual)
for i := range actual {
require.Equal(t, tc.expected[i].URL(), actual[i].URL())
}
})
}
}

func trueValue() *bool {
a := true
return &a
}
Loading