Skip to content

Commit

Permalink
fix(profiler): migrate to protobuf-go v2 (#8730)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
noahdietz committed Oct 16, 2023
1 parent 2077158 commit deeb583
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
4 changes: 2 additions & 2 deletions profiler/go.mod
Expand Up @@ -7,20 +7,21 @@ require (
cloud.google.com/go/compute/metadata v0.2.3
cloud.google.com/go/storage v1.30.1
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751
github.com/googleapis/gax-go/v2 v2.12.0
golang.org/x/oauth2 v0.8.0
google.golang.org/api v0.128.0
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.31.0
)

require (
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -33,5 +34,4 @@ require (
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
34 changes: 10 additions & 24 deletions profiler/profiler.go
Expand Up @@ -52,8 +52,6 @@ import (
gcemd "cloud.google.com/go/compute/metadata"
"cloud.google.com/go/internal/version"
"cloud.google.com/go/profiler/internal"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/pprof/profile"
gax "github.com/googleapis/gax-go/v2"
"google.golang.org/api/option"
Expand All @@ -64,6 +62,7 @@ import (
"google.golang.org/grpc/codes"
grpcmd "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

var (
Expand Down Expand Up @@ -297,14 +296,13 @@ func abortedBackoffDuration(md grpcmd.MD) (time.Duration, error) {
var retryInfo edpb.RetryInfo
if err := proto.Unmarshal([]byte(elem[0]), &retryInfo); err != nil {
return 0, err
} else if time, err := ptypes.Duration(retryInfo.RetryDelay); err != nil {
return 0, err
} else {
if time < 0 {
return 0, errors.New("negative retry duration")
}
return time, nil
}

time := retryInfo.RetryDelay.AsDuration()
if time < 0 {
return 0, errors.New("negative retry duration")
}
return time, nil
}

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

switch pt {
case pb.ProfileType_CPU:
duration, err := ptypes.Duration(p.Duration)
if err != nil {
debugLog("failed to get profile duration for CPU profile: %v", err)
return
}
duration := p.Duration.AsDuration()
if err := startCPUProfile(&prof); err != nil {
debugLog("failed to start CPU profile: %v", err)
return
Expand All @@ -403,11 +397,7 @@ func (a *agent) profileAndUpload(ctx context.Context, p *pb.Profile) {
return
}
case pb.ProfileType_HEAP_ALLOC:
duration, err := ptypes.Duration(p.Duration)
if err != nil {
debugLog("failed to get profile duration for allocation profile: %v", err)
return
}
duration := p.Duration.AsDuration()
if err := deltaAllocProfile(ctx, duration, config.AllocForceGC, &prof); err != nil {
debugLog("failed to collect allocation profile: %v", err)
return
Expand All @@ -418,11 +408,7 @@ func (a *agent) profileAndUpload(ctx context.Context, p *pb.Profile) {
return
}
case pb.ProfileType_CONTENTION:
duration, err := ptypes.Duration(p.Duration)
if err != nil {
debugLog("failed to get profile duration: %v", err)
return
}
duration := p.Duration.AsDuration()
if err := deltaMutexProfile(ctx, duration, &prof); err != nil {
debugLog("failed to collect mutex profile: %v", err)
return
Expand Down
10 changes: 5 additions & 5 deletions profiler/profiler_test.go
Expand Up @@ -35,8 +35,6 @@ import (
"cloud.google.com/go/profiler/mocks"
"cloud.google.com/go/profiler/testdata"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/pprof/profile"
gax "github.com/googleapis/gax-go/v2"
"google.golang.org/api/option"
Expand All @@ -47,6 +45,8 @@ import (
"google.golang.org/grpc/codes"
grpcmd "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
)

const (
Expand Down Expand Up @@ -82,7 +82,7 @@ func createTestAgent(psc pb.ProfilerServiceClient) *agent {

func createTrailers(dur time.Duration) map[string]string {
b, _ := proto.Marshal(&edpb.RetryInfo{
RetryDelay: ptypes.DurationProto(dur),
RetryDelay: durationpb.New(dur),
})
return map[string]string{
retryInfoMetadata: string(b),
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestProfileAndUpload(t *testing.T) {
}
p := &pb.Profile{ProfileType: tt.profileType}
if tt.duration != nil {
p.Duration = ptypes.DurationProto(*tt.duration)
p.Duration = durationpb.New(*tt.duration)
}
if tt.wantBytes != nil {
wantProfile := &pb.Profile{
Expand Down Expand Up @@ -777,7 +777,7 @@ func (fs *fakeProfilerServer) CreateProfile(ctx context.Context, in *pb.CreatePr
fs.count++
switch fs.count % 2 {
case 1:
return &pb.Profile{Name: "testCPU", ProfileType: pb.ProfileType_CPU, Duration: ptypes.DurationProto(testProfileDuration)}, nil
return &pb.Profile{Name: "testCPU", ProfileType: pb.ProfileType_CPU, Duration: durationpb.New(testProfileDuration)}, nil
default:
return &pb.Profile{Name: "testHeap", ProfileType: pb.ProfileType_HEAP}, nil
}
Expand Down

0 comments on commit deeb583

Please sign in to comment.