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

Add exe path metadata #1

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all 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
32 changes: 24 additions & 8 deletions reporter/datadog_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ type traceFramesCounts struct {

}

type processMetadata struct {
execPath string
}

// DatadogReporter receives and transforms information to be OTLP/profiles compliant.
type DatadogReporter struct {
// name is the ScopeProfile's name.
Expand Down Expand Up @@ -107,7 +111,7 @@ type DatadogReporter struct {
traceEvents xsync.RWMutex[map[traceAndMetaKey]*traceFramesCounts]

// execPathes stores the last known execPath for a PID.
execPathes *lru.SyncedLRU[libpf.PID, string]
processes *lru.SyncedLRU[libpf.PID, processMetadata]

// samplesPerSecond is the number of samples per second.
samplesPerSecond int
Expand Down Expand Up @@ -136,6 +140,13 @@ func (r *DatadogReporter) ReportTraceEvent(trace *libpf.Trace, meta *reporter.Tr
traceEvents := r.traceEvents.WLock()
defer r.traceEvents.WUnlock(&traceEvents)

if _, ok := r.processes.Get(meta.PID); !ok {
processMeta, err := processMetadataFromPID(meta.PID)
if err != nil {
log.Debugf("Failed to get process metadata for PID %d: %v", meta.PID, err)
}
r.processes.Add(meta.PID, processMeta)
}
// FIXME{DD}
// containerID, err := r.lookupCgroupv2(meta.PID)
// if err != nil {
Expand Down Expand Up @@ -207,10 +218,6 @@ func (r *DatadogReporter) ExecutableMetadata(fileID libpf.FileID, fileName, buil
})
}

func (r *DatadogReporter) ProcessMetadata(_ context.Context, pid libpf.PID, execPath string) {
r.execPathes.Add(pid, execPath)
}

// FrameMetadata accepts metadata associated with a frame and caches this information.
func (r *DatadogReporter) FrameMetadata(fileID libpf.FileID, addressOrLine libpf.AddressOrLineno,
lineNumber util.SourceLineno, functionOffset uint32, functionName, filePath string) {
Expand Down Expand Up @@ -297,7 +304,7 @@ func Start(mainCtx context.Context, cfg *Config) (reporter.Reporter, error) {
return nil, err
}

execPathes, err := lru.NewSynced[libpf.PID, string](cfg.CacheSize, libpf.PID.Hash32)
processes, err := lru.NewSynced[libpf.PID, processMetadata](cfg.CacheSize, libpf.PID.Hash32)
if err != nil {
return nil, err
}
Expand All @@ -324,7 +331,7 @@ func Start(mainCtx context.Context, cfg *Config) (reporter.Reporter, error) {
frames: frames,
hostmetadata: hostmetadata,
traceEvents: xsync.NewRWMutex(map[traceAndMetaKey]*traceFramesCounts{}),
execPathes: execPathes,
processes: processes,
agentAddr: cfg.CollAgentAddr,
saveCPUProfile: cfg.SaveCPUProfile,
tags: cfg.Tags,
Expand Down Expand Up @@ -559,7 +566,8 @@ func (r *DatadogReporter) getPprofProfile() (profile *pprofile.Profile,
sample.Location = append(sample.Location, loc)
}

execPath, _ := r.execPathes.Get(traceAndMetaKey.pid)
processMeta, _ := r.processes.Get(traceAndMetaKey.pid)
execPath := processMeta.execPath

// Check if the last frame is a kernel frame.
if len(traceInfo.frameTypes) > 0 &&
Expand Down Expand Up @@ -691,3 +699,11 @@ func createPprofMapping(profile *pprofile.Profile, offset uint64,
profile.Mapping = append(profile.Mapping, mapping)
return mapping
}

func processMetadataFromPID(pid libpf.PID) (processMetadata, error) {
execPath, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
if err != nil {
return processMetadata{}, err
}
return processMetadata{execPath: execPath}, nil
}