Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit bebc620

Browse files
authored
Merge pull request #354 from grafana/20221024_distributor-validate-timestamp
Set timestamp in profile, if not set
2 parents ab6d06f + 92e0e00 commit bebc620

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/pprof/pprof.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"sort"
99
"sync"
10+
"time"
1011

1112
"github.com/cespare/xxhash/v2"
1213
"github.com/klauspost/compress/gzip"
@@ -188,14 +189,24 @@ func (s *sortedSample) Swap(i, j int) {
188189
s.hashes[i], s.hashes[j] = s.hashes[j], s.hashes[i]
189190
}
190191

192+
var (
193+
currentTime = time.Now
194+
)
195+
191196
// Normalize normalizes the profile by:
192197
// - Removing all duplicate samples (summing their values).
193198
// - Removing redundant profile labels (byte => unique of an allocation site)
194199
// todo: We should reassess if this was a good choice because by merging duplicate stacktrace samples
195200
// we cannot recompute the allocation per site ("bytes") profile label.
196201
// - Removing empty samples.
197202
// - Then remove unused references.
203+
// - Ensure that the profile has a time_nanos set
198204
func (p *Profile) Normalize() {
205+
// if the profile has no time, set it to now
206+
if p.TimeNanos == 0 {
207+
p.TimeNanos = currentTime().UnixNano()
208+
}
209+
199210
// first we sort the samples location ids.
200211
hashes := p.hasher.Hashes(p.Sample)
201212

pkg/pprof/pprof_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pprof
33
import (
44
"math/rand"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/require"
89

@@ -11,6 +12,14 @@ import (
1112
)
1213

1314
func TestNormalizeProfile(t *testing.T) {
15+
currentTime = func() time.Time {
16+
t, _ := time.Parse(time.RFC3339, "2020-01-01T00:00:00Z")
17+
return t
18+
}
19+
defer func() {
20+
currentTime = time.Now
21+
}()
22+
1423
p := &profilev1.Profile{
1524
SampleType: []*profilev1.ValueType{
1625
{Type: 2, Unit: 1},
@@ -78,6 +87,7 @@ func TestNormalizeProfile(t *testing.T) {
7887
},
7988
PeriodType: &profilev1.ValueType{Type: 0, Unit: 1},
8089
Comment: []int64{},
90+
TimeNanos: 1577836800000000000,
8191
DefaultSampleType: 0,
8292
})
8393
}

0 commit comments

Comments
 (0)