@@ -2,23 +2,30 @@ package api
2
2
3
3
import (
4
4
"context"
5
+ "time"
5
6
6
7
"github.com/centrifugal/centrifuge"
7
8
)
8
9
9
10
// apiExecutor can run API methods.
10
11
type apiExecutor struct {
11
- node * centrifuge.Node
12
+ node * centrifuge.Node
13
+ protocol string
12
14
}
13
15
14
- func newAPIExecutor (n * centrifuge.Node ) * apiExecutor {
16
+ func newAPIExecutor (n * centrifuge.Node , protocol string ) * apiExecutor {
15
17
return & apiExecutor {
16
- node : n ,
18
+ node : n ,
19
+ protocol : protocol ,
17
20
}
18
21
}
19
22
20
23
// Publish publishes data into channel.
21
24
func (h * apiExecutor ) Publish (ctx context.Context , cmd * PublishRequest ) * PublishResponse {
25
+ defer func (started time.Time ) {
26
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "publish" ).Observe (time .Since (started ).Seconds ())
27
+ }(time .Now ())
28
+
22
29
ch := cmd .Channel
23
30
data := cmd .Data
24
31
@@ -60,6 +67,9 @@ func (h *apiExecutor) Publish(ctx context.Context, cmd *PublishRequest) *Publish
60
67
61
68
// Broadcast publishes the same data into many channels.
62
69
func (h * apiExecutor ) Broadcast (ctx context.Context , cmd * BroadcastRequest ) * BroadcastResponse {
70
+ defer func (started time.Time ) {
71
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "broadcast" ).Observe (time .Since (started ).Seconds ())
72
+ }(time .Now ())
63
73
64
74
resp := & BroadcastResponse {}
65
75
@@ -125,6 +135,9 @@ func (h *apiExecutor) Broadcast(ctx context.Context, cmd *BroadcastRequest) *Bro
125
135
// Unsubscribe unsubscribes user from channel and sends unsubscribe
126
136
// control message to other nodes so they could also unsubscribe user.
127
137
func (h * apiExecutor ) Unsubscribe (ctx context.Context , cmd * UnsubscribeRequest ) * UnsubscribeResponse {
138
+ defer func (started time.Time ) {
139
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "unsibscribe" ).Observe (time .Since (started ).Seconds ())
140
+ }(time .Now ())
128
141
129
142
resp := & UnsubscribeResponse {}
130
143
@@ -157,6 +170,9 @@ func (h *apiExecutor) Unsubscribe(ctx context.Context, cmd *UnsubscribeRequest)
157
170
// Disconnect disconnects user by its ID and sends disconnect
158
171
// control message to other nodes so they could also disconnect user.
159
172
func (h * apiExecutor ) Disconnect (ctx context.Context , cmd * DisconnectRequest ) * DisconnectResponse {
173
+ defer func (started time.Time ) {
174
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "disconnect" ).Observe (time .Since (started ).Seconds ())
175
+ }(time .Now ())
160
176
161
177
resp := & DisconnectResponse {}
162
178
@@ -178,6 +194,9 @@ func (h *apiExecutor) Disconnect(ctx context.Context, cmd *DisconnectRequest) *D
178
194
179
195
// Presence returns response with presence information for channel.
180
196
func (h * apiExecutor ) Presence (ctx context.Context , cmd * PresenceRequest ) * PresenceResponse {
197
+ defer func (started time.Time ) {
198
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "presence" ).Observe (time .Since (started ).Seconds ())
199
+ }(time .Now ())
181
200
182
201
resp := & PresenceResponse {}
183
202
@@ -224,6 +243,9 @@ func (h *apiExecutor) Presence(ctx context.Context, cmd *PresenceRequest) *Prese
224
243
225
244
// PresenceStats returns response with presence stats information for channel.
226
245
func (h * apiExecutor ) PresenceStats (ctx context.Context , cmd * PresenceStatsRequest ) * PresenceStatsResponse {
246
+ defer func (started time.Time ) {
247
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "presence_stats" ).Observe (time .Since (started ).Seconds ())
248
+ }(time .Now ())
227
249
228
250
resp := & PresenceStatsResponse {}
229
251
@@ -262,6 +284,9 @@ func (h *apiExecutor) PresenceStats(ctx context.Context, cmd *PresenceStatsReque
262
284
263
285
// History returns response with history information for channel.
264
286
func (h * apiExecutor ) History (ctx context.Context , cmd * HistoryRequest ) * HistoryResponse {
287
+ defer func (started time.Time ) {
288
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "history" ).Observe (time .Since (started ).Seconds ())
289
+ }(time .Now ())
265
290
266
291
resp := & HistoryResponse {}
267
292
@@ -316,6 +341,9 @@ func (h *apiExecutor) History(ctx context.Context, cmd *HistoryRequest) *History
316
341
317
342
// HistoryRemove removes all history information for channel.
318
343
func (h * apiExecutor ) HistoryRemove (ctx context.Context , cmd * HistoryRemoveRequest ) * HistoryRemoveResponse {
344
+ defer func (started time.Time ) {
345
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "history_remove" ).Observe (time .Since (started ).Seconds ())
346
+ }(time .Now ())
319
347
320
348
resp := & HistoryRemoveResponse {}
321
349
@@ -349,6 +377,9 @@ func (h *apiExecutor) HistoryRemove(ctx context.Context, cmd *HistoryRemoveReque
349
377
350
378
// Channels returns active channels.
351
379
func (h * apiExecutor ) Channels (ctx context.Context , cmd * ChannelsRequest ) * ChannelsResponse {
380
+ defer func (started time.Time ) {
381
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "channels" ).Observe (time .Since (started ).Seconds ())
382
+ }(time .Now ())
352
383
353
384
resp := & ChannelsResponse {}
354
385
@@ -367,6 +398,9 @@ func (h *apiExecutor) Channels(ctx context.Context, cmd *ChannelsRequest) *Chann
367
398
368
399
// Info returns information about running nodes.
369
400
func (h * apiExecutor ) Info (ctx context.Context , cmd * InfoRequest ) * InfoResponse {
401
+ defer func (started time.Time ) {
402
+ apiCommandDurationSummary .WithLabelValues (h .protocol , "info" ).Observe (time .Since (started ).Seconds ())
403
+ }(time .Now ())
370
404
371
405
resp := & InfoResponse {}
372
406
0 commit comments