Skip to content

Commit 35dcd72

Browse files
kalyanacaalexand
andauthored
fix(profiler): Force gax to retry in case of certificate errors (#3178)
* fix(profiler): force gax.Invoke() to retry cert errors Co-authored-by: Alexey Alexandrov <aalexand@users.noreply.github.com>
1 parent b75df35 commit 35dcd72

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

profiler/profiler.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"regexp"
4545
"runtime"
4646
"runtime/pprof"
47+
"strings"
4748
"sync"
4849
"time"
4950

@@ -67,6 +68,7 @@ var (
6768
config Config
6869
startOnce allowUntilSuccess
6970
mutexEnabled bool
71+
logger *log.Logger
7072
// The functions below are stubbed to be overrideable for testing.
7173
getProjectID = gcemd.ProjectID
7274
getInstanceName = gcemd.InstanceName
@@ -222,6 +224,7 @@ func Start(cfg Config, options ...option.ClientOption) error {
222224
}
223225

224226
func start(cfg Config, options ...option.ClientOption) error {
227+
logger = log.New(os.Stderr, "Cloud Profiler: ", log.LstdFlags)
225228
if err := initializeConfig(cfg); err != nil {
226229
debugLog("failed to initialize config: %v", err)
227230
return err
@@ -261,7 +264,7 @@ func start(cfg Config, options ...option.ClientOption) error {
261264

262265
func debugLog(format string, e ...interface{}) {
263266
if config.DebugLogging {
264-
log.Printf(format, e...)
267+
logger.Printf(format, e...)
265268
}
266269
}
267270

@@ -315,7 +318,8 @@ func (r *retryer) Retry(err error) (time.Duration, bool) {
315318
// createProfile talks to the profiler server to create profile. In
316319
// case of error, the goroutine will sleep and retry. Sleep duration may
317320
// be specified by the server. Otherwise it will be an exponentially
318-
// increasing value, bounded by maxBackoff.
321+
// increasing value, bounded by maxBackoff. Special handling for
322+
// certificate errors is described below.
319323
func (a *agent) createProfile(ctx context.Context) *pb.Profile {
320324
req := pb.CreateProfileRequest{
321325
Parent: "projects/" + a.deployment.ProjectId,
@@ -332,6 +336,11 @@ func (a *agent) createProfile(ctx context.Context) *pb.Profile {
332336
p, err = a.client.CreateProfile(ctx, &req, grpc.Trailer(&md))
333337
if err != nil {
334338
debugLog("failed to create profile, will retry: %v", err)
339+
if strings.Contains(err.Error(), "x509: certificate signed by unknown authority") {
340+
// gax.Invoke does not retry missing certificate error. Force a retry by returning
341+
// a different error. See https://github.com/googleapis/google-cloud-go/issues/3158.
342+
err = errors.New("retry the certificate error")
343+
}
335344
}
336345
return err
337346
}, gax.WithRetry(func() gax.Retryer {

0 commit comments

Comments
 (0)