File: README.md

package info (click to toggle)
golang-google-grpc 1.64.0-7
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-proposed-updates
  • size: 13,272 kB
  • sloc: sh: 1,096; makefile: 72
file content (58 lines) | stat: -rw-r--r-- 2,725 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Stats Monitoring Handler

This example demonstrates the use of the [`stats`](https://pkg.go.dev/google.golang.org/grpc/stats) package for reporting various  
network and RPC stats.  
_Note that all fields are READ-ONLY and the APIs of the `stats` package are  
experimental_.

## Try it

```
go run server/main.go
```

```
go run client/main.go
```

## Explanation

gRPC provides a mechanism to hook on to various events (phases) of the  
request-response network cycle through the [`stats.Handler`](https://pkg.go.dev/google.golang.org/grpc/stats#Handler) interface. To access  
these events, a concrete type that implements `stats.Handler` should be passed to  
`grpc.WithStatsHandler()` on the client side and `grpc.StatsHandler()` on the  
server side.

The `HandleRPC(context.Context, RPCStats)` method on `stats.Handler` is called  
multiple times during a request-response cycle, and various event stats are  
passed to its `RPCStats` parameter (an interface). The concrete types that  
implement this interface are: `*stats.Begin`, `*stats.InHeader`, `*stats.InPayload`,  
`*stats.InTrailer`, `*stats.OutHeader`, `*stats.OutPayload`, `*stats.OutTrailer`, and  
`*stats.End`. The order of these events differs on client and server.

Similarly, the `HandleConn(context.Context, ConnStats)` method on `stats.Handler`  
is called twice, once at the beginning of the connection with `*stats.ConnBegin`  
and once at the end with `*stats.ConnEnd`.

The [`stats.Handler`](https://pkg.go.dev/google.golang.org/grpc/stats#Handler) interface also provides  
`TagRPC(context.Context, *RPCTagInfo) context.Context` and  
`TagConn(context.Context, *ConnTagInfo) context.Context` methods. These methods  
are mainly used to attach network related information to the given context.

The `TagRPC(context.Context, *RPCTagInfo) context.Context` method returns a  
context from which the context used for the rest lifetime of the RPC will be  
derived. This behavior is consistent between the gRPC client and server.

The context returned from  
`TagConn(context.Context, *ConnTagInfo) context.Context` has varied lifespan:

- In the gRPC client:  
  The context used for the rest lifetime of the RPC will NOT be derived from  
  this context. Hence the information attached to this context can only be  
  consumed by `HandleConn(context.Context, ConnStats)` method.
- In the gRPC server:  
  The context used for the rest lifetime of the RPC will be derived from  
  this context.

NOTE: The [stats](https://pkg.go.dev/google.golang.org/grpc/stats) package should only be used for network monitoring purposes,  
and not as an alternative to [interceptors](https://github.com/grpc/grpc-go/blob/master/examples/features/interceptor).