profiler: readability improvements
@@ -48,7 +48,6 @@ import (
"github.com/golang/protobuf/ptypes"
gax "github.com/googleapis/gax-go"
"golang.org/x/net/context"
- "golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/transport"
@@ -124,20 +123,28 @@ type Config struct {
APIAddr string
}
+// startError represents the error occured during the
+// initializating and starting of the agent.
+var startError error
+
// Start starts a goroutine to collect and upload profiles.
// See package level documentation for details.
func Start(cfg *Config, options ...option.ClientOption) error {
- var err error
startOnce.Do(func() {
+ startError = start(cfg, options...)
+ })
+ return startError
+}
+
+func start(cfg *Config, options ...option.ClientOption) error {
initializeConfig(cfg)
ctx := context.Background()
- var ts oauth2.TokenSource
- ts, err = google.DefaultTokenSource(ctx, scope)
+ ts, err := google.DefaultTokenSource(ctx, scope)
if err != nil {
debugLog("failed to get application default credentials: %v", err)
- return
+ return err
}
opts := []option.ClientOption{
@@ -147,25 +154,21 @@ func Start(cfg *Config, options ...option.ClientOption) error {
}
opts = append(opts, options...)
- var conn *grpc.ClientConn
- conn, err = transport.DialGRPC(ctx, opts...)
+ conn, err := transport.DialGRPC(ctx, opts...)
if err != nil {
debugLog("failed to dial GRPC: %v", err)
- return
+ return err
}
- var d *pb.Deployment
- d, err = initializeDeployment()
+ d, err := initializeDeployment()
if err != nil {
debugLog("failed to initialize deployment: %v", err)
- return
+ return err
}
a, ctx := initializeResources(ctx, conn, d)
go pollProfilerService(ctx, a)
- })
-
- return err
+ return nil
}
func debugLog(format string, e ...interface{}) {
@@ -332,44 +335,39 @@ func (c *client) insertMetadata(ctx context.Context) context.Context {
}
func initializeDeployment() (*pb.Deployment, error) {
- var projectID, instance, zone string
var err error
- if config.ProjectID != "" {
- projectID = config.ProjectID
- } else {
+ projectID := config.ProjectID
+ if projectID == "" {
projectID, err = getProjectID()
if err != nil {
return nil, err
}
}
- if config.InstanceName != "" {
- instance = config.InstanceName
- } else {
+ instance := config.InstanceName
+ if instance == "" {
instance, err = getInstanceName()
if err != nil {
return nil, err
}
}
- if config.ZoneName != "" {
- zone = config.ZoneName
- } else {
+ zone := config.ZoneName
+ if zone == "" {
zone, err = getZone()
if err != nil {
return nil, err
}
}
- labels := make(map[string]string)
- labels[zoneNameLabel] = zone
- labels[instanceLabel] = instance
-
return &pb.Deployment{
ProjectId: projectID,
Target: config.Target,
- Labels: labels,
+ Labels: map[string]string{
+ instanceLabel: instance,
+ zoneNameLabel: zone,
+ },
}, nil
}
