@@ -72,6 +72,7 @@ func NewClient(cxt context.Context, cancel context.CancelFunc, endpoint string)
7272 Timeout : time .Second ,
7373 PermitWithoutStream : true ,
7474 }),
75+ grpc .WithStreamInterceptor (setupStreamClientInterceptor ()),
7576 }
7677 if ! viper .GetBool ("dcsServiceUseSystemProxy" ) {
7778 dialOptions = append (dialOptions , grpc .WithNoProxy ())
@@ -126,6 +127,57 @@ func NewClient(cxt context.Context, cancel context.CancelFunc, endpoint string)
126127 return client
127128}
128129
130+ type measuredClientStream struct {
131+ grpc.ClientStream
132+ method string
133+ }
134+
135+ func (t * measuredClientStream ) RecvMsg (m interface {}) error {
136+ metric := monitoring .NewMetric ("dcs_stream" )
137+ metric .AddTag ("method" , t .method )
138+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
139+ err := t .ClientStream .RecvMsg (m )
140+
141+ return err
142+ }
143+
144+ func convertMethodName (method string ) (converted string ) {
145+ switch method {
146+ case dcspb .Configurator_Subscribe_FullMethodName :
147+ converted = "Subscribe"
148+ case dcspb .Configurator_PrepareForRun_FullMethodName :
149+ converted = "PFR"
150+ case dcspb .Configurator_StartOfRun_FullMethodName :
151+ converted = "SOR"
152+ case dcspb .Configurator_EndOfRun_FullMethodName :
153+ converted = "EOR"
154+ default :
155+ converted = "Unknown"
156+ }
157+ return
158+ }
159+
160+ func setupStreamClientInterceptor () grpc.StreamClientInterceptor {
161+ return func (
162+ ctx context.Context ,
163+ desc * grpc.StreamDesc ,
164+ cc * grpc.ClientConn ,
165+ method string ,
166+ streamer grpc.Streamer ,
167+ opts ... grpc.CallOption ,
168+ ) (grpc.ClientStream , error ) {
169+ clientStream , err := streamer (ctx , desc , cc , method , opts ... )
170+ if err != nil {
171+ return nil , err
172+ }
173+
174+ return & measuredClientStream {
175+ ClientStream : clientStream ,
176+ method : convertMethodName (method ),
177+ }, nil
178+ }
179+ }
180+
129181func (m * RpcClient ) GetConnState () connectivity.State {
130182 if m .conn == nil {
131183 return connectivity .Idle
@@ -140,28 +192,28 @@ func (m *RpcClient) Close() error {
140192
141193func (m * RpcClient ) Subscribe (ctx context.Context , in * dcspb.SubscriptionRequest , opts ... grpc.CallOption ) (dcspb.Configurator_SubscribeClient , error ) {
142194 metric := newMetric ()
143- metric .AddTag ("method " , "Subscribe" )
195+ metric .AddTag ("stream_setup " , "Subscribe" )
144196 defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
145197 return m .ConfiguratorClient .Subscribe (ctx , in , opts ... )
146198}
147199
148200func (m * RpcClient ) PrepareForRun (ctx context.Context , in * dcspb.PfrRequest , opts ... grpc.CallOption ) (dcspb.Configurator_PrepareForRunClient , error ) {
149201 metric := newMetric ()
150- metric .AddTag ("method " , "PFR" )
202+ metric .AddTag ("stream_setup " , "PFR" )
151203 defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
152204 return m .ConfiguratorClient .PrepareForRun (ctx , in , opts ... )
153205}
154206
155207func (m * RpcClient ) StartOfRun (ctx context.Context , in * dcspb.SorRequest , opts ... grpc.CallOption ) (dcspb.Configurator_StartOfRunClient , error ) {
156208 metric := newMetric ()
157- metric .AddTag ("method " , "SOR" )
209+ metric .AddTag ("stream_setup " , "SOR" )
158210 defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
159211 return m .ConfiguratorClient .StartOfRun (ctx , in , opts ... )
160212}
161213
162214func (m * RpcClient ) EndOfRun (ctx context.Context , in * dcspb.EorRequest , opts ... grpc.CallOption ) (dcspb.Configurator_EndOfRunClient , error ) {
163215 metric := newMetric ()
164- metric .AddTag ("method " , "EOR" )
216+ metric .AddTag ("stream_setup " , "EOR" )
165217 defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
166218 return m .ConfiguratorClient .EndOfRun (ctx , in , opts ... )
167219}
0 commit comments