gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/grpc/profiling/cmd/remote.go (about) 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 "gitee.com/zhaochuninhefei/gmgo/grpc/credentials/insecure" 26 "os" 27 "time" 28 29 "gitee.com/zhaochuninhefei/gmgo/grpc" 30 ppb "gitee.com/zhaochuninhefei/gmgo/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 func(file *os.File) { 60 _ = file.Close() 61 }(file) 62 63 logger.Infof("encoding data and writing to snapshot file %s", f) 64 encoder := gob.NewEncoder(file) 65 err = encoder.Encode(s) 66 if err != nil { 67 logger.Infof("error encoding: %v", err) 68 return err 69 } 70 71 logger.Infof("successfully wrote profiling snapshot to %s", f) 72 return nil 73 } 74 75 func remoteCommand() error { 76 ctx := context.Background() 77 if *flagTimeout > 0 { 78 var cancel func() 79 ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*flagTimeout)*time.Second) 80 defer cancel() 81 } 82 83 logger.Infof("dialing %s", *flagAddress) 84 // grpc.WithInsecure() is deprecated, use WithTransportCredentials and insecure.NewCredentials() instead. 85 //cc, err := grpc.Dial(*flagAddress, grpc.WithInsecure()) 86 cc, err := grpc.Dial(*flagAddress, grpc.WithTransportCredentials(insecure.NewCredentials())) 87 if err != nil { 88 logger.Errorf("cannot dial %s: %v", *flagAddress, err) 89 return err 90 } 91 defer func(cc *grpc.ClientConn) { 92 _ = cc.Close() 93 }(cc) 94 95 c := ppb.NewProfilingClient(cc) 96 97 if *flagEnableProfiling || *flagDisableProfiling { 98 return setEnabled(ctx, c, *flagEnableProfiling) 99 } else if *flagRetrieveSnapshot { 100 return retrieveSnapshot(ctx, c, *flagSnapshot) 101 } else { 102 //goland:noinspection GoErrorStringFormat 103 return fmt.Errorf("what should I do with the remote target?") 104 } 105 }