Skip to content

Commit f06eabb

Browse files
authored
feat: Add settings for cpu/mutex/block profiling options (#13278)
The HTTP endpoints for scraping CPU, mutex and block profiles are already exposed by the server, however, block and mutex profiling must be explicitly enabled by setting a non-zero value for `SetBlockProfileRate` and `SetMutexProfileFraction` respectively. For more information about the available options consult the Go documentation: * https://pkg.go.dev/runtime#SetBlockProfileRate * https://pkg.go.dev/runtime#SetMutexProfileFraction * https://pkg.go.dev/runtime#SetCPUProfileRate Signed-off-by: Christian Haudum <[email protected]>
1 parent dbc0e24 commit f06eabb

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

cmd/loki/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func main() {
106106
}()
107107
}
108108

109+
setProfilingOptions(config.Profiling)
110+
109111
// Allocate a block of memory to reduce the frequency of garbage collection.
110112
// The larger the ballast, the lower the garbage collection frequency.
111113
// https://github.com/grafana/loki/issues/781
@@ -127,3 +129,15 @@ func main() {
127129
err = t.Run(loki.RunOpts{StartTime: startTime})
128130
util_log.CheckFatal("running loki", err, util_log.Logger)
129131
}
132+
133+
func setProfilingOptions(cfg loki.ProfilingConfig) {
134+
if cfg.BlockProfileRate > 0 {
135+
runtime.SetBlockProfileRate(cfg.BlockProfileRate)
136+
}
137+
if cfg.CPUProfileRate > 0 {
138+
runtime.SetCPUProfileRate(cfg.CPUProfileRate)
139+
}
140+
if cfg.MutexProfileFraction > 0 {
141+
runtime.SetMutexProfileFraction(cfg.MutexProfileFraction)
142+
}
143+
}

docs/sources/shared/configuration.md

+21
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ compactor_grpc_client:
567567
# Configuration for analytics.
568568
[analytics: <analytics>]
569569

570+
# Configuration for profiling options.
571+
[profiling: <profiling>]
572+
570573
# Common configuration to be shared between multiple modules. If a more specific
571574
# configuration is given in other sections, the related configuration within
572575
# this section will be ignored.
@@ -3850,6 +3853,24 @@ chunks:
38503853
[row_shards: <int> | default = 16]
38513854
```
38523855

3856+
### profiling
3857+
3858+
Configuration for `profiling` options.
3859+
3860+
```yaml
3861+
# Sets the value for runtime.SetBlockProfilingRate
3862+
# CLI flag: -profiling.block-profile-rate
3863+
[block_profile_rate: <int> | default = 0]
3864+
3865+
# Sets the value for runtime.SetCPUProfileRate
3866+
# CLI flag: -profiling.cpu-profile-rate
3867+
[cpu_profile_rate: <int> | default = 0]
3868+
3869+
# Sets the value for runtime.SetMutexProfileFraction
3870+
# CLI flag: -profiling.mutex-profile-fraction
3871+
[mutex_profile_fraction: <int> | default = 0]
3872+
```
3873+
38533874
### querier
38543875

38553876
Configures the `querier`. Only appropriate when running all modules or just the querier.

pkg/loki/loki.go

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type Config struct {
108108
OperationalConfig runtime.Config `yaml:"operational_config,omitempty"`
109109
Tracing tracing.Config `yaml:"tracing"`
110110
Analytics analytics.Config `yaml:"analytics"`
111+
Profiling ProfilingConfig `yaml:"profiling,omitempty"`
111112

112113
LegacyReadTarget bool `yaml:"legacy_read_target,omitempty" doc:"hidden|deprecated"`
113114

@@ -179,6 +180,7 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) {
179180
c.QueryScheduler.RegisterFlags(f)
180181
c.Analytics.RegisterFlags(f)
181182
c.OperationalConfig.RegisterFlags(f)
183+
c.Profiling.RegisterFlags(f)
182184
}
183185

184186
func (c *Config) registerServerFlagsWithChangedDefaultValues(fs *flag.FlagSet) {

pkg/loki/profiling_config.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package loki
2+
3+
import "flag"
4+
5+
type ProfilingConfig struct {
6+
BlockProfileRate int `yaml:"block_profile_rate"`
7+
CPUProfileRate int `yaml:"cpu_profile_rate"`
8+
MutexProfileFraction int `yaml:"mutex_profile_fraction"`
9+
}
10+
11+
// RegisterFlags registers flag.
12+
func (c *ProfilingConfig) RegisterFlags(f *flag.FlagSet) {
13+
c.RegisterFlagsWithPrefix("profiling.", f)
14+
}
15+
16+
// RegisterFlagsWithPrefix registers flag with a common prefix.
17+
func (c *ProfilingConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
18+
f.IntVar(&c.BlockProfileRate, prefix+"block-profile-rate", 0, "Sets the value for runtime.SetBlockProfilingRate")
19+
f.IntVar(&c.CPUProfileRate, prefix+"cpu-profile-rate", 0, "Sets the value for runtime.SetCPUProfileRate")
20+
f.IntVar(&c.MutexProfileFraction, prefix+"mutex-profile-fraction", 0, "Sets the value for runtime.SetMutexProfileFraction")
21+
}

tools/doc-generator/parse/root_blocks.go

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/grafana/loki/v3/pkg/ingester"
2424
ingester_client "github.com/grafana/loki/v3/pkg/ingester/client"
2525
"github.com/grafana/loki/v3/pkg/loghttp/push"
26+
"github.com/grafana/loki/v3/pkg/loki"
2627
"github.com/grafana/loki/v3/pkg/loki/common"
2728
frontend "github.com/grafana/loki/v3/pkg/lokifrontend"
2829
"github.com/grafana/loki/v3/pkg/querier"
@@ -168,6 +169,11 @@ var (
168169
StructType: []reflect.Type{reflect.TypeOf(analytics.Config{})},
169170
Desc: "Configuration for analytics.",
170171
},
172+
{
173+
Name: "profiling",
174+
StructType: []reflect.Type{reflect.TypeOf(loki.ProfilingConfig{})},
175+
Desc: "Configuration for profiling options.",
176+
},
171177

172178
{
173179
Name: "common",

0 commit comments

Comments
 (0)