Skip to content

Commit 2eb281c

Browse files
authored
feat: Add lazy mode argument to pyroscope.ebpf (#4824)
1 parent 217d6d9 commit 2eb281c

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ v1.12.0
9090

9191
- Add a `regex` argument to the `structured_metadata` stage in `loki.process` to extract labels matching a regular expression. (@timonegk)
9292

93+
- Add `lazy_mode` argument to the `pyroscope.ebpf` to defer eBPF profiler startup until there are targets to profile. (@luweglarz)
94+
9395
- OpenTelemetry Collector dependencies upgraded from v0.134.0 to v0.139.0. (@dehaansa)
9496
- All `otelcol.receiver.*` components leveraging an HTTP server can configure HTTP keep alive behavior with `keep_alives_enabled`.
9597
- All `otelcol.exporter.*` components providing the `sending_queue` > `batch` block have default `batch` values.

docs/sources/reference/components/pyroscope/pyroscope.ebpf.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ You can use the following arguments with `pyroscope.ebpf`:
7878
| `v8_enabled` | `bool` | A flag to enable/disable V8 profiling. | `true` | no |
7979
| `off_cpu_threshold` | `float` | A flag to adjust the off-cpu profiling threshold between 0 and 1 as float. | `0` | no |
8080
| `verbose_mode` | `bool` | Enable verbose logging for the eBPF profiler. | `false` | no |
81+
| `lazy_mode` | `bool` | Enable lazy mode to defer eBPF profiler startup until targets are discovered. | `false` | no |
8182

8283
Only the `forward_to` and `targets` fields are required.
8384
Omitted fields take their default values.

internal/component/pyroscope/ebpf/args.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Arguments struct {
2525
LoadProbe bool `alloy:"load_probe,attr,optional"`
2626
UProbeLinks []string `alloy:"u_probe_links,attr,optional"`
2727
VerboseMode bool `alloy:"verbose_mode,attr,optional"`
28+
LazyMode bool `alloy:"lazy_mode,attr,optional"`
2829
DeprecatedArguments DeprecatedArguments `alloy:",squash"`
2930

3031
// undocumented

internal/component/pyroscope/ebpf/ebpf_linux.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ type Component struct {
122122

123123
func (c *Component) Run(ctx context.Context) error {
124124
c.checkTraceFS()
125+
126+
if c.args.LazyMode && len(c.args.Targets) == 0 {
127+
_ = level.Info(c.logger).Log("msg", "lazy mode enabled, waiting for targets to profile")
128+
if err := c.waitForTargets(ctx); err != nil {
129+
return err
130+
}
131+
}
132+
125133
ctlr := controller.New(c.cfg)
126134
const sessionMaxErrors = 3
127135
var err error
@@ -156,16 +164,34 @@ func (c *Component) Run(ctx context.Context) error {
156164
case <-ctx.Done():
157165
return nil
158166
case newArgs := <-c.argsUpdate:
159-
c.args = newArgs
160-
c.targetFinder.Update(c.args.targetsOptions(c.dynamicProfilingPolicy))
161-
c.appendable.UpdateChildren(newArgs.ForwardTo)
162-
c.metrics.targetsActive.Set(float64(len(c.args.Targets)))
167+
c.updateArgs(newArgs)
163168
}
164169
}
165170
}, func(error) {})
166171
return g.Run()
167172
}
168173

174+
func (c *Component) updateArgs(newArgs Arguments) {
175+
c.args = newArgs
176+
c.targetFinder.Update(c.args.targetsOptions(c.dynamicProfilingPolicy))
177+
c.appendable.UpdateChildren(newArgs.ForwardTo)
178+
c.metrics.targetsActive.Set(float64(len(c.args.Targets)))
179+
}
180+
181+
func (c *Component) waitForTargets(ctx context.Context) error {
182+
for {
183+
select {
184+
case <-ctx.Done():
185+
return ctx.Err()
186+
case newArgs := <-c.argsUpdate:
187+
c.updateArgs(newArgs)
188+
if len(c.args.Targets) > 0 {
189+
return nil
190+
}
191+
}
192+
}
193+
}
194+
169195
func (c *Component) Update(args component.Arguments) error {
170196
newArgs := args.(Arguments)
171197
select {
@@ -244,6 +270,7 @@ func NewDefaultArguments() Arguments {
244270
LoadProbe: false,
245271
UProbeLinks: []string{},
246272
VerboseMode: false,
273+
LazyMode: false,
247274

248275
// undocumented
249276
PyroscopeDynamicProfilingPolicy: true,

0 commit comments

Comments
 (0)