@@ -29,6 +29,7 @@ import (
2929 "time"
3030
3131 "github.com/AliceO2Group/Control/common/logger"
32+ "github.com/AliceO2Group/Control/common/monitoring"
3233 dcspb "github.com/AliceO2Group/Control/core/integration/dcs/protos"
3334 "github.com/sirupsen/logrus"
3435 "github.com/spf13/viper"
@@ -40,6 +41,10 @@ import (
4041
4142var log = logger .New (logrus .StandardLogger (), "dcsclient" )
4243
44+ func newMetric () monitoring.Metric {
45+ return monitoring .NewMetric ("dcs" )
46+ }
47+
4348type RpcClient struct {
4449 dcspb.ConfiguratorClient
4550 conn * grpc.ClientConn
@@ -67,6 +72,7 @@ func NewClient(cxt context.Context, cancel context.CancelFunc, endpoint string)
6772 Timeout : time .Second ,
6873 PermitWithoutStream : true ,
6974 }),
75+ grpc .WithStreamInterceptor (setupStreamClientInterceptor ()),
7076 }
7177 if ! viper .GetBool ("dcsServiceUseSystemProxy" ) {
7278 dialOptions = append (dialOptions , grpc .WithNoProxy ())
@@ -121,6 +127,57 @@ func NewClient(cxt context.Context, cancel context.CancelFunc, endpoint string)
121127 return client
122128}
123129
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+
124181func (m * RpcClient ) GetConnState () connectivity.State {
125182 if m .conn == nil {
126183 return connectivity .Idle
@@ -132,3 +189,38 @@ func (m *RpcClient) Close() error {
132189 m .cancel ()
133190 return m .conn .Close ()
134191}
192+
193+ func (m * RpcClient ) Subscribe (ctx context.Context , in * dcspb.SubscriptionRequest , opts ... grpc.CallOption ) (dcspb.Configurator_SubscribeClient , error ) {
194+ metric := newMetric ()
195+ metric .AddTag ("stream_setup" , "Subscribe" )
196+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
197+ return m .ConfiguratorClient .Subscribe (ctx , in , opts ... )
198+ }
199+
200+ func (m * RpcClient ) PrepareForRun (ctx context.Context , in * dcspb.PfrRequest , opts ... grpc.CallOption ) (dcspb.Configurator_PrepareForRunClient , error ) {
201+ metric := newMetric ()
202+ metric .AddTag ("stream_setup" , "PFR" )
203+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
204+ return m .ConfiguratorClient .PrepareForRun (ctx , in , opts ... )
205+ }
206+
207+ func (m * RpcClient ) StartOfRun (ctx context.Context , in * dcspb.SorRequest , opts ... grpc.CallOption ) (dcspb.Configurator_StartOfRunClient , error ) {
208+ metric := newMetric ()
209+ metric .AddTag ("stream_setup" , "SOR" )
210+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
211+ return m .ConfiguratorClient .StartOfRun (ctx , in , opts ... )
212+ }
213+
214+ func (m * RpcClient ) EndOfRun (ctx context.Context , in * dcspb.EorRequest , opts ... grpc.CallOption ) (dcspb.Configurator_EndOfRunClient , error ) {
215+ metric := newMetric ()
216+ metric .AddTag ("stream_setup" , "EOR" )
217+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
218+ return m .ConfiguratorClient .EndOfRun (ctx , in , opts ... )
219+ }
220+
221+ func (m * RpcClient ) GetStatus (ctx context.Context , in * dcspb.StatusRequest , opts ... grpc.CallOption ) (* dcspb.StatusReply , error ) {
222+ metric := newMetric ()
223+ metric .AddTag ("method" , "GetStatus" )
224+ defer monitoring .TimerSend (& metric , monitoring .Milliseconds )()
225+ return m .ConfiguratorClient .GetStatus (ctx , in , opts ... )
226+ }
0 commit comments