Skip to content

Commit

Permalink
Add external labels to all profiles (opensearch-project#28)
Browse files Browse the repository at this point in the history
* Add external labels to all profiles

Add support for an "--external-label" flag to enable
attaching multiple user-defined flags to all profiles.

Example usage:
	dist/parca-agent --external-label=key1=value1 --external-label=key1=value2

Also add documentation for --external-label flag

Fixes opensearch-project#6

Signed-off-by: Sumera Priyadarsini <[email protected]>
  • Loading branch information
Sylfrena authored Sep 16, 2021
1 parent 62dec5a commit b026df4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ Flags:
--node=STRING Name node the process is running on. If on
Kubernetes, this must match the Kubernetes node
name.
--external-label=KEY=VALUE;...
Label(s) to attach to all profiles.
--store-address=STRING gRPC address to send profiles and symbols to.
--bearer-token=STRING Bearer token to authenticate with store.
--bearer-token-file=STRING
Expand Down
29 changes: 16 additions & 13 deletions cmd/parca-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ import (
)

type flags struct {
LogLevel string `kong:"enum='error,warn,info,debug',help='Log level.',default='info'"`
HttpAddress string `kong:"help='Address to bind HTTP server to.',default=':7071'"`
Node string `kong:"required,help='Name node the process is running on. If on Kubernetes, this must match the Kubernetes node name.'"`
StoreAddress string `kong:"help='gRPC address to send profiles and symbols to.'"`
BearerToken string `kong:"help='Bearer token to authenticate with store.'"`
BearerTokenFile string `kong:"help='File to read bearer token from to authenticate with store.'"`
Insecure bool `kong:"help='Send gRPC requests via plaintext instead of TLS.'"`
InsecureSkipVerify bool `kong:"help='Skip TLS certificate verification.'"`
SamplingRatio float64 `kong:"help='Sampling ratio to control how many of the discovered targets to profile. Defaults to 1.0, which is all.',default='1.0'"`
Kubernetes bool `kong:"help='Discover containers running on this node to profile automatically.',default='true'"`
PodLabelSelector string `kong:"help='Label selector to control which Kubernetes Pods to select.'"`
SystemdUnits []string `kong:"help='SystemD units to profile on this node.'"`
TempDir string `kong:"help='Temporary directory path to use for object files.',default='/tmp'"`
LogLevel string `kong:"enum='error,warn,info,debug',help='Log level.',default='info'"`
HttpAddress string `kong:"help='Address to bind HTTP server to.',default=':7071'"`
Node string `kong:"required,help='Name node the process is running on. If on Kubernetes, this must match the Kubernetes node name.'"`
ExternalLabel map[string]string `kong:"help='Label(s) to attach to all profiles.'"`
StoreAddress string `kong:"help='gRPC address to send profiles and symbols to.'"`
BearerToken string `kong:"help='Bearer token to authenticate with store.'"`
BearerTokenFile string `kong:"help='File to read bearer token from to authenticate with store.'"`
Insecure bool `kong:"help='Send gRPC requests via plaintext instead of TLS.'"`
InsecureSkipVerify bool `kong:"help='Skip TLS certificate verification.'"`
SamplingRatio float64 `kong:"help='Sampling ratio to control how many of the discovered targets to profile. Defaults to 1.0, which is all.',default='1.0'"`
Kubernetes bool `kong:"help='Discover containers running on this node to profile automatically.',default='true'"`
PodLabelSelector string `kong:"help='Label selector to control which Kubernetes Pods to select.'"`
SystemdUnits []string `kong:"help='SystemD units to profile on this node.'"`
TempDir string `kong:"help='Temporary directory path to use for object files.',default='/tmp'"`
}

func main() {
Expand Down Expand Up @@ -102,6 +103,7 @@ func main() {
if flags.Kubernetes {
pm, err = agent.NewPodManager(
logger,
flags.ExternalLabel,
node,
flags.PodLabelSelector,
flags.SamplingRatio,
Expand All @@ -123,6 +125,7 @@ func main() {
node,
flags.SystemdUnits,
flags.SamplingRatio,
flags.ExternalLabel,
ksymCache,
wc,
dc,
Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/podmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
type PodManager struct {
logger log.Logger

externalLabels map[string]string

// node where this instance is running
nodeName string
ksymCache *ksym.KsymCache
Expand Down Expand Up @@ -97,6 +99,7 @@ func (g *PodManager) Run(ctx context.Context) error {
logger := log.With(g.logger, "namespace", container.Namespace, "pod", container.PodName, "container", container.ContainerName)
containerProfiler := NewCgroupProfiler(
logger,
g.externalLabels,
g.ksymCache,
g.writeClient,
g.debugInfoClient,
Expand Down Expand Up @@ -160,6 +163,7 @@ func (g *PodManager) Run(ctx context.Context) error {

func NewPodManager(
logger log.Logger,
externalLabels map[string]string,
nodeName string,
podLabelSelector string,
samplingRatio float64,
Expand All @@ -182,6 +186,7 @@ func NewPodManager(
}
g := &PodManager{
logger: logger,
externalLabels: externalLabels,
nodeName: nodeName,
samplingRatio: samplingRatio,
ksymCache: ksymCache,
Expand Down
30 changes: 21 additions & 9 deletions pkg/agent/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ type DebugInfoClient interface {
}

type CgroupProfiler struct {
logger log.Logger
ksymCache *ksym.KsymCache
target CgroupProfilingTarget
sink func(Record)
cancel func()
logger log.Logger
externalLabels map[string]string
ksymCache *ksym.KsymCache
target CgroupProfilingTarget
sink func(Record)
cancel func()

pidMappingFileCache *maps.PidMappingFileCache
writeClient profilestorepb.ProfileStoreServiceClient
Expand All @@ -117,6 +118,7 @@ type CgroupProfiler struct {

func NewCgroupProfiler(
logger log.Logger,
externalLabels map[string]string,
ksymCache *ksym.KsymCache,
writeClient profilestorepb.ProfileStoreServiceClient,
debugInfoClient DebugInfoClient,
Expand All @@ -126,6 +128,7 @@ func NewCgroupProfiler(
) *CgroupProfiler {
return &CgroupProfiler{
logger: logger,
externalLabels: externalLabels,
ksymCache: ksymCache,
target: target,
sink: sink,
Expand Down Expand Up @@ -164,10 +167,19 @@ func (p *CgroupProfiler) Stop() {
}

func (p *CgroupProfiler) Labels() []*profilestorepb.Label {
return append(p.target.Labels(), &profilestorepb.Label{
Name: "__name__",
Value: "cpu",
})
labels := append(p.target.Labels(),
&profilestorepb.Label{
Name: "__name__",
Value: "cpu",
})
for key, value := range p.externalLabels {
labels = append(labels, &profilestorepb.Label{
Name: key,
Value: value,
})
}

return labels
}

func (p *CgroupProfiler) Run(ctx context.Context) error {
Expand Down
4 changes: 4 additions & 0 deletions pkg/agent/systemdmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SystemdManager struct {
logger log.Logger
nodeName string
samplingRatio float64
externalLabels map[string]string
ksymCache *ksym.KsymCache
writeClient profilestorepb.ProfileStoreServiceClient
debugInfoClient DebugInfoClient
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewSystemdManager(
nodeName string,
units []string,
samplingRatio float64,
externalLabels map[string]string,
ksymCache *ksym.KsymCache,
writeClient profilestorepb.ProfileStoreServiceClient,
debugInfoClient DebugInfoClient,
Expand All @@ -85,6 +87,7 @@ func NewSystemdManager(
logger: logger,
nodeName: nodeName,
samplingRatio: samplingRatio,
externalLabels: externalLabels,
ksymCache: ksymCache,
writeClient: writeClient,
debugInfoClient: debugInfoClient,
Expand Down Expand Up @@ -181,6 +184,7 @@ func (m *SystemdManager) reconcileUnit(ctx context.Context, unit string) error {
logger := log.With(m.logger, "systemdunit", unit)
p := NewCgroupProfiler(
logger,
m.externalLabels,
m.ksymCache,
m.writeClient,
m.debugInfoClient,
Expand Down

0 comments on commit b026df4

Please sign in to comment.