Skip to content

Commit

Permalink
pkg/discovery: take user provided systemd cgroup path into account
Browse files Browse the repository at this point in the history
This patch wires up the --systemd-cgroup-path flag and actually plumbs
the user provided value into the systemd config and subsequently into
the systemd discoverer to be used when trying to reconcile unit files.

Fixes parca-dev#192
  • Loading branch information
derekparker committed Jan 6, 2022
1 parent 9933b45 commit b02cb76
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/parca-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func main() {
if len(flags.SystemdUnits) > 0 {
configs = append(configs, discovery.NewSystemdConfig(
flags.SystemdUnits,
flags.SystemdCgroupPath,
))
}

Expand Down
21 changes: 16 additions & 5 deletions pkg/discovery/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"sync"
"syscall"
"time"
Expand All @@ -33,13 +34,15 @@ import (
)

type SystemdConfig struct {
units map[string]struct{}
units map[string]struct{}
cgroupPath string
}

type SystemdDiscoverer struct {
units map[string]struct{}
unitProfilers map[string]struct{}
logger log.Logger
cgroupPath string

mtx sync.RWMutex
}
Expand All @@ -48,21 +51,23 @@ func (c *SystemdConfig) Name() string {
return "systemd"
}

func NewSystemdConfig(systemdUnits []string) *SystemdConfig {
func NewSystemdConfig(systemdUnits []string, systemdCgroupPath string) *SystemdConfig {

units := map[string]struct{}{}

for _, unit := range systemdUnits {
units[unit] = struct{}{}
}
return &SystemdConfig{
units: units,
units: units,
cgroupPath: systemdCgroupPath,
}
}

func (c *SystemdConfig) NewDiscoverer(d DiscovererOptions) (Discoverer, error) {
return &SystemdDiscoverer{
units: c.units,
cgroupPath: c.cgroupPath,
unitProfilers: make(map[string]struct{}),
logger: d.Logger,
}, nil
Expand Down Expand Up @@ -101,13 +106,19 @@ func (c *SystemdDiscoverer) Run(ctx context.Context, up chan<- []*target.Group)
}

func (c *SystemdDiscoverer) ReconcileUnit(ctx context.Context, unit string) (model.LabelSet, error) {
// If the user has specified a cgroup path prefer that as opposed to
// trying to figure it out ourselves.
if c.cgroupPath != "" {
p := path.Join(c.cgroupPath, unit)
return model.LabelSet{agent.CgroupPathLabelName: model.LabelValue(p)}, nil
}
// Note: It's ok to call cgroups.Mode() here instead of storing it somewhere because
// internally it's memoized.
if cgroups.Mode() == cgroups.Unified {
// If we're running under cgroupv2 the perf_event controller is always implicitly enabled
// so we don't have to do anything special here.
path := fmt.Sprintf("/sys/fs/cgroup/system.slice/%s/", unit)
return model.LabelSet{agent.CgroupPathLabelName: model.LabelValue(path)}, nil
p := fmt.Sprintf("/sys/fs/cgroup/system.slice/%s/", unit)
return model.LabelSet{agent.CgroupPathLabelName: model.LabelValue(p)}, nil
}

f, err := os.Open(fmt.Sprintf("/sys/fs/cgroup/systemd/system.slice/%s/cgroup.procs", unit))
Expand Down
36 changes: 36 additions & 0 deletions pkg/discovery/systemd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package discovery

import (
"context"
"testing"

"github.com/go-kit/log"
"github.com/parca-dev/parca-agent/pkg/agent"
)

func TestReconcileUnitWithCgroupPath(t *testing.T) {
service := "foobar.service"
conf := NewSystemdConfig([]string{service}, "/sys/fs/cgroup/machine.slice/foobar/")
dopts := DiscovererOptions{
Logger: log.NewNopLogger(),
}
d, err := conf.NewDiscoverer(dopts)
if err != nil {
t.Fatal(err)
}
ls, err := d.(*SystemdDiscoverer).ReconcileUnit(context.TODO(), service)
if err != nil {
t.Fatal(err)
}
if len(ls) != 1 {
t.Fatalf("expected 1 line, got %d", len(ls))
}
path, ok := ls[agent.CgroupPathLabelName]
if !ok {
t.Fatal("expected cgroup path label")
}
expected := "/sys/fs/cgroup/machine.slice/foobar/foobar.service"
if string(path) != expected {
t.Fatalf("expected %q, got %q", expected, path)
}
}

0 comments on commit b02cb76

Please sign in to comment.