cubefs
312 строк · 10.4 Кб
1/*
2*
3* Copyright 2016 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 stats is for collecting and reporting various network and RPC stats.
20// This package is for monitoring purpose only. All fields are read-only.
21// All APIs are experimental.
22package stats // import "google.golang.org/grpc/stats"23
24import (25"context"26"net"27"time"28
29"google.golang.org/grpc/metadata"30)
31
32// RPCStats contains stats information about RPCs.
33type RPCStats interface {34isRPCStats()35// IsClient returns true if this RPCStats is from client side.36IsClient() bool37}
38
39// Begin contains stats when an RPC begins.
40// FailFast is only valid if this Begin is from client side.
41type Begin struct {42// Client is true if this Begin is from client side.43Client bool44// BeginTime is the time when the RPC begins.45BeginTime time.Time46// FailFast indicates if this RPC is failfast.47FailFast bool48}
49
50// IsClient indicates if the stats information is from client side.
51func (s *Begin) IsClient() bool { return s.Client }52
53func (s *Begin) isRPCStats() {}54
55// InPayload contains the information for an incoming payload.
56type InPayload struct {57// Client is true if this InPayload is from client side.58Client bool59// Payload is the payload with original type.60Payload interface{}61// Data is the serialized message payload.62Data []byte63// Length is the length of uncompressed data.64Length int65// WireLength is the length of data on wire (compressed, signed, encrypted).66WireLength int67// RecvTime is the time when the payload is received.68RecvTime time.Time69}
70
71// IsClient indicates if the stats information is from client side.
72func (s *InPayload) IsClient() bool { return s.Client }73
74func (s *InPayload) isRPCStats() {}75
76// InHeader contains stats when a header is received.
77type InHeader struct {78// Client is true if this InHeader is from client side.79Client bool80// WireLength is the wire length of header.81WireLength int82// Compression is the compression algorithm used for the RPC.83Compression string84// Header contains the header metadata received.85Header metadata.MD86
87// The following fields are valid only if Client is false.88// FullMethod is the full RPC method string, i.e., /package.service/method.89FullMethod string90// RemoteAddr is the remote address of the corresponding connection.91RemoteAddr net.Addr92// LocalAddr is the local address of the corresponding connection.93LocalAddr net.Addr94}
95
96// IsClient indicates if the stats information is from client side.
97func (s *InHeader) IsClient() bool { return s.Client }98
99func (s *InHeader) isRPCStats() {}100
101// InTrailer contains stats when a trailer is received.
102type InTrailer struct {103// Client is true if this InTrailer is from client side.104Client bool105// WireLength is the wire length of trailer.106WireLength int107// Trailer contains the trailer metadata received from the server. This108// field is only valid if this InTrailer is from the client side.109Trailer metadata.MD110}
111
112// IsClient indicates if the stats information is from client side.
113func (s *InTrailer) IsClient() bool { return s.Client }114
115func (s *InTrailer) isRPCStats() {}116
117// OutPayload contains the information for an outgoing payload.
118type OutPayload struct {119// Client is true if this OutPayload is from client side.120Client bool121// Payload is the payload with original type.122Payload interface{}123// Data is the serialized message payload.124Data []byte125// Length is the length of uncompressed data.126Length int127// WireLength is the length of data on wire (compressed, signed, encrypted).128WireLength int129// SentTime is the time when the payload is sent.130SentTime time.Time131}
132
133// IsClient indicates if this stats information is from client side.
134func (s *OutPayload) IsClient() bool { return s.Client }135
136func (s *OutPayload) isRPCStats() {}137
138// OutHeader contains stats when a header is sent.
139type OutHeader struct {140// Client is true if this OutHeader is from client side.141Client bool142// Compression is the compression algorithm used for the RPC.143Compression string144// Header contains the header metadata sent.145Header metadata.MD146
147// The following fields are valid only if Client is true.148// FullMethod is the full RPC method string, i.e., /package.service/method.149FullMethod string150// RemoteAddr is the remote address of the corresponding connection.151RemoteAddr net.Addr152// LocalAddr is the local address of the corresponding connection.153LocalAddr net.Addr154}
155
156// IsClient indicates if this stats information is from client side.
157func (s *OutHeader) IsClient() bool { return s.Client }158
159func (s *OutHeader) isRPCStats() {}160
161// OutTrailer contains stats when a trailer is sent.
162type OutTrailer struct {163// Client is true if this OutTrailer is from client side.164Client bool165// WireLength is the wire length of trailer.166//167// Deprecated: This field is never set. The length is not known when this message is168// emitted because the trailer fields are compressed with hpack after that.169WireLength int170// Trailer contains the trailer metadata sent to the client. This171// field is only valid if this OutTrailer is from the server side.172Trailer metadata.MD173}
174
175// IsClient indicates if this stats information is from client side.
176func (s *OutTrailer) IsClient() bool { return s.Client }177
178func (s *OutTrailer) isRPCStats() {}179
180// End contains stats when an RPC ends.
181type End struct {182// Client is true if this End is from client side.183Client bool184// BeginTime is the time when the RPC began.185BeginTime time.Time186// EndTime is the time when the RPC ends.187EndTime time.Time188// Trailer contains the trailer metadata received from the server. This189// field is only valid if this End is from the client side.190// Deprecated: use Trailer in InTrailer instead.191Trailer metadata.MD192// Error is the error the RPC ended with. It is an error generated from193// status.Status and can be converted back to status.Status using194// status.FromError if non-nil.195Error error196}
197
198// IsClient indicates if this is from client side.
199func (s *End) IsClient() bool { return s.Client }200
201func (s *End) isRPCStats() {}202
203// ConnStats contains stats information about connections.
204type ConnStats interface {205isConnStats()206// IsClient returns true if this ConnStats is from client side.207IsClient() bool208}
209
210// ConnBegin contains the stats of a connection when it is established.
211type ConnBegin struct {212// Client is true if this ConnBegin is from client side.213Client bool214}
215
216// IsClient indicates if this is from client side.
217func (s *ConnBegin) IsClient() bool { return s.Client }218
219func (s *ConnBegin) isConnStats() {}220
221// ConnEnd contains the stats of a connection when it ends.
222type ConnEnd struct {223// Client is true if this ConnEnd is from client side.224Client bool225}
226
227// IsClient indicates if this is from client side.
228func (s *ConnEnd) IsClient() bool { return s.Client }229
230func (s *ConnEnd) isConnStats() {}231
232type incomingTagsKey struct{}233type outgoingTagsKey struct{}234
235// SetTags attaches stats tagging data to the context, which will be sent in
236// the outgoing RPC with the header grpc-tags-bin. Subsequent calls to
237// SetTags will overwrite the values from earlier calls.
238//
239// NOTE: this is provided only for backward compatibility with existing clients
240// and will likely be removed in an upcoming release. New uses should transmit
241// this type of data using metadata with a different, non-reserved (i.e. does
242// not begin with "grpc-") header name.
243func SetTags(ctx context.Context, b []byte) context.Context {244return context.WithValue(ctx, outgoingTagsKey{}, b)245}
246
247// Tags returns the tags from the context for the inbound RPC.
248//
249// NOTE: this is provided only for backward compatibility with existing clients
250// and will likely be removed in an upcoming release. New uses should transmit
251// this type of data using metadata with a different, non-reserved (i.e. does
252// not begin with "grpc-") header name.
253func Tags(ctx context.Context) []byte {254b, _ := ctx.Value(incomingTagsKey{}).([]byte)255return b256}
257
258// SetIncomingTags attaches stats tagging data to the context, to be read by
259// the application (not sent in outgoing RPCs).
260//
261// This is intended for gRPC-internal use ONLY.
262func SetIncomingTags(ctx context.Context, b []byte) context.Context {263return context.WithValue(ctx, incomingTagsKey{}, b)264}
265
266// OutgoingTags returns the tags from the context for the outbound RPC.
267//
268// This is intended for gRPC-internal use ONLY.
269func OutgoingTags(ctx context.Context) []byte {270b, _ := ctx.Value(outgoingTagsKey{}).([]byte)271return b272}
273
274type incomingTraceKey struct{}275type outgoingTraceKey struct{}276
277// SetTrace attaches stats tagging data to the context, which will be sent in
278// the outgoing RPC with the header grpc-trace-bin. Subsequent calls to
279// SetTrace will overwrite the values from earlier calls.
280//
281// NOTE: this is provided only for backward compatibility with existing clients
282// and will likely be removed in an upcoming release. New uses should transmit
283// this type of data using metadata with a different, non-reserved (i.e. does
284// not begin with "grpc-") header name.
285func SetTrace(ctx context.Context, b []byte) context.Context {286return context.WithValue(ctx, outgoingTraceKey{}, b)287}
288
289// Trace returns the trace from the context for the inbound RPC.
290//
291// NOTE: this is provided only for backward compatibility with existing clients
292// and will likely be removed in an upcoming release. New uses should transmit
293// this type of data using metadata with a different, non-reserved (i.e. does
294// not begin with "grpc-") header name.
295func Trace(ctx context.Context) []byte {296b, _ := ctx.Value(incomingTraceKey{}).([]byte)297return b298}
299
300// SetIncomingTrace attaches stats tagging data to the context, to be read by
301// the application (not sent in outgoing RPCs). It is intended for
302// gRPC-internal use.
303func SetIncomingTrace(ctx context.Context, b []byte) context.Context {304return context.WithValue(ctx, incomingTraceKey{}, b)305}
306
307// OutgoingTrace returns the trace from the context for the outbound RPC. It is
308// intended for gRPC-internal use.
309func OutgoingTrace(ctx context.Context) []byte {310b, _ := ctx.Value(outgoingTraceKey{}).([]byte)311return b312}
313