Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 8 additions & 7 deletions reporter/internal/pdata/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package pdata // import "go.opentelemetry.io/ebpf-profiler/reporter/internal/pda

import (
"fmt"
"math"
"path/filepath"
"slices"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -129,14 +129,15 @@ func (p *Pdata) setProfile(
attrMgr := samples.NewAttrTableManager(dic.AttributeTable())

locationIndex := int32(profile.LocationIndices().Len())
var startTS, endTS pcommon.Timestamp
startTS, endTS := uint64(math.MaxUint64), uint64(0)
for traceKey, traceInfo := range events {
sample := profile.Sample().AppendEmpty()
sample.SetLocationsStartIndex(locationIndex)

slices.Sort(traceInfo.Timestamps)
startTS = pcommon.Timestamp(traceInfo.Timestamps[0])
endTS = pcommon.Timestamp(traceInfo.Timestamps[len(traceInfo.Timestamps)-1])
for _, ts := range traceInfo.Timestamps {
startTS = min(startTS, ts)
endTS = max(endTS, ts)
}
sample.TimestampsUnixNano().FromRaw(traceInfo.Timestamps)

switch origin {
Expand Down Expand Up @@ -283,8 +284,8 @@ func (p *Pdata) setProfile(

log.Debugf("Reporting OTLP profile with %d samples", profile.Sample().Len())

profile.SetDuration(endTS - startTS)
profile.SetStartTime(startTS)
profile.SetDuration(pcommon.Timestamp(endTS - startTS))
profile.SetStartTime(pcommon.Timestamp(startTS))

return nil
}
36 changes: 36 additions & 0 deletions reporter/internal/pdata/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pprofile"

semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
Expand Down Expand Up @@ -217,3 +218,38 @@ func TestFunctionTableOrder(t *testing.T) {
})
}
}

func TestProfileDuration(t *testing.T) {
Comment thread
tsint marked this conversation as resolved.
for _, tt := range []struct {
name string
events map[libpf.Origin]samples.KeyToEventMapping
}{
{
name: "profile duration",
events: map[libpf.Origin]samples.KeyToEventMapping{
support.TraceOriginSampling: map[samples.TraceAndMetaKey]*samples.TraceEvents{
{Pid: 1}: {
Timestamps: []uint64{2, 1, 3, 4, 7},
},
{Pid: 2}: {
Timestamps: []uint64{8},
},
},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
d, err := New(100, 100, 100, nil)
require.NoError(t, err)

tree := make(samples.TraceEventsTree)
tree[""] = tt.events
res, err := d.Generate(tree, tt.name, "version")
require.NoError(t, err)

profile := res.ResourceProfiles().At(0).ScopeProfiles().At(0).Profiles().At(0)
require.Equal(t, pcommon.Timestamp(7), profile.Duration())
require.Equal(t, pcommon.Timestamp(1), profile.StartTime())
})
}
}