-
Notifications
You must be signed in to change notification settings - Fork 585
/
sample_merge.go
197 lines (180 loc) · 6.16 KB
/
sample_merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package phlaredb
import (
"context"
"sort"
"strings"
"github.com/google/pprof/profile"
"github.com/grafana/dskit/runutil"
"github.com/opentracing/opentracing-go"
"github.com/parquet-go/parquet-go"
"github.com/prometheus/common/model"
"github.com/samber/lo"
typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1"
"github.com/grafana/pyroscope/pkg/iter"
phlaremodel "github.com/grafana/pyroscope/pkg/model"
"github.com/grafana/pyroscope/pkg/phlaredb/query"
v1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
"github.com/grafana/pyroscope/pkg/phlaredb/symdb"
)
func (b *singleBlockQuerier) MergeByStacktraces(ctx context.Context, rows iter.Iterator[Profile]) (*phlaremodel.Tree, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByStacktraces - Block")
defer sp.Finish()
sp.SetTag("block ULID", b.meta.ULID.String())
r := symdb.NewResolver(ctx, b.symbols)
defer r.Release()
if err := mergeByStacktraces(ctx, b.profiles.file, rows, r); err != nil {
return nil, err
}
return r.Tree()
}
func (b *singleBlockQuerier) MergePprof(ctx context.Context, rows iter.Iterator[Profile]) (*profile.Profile, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergePprof - Block")
defer sp.Finish()
sp.SetTag("block ULID", b.meta.ULID.String())
r := symdb.NewResolver(ctx, b.symbols)
defer r.Release()
if err := mergeByStacktraces(ctx, b.profiles.file, rows, r); err != nil {
return nil, err
}
return r.Profile()
}
func (b *singleBlockQuerier) MergeByLabels(ctx context.Context, rows iter.Iterator[Profile], by ...string) ([]*typesv1.Series, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "MergeByLabels - Block")
defer sp.Finish()
m := make(seriesByLabels)
columnName := "TotalValue"
if b.meta.Version == 1 {
columnName = "Samples.list.element.Value"
}
if err := mergeByLabels(ctx, b.profiles.file, columnName, rows, m, by...); err != nil {
return nil, err
}
return m.normalize(), nil
}
func (b *singleBlockQuerier) MergeBySpans(ctx context.Context, rows iter.Iterator[Profile], spanSelector phlaremodel.SpanSelector) (*phlaremodel.Tree, error) {
sp, _ := opentracing.StartSpanFromContext(ctx, "MergeBySpans - Block")
defer sp.Finish()
r := symdb.NewResolver(ctx, b.symbols)
defer r.Release()
if err := mergeBySpans(ctx, b.profiles.file, rows, r, spanSelector); err != nil {
return nil, err
}
return r.Tree()
}
type Source interface {
Schema() *parquet.Schema
RowGroups() []parquet.RowGroup
}
func mergeByStacktraces(ctx context.Context, profileSource Source, rows iter.Iterator[Profile], r *symdb.Resolver) (err error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "mergeByStacktraces")
defer sp.Finish()
var columns v1.SampleColumns
if err = columns.Resolve(profileSource.Schema()); err != nil {
return err
}
profiles := query.NewRepeatedRowIterator(ctx, rows, profileSource.RowGroups(),
columns.StacktraceID.ColumnIndex,
columns.Value.ColumnIndex,
)
defer runutil.CloseWithErrCapture(&err, profiles, "failed to close profile stream")
for profiles.Next() {
p := profiles.At()
partition := r.Partition(p.Row.StacktracePartition())
stacktraces := p.Values[0]
values := p.Values[1]
for i, sid := range stacktraces {
partition[sid.Uint32()] += values[i].Int64()
}
}
return profiles.Err()
}
func mergeBySpans(ctx context.Context, profileSource Source, rows iter.Iterator[Profile], r *symdb.Resolver, spanSelector phlaremodel.SpanSelector) (err error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "mergeBySpans")
defer sp.Finish()
var columns v1.SampleColumns
if err = columns.Resolve(profileSource.Schema()); err != nil {
return err
}
if !columns.HasSpanID() {
return nil
}
profiles := query.NewRepeatedRowIterator(ctx, rows, profileSource.RowGroups(),
columns.StacktraceID.ColumnIndex,
columns.Value.ColumnIndex,
columns.SpanID.ColumnIndex,
)
defer runutil.CloseWithErrCapture(&err, profiles, "failed to close profile stream")
for profiles.Next() {
p := profiles.At()
partition := r.Partition(p.Row.StacktracePartition())
stacktraces := p.Values[0]
values := p.Values[1]
spans := p.Values[2]
for i, sid := range stacktraces {
spanID := spans[i].Uint64()
if spanID == 0 {
continue
}
if _, ok := spanSelector[spanID]; ok {
partition[sid.Uint32()] += values[i].Int64()
}
}
}
return profiles.Err()
}
type seriesByLabels map[string]*typesv1.Series
func (m seriesByLabels) normalize() []*typesv1.Series {
result := lo.Values(m)
sort.Slice(result, func(i, j int) bool {
return phlaremodel.CompareLabelPairs(result[i].Labels, result[j].Labels) < 0
})
// we have to sort the points in each series because labels reduction may have changed the order
for _, s := range result {
sort.Slice(s.Points, func(i, j int) bool {
return s.Points[i].Timestamp < s.Points[j].Timestamp
})
}
return result
}
func mergeByLabels(ctx context.Context, profileSource Source, columnName string, rows iter.Iterator[Profile], m seriesByLabels, by ...string) (err error) {
column, err := v1.ResolveColumnByPath(profileSource.Schema(), strings.Split(columnName, "."))
if err != nil {
return err
}
profiles := query.NewRepeatedRowIterator(ctx, rows, profileSource.RowGroups(), column.ColumnIndex)
defer runutil.CloseWithErrCapture(&err, profiles, "failed to close profile stream")
labelsByFingerprint := map[model.Fingerprint]string{}
labelBuf := make([]byte, 0, 1024)
for profiles.Next() {
values := profiles.At()
p := values.Row
var total int64
for _, e := range values.Values {
total += e[0].Int64()
}
labelsByString, ok := labelsByFingerprint[p.Fingerprint()]
if !ok {
labelBuf = p.Labels().BytesWithLabels(labelBuf, by...)
labelsByString = string(labelBuf)
labelsByFingerprint[p.Fingerprint()] = labelsByString
if _, ok := m[labelsByString]; !ok {
m[labelsByString] = &typesv1.Series{
Labels: p.Labels().WithLabels(by...),
Points: []*typesv1.Point{
{
Timestamp: int64(p.Timestamp()),
Value: float64(total),
},
},
}
continue
}
}
series := m[labelsByString]
series.Points = append(series.Points, &typesv1.Point{
Timestamp: int64(p.Timestamp()),
Value: float64(total),
})
}
return profiles.Err()
}