snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
394
profiling/cmd/catapult.go
Normal file
394
profiling/cmd/catapult.go
Normal file
@@ -0,0 +1,394 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
ppb "google.golang.org/grpc/profiling/proto"
|
||||
)
|
||||
|
||||
type jsonNode struct {
|
||||
Name string `json:"name"`
|
||||
Cat string `json:"cat"`
|
||||
ID string `json:"id"`
|
||||
Cname string `json:"cname"`
|
||||
Phase string `json:"ph"`
|
||||
Timestamp float64 `json:"ts"`
|
||||
PID string `json:"pid"`
|
||||
TID string `json:"tid"`
|
||||
}
|
||||
|
||||
// Catapult does not allow specifying colours manually; a 20-odd predefined
|
||||
// labels are used (that don't make much sense outside the context of
|
||||
// Chromium). See this for more details:
|
||||
//
|
||||
// https://github.com/catapult-project/catapult/blob/bef344f7017fc9e04f7049d0f58af6d9ce9f4ab6/tracing/tracing/base/color_scheme.html#L29
|
||||
func hashCname(tag string) string {
|
||||
if strings.Contains(tag, "encoding") {
|
||||
return "rail_response"
|
||||
}
|
||||
|
||||
if strings.Contains(tag, "compression") {
|
||||
return "cq_build_passed"
|
||||
}
|
||||
|
||||
if strings.Contains(tag, "transport") {
|
||||
if strings.Contains(tag, "blocking") {
|
||||
return "rail_animation"
|
||||
}
|
||||
return "good"
|
||||
}
|
||||
|
||||
if strings.Contains(tag, "header") {
|
||||
return "cq_build_attempt_failed"
|
||||
}
|
||||
|
||||
if tag == "/" {
|
||||
return "heap_dump_stack_frame"
|
||||
}
|
||||
|
||||
if strings.Contains(tag, "flow") || strings.Contains(tag, "tmp") {
|
||||
return "heap_dump_stack_frame"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// filterCounter identifies the counter-th instance of a timer of the type
|
||||
// `filter` within a Stat. This, in conjunction with the counter data structure
|
||||
// defined below, is used to draw flows between linked loopy writer/reader
|
||||
// events with application goroutine events in trace-viewer. This is possible
|
||||
// because enqueues and dequeues are ordered -- that is, the first dequeue must
|
||||
// be dequeueing the first enqueue operation.
|
||||
func filterCounter(stat *ppb.Stat, filter string, counter int) int {
|
||||
localCounter := 0
|
||||
for i := 0; i < len(stat.Timers); i++ {
|
||||
if stat.Timers[i].Tags == filter {
|
||||
if localCounter == counter {
|
||||
return i
|
||||
}
|
||||
localCounter++
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// counter is state object used to store and retrieve the number of timers of a
|
||||
// particular type that have been seen.
|
||||
type counter struct {
|
||||
c map[string]int
|
||||
}
|
||||
|
||||
func newCounter() *counter {
|
||||
return &counter{c: make(map[string]int)}
|
||||
}
|
||||
|
||||
func (c *counter) GetAndInc(s string) int {
|
||||
ret := c.c[s]
|
||||
c.c[s]++
|
||||
return ret
|
||||
}
|
||||
|
||||
func catapultNs(sec int64, nsec int32) float64 {
|
||||
return float64((sec * 1000000000) + int64(nsec))
|
||||
}
|
||||
|
||||
// streamStatsCatapultJSONSingle processes a single proto Stat object to return
|
||||
// an array of jsonNodes in trace-viewer's format.
|
||||
func streamStatsCatapultJSONSingle(stat *ppb.Stat, baseSec int64, baseNsec int32) []jsonNode {
|
||||
if len(stat.Timers) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
connectionCounter := binary.BigEndian.Uint64(stat.Metadata[0:8])
|
||||
streamID := binary.BigEndian.Uint32(stat.Metadata[8:12])
|
||||
opid := fmt.Sprintf("/%s/%d/%d", stat.Tags, connectionCounter, streamID)
|
||||
|
||||
var loopyReaderGoID, loopyWriterGoID int64
|
||||
for i := 0; i < len(stat.Timers) && (loopyReaderGoID == 0 || loopyWriterGoID == 0); i++ {
|
||||
if strings.Contains(stat.Timers[i].Tags, "/loopyReader") {
|
||||
loopyReaderGoID = stat.Timers[i].GoId
|
||||
} else if strings.Contains(stat.Timers[i].Tags, "/loopyWriter") {
|
||||
loopyWriterGoID = stat.Timers[i].GoId
|
||||
}
|
||||
}
|
||||
|
||||
lrc, lwc := newCounter(), newCounter()
|
||||
|
||||
var result []jsonNode
|
||||
result = append(result,
|
||||
jsonNode{
|
||||
Name: "loopyReaderTmp",
|
||||
ID: opid,
|
||||
Cname: hashCname("tmp"),
|
||||
Phase: "i",
|
||||
Timestamp: 0,
|
||||
PID: fmt.Sprintf("/%s/%d/loopyReader", stat.Tags, connectionCounter),
|
||||
TID: fmt.Sprintf("%d", loopyReaderGoID),
|
||||
},
|
||||
jsonNode{
|
||||
Name: "loopyWriterTmp",
|
||||
ID: opid,
|
||||
Cname: hashCname("tmp"),
|
||||
Phase: "i",
|
||||
Timestamp: 0,
|
||||
PID: fmt.Sprintf("/%s/%d/loopyWriter", stat.Tags, connectionCounter),
|
||||
TID: fmt.Sprintf("%d", loopyWriterGoID),
|
||||
},
|
||||
)
|
||||
|
||||
for i := 0; i < len(stat.Timers); i++ {
|
||||
categories := stat.Tags
|
||||
pid, tid := opid, fmt.Sprintf("%d", stat.Timers[i].GoId)
|
||||
|
||||
if stat.Timers[i].GoId == loopyReaderGoID {
|
||||
pid, tid = fmt.Sprintf("/%s/%d/loopyReader", stat.Tags, connectionCounter), fmt.Sprintf("%d", stat.Timers[i].GoId)
|
||||
|
||||
var flowEndID int
|
||||
var flowEndPID, flowEndTID string
|
||||
switch stat.Timers[i].Tags {
|
||||
case "/http2/recv/header":
|
||||
flowEndID = filterCounter(stat, "/grpc/stream/recv/header", lrc.GetAndInc("/http2/recv/header"))
|
||||
if flowEndID != -1 {
|
||||
flowEndPID = opid
|
||||
flowEndTID = fmt.Sprintf("%d", stat.Timers[flowEndID].GoId)
|
||||
} else {
|
||||
logger.Infof("cannot find %s/grpc/stream/recv/header for %s/http2/recv/header", opid, opid)
|
||||
}
|
||||
case "/http2/recv/dataFrame/loopyReader":
|
||||
flowEndID = filterCounter(stat, "/recvAndDecompress", lrc.GetAndInc("/http2/recv/dataFrame/loopyReader"))
|
||||
if flowEndID != -1 {
|
||||
flowEndPID = opid
|
||||
flowEndTID = fmt.Sprintf("%d", stat.Timers[flowEndID].GoId)
|
||||
} else {
|
||||
logger.Infof("cannot find %s/recvAndDecompress for %s/http2/recv/dataFrame/loopyReader", opid, opid)
|
||||
}
|
||||
default:
|
||||
flowEndID = -1
|
||||
}
|
||||
|
||||
if flowEndID != -1 {
|
||||
flowID := fmt.Sprintf("lrc begin:/%d%s end:/%d%s begin:(%d, %s, %s) end:(%d, %s, %s)", connectionCounter, stat.Timers[i].Tags, connectionCounter, stat.Timers[flowEndID].Tags, i, pid, tid, flowEndID, flowEndPID, flowEndTID)
|
||||
result = append(result,
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("%s/flow", opid),
|
||||
Cat: categories + ",flow",
|
||||
ID: flowID,
|
||||
Cname: hashCname("flow"),
|
||||
Phase: "s",
|
||||
Timestamp: catapultNs(stat.Timers[i].EndSec-baseSec, stat.Timers[i].EndNsec-baseNsec),
|
||||
PID: pid,
|
||||
TID: tid,
|
||||
},
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("%s/flow", opid),
|
||||
Cat: categories + ",flow",
|
||||
ID: flowID,
|
||||
Cname: hashCname("flow"),
|
||||
Phase: "f",
|
||||
Timestamp: catapultNs(stat.Timers[flowEndID].BeginSec-baseSec, stat.Timers[flowEndID].BeginNsec-baseNsec),
|
||||
PID: flowEndPID,
|
||||
TID: flowEndTID,
|
||||
},
|
||||
)
|
||||
}
|
||||
} else if stat.Timers[i].GoId == loopyWriterGoID {
|
||||
pid, tid = fmt.Sprintf("/%s/%d/loopyWriter", stat.Tags, connectionCounter), fmt.Sprintf("%d", stat.Timers[i].GoId)
|
||||
|
||||
var flowBeginID int
|
||||
var flowBeginPID, flowBeginTID string
|
||||
switch stat.Timers[i].Tags {
|
||||
case "/http2/recv/header/loopyWriter/registerOutStream":
|
||||
flowBeginID = filterCounter(stat, "/http2/recv/header", lwc.GetAndInc("/http2/recv/header/loopyWriter/registerOutStream"))
|
||||
flowBeginPID = fmt.Sprintf("/%s/%d/loopyReader", stat.Tags, connectionCounter)
|
||||
flowBeginTID = fmt.Sprintf("%d", loopyReaderGoID)
|
||||
case "/http2/send/dataFrame/loopyWriter/preprocess":
|
||||
flowBeginID = filterCounter(stat, "/transport/enqueue", lwc.GetAndInc("/http2/send/dataFrame/loopyWriter/preprocess"))
|
||||
if flowBeginID != -1 {
|
||||
flowBeginPID = opid
|
||||
flowBeginTID = fmt.Sprintf("%d", stat.Timers[flowBeginID].GoId)
|
||||
} else {
|
||||
logger.Infof("cannot find /%d/transport/enqueue for /%d/http2/send/dataFrame/loopyWriter/preprocess", connectionCounter, connectionCounter)
|
||||
}
|
||||
default:
|
||||
flowBeginID = -1
|
||||
}
|
||||
|
||||
if flowBeginID != -1 {
|
||||
flowID := fmt.Sprintf("lwc begin:/%d%s end:/%d%s begin:(%d, %s, %s) end:(%d, %s, %s)", connectionCounter, stat.Timers[flowBeginID].Tags, connectionCounter, stat.Timers[i].Tags, flowBeginID, flowBeginPID, flowBeginTID, i, pid, tid)
|
||||
result = append(result,
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("/%s/%d/%d/flow", stat.Tags, connectionCounter, streamID),
|
||||
Cat: categories + ",flow",
|
||||
ID: flowID,
|
||||
Cname: hashCname("flow"),
|
||||
Phase: "s",
|
||||
Timestamp: catapultNs(stat.Timers[flowBeginID].EndSec-baseSec, stat.Timers[flowBeginID].EndNsec-baseNsec),
|
||||
PID: flowBeginPID,
|
||||
TID: flowBeginTID,
|
||||
},
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("/%s/%d/%d/flow", stat.Tags, connectionCounter, streamID),
|
||||
Cat: categories + ",flow",
|
||||
ID: flowID,
|
||||
Cname: hashCname("flow"),
|
||||
Phase: "f",
|
||||
Timestamp: catapultNs(stat.Timers[i].BeginSec-baseSec, stat.Timers[i].BeginNsec-baseNsec),
|
||||
PID: pid,
|
||||
TID: tid,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result,
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("%s%s", opid, stat.Timers[i].Tags),
|
||||
Cat: categories,
|
||||
ID: opid,
|
||||
Cname: hashCname(stat.Timers[i].Tags),
|
||||
Phase: "B",
|
||||
Timestamp: catapultNs(stat.Timers[i].BeginSec-baseSec, stat.Timers[i].BeginNsec-baseNsec),
|
||||
PID: pid,
|
||||
TID: tid,
|
||||
},
|
||||
jsonNode{
|
||||
Name: fmt.Sprintf("%s%s", opid, stat.Timers[i].Tags),
|
||||
Cat: categories,
|
||||
ID: opid,
|
||||
Cname: hashCname(stat.Timers[i].Tags),
|
||||
Phase: "E",
|
||||
Timestamp: catapultNs(stat.Timers[i].EndSec-baseSec, stat.Timers[i].EndNsec-baseNsec),
|
||||
PID: pid,
|
||||
TID: tid,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// timerBeginIsBefore compares two proto Timer objects to determine if the
|
||||
// first comes before the second chronologically.
|
||||
func timerBeginIsBefore(ti *ppb.Timer, tj *ppb.Timer) bool {
|
||||
if ti.BeginSec == tj.BeginSec {
|
||||
return ti.BeginNsec < tj.BeginNsec
|
||||
}
|
||||
return ti.BeginSec < tj.BeginSec
|
||||
}
|
||||
|
||||
// streamStatsCatapultJSON receives a *snapshot and the name of a JSON file to
|
||||
// write to. The grpc-go profiling snapshot is processed and converted to a
|
||||
// JSON format that can be understood by trace-viewer.
|
||||
func streamStatsCatapultJSON(s *snapshot, streamStatsCatapultJSONFileName string) (err error) {
|
||||
logger.Infof("calculating stream stats filters")
|
||||
filterArray := strings.Split(*flagStreamStatsFilter, ",")
|
||||
filter := make(map[string]bool)
|
||||
for _, f := range filterArray {
|
||||
filter[f] = true
|
||||
}
|
||||
|
||||
logger.Infof("filter stream stats for %s", *flagStreamStatsFilter)
|
||||
var streamStats []*ppb.Stat
|
||||
for _, stat := range s.StreamStats {
|
||||
if _, ok := filter[stat.Tags]; ok {
|
||||
streamStats = append(streamStats, stat)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof("sorting timers within all stats")
|
||||
for id := range streamStats {
|
||||
sort.Slice(streamStats[id].Timers, func(i, j int) bool {
|
||||
return timerBeginIsBefore(streamStats[id].Timers[i], streamStats[id].Timers[j])
|
||||
})
|
||||
}
|
||||
|
||||
logger.Infof("sorting stream stats")
|
||||
sort.Slice(streamStats, func(i, j int) bool {
|
||||
if len(streamStats[j].Timers) == 0 {
|
||||
return true
|
||||
} else if len(streamStats[i].Timers) == 0 {
|
||||
return false
|
||||
}
|
||||
pi := binary.BigEndian.Uint64(streamStats[i].Metadata[0:8])
|
||||
pj := binary.BigEndian.Uint64(streamStats[j].Metadata[0:8])
|
||||
if pi == pj {
|
||||
return timerBeginIsBefore(streamStats[i].Timers[0], streamStats[j].Timers[0])
|
||||
}
|
||||
|
||||
return pi < pj
|
||||
})
|
||||
|
||||
// Clip the last stat as it's from the /Profiling/GetStreamStats call that we
|
||||
// made to retrieve the stats themselves. This likely happened millions of
|
||||
// nanoseconds after the last stream we want to profile, so it'd just make
|
||||
// the catapult graph less readable.
|
||||
if len(streamStats) > 0 {
|
||||
streamStats = streamStats[:len(streamStats)-1]
|
||||
}
|
||||
|
||||
// All timestamps use the earliest timestamp available as the reference.
|
||||
logger.Infof("calculating the earliest timestamp across all timers")
|
||||
var base *ppb.Timer
|
||||
for _, stat := range streamStats {
|
||||
for _, timer := range stat.Timers {
|
||||
if base == nil || timerBeginIsBefore(base, timer) {
|
||||
base = timer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof("converting %d stats to catapult JSON format", len(streamStats))
|
||||
var jsonNodes []jsonNode
|
||||
for _, stat := range streamStats {
|
||||
jsonNodes = append(jsonNodes, streamStatsCatapultJSONSingle(stat, base.BeginSec, base.BeginNsec)...)
|
||||
}
|
||||
|
||||
logger.Infof("marshalling catapult JSON")
|
||||
b, err := json.Marshal(jsonNodes)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot marshal JSON: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("creating catapult JSON file")
|
||||
streamStatsCatapultJSONFile, err := os.Create(streamStatsCatapultJSONFileName)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot create file %s: %v", streamStatsCatapultJSONFileName, err)
|
||||
return err
|
||||
}
|
||||
defer streamStatsCatapultJSONFile.Close()
|
||||
|
||||
logger.Infof("writing catapult JSON to disk")
|
||||
_, err = streamStatsCatapultJSONFile.Write(b)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot write marshalled JSON: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("successfully wrote catapult JSON file %s", streamStatsCatapultJSONFileName)
|
||||
return nil
|
||||
}
|
||||
76
profiling/cmd/flags.go
Normal file
76
profiling/cmd/flags.go
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var flagAddress = flag.String("address", "", "address of a remote gRPC server with profiling turned on to retrieve stats from")
|
||||
var flagTimeout = flag.Int("timeout", 0, "network operations timeout in seconds to remote target (0 indicates unlimited)")
|
||||
|
||||
var flagRetrieveSnapshot = flag.Bool("retrieve-snapshot", false, "connect to remote target and retrieve a profiling snapshot locally for processing")
|
||||
var flagSnapshot = flag.String("snapshot", "", "snapshot file to write to when retrieving profiling data or snapshot file to read from when processing profiling data")
|
||||
|
||||
var flagEnableProfiling = flag.Bool("enable-profiling", false, "enable profiling in remote target")
|
||||
var flagDisableProfiling = flag.Bool("disable-profiling", false, "disable profiling in remote target")
|
||||
|
||||
var flagStreamStatsCatapultJSON = flag.String("stream-stats-catapult-json", "", "path to a file to write to after transforming a snapshot into catapult's JSON format")
|
||||
var flagStreamStatsFilter = flag.String("stream-stats-filter", "server,client", "comma-separated list of stat tags to filter for")
|
||||
|
||||
func exactlyOneOf(opts ...bool) bool {
|
||||
first := true
|
||||
for _, o := range opts {
|
||||
if !o {
|
||||
continue
|
||||
}
|
||||
|
||||
if !first {
|
||||
return false
|
||||
}
|
||||
first = false
|
||||
}
|
||||
|
||||
return !first
|
||||
}
|
||||
|
||||
func parseArgs() error {
|
||||
flag.Parse()
|
||||
|
||||
if *flagAddress != "" {
|
||||
if !exactlyOneOf(*flagEnableProfiling, *flagDisableProfiling, *flagRetrieveSnapshot) {
|
||||
return fmt.Errorf("when -address is specified, you must include exactly only one of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
|
||||
}
|
||||
|
||||
if *flagStreamStatsCatapultJSON != "" {
|
||||
return fmt.Errorf("when -address is specified, you must not include -stream-stats-catapult-json")
|
||||
}
|
||||
} else {
|
||||
if *flagEnableProfiling || *flagDisableProfiling || *flagRetrieveSnapshot {
|
||||
return fmt.Errorf("when -address isn't specified, you must not include any of -enable-profiling, -disable-profiling, and -retrieve-snapshot")
|
||||
}
|
||||
|
||||
if *flagStreamStatsCatapultJSON == "" {
|
||||
return fmt.Errorf("when -address isn't specified, you must include -stream-stats-catapult-json")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
68
profiling/cmd/local.go
Normal file
68
profiling/cmd/local.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func loadSnapshot(snapshotFileName string) (*snapshot, error) {
|
||||
logger.Infof("opening snapshot file %s", snapshotFileName)
|
||||
snapshotFile, err := os.Open(snapshotFileName)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot open %s: %v", snapshotFileName, err)
|
||||
return nil, err
|
||||
}
|
||||
defer snapshotFile.Close()
|
||||
|
||||
logger.Infof("decoding snapshot file %s", snapshotFileName)
|
||||
s := &snapshot{}
|
||||
decoder := gob.NewDecoder(snapshotFile)
|
||||
if err = decoder.Decode(s); err != nil {
|
||||
logger.Errorf("cannot decode %s: %v", snapshotFileName, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func localCommand() error {
|
||||
if *flagSnapshot == "" {
|
||||
return fmt.Errorf("-snapshot flag missing")
|
||||
}
|
||||
|
||||
s, err := loadSnapshot(*flagSnapshot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *flagStreamStatsCatapultJSON == "" {
|
||||
return fmt.Errorf("snapshot file specified without an action to perform")
|
||||
}
|
||||
|
||||
if *flagStreamStatsCatapultJSON != "" {
|
||||
if err = streamStatsCatapultJSON(s, *flagStreamStatsCatapultJSON); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
53
profiling/cmd/main.go
Normal file
53
profiling/cmd/main.go
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Binary cmd is a command-line tool for profiling management. It retrieves and
|
||||
// processes data from the profiling service.
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"google.golang.org/grpc/grpclog"
|
||||
ppb "google.golang.org/grpc/profiling/proto"
|
||||
)
|
||||
|
||||
var logger = grpclog.Component("profiling")
|
||||
|
||||
type snapshot struct {
|
||||
StreamStats []*ppb.Stat
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := parseArgs(); err != nil {
|
||||
logger.Errorf("error parsing flags: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *flagAddress != "" {
|
||||
if err := remoteCommand(); err != nil {
|
||||
logger.Errorf("error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
if err := localCommand(); err != nil {
|
||||
logger.Errorf("error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
98
profiling/cmd/remote.go
Normal file
98
profiling/cmd/remote.go
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
ppb "google.golang.org/grpc/profiling/proto"
|
||||
)
|
||||
|
||||
func setEnabled(ctx context.Context, c ppb.ProfilingClient, enabled bool) error {
|
||||
_, err := c.Enable(ctx, &ppb.EnableRequest{Enabled: enabled})
|
||||
if err != nil {
|
||||
logger.Infof("error calling Enable: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("successfully set enabled = %v", enabled)
|
||||
return nil
|
||||
}
|
||||
|
||||
func retrieveSnapshot(ctx context.Context, c ppb.ProfilingClient, f string) error {
|
||||
logger.Infof("getting stream stats")
|
||||
resp, err := c.GetStreamStats(ctx, &ppb.GetStreamStatsRequest{})
|
||||
if err != nil {
|
||||
logger.Errorf("error calling GetStreamStats: %v\n", err)
|
||||
return err
|
||||
}
|
||||
s := &snapshot{StreamStats: resp.StreamStats}
|
||||
|
||||
logger.Infof("creating snapshot file %s", f)
|
||||
file, err := os.Create(f)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot create %s: %v", f, err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
logger.Infof("encoding data and writing to snapshot file %s", f)
|
||||
encoder := gob.NewEncoder(file)
|
||||
err = encoder.Encode(s)
|
||||
if err != nil {
|
||||
logger.Infof("error encoding: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("successfully wrote profiling snapshot to %s", f)
|
||||
return nil
|
||||
}
|
||||
|
||||
func remoteCommand() error {
|
||||
ctx := context.Background()
|
||||
if *flagTimeout > 0 {
|
||||
var cancel func()
|
||||
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*flagTimeout)*time.Second)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
logger.Infof("dialing %s", *flagAddress)
|
||||
cc, err := grpc.NewClient(*flagAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
logger.Fatalf("grpc.NewClient(%q) = %v", *flagAddress, err)
|
||||
return err
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
c := ppb.NewProfilingClient(cc)
|
||||
|
||||
if *flagEnableProfiling || *flagDisableProfiling {
|
||||
return setEnabled(ctx, c, *flagEnableProfiling)
|
||||
}
|
||||
if *flagRetrieveSnapshot {
|
||||
return retrieveSnapshot(ctx, c, *flagSnapshot)
|
||||
}
|
||||
return fmt.Errorf("what should I do with the remote target?")
|
||||
}
|
||||
40
profiling/profiling.go
Normal file
40
profiling/profiling.go
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Package profiling exposes methods to manage profiling within gRPC.
|
||||
//
|
||||
// # Experimental
|
||||
//
|
||||
// Notice: This package is EXPERIMENTAL and may be changed or removed in a
|
||||
// later release.
|
||||
package profiling
|
||||
|
||||
import (
|
||||
internal "google.golang.org/grpc/internal/profiling"
|
||||
)
|
||||
|
||||
// Enable turns profiling on and off. This operation is safe for concurrent
|
||||
// access from different goroutines.
|
||||
//
|
||||
// Note that this is the only operation that's accessible through the publicly
|
||||
// exposed profiling package. Everything else (such as retrieving stats) must
|
||||
// be done through the profiling service. This is allowed so that users can use
|
||||
// heuristics to turn profiling on and off automatically.
|
||||
func Enable(enabled bool) {
|
||||
internal.Enable(enabled)
|
||||
}
|
||||
455
profiling/proto/service.pb.go
Normal file
455
profiling/proto/service.pb.go
Normal file
@@ -0,0 +1,455 @@
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.12
|
||||
// protoc v5.27.1
|
||||
// source: profiling/proto/service.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// EnableRequest defines the fields in a /Profiling/Enable method request to
|
||||
// toggle profiling on and off within a gRPC program.
|
||||
type EnableRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
// Setting this to true will enable profiling. Setting this to false will
|
||||
// disable profiling.
|
||||
Enabled bool `protobuf:"varint,1,opt,name=enabled,proto3" json:"enabled,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *EnableRequest) Reset() {
|
||||
*x = EnableRequest{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *EnableRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*EnableRequest) ProtoMessage() {}
|
||||
|
||||
func (x *EnableRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use EnableRequest.ProtoReflect.Descriptor instead.
|
||||
func (*EnableRequest) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *EnableRequest) GetEnabled() bool {
|
||||
if x != nil {
|
||||
return x.Enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// EnableResponse defines the fields in a /Profiling/Enable method response.
|
||||
type EnableResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *EnableResponse) Reset() {
|
||||
*x = EnableResponse{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *EnableResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*EnableResponse) ProtoMessage() {}
|
||||
|
||||
func (x *EnableResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use EnableResponse.ProtoReflect.Descriptor instead.
|
||||
func (*EnableResponse) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
// GetStreamStatsRequest defines the fields in a /Profiling/GetStreamStats
|
||||
// method request to retrieve stream-level stats in a gRPC client/server.
|
||||
type GetStreamStatsRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetStreamStatsRequest) Reset() {
|
||||
*x = GetStreamStatsRequest{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetStreamStatsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetStreamStatsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetStreamStatsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetStreamStatsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetStreamStatsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
// GetStreamStatsResponse defines the fields in a /Profiling/GetStreamStats
|
||||
// method response.
|
||||
type GetStreamStatsResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
StreamStats []*Stat `protobuf:"bytes,1,rep,name=stream_stats,json=streamStats,proto3" json:"stream_stats,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetStreamStatsResponse) Reset() {
|
||||
*x = GetStreamStatsResponse{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetStreamStatsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetStreamStatsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetStreamStatsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetStreamStatsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetStreamStatsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *GetStreamStatsResponse) GetStreamStats() []*Stat {
|
||||
if x != nil {
|
||||
return x.StreamStats
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// A Timer measures the start and end of execution of a component within
|
||||
// gRPC that's being profiled. It includes a tag and some additional metadata
|
||||
// to identify itself.
|
||||
type Timer struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
// tags is a comma-separated list of strings used to tag a timer.
|
||||
Tags string `protobuf:"bytes,1,opt,name=tags,proto3" json:"tags,omitempty"`
|
||||
// begin_sec and begin_nsec are the start epoch second and nanosecond,
|
||||
// respectively, of the component profiled by this timer in UTC. begin_nsec
|
||||
// must be a non-negative integer.
|
||||
BeginSec int64 `protobuf:"varint,2,opt,name=begin_sec,json=beginSec,proto3" json:"begin_sec,omitempty"`
|
||||
BeginNsec int32 `protobuf:"varint,3,opt,name=begin_nsec,json=beginNsec,proto3" json:"begin_nsec,omitempty"`
|
||||
// end_sec and end_nsec are the end epoch second and nanosecond,
|
||||
// respectively, of the component profiled by this timer in UTC. end_nsec
|
||||
// must be a non-negative integer.
|
||||
EndSec int64 `protobuf:"varint,4,opt,name=end_sec,json=endSec,proto3" json:"end_sec,omitempty"`
|
||||
EndNsec int32 `protobuf:"varint,5,opt,name=end_nsec,json=endNsec,proto3" json:"end_nsec,omitempty"`
|
||||
// go_id is the goroutine ID of the component being profiled.
|
||||
GoId int64 `protobuf:"varint,6,opt,name=go_id,json=goId,proto3" json:"go_id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Timer) Reset() {
|
||||
*x = Timer{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Timer) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Timer) ProtoMessage() {}
|
||||
|
||||
func (x *Timer) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Timer.ProtoReflect.Descriptor instead.
|
||||
func (*Timer) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *Timer) GetTags() string {
|
||||
if x != nil {
|
||||
return x.Tags
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Timer) GetBeginSec() int64 {
|
||||
if x != nil {
|
||||
return x.BeginSec
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Timer) GetBeginNsec() int32 {
|
||||
if x != nil {
|
||||
return x.BeginNsec
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Timer) GetEndSec() int64 {
|
||||
if x != nil {
|
||||
return x.EndSec
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Timer) GetEndNsec() int32 {
|
||||
if x != nil {
|
||||
return x.EndNsec
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Timer) GetGoId() int64 {
|
||||
if x != nil {
|
||||
return x.GoId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// A Stat is a collection of Timers along with some additional
|
||||
// metadata to tag and identify itself.
|
||||
type Stat struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
// tags is a comma-separated list of strings used to categorize a stat.
|
||||
Tags string `protobuf:"bytes,1,opt,name=tags,proto3" json:"tags,omitempty"`
|
||||
// timers is an array of Timers, each representing a different
|
||||
// (but possibly overlapping) component within this stat.
|
||||
Timers []*Timer `protobuf:"bytes,2,rep,name=timers,proto3" json:"timers,omitempty"`
|
||||
// metadata is an array of bytes used to uniquely identify a stat with an
|
||||
// undefined encoding format. For example, the Stats returned by the
|
||||
// /Profiling/GetStreamStats service use the metadata field to encode the
|
||||
// connection ID and the stream ID of each query.
|
||||
Metadata []byte `protobuf:"bytes,3,opt,name=metadata,proto3" json:"metadata,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Stat) Reset() {
|
||||
*x = Stat{}
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *Stat) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Stat) ProtoMessage() {}
|
||||
|
||||
func (x *Stat) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_profiling_proto_service_proto_msgTypes[5]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Stat.ProtoReflect.Descriptor instead.
|
||||
func (*Stat) Descriptor() ([]byte, []int) {
|
||||
return file_profiling_proto_service_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
func (x *Stat) GetTags() string {
|
||||
if x != nil {
|
||||
return x.Tags
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Stat) GetTimers() []*Timer {
|
||||
if x != nil {
|
||||
return x.Timers
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Stat) GetMetadata() []byte {
|
||||
if x != nil {
|
||||
return x.Metadata
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_profiling_proto_service_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_profiling_proto_service_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x1dprofiling/proto/service.proto\x12\x19grpc.go.profiling.v1alpha\")\n" +
|
||||
"\rEnableRequest\x12\x18\n" +
|
||||
"\aenabled\x18\x01 \x01(\bR\aenabled\"\x10\n" +
|
||||
"\x0eEnableResponse\"\x17\n" +
|
||||
"\x15GetStreamStatsRequest\"\\\n" +
|
||||
"\x16GetStreamStatsResponse\x12B\n" +
|
||||
"\fstream_stats\x18\x01 \x03(\v2\x1f.grpc.go.profiling.v1alpha.StatR\vstreamStats\"\xa0\x01\n" +
|
||||
"\x05Timer\x12\x12\n" +
|
||||
"\x04tags\x18\x01 \x01(\tR\x04tags\x12\x1b\n" +
|
||||
"\tbegin_sec\x18\x02 \x01(\x03R\bbeginSec\x12\x1d\n" +
|
||||
"\n" +
|
||||
"begin_nsec\x18\x03 \x01(\x05R\tbeginNsec\x12\x17\n" +
|
||||
"\aend_sec\x18\x04 \x01(\x03R\x06endSec\x12\x19\n" +
|
||||
"\bend_nsec\x18\x05 \x01(\x05R\aendNsec\x12\x13\n" +
|
||||
"\x05go_id\x18\x06 \x01(\x03R\x04goId\"p\n" +
|
||||
"\x04Stat\x12\x12\n" +
|
||||
"\x04tags\x18\x01 \x01(\tR\x04tags\x128\n" +
|
||||
"\x06timers\x18\x02 \x03(\v2 .grpc.go.profiling.v1alpha.TimerR\x06timers\x12\x1a\n" +
|
||||
"\bmetadata\x18\x03 \x01(\fR\bmetadata2\xe1\x01\n" +
|
||||
"\tProfiling\x12]\n" +
|
||||
"\x06Enable\x12(.grpc.go.profiling.v1alpha.EnableRequest\x1a).grpc.go.profiling.v1alpha.EnableResponse\x12u\n" +
|
||||
"\x0eGetStreamStats\x120.grpc.go.profiling.v1alpha.GetStreamStatsRequest\x1a1.grpc.go.profiling.v1alpha.GetStreamStatsResponseB(Z&google.golang.org/grpc/profiling/protob\x06proto3"
|
||||
|
||||
var (
|
||||
file_profiling_proto_service_proto_rawDescOnce sync.Once
|
||||
file_profiling_proto_service_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_profiling_proto_service_proto_rawDescGZIP() []byte {
|
||||
file_profiling_proto_service_proto_rawDescOnce.Do(func() {
|
||||
file_profiling_proto_service_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_profiling_proto_service_proto_rawDesc), len(file_profiling_proto_service_proto_rawDesc)))
|
||||
})
|
||||
return file_profiling_proto_service_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_profiling_proto_service_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_profiling_proto_service_proto_goTypes = []any{
|
||||
(*EnableRequest)(nil), // 0: grpc.go.profiling.v1alpha.EnableRequest
|
||||
(*EnableResponse)(nil), // 1: grpc.go.profiling.v1alpha.EnableResponse
|
||||
(*GetStreamStatsRequest)(nil), // 2: grpc.go.profiling.v1alpha.GetStreamStatsRequest
|
||||
(*GetStreamStatsResponse)(nil), // 3: grpc.go.profiling.v1alpha.GetStreamStatsResponse
|
||||
(*Timer)(nil), // 4: grpc.go.profiling.v1alpha.Timer
|
||||
(*Stat)(nil), // 5: grpc.go.profiling.v1alpha.Stat
|
||||
}
|
||||
var file_profiling_proto_service_proto_depIdxs = []int32{
|
||||
5, // 0: grpc.go.profiling.v1alpha.GetStreamStatsResponse.stream_stats:type_name -> grpc.go.profiling.v1alpha.Stat
|
||||
4, // 1: grpc.go.profiling.v1alpha.Stat.timers:type_name -> grpc.go.profiling.v1alpha.Timer
|
||||
0, // 2: grpc.go.profiling.v1alpha.Profiling.Enable:input_type -> grpc.go.profiling.v1alpha.EnableRequest
|
||||
2, // 3: grpc.go.profiling.v1alpha.Profiling.GetStreamStats:input_type -> grpc.go.profiling.v1alpha.GetStreamStatsRequest
|
||||
1, // 4: grpc.go.profiling.v1alpha.Profiling.Enable:output_type -> grpc.go.profiling.v1alpha.EnableResponse
|
||||
3, // 5: grpc.go.profiling.v1alpha.Profiling.GetStreamStats:output_type -> grpc.go.profiling.v1alpha.GetStreamStatsResponse
|
||||
4, // [4:6] is the sub-list for method output_type
|
||||
2, // [2:4] is the sub-list for method input_type
|
||||
2, // [2:2] is the sub-list for extension type_name
|
||||
2, // [2:2] is the sub-list for extension extendee
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_profiling_proto_service_proto_init() }
|
||||
func file_profiling_proto_service_proto_init() {
|
||||
if File_profiling_proto_service_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_profiling_proto_service_proto_rawDesc), len(file_profiling_proto_service_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 6,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_profiling_proto_service_proto_goTypes,
|
||||
DependencyIndexes: file_profiling_proto_service_proto_depIdxs,
|
||||
MessageInfos: file_profiling_proto_service_proto_msgTypes,
|
||||
}.Build()
|
||||
File_profiling_proto_service_proto = out.File
|
||||
file_profiling_proto_service_proto_goTypes = nil
|
||||
file_profiling_proto_service_proto_depIdxs = nil
|
||||
}
|
||||
98
profiling/proto/service.proto
Normal file
98
profiling/proto/service.proto
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package grpc.go.profiling.v1alpha;
|
||||
|
||||
// This package defines the proto messages and RPC services exposed by gRPC for
|
||||
// profiling management. A reference client implementation to interact with
|
||||
// this service is provided as a command-line application. This service can be
|
||||
// used to toggle profiling on and off and retrieve stats from a gRPC
|
||||
// application.
|
||||
option go_package = "google.golang.org/grpc/profiling/proto";
|
||||
|
||||
// EnableRequest defines the fields in a /Profiling/Enable method request to
|
||||
// toggle profiling on and off within a gRPC program.
|
||||
message EnableRequest {
|
||||
// Setting this to true will enable profiling. Setting this to false will
|
||||
// disable profiling.
|
||||
bool enabled = 1;
|
||||
}
|
||||
|
||||
// EnableResponse defines the fields in a /Profiling/Enable method response.
|
||||
message EnableResponse {
|
||||
}
|
||||
|
||||
// GetStreamStatsRequest defines the fields in a /Profiling/GetStreamStats
|
||||
// method request to retrieve stream-level stats in a gRPC client/server.
|
||||
message GetStreamStatsRequest {
|
||||
}
|
||||
|
||||
// GetStreamStatsResponse defines the fields in a /Profiling/GetStreamStats
|
||||
// method response.
|
||||
message GetStreamStatsResponse {
|
||||
repeated Stat stream_stats = 1;
|
||||
}
|
||||
|
||||
// A Timer measures the start and end of execution of a component within
|
||||
// gRPC that's being profiled. It includes a tag and some additional metadata
|
||||
// to identify itself.
|
||||
message Timer {
|
||||
// tags is a comma-separated list of strings used to tag a timer.
|
||||
string tags = 1;
|
||||
|
||||
// begin_sec and begin_nsec are the start epoch second and nanosecond,
|
||||
// respectively, of the component profiled by this timer in UTC. begin_nsec
|
||||
// must be a non-negative integer.
|
||||
int64 begin_sec = 2;
|
||||
int32 begin_nsec = 3;
|
||||
|
||||
// end_sec and end_nsec are the end epoch second and nanosecond,
|
||||
// respectively, of the component profiled by this timer in UTC. end_nsec
|
||||
// must be a non-negative integer.
|
||||
int64 end_sec = 4;
|
||||
int32 end_nsec = 5;
|
||||
|
||||
// go_id is the goroutine ID of the component being profiled.
|
||||
int64 go_id = 6;
|
||||
}
|
||||
|
||||
// A Stat is a collection of Timers along with some additional
|
||||
// metadata to tag and identify itself.
|
||||
message Stat {
|
||||
// tags is a comma-separated list of strings used to categorize a stat.
|
||||
string tags = 1;
|
||||
|
||||
// timers is an array of Timers, each representing a different
|
||||
// (but possibly overlapping) component within this stat.
|
||||
repeated Timer timers = 2;
|
||||
|
||||
// metadata is an array of bytes used to uniquely identify a stat with an
|
||||
// undefined encoding format. For example, the Stats returned by the
|
||||
// /Profiling/GetStreamStats service use the metadata field to encode the
|
||||
// connection ID and the stream ID of each query.
|
||||
bytes metadata = 3;
|
||||
}
|
||||
|
||||
// The Profiling service exposes functions to remotely manage the gRPC
|
||||
// profiling behaviour in a program.
|
||||
service Profiling {
|
||||
// Enable allows users to toggle profiling on and off remotely.
|
||||
rpc Enable (EnableRequest) returns (EnableResponse);
|
||||
|
||||
// GetStreamStats is used to retrieve an array of stream-level stats from a
|
||||
// gRPC client/server.
|
||||
rpc GetStreamStats (GetStreamStatsRequest) returns (GetStreamStatsResponse);
|
||||
}
|
||||
183
profiling/proto/service_grpc.pb.go
Normal file
183
profiling/proto/service_grpc.pb.go
Normal file
@@ -0,0 +1,183 @@
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.2
|
||||
// - protoc v5.27.1
|
||||
// source: profiling/proto/service.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
Profiling_Enable_FullMethodName = "/grpc.go.profiling.v1alpha.Profiling/Enable"
|
||||
Profiling_GetStreamStats_FullMethodName = "/grpc.go.profiling.v1alpha.Profiling/GetStreamStats"
|
||||
)
|
||||
|
||||
// ProfilingClient is the client API for Profiling service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// The Profiling service exposes functions to remotely manage the gRPC
|
||||
// profiling behaviour in a program.
|
||||
type ProfilingClient interface {
|
||||
// Enable allows users to toggle profiling on and off remotely.
|
||||
Enable(ctx context.Context, in *EnableRequest, opts ...grpc.CallOption) (*EnableResponse, error)
|
||||
// GetStreamStats is used to retrieve an array of stream-level stats from a
|
||||
// gRPC client/server.
|
||||
GetStreamStats(ctx context.Context, in *GetStreamStatsRequest, opts ...grpc.CallOption) (*GetStreamStatsResponse, error)
|
||||
}
|
||||
|
||||
type profilingClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewProfilingClient(cc grpc.ClientConnInterface) ProfilingClient {
|
||||
return &profilingClient{cc}
|
||||
}
|
||||
|
||||
func (c *profilingClient) Enable(ctx context.Context, in *EnableRequest, opts ...grpc.CallOption) (*EnableResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EnableResponse)
|
||||
err := c.cc.Invoke(ctx, Profiling_Enable_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *profilingClient) GetStreamStats(ctx context.Context, in *GetStreamStatsRequest, opts ...grpc.CallOption) (*GetStreamStatsResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetStreamStatsResponse)
|
||||
err := c.cc.Invoke(ctx, Profiling_GetStreamStats_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ProfilingServer is the server API for Profiling service.
|
||||
// All implementations should embed UnimplementedProfilingServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// The Profiling service exposes functions to remotely manage the gRPC
|
||||
// profiling behaviour in a program.
|
||||
type ProfilingServer interface {
|
||||
// Enable allows users to toggle profiling on and off remotely.
|
||||
Enable(context.Context, *EnableRequest) (*EnableResponse, error)
|
||||
// GetStreamStats is used to retrieve an array of stream-level stats from a
|
||||
// gRPC client/server.
|
||||
GetStreamStats(context.Context, *GetStreamStatsRequest) (*GetStreamStatsResponse, error)
|
||||
}
|
||||
|
||||
// UnimplementedProfilingServer should be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedProfilingServer struct{}
|
||||
|
||||
func (UnimplementedProfilingServer) Enable(context.Context, *EnableRequest) (*EnableResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method Enable not implemented")
|
||||
}
|
||||
func (UnimplementedProfilingServer) GetStreamStats(context.Context, *GetStreamStatsRequest) (*GetStreamStatsResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetStreamStats not implemented")
|
||||
}
|
||||
func (UnimplementedProfilingServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeProfilingServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to ProfilingServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeProfilingServer interface {
|
||||
mustEmbedUnimplementedProfilingServer()
|
||||
}
|
||||
|
||||
func RegisterProfilingServer(s grpc.ServiceRegistrar, srv ProfilingServer) {
|
||||
// If the following call panics, it indicates UnimplementedProfilingServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&Profiling_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Profiling_Enable_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(EnableRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(ProfilingServer).Enable(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Profiling_Enable_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(ProfilingServer).Enable(ctx, req.(*EnableRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Profiling_GetStreamStats_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetStreamStatsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(ProfilingServer).GetStreamStats(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Profiling_GetStreamStats_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(ProfilingServer).GetStreamStats(ctx, req.(*GetStreamStatsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Profiling_ServiceDesc is the grpc.ServiceDesc for Profiling service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Profiling_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "grpc.go.profiling.v1alpha.Profiling",
|
||||
HandlerType: (*ProfilingServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Enable",
|
||||
Handler: _Profiling_Enable_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetStreamStats",
|
||||
Handler: _Profiling_GetStreamStats_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "profiling/proto/service.proto",
|
||||
}
|
||||
150
profiling/service/service.go
Normal file
150
profiling/service/service.go
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Package service defines methods to register a gRPC client/service for a
|
||||
// profiling service that is exposed in the same server. This service can be
|
||||
// queried by a client to remotely manage the gRPC profiling behaviour of an
|
||||
// application.
|
||||
//
|
||||
// # Experimental
|
||||
//
|
||||
// Notice: This package is EXPERIMENTAL and may be changed or removed in a
|
||||
// later release.
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
"google.golang.org/grpc/internal/profiling"
|
||||
ppb "google.golang.org/grpc/profiling/proto"
|
||||
)
|
||||
|
||||
var logger = grpclog.Component("profiling")
|
||||
|
||||
// ProfilingConfig defines configuration options for the Init method.
|
||||
type ProfilingConfig struct {
|
||||
// Setting this to true will enable profiling.
|
||||
Enabled bool
|
||||
|
||||
// Profiling uses a circular buffer (ring buffer) to store statistics for
|
||||
// only the last few RPCs so that profiling stats do not grow unbounded. This
|
||||
// parameter defines the upper limit on the number of RPCs for which
|
||||
// statistics should be stored at any given time. An average RPC requires
|
||||
// approximately 2-3 KiB of memory for profiling-related statistics, so
|
||||
// choose an appropriate number based on the amount of memory you can afford.
|
||||
StreamStatsSize uint32
|
||||
|
||||
// To expose the profiling service and its methods, a *grpc.Server must be
|
||||
// provided.
|
||||
Server *grpc.Server
|
||||
}
|
||||
|
||||
var errorNilServer = errors.New("profiling: no grpc.Server provided")
|
||||
|
||||
// Init takes a *ProfilingConfig to initialize profiling (turned on/off
|
||||
// depending on the value set in pc.Enabled) and register the profiling service
|
||||
// in the server provided in pc.Server.
|
||||
func Init(pc *ProfilingConfig) error {
|
||||
if pc.Server == nil {
|
||||
return errorNilServer
|
||||
}
|
||||
|
||||
if err := profiling.InitStats(pc.StreamStatsSize); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ppb.RegisterProfilingServer(pc.Server, getProfilingServerInstance())
|
||||
|
||||
// Do this last after everything has been initialized and allocated.
|
||||
profiling.Enable(pc.Enabled)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type profilingServer struct {
|
||||
ppb.UnimplementedProfilingServer
|
||||
drainMutex sync.Mutex
|
||||
}
|
||||
|
||||
var profilingServerInstance *profilingServer
|
||||
var profilingServerOnce sync.Once
|
||||
|
||||
// getProfilingServerInstance creates and returns a singleton instance of
|
||||
// profilingServer. Only one instance of profilingServer is created to use a
|
||||
// shared mutex across all profilingServer instances.
|
||||
func getProfilingServerInstance() *profilingServer {
|
||||
profilingServerOnce.Do(func() {
|
||||
profilingServerInstance = &profilingServer{}
|
||||
})
|
||||
|
||||
return profilingServerInstance
|
||||
}
|
||||
|
||||
func (s *profilingServer) Enable(_ context.Context, req *ppb.EnableRequest) (*ppb.EnableResponse, error) {
|
||||
if req.Enabled {
|
||||
logger.Infof("profilingServer: Enable: enabling profiling")
|
||||
} else {
|
||||
logger.Infof("profilingServer: Enable: disabling profiling")
|
||||
}
|
||||
profiling.Enable(req.Enabled)
|
||||
|
||||
return &ppb.EnableResponse{}, nil
|
||||
}
|
||||
|
||||
func timerToProtoTimer(timer *profiling.Timer) *ppb.Timer {
|
||||
return &ppb.Timer{
|
||||
Tags: timer.Tags,
|
||||
BeginSec: timer.Begin.Unix(),
|
||||
BeginNsec: int32(timer.Begin.Nanosecond()),
|
||||
EndSec: timer.End.Unix(),
|
||||
EndNsec: int32(timer.End.Nanosecond()),
|
||||
GoId: timer.GoID,
|
||||
}
|
||||
}
|
||||
|
||||
func statToProtoStat(stat *profiling.Stat) *ppb.Stat {
|
||||
protoStat := &ppb.Stat{
|
||||
Tags: stat.Tags,
|
||||
Timers: make([]*ppb.Timer, 0, len(stat.Timers)),
|
||||
Metadata: stat.Metadata,
|
||||
}
|
||||
for _, t := range stat.Timers {
|
||||
protoStat.Timers = append(protoStat.Timers, timerToProtoTimer(t))
|
||||
}
|
||||
return protoStat
|
||||
}
|
||||
|
||||
func (s *profilingServer) GetStreamStats(context.Context, *ppb.GetStreamStatsRequest) (*ppb.GetStreamStatsResponse, error) {
|
||||
// Since the drain operation is destructive, only one client request should
|
||||
// be served at a time.
|
||||
logger.Infof("profilingServer: GetStreamStats: processing request")
|
||||
s.drainMutex.Lock()
|
||||
results := profiling.StreamStats.Drain()
|
||||
s.drainMutex.Unlock()
|
||||
|
||||
logger.Infof("profilingServer: GetStreamStats: returning %v records", len(results))
|
||||
streamStats := make([]*ppb.Stat, 0)
|
||||
for _, stat := range results {
|
||||
streamStats = append(streamStats, statToProtoStat(stat.(*profiling.Stat)))
|
||||
}
|
||||
return &ppb.GetStreamStatsResponse{StreamStats: streamStats}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user