Source File
stats_common.go
Belonging Package
go.opencensus.io/plugin/ocgrpc
package ocgrpc
import (
ocstats
)
type grpcInstrumentationKey string
var (
DefaultBytesDistribution = view.Distribution(1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296)
DefaultMillisecondsDistribution = view.Distribution(0.01, 0.05, 0.1, 0.3, 0.6, 0.8, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000)
DefaultMessageCountDistribution = view.Distribution(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536)
)
var (
KeyServerMethod = tag.MustNewKey("grpc_server_method")
KeyServerStatus = tag.MustNewKey("grpc_server_status")
)
var (
KeyClientMethod = tag.MustNewKey("grpc_client_method")
KeyClientStatus = tag.MustNewKey("grpc_client_status")
)
var (
rpcDataKey = grpcInstrumentationKey("opencensus-rpcData")
)
func ( string) string {
return strings.TrimLeft(, "/")
}
case *stats.OutPayload:
handleRPCOutPayload(, )
case *stats.InPayload:
handleRPCInPayload(, )
case *stats.End:
handleRPCEnd(, )
default:
grpclog.Infof("unexpected stats: %T", )
}
}
func ( context.Context, *stats.OutPayload) {
, := .Value(rpcDataKey).(*rpcData)
if ! {
if grpclog.V(2) {
grpclog.Infoln("Failed to retrieve *rpcData from context.")
}
return
}
atomic.AddInt64(&.sentBytes, int64(.Length))
atomic.AddInt64(&.sentCount, 1)
}
func ( context.Context, *stats.InPayload) {
, := .Value(rpcDataKey).(*rpcData)
if ! {
if grpclog.V(2) {
grpclog.Infoln("Failed to retrieve *rpcData from context.")
}
return
}
atomic.AddInt64(&.recvBytes, int64(.Length))
atomic.AddInt64(&.recvCount, 1)
}
func ( context.Context, *stats.End) {
, := .Value(rpcDataKey).(*rpcData)
if ! {
if grpclog.V(2) {
grpclog.Infoln("Failed to retrieve *rpcData from context.")
}
return
}
:= time.Since(.startTime)
var string
if .Error != nil {
, := status.FromError(.Error)
if {
= statusCodeToString()
}
} else {
= "OK"
}
:= float64() / float64(time.Millisecond)
:= getSpanCtxAttachment()
if .Client {
ocstats.RecordWithOptions(,
ocstats.WithTags(
tag.Upsert(KeyClientMethod, methodName(.method)),
tag.Upsert(KeyClientStatus, )),
ocstats.WithAttachments(),
ocstats.WithMeasurements(
ClientSentBytesPerRPC.M(atomic.LoadInt64(&.sentBytes)),
ClientSentMessagesPerRPC.M(atomic.LoadInt64(&.sentCount)),
ClientReceivedMessagesPerRPC.M(atomic.LoadInt64(&.recvCount)),
ClientReceivedBytesPerRPC.M(atomic.LoadInt64(&.recvBytes)),
ClientRoundtripLatency.M()))
} else {
ocstats.RecordWithOptions(,
ocstats.WithTags(
tag.Upsert(KeyServerStatus, ),
),
ocstats.WithAttachments(),
ocstats.WithMeasurements(
ServerSentBytesPerRPC.M(atomic.LoadInt64(&.sentBytes)),
ServerSentMessagesPerRPC.M(atomic.LoadInt64(&.sentCount)),
ServerReceivedMessagesPerRPC.M(atomic.LoadInt64(&.recvCount)),
ServerReceivedBytesPerRPC.M(atomic.LoadInt64(&.recvBytes)),
ServerLatency.M()))
}
}
switch := .Code(); {
case codes.OK:
return "OK"
case codes.Canceled:
return "CANCELLED"
case codes.Unknown:
return "UNKNOWN"
case codes.InvalidArgument:
return "INVALID_ARGUMENT"
case codes.DeadlineExceeded:
return "DEADLINE_EXCEEDED"
case codes.NotFound:
return "NOT_FOUND"
case codes.AlreadyExists:
return "ALREADY_EXISTS"
case codes.PermissionDenied:
return "PERMISSION_DENIED"
case codes.ResourceExhausted:
return "RESOURCE_EXHAUSTED"
case codes.FailedPrecondition:
return "FAILED_PRECONDITION"
case codes.Aborted:
return "ABORTED"
case codes.OutOfRange:
return "OUT_OF_RANGE"
case codes.Unimplemented:
return "UNIMPLEMENTED"
case codes.Internal:
return "INTERNAL"
case codes.Unavailable:
return "UNAVAILABLE"
case codes.DataLoss:
return "DATA_LOSS"
case codes.Unauthenticated:
return "UNAUTHENTICATED"
default:
return "CODE_" + strconv.FormatInt(int64(), 10)
}
}
func ( context.Context) metricdata.Attachments {
:= map[string]interface{}{}
:= trace.FromContext()
if == nil {
return
}
:= .SpanContext()
if .IsSampled() {
[metricdata.AttachmentKeySpanContext] =
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |