...

Source file src/google.golang.org/grpc/profiling/cmd/remote.go

Documentation: google.golang.org/grpc/profiling/cmd

     1  /*
     2   *
     3   * Copyright 2019 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"encoding/gob"
    24  	"fmt"
    25  	"os"
    26  	"time"
    27  
    28  	"google.golang.org/grpc"
    29  	"google.golang.org/grpc/credentials/insecure"
    30  	ppb "google.golang.org/grpc/profiling/proto"
    31  )
    32  
    33  func setEnabled(ctx context.Context, c ppb.ProfilingClient, enabled bool) error {
    34  	_, err := c.Enable(ctx, &ppb.EnableRequest{Enabled: enabled})
    35  	if err != nil {
    36  		logger.Infof("error calling Enable: %v\n", err)
    37  		return err
    38  	}
    39  
    40  	logger.Infof("successfully set enabled = %v", enabled)
    41  	return nil
    42  }
    43  
    44  func retrieveSnapshot(ctx context.Context, c ppb.ProfilingClient, f string) error {
    45  	logger.Infof("getting stream stats")
    46  	resp, err := c.GetStreamStats(ctx, &ppb.GetStreamStatsRequest{})
    47  	if err != nil {
    48  		logger.Errorf("error calling GetStreamStats: %v\n", err)
    49  		return err
    50  	}
    51  	s := &snapshot{StreamStats: resp.StreamStats}
    52  
    53  	logger.Infof("creating snapshot file %s", f)
    54  	file, err := os.Create(f)
    55  	if err != nil {
    56  		logger.Errorf("cannot create %s: %v", f, err)
    57  		return err
    58  	}
    59  	defer file.Close()
    60  
    61  	logger.Infof("encoding data and writing to snapshot file %s", f)
    62  	encoder := gob.NewEncoder(file)
    63  	err = encoder.Encode(s)
    64  	if err != nil {
    65  		logger.Infof("error encoding: %v", err)
    66  		return err
    67  	}
    68  
    69  	logger.Infof("successfully wrote profiling snapshot to %s", f)
    70  	return nil
    71  }
    72  
    73  func remoteCommand() error {
    74  	ctx := context.Background()
    75  	if *flagTimeout > 0 {
    76  		var cancel func()
    77  		ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*flagTimeout)*time.Second)
    78  		defer cancel()
    79  	}
    80  
    81  	logger.Infof("dialing %s", *flagAddress)
    82  	cc, err := grpc.Dial(*flagAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
    83  	if err != nil {
    84  		logger.Errorf("cannot dial %s: %v", *flagAddress, err)
    85  		return err
    86  	}
    87  	defer cc.Close()
    88  
    89  	c := ppb.NewProfilingClient(cc)
    90  
    91  	if *flagEnableProfiling || *flagDisableProfiling {
    92  		return setEnabled(ctx, c, *flagEnableProfiling)
    93  	} else if *flagRetrieveSnapshot {
    94  		return retrieveSnapshot(ctx, c, *flagSnapshot)
    95  	} else {
    96  		return fmt.Errorf("what should I do with the remote target?")
    97  	}
    98  }
    99  

View as plain text