@@ -32,6 +32,10 @@ type Options struct {
3232 // handling individual clients. It is not called if nil.
3333 Log func (error )
3434
35+ // CollectStats controls whether the server should collect stats on the
36+ // rpcs it serves.
37+ CollectStats bool
38+
3539 // TLSConfig, if non-nil, is used to wrap the listener with tls.NewListener
3640 // in Serve(). The TLS handshake is performed explicitly in ServeOne before
3741 // processing requests.
@@ -43,10 +47,6 @@ type Options struct {
4347 // restrictions. If it returns a non-nil error the connection is rejected.
4448 TLSCipherRestrict func (conn net.Conn ) error
4549
46- // CollectStats controls whether the server should collect stats on the
47- // rpcs it serves.
48- CollectStats bool
49-
5050 // Metrics holds optional metrics the server will populate. If nil, no
5151 // metrics are recorded.
5252 Metrics * ServerMetrics
@@ -86,32 +86,27 @@ func (m *ServerMetrics) wrapTransport(tr drpc.Transport) drpc.Transport {
8686 return & drpcmetrics.MeteredTransport {Transport : tr , BytesSent : m .BytesSent , BytesRecv : m .BytesRecv }
8787}
8888
89- // rpcLabels builds a label map for the given RPC string.
90- func (s * Server ) rpcLabels (rpc string ) map [string ]string {
91- return map [string ]string {
92- drpcmetrics .LabelRPCMethodName : rpc ,
93- }
94- }
95-
9689// recordStreamStart records stream start metrics.
97- func (s * Server ) recordStreamStart (labels map [ string ] string ) {
90+ func (s * Server ) recordStreamStart (rpc string ) {
9891 sm := s .opts .Metrics
9992 if sm != nil && sm .Stream != nil && sm .Stream .StreamsTotal != nil {
100- sm .Stream .StreamsTotal .Inc (drpcmetrics .WithState (labels ,
93+ sm .Stream .StreamsTotal .Inc (drpcmetrics .WithState (rpc ,
10194 drpcmetrics .StateStarted ), 1 )
10295 }
10396}
10497
10598// recordStreamEnd records stream end metrics.
106- func (s * Server ) recordStreamEnd (labels map [string ]string , err error , duration time.Duration ) {
99+ func (s * Server ) recordStreamEnd (rpc string , err error ,
100+ duration time.Duration ) {
107101 sm := s .opts .Metrics
108102 if sm != nil && sm .Stream != nil {
109103 if sm .Stream .StreamsTotal != nil {
110- sm .Stream .StreamsTotal .Inc (drpcmetrics .WithStateAndStatusCode (labels ,
104+ sm .Stream .StreamsTotal .Inc (drpcmetrics .WithStateAndStatusCode (rpc ,
111105 drpcmetrics .StateCompleted , err ), 1 )
112106 }
113107 if sm .Stream .StreamDuration != nil {
114- sm .Stream .StreamDuration .Observe (drpcmetrics .WithStatusCode (labels , err ),
108+ sm .Stream .StreamDuration .Observe (drpcmetrics .WithStatusCode (rpc ,
109+ err ),
115110 duration .Seconds ())
116111 }
117112 }
@@ -293,26 +288,24 @@ func (s *Server) Serve(ctx context.Context, lis net.Listener) (err error) {
293288
294289// handleRPC handles the rpc that has been requested by the stream.
295290func (s * Server ) handleRPC (stream * drpcstream.Stream , rpc string ) (err error ) {
296- var labels map [string ]string
297291 var start time.Time
298292 if s .opts .Metrics != nil {
299- labels = s .rpcLabels (rpc )
300- s .recordStreamStart (labels )
293+ s .recordStreamStart (rpc )
301294 start = time .Now ()
302295 }
303296
304297 handlerErr := s .handler .HandleRPC (stream , rpc )
305298
306299 if handlerErr != nil {
307300 sendErr := errs .Wrap (stream .SendError (handlerErr ))
308- s .recordStreamEnd (labels , handlerErr , time .Since (start ))
301+ s .recordStreamEnd (rpc , handlerErr , time .Since (start ))
309302 return sendErr
310303 }
311304
312305 closeErr := errs .Wrap (stream .CloseSend ())
313306 // Record success: the handler returned nil, so the RPC itself
314307 // succeeded. A CloseSend failure is a transport-level issue and
315308 // should not be attributed as the RPC's error code.
316- s .recordStreamEnd (labels , nil , time .Since (start ))
309+ s .recordStreamEnd (rpc , nil , time .Since (start ))
317310 return closeErr
318311}
0 commit comments