-
Notifications
You must be signed in to change notification settings - Fork 178
/
uploader.go
121 lines (101 loc) · 2.73 KB
/
uploader.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
package profiler
import (
"context"
"fmt"
"os"
"github.com/rs/zerolog"
"google.golang.org/api/option"
gtransport "google.golang.org/api/transport/grpc"
pb "google.golang.org/genproto/googleapis/devtools/cloudprofiler/v2"
"google.golang.org/grpc"
)
const (
apiAddress = "cloudprofiler.googleapis.com:443"
scope = "https://www.googleapis.com/auth/monitoring.write"
maxMsgSize = 1 << 30 // 1GB
)
type NoopUploader struct{}
func (u *NoopUploader) Upload(ctx context.Context, filename string, pt pb.ProfileType) error {
return nil
}
type Uploader interface {
Upload(ctx context.Context, filename string, pt pb.ProfileType) error
}
type Params struct {
ProjectID string
ChainID string
Role string
Version string
Commit string
Instance string
}
type UploaderImpl struct {
log zerolog.Logger
client pb.ProfilerServiceClient
ProjectId string
Deployment *pb.Deployment
}
func NewUploader(log zerolog.Logger, params Params, opts ...option.ClientOption) (Uploader, error) {
log = log.With().Str("component", "profile_uploader").Logger()
defaultOpts := []option.ClientOption{
option.WithEndpoint(apiAddress),
option.WithScopes(scope),
option.WithUserAgent("OfflinePprofUploader"),
option.WithTelemetryDisabled(),
option.WithGRPCDialOption(
grpc.WithDefaultCallOptions(
grpc.MaxCallSendMsgSize(maxMsgSize),
),
),
}
opts = append(defaultOpts, opts...)
connPool, err := gtransport.DialPool(
context.Background(),
opts...,
)
if err != nil {
return &NoopUploader{}, fmt.Errorf("failed to create connection pool: %w", err)
}
version := fmt.Sprintf("%s-%s", params.Version, params.Commit)
targetName := fmt.Sprintf("%s-%s", params.ChainID, params.Role)
deployment := &pb.Deployment{
ProjectId: params.ProjectID,
Target: targetName,
Labels: map[string]string{
"language": "go",
"version": version,
"instance": params.Instance,
},
}
u := &UploaderImpl{
log: log,
client: pb.NewProfilerServiceClient(connPool),
ProjectId: params.ProjectID,
Deployment: deployment,
}
return u, nil
}
func (u *UploaderImpl) Upload(ctx context.Context, filename string, pt pb.ProfileType) error {
profileBytes, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read profile: %w", err)
}
req := pb.CreateOfflineProfileRequest{
Parent: u.ProjectId,
Profile: &pb.Profile{
ProfileType: pt,
Deployment: u.Deployment,
ProfileBytes: profileBytes,
},
}
resp, err := u.client.CreateOfflineProfile(ctx, &req)
if err != nil {
return fmt.Errorf("failed to create offline profile: %w", err)
}
u.log.Info().
Str("file_name", filename).
Str("profile_name", resp.GetName()).
Int("profile_size", len(profileBytes)).
Msg("uploaded profile")
return nil
}