forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.go
More file actions
164 lines (135 loc) · 3.71 KB
/
shared.go
File metadata and controls
164 lines (135 loc) · 3.71 KB
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
package main
import (
"context"
"crypto/x509"
"errors"
"fmt"
"sort"
"strconv"
"time"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/google/uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/grafana/tempo/pkg/boundedwaitgroup"
"github.com/grafana/tempo/tempodb/backend"
)
type unifiedBlockMeta struct {
backend.BlockMeta
backend.CompactedBlockMeta
window int64
compacted bool
}
func getMeta(meta *backend.BlockMeta, compactedMeta *backend.CompactedBlockMeta, windowRange time.Duration) unifiedBlockMeta {
if meta != nil {
return unifiedBlockMeta{
BlockMeta: *meta,
window: meta.EndTime.Unix() / int64(windowRange/time.Second),
compacted: false,
}
}
if compactedMeta != nil {
return unifiedBlockMeta{
BlockMeta: compactedMeta.BlockMeta,
CompactedBlockMeta: *compactedMeta,
window: compactedMeta.EndTime.Unix() / int64(windowRange/time.Second),
compacted: true,
}
}
return unifiedBlockMeta{
BlockMeta: backend.BlockMeta{
BlockID: backend.UUID{},
CompactionLevel: 0,
TotalObjects: -1,
},
window: -1,
compacted: false,
}
}
type blockStats struct {
unifiedBlockMeta
}
func loadBucket(r backend.Reader, c backend.Compactor, tenantID string, windowRange time.Duration, includeCompacted bool) ([]blockStats, error) {
blockIDs, compactedBlockIDs, err := r.Blocks(context.Background(), tenantID)
if err != nil {
return nil, err
}
blockIDs = append(blockIDs, compactedBlockIDs...)
fmt.Println("total blocks: ", len(blockIDs))
// Load in parallel
wg := boundedwaitgroup.New(20)
resultsCh := make(chan blockStats, len(blockIDs))
for blockNum, id := range blockIDs {
wg.Add(1)
go func(id2 backend.UUID, blockNum2 int) {
defer wg.Done()
b, err := loadBlock(r, c, tenantID, id2, blockNum2, windowRange, includeCompacted)
if err != nil {
fmt.Println("Error loading block:", id2, err)
return
}
if b != nil {
resultsCh <- *b
}
}(backend.UUID(id), blockNum)
}
wg.Wait()
close(resultsCh)
results := make([]blockStats, 0)
for b := range resultsCh {
results = append(results, b)
}
sort.Slice(results, func(i, j int) bool {
return results[i].EndTime.Before(results[j].EndTime)
})
return results, nil
}
func loadBlock(r backend.Reader, c backend.Compactor, tenantID string, id backend.UUID, blockNum int, windowRange time.Duration, includeCompacted bool) (*blockStats, error) {
fmt.Print(".")
if blockNum%100 == 0 {
fmt.Print(strconv.Itoa(blockNum))
}
meta, err := r.BlockMeta(context.Background(), (uuid.UUID)(id), tenantID)
if errors.Is(err, backend.ErrDoesNotExist) && !includeCompacted {
return nil, nil
} else if err != nil && !errors.Is(err, backend.ErrDoesNotExist) {
return nil, err
}
compactedMeta, err := c.CompactedBlockMeta((uuid.UUID)(id), tenantID)
if err != nil && !errors.Is(err, backend.ErrDoesNotExist) {
return nil, err
}
return &blockStats{
unifiedBlockMeta: getMeta(meta, compactedMeta, windowRange),
}, nil
}
func printAsJSON(pb proto.Message) error {
m := jsonpb.Marshaler{}
traceJSON, err := m.MarshalToString(pb)
if err != nil {
return err
}
fmt.Println(string(traceJSON))
return nil
}
func httpScheme(secure bool) string {
if secure {
return "https"
}
return "http"
}
func grpcTransportCredentials(secure bool) (opt grpc.DialOption, err error) {
var creds credentials.TransportCredentials
if secure {
certPool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
creds = credentials.NewClientTLSFromCert(certPool, "")
} else {
creds = insecure.NewCredentials()
}
return grpc.WithTransportCredentials(creds), nil
}