Skip to content

Commit deeb583

Browse files
authored
fix(profiler): migrate to protobuf-go v2 (#8730)
Refactors all protobuf-go v1 references in `cloud.google.com/go/profiler` to protobuf-go v2. This does remove a number of error scenarios and I'm not sure if those are actually real or not, I am just doing an in place refactor. Updates #8585
1 parent 2077158 commit deeb583

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

profiler/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ require (
77
cloud.google.com/go/compute/metadata v0.2.3
88
cloud.google.com/go/storage v1.30.1
99
github.com/golang/mock v1.6.0
10-
github.com/golang/protobuf v1.5.3
1110
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751
1211
github.com/googleapis/gax-go/v2 v2.12.0
1312
golang.org/x/oauth2 v0.8.0
1413
google.golang.org/api v0.128.0
1514
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc
1615
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
1716
google.golang.org/grpc v1.56.1
17+
google.golang.org/protobuf v1.31.0
1818
)
1919

2020
require (
2121
cloud.google.com/go/compute v1.19.3 // indirect
2222
cloud.google.com/go/iam v0.13.0 // indirect
2323
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
24+
github.com/golang/protobuf v1.5.3 // indirect
2425
github.com/google/go-cmp v0.5.9 // indirect
2526
github.com/google/s2a-go v0.1.4 // indirect
2627
github.com/google/uuid v1.3.0 // indirect
@@ -33,5 +34,4 @@ require (
3334
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
3435
google.golang.org/appengine v1.6.7 // indirect
3536
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
36-
google.golang.org/protobuf v1.31.0 // indirect
3737
)

profiler/profiler.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ import (
5252
gcemd "cloud.google.com/go/compute/metadata"
5353
"cloud.google.com/go/internal/version"
5454
"cloud.google.com/go/profiler/internal"
55-
"github.com/golang/protobuf/proto"
56-
"github.com/golang/protobuf/ptypes"
5755
"github.com/google/pprof/profile"
5856
gax "github.com/googleapis/gax-go/v2"
5957
"google.golang.org/api/option"
@@ -64,6 +62,7 @@ import (
6462
"google.golang.org/grpc/codes"
6563
grpcmd "google.golang.org/grpc/metadata"
6664
"google.golang.org/grpc/status"
65+
"google.golang.org/protobuf/proto"
6766
)
6867

6968
var (
@@ -297,14 +296,13 @@ func abortedBackoffDuration(md grpcmd.MD) (time.Duration, error) {
297296
var retryInfo edpb.RetryInfo
298297
if err := proto.Unmarshal([]byte(elem[0]), &retryInfo); err != nil {
299298
return 0, err
300-
} else if time, err := ptypes.Duration(retryInfo.RetryDelay); err != nil {
301-
return 0, err
302-
} else {
303-
if time < 0 {
304-
return 0, errors.New("negative retry duration")
305-
}
306-
return time, nil
307299
}
300+
301+
time := retryInfo.RetryDelay.AsDuration()
302+
if time < 0 {
303+
return 0, errors.New("negative retry duration")
304+
}
305+
return time, nil
308306
}
309307

310308
type retryer struct {
@@ -386,11 +384,7 @@ func (a *agent) profileAndUpload(ctx context.Context, p *pb.Profile) {
386384

387385
switch pt {
388386
case pb.ProfileType_CPU:
389-
duration, err := ptypes.Duration(p.Duration)
390-
if err != nil {
391-
debugLog("failed to get profile duration for CPU profile: %v", err)
392-
return
393-
}
387+
duration := p.Duration.AsDuration()
394388
if err := startCPUProfile(&prof); err != nil {
395389
debugLog("failed to start CPU profile: %v", err)
396390
return
@@ -403,11 +397,7 @@ func (a *agent) profileAndUpload(ctx context.Context, p *pb.Profile) {
403397
return
404398
}
405399
case pb.ProfileType_HEAP_ALLOC:
406-
duration, err := ptypes.Duration(p.Duration)
407-
if err != nil {
408-
debugLog("failed to get profile duration for allocation profile: %v", err)
409-
return
410-
}
400+
duration := p.Duration.AsDuration()
411401
if err := deltaAllocProfile(ctx, duration, config.AllocForceGC, &prof); err != nil {
412402
debugLog("failed to collect allocation profile: %v", err)
413403
return
@@ -418,11 +408,7 @@ func (a *agent) profileAndUpload(ctx context.Context, p *pb.Profile) {
418408
return
419409
}
420410
case pb.ProfileType_CONTENTION:
421-
duration, err := ptypes.Duration(p.Duration)
422-
if err != nil {
423-
debugLog("failed to get profile duration: %v", err)
424-
return
425-
}
411+
duration := p.Duration.AsDuration()
426412
if err := deltaMutexProfile(ctx, duration, &prof); err != nil {
427413
debugLog("failed to collect mutex profile: %v", err)
428414
return

profiler/profiler_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import (
3535
"cloud.google.com/go/profiler/mocks"
3636
"cloud.google.com/go/profiler/testdata"
3737
"github.com/golang/mock/gomock"
38-
"github.com/golang/protobuf/proto"
39-
"github.com/golang/protobuf/ptypes"
4038
"github.com/google/pprof/profile"
4139
gax "github.com/googleapis/gax-go/v2"
4240
"google.golang.org/api/option"
@@ -47,6 +45,8 @@ import (
4745
"google.golang.org/grpc/codes"
4846
grpcmd "google.golang.org/grpc/metadata"
4947
"google.golang.org/grpc/status"
48+
"google.golang.org/protobuf/proto"
49+
"google.golang.org/protobuf/types/known/durationpb"
5050
)
5151

5252
const (
@@ -82,7 +82,7 @@ func createTestAgent(psc pb.ProfilerServiceClient) *agent {
8282

8383
func createTrailers(dur time.Duration) map[string]string {
8484
b, _ := proto.Marshal(&edpb.RetryInfo{
85-
RetryDelay: ptypes.DurationProto(dur),
85+
RetryDelay: durationpb.New(dur),
8686
})
8787
return map[string]string{
8888
retryInfoMetadata: string(b),
@@ -238,7 +238,7 @@ func TestProfileAndUpload(t *testing.T) {
238238
}
239239
p := &pb.Profile{ProfileType: tt.profileType}
240240
if tt.duration != nil {
241-
p.Duration = ptypes.DurationProto(*tt.duration)
241+
p.Duration = durationpb.New(*tt.duration)
242242
}
243243
if tt.wantBytes != nil {
244244
wantProfile := &pb.Profile{
@@ -777,7 +777,7 @@ func (fs *fakeProfilerServer) CreateProfile(ctx context.Context, in *pb.CreatePr
777777
fs.count++
778778
switch fs.count % 2 {
779779
case 1:
780-
return &pb.Profile{Name: "testCPU", ProfileType: pb.ProfileType_CPU, Duration: ptypes.DurationProto(testProfileDuration)}, nil
780+
return &pb.Profile{Name: "testCPU", ProfileType: pb.ProfileType_CPU, Duration: durationpb.New(testProfileDuration)}, nil
781781
default:
782782
return &pb.Profile{Name: "testHeap", ProfileType: pb.ProfileType_HEAP}, nil
783783
}

0 commit comments

Comments
 (0)