| 1 |
package centrifuge |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
"sync" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/centrifugal/protocol" |
| 10 |
|
| 11 |
"github.com/prometheus/client_golang/prometheus" |
| 12 |
) |
| 13 |
|
| 14 |
// default namespace for prometheus metrics. Can be changed over Config. |
| 15 |
var defaultMetricsNamespace = "centrifuge" |
| 16 |
|
| 17 |
var registryMu sync.RWMutex |
| 18 |
|
| 19 |
var ( |
| 20 |
messagesSentCount *prometheus.CounterVec |
| 21 |
messagesReceivedCount *prometheus.CounterVec |
| 22 |
actionCount *prometheus.CounterVec |
| 23 |
buildInfoGauge *prometheus.GaugeVec |
| 24 |
numClientsGauge prometheus.Gauge |
| 25 |
numUsersGauge prometheus.Gauge |
| 26 |
numChannelsGauge prometheus.Gauge |
| 27 |
numNodesGauge prometheus.Gauge |
| 28 |
replyErrorCount *prometheus.CounterVec |
| 29 |
serverDisconnectCount *prometheus.CounterVec |
| 30 |
commandDurationSummary *prometheus.SummaryVec |
| 31 |
recoverCount *prometheus.CounterVec |
| 32 |
transportConnectCount *prometheus.CounterVec |
| 33 |
transportMessagesSent *prometheus.CounterVec |
| 34 |
|
| 35 |
messagesReceivedCountPublication prometheus.Counter |
| 36 |
messagesReceivedCountJoin prometheus.Counter |
| 37 |
messagesReceivedCountLeave prometheus.Counter |
| 38 |
messagesReceivedCountControl prometheus.Counter |
| 39 |
|
| 40 |
messagesSentCountPublication prometheus.Counter |
| 41 |
messagesSentCountJoin prometheus.Counter |
| 42 |
messagesSentCountLeave prometheus.Counter |
| 43 |
messagesSentCountControl prometheus.Counter |
| 44 |
|
| 45 |
actionCountAddClient prometheus.Counter |
| 46 |
actionCountRemoveClient prometheus.Counter |
| 47 |
actionCountAddSub prometheus.Counter |
| 48 |
actionCountRemoveSub prometheus.Counter |
| 49 |
actionCountAddPresence prometheus.Counter |
| 50 |
actionCountRemovePresence prometheus.Counter |
| 51 |
actionCountPresence prometheus.Counter |
| 52 |
actionCountPresenceStats prometheus.Counter |
| 53 |
actionCountHistory prometheus.Counter |
| 54 |
actionCountHistoryRecover prometheus.Counter |
| 55 |
actionCountHistoryStreamTop prometheus.Counter |
| 56 |
actionCountHistoryRemove prometheus.Counter |
| 57 |
actionCountSurvey prometheus.Counter |
| 58 |
|
| 59 |
recoverCountYes prometheus.Counter |
| 60 |
recoverCountNo prometheus.Counter |
| 61 |
|
| 62 |
transportConnectCountWebsocket prometheus.Counter |
| 63 |
transportConnectCountSockJS prometheus.Counter |
| 64 |
|
| 65 |
transportMessagesSentWebsocket prometheus.Counter |
| 66 |
transportMessagesSentSockJS prometheus.Counter |
| 67 |
|
| 68 |
commandDurationConnect prometheus.Observer |
| 69 |
commandDurationSubscribe prometheus.Observer |
| 70 |
commandDurationUnsubscribe prometheus.Observer |
| 71 |
commandDurationPublish prometheus.Observer |
| 72 |
commandDurationPresence prometheus.Observer |
| 73 |
commandDurationPresenceStats prometheus.Observer |
| 74 |
commandDurationHistory prometheus.Observer |
| 75 |
commandDurationPing prometheus.Observer |
| 76 |
commandDurationSend prometheus.Observer |
| 77 |
commandDurationRPC prometheus.Observer |
| 78 |
commandDurationRefresh prometheus.Observer |
| 79 |
commandDurationSubRefresh prometheus.Observer |
| 80 |
commandDurationUnknown prometheus.Observer |
| 81 |
) |
| 82 |
|
| 83 |
func observeCommandDuration(method protocol.MethodType, d time.Duration) { |
| 84 |
registryMu.RLock() |
| 85 |
defer registryMu.RUnlock() |
| 86 |
|
| 87 |
var observer prometheus.Observer |
| 88 |
|
| 89 |
switch method { |
| 90 |
case protocol.MethodTypeConnect: |
| 91 |
observer = commandDurationConnect |
| 92 |
case protocol.MethodTypeSubscribe: |
| 93 |
observer = commandDurationSubscribe |
| 94 |
case protocol.MethodTypeUnsubscribe: |
| 95 |
observer = commandDurationUnsubscribe |
| 96 |
case protocol.MethodTypePublish: |
| 97 |
observer = commandDurationPublish |
| 98 |
case protocol.MethodTypePresence: |
| 99 |
observer = commandDurationPresence |
| 100 |
case protocol.MethodTypePresenceStats: |
| 101 |
observer = commandDurationPresenceStats |
| 102 |
case protocol.MethodTypeHistory: |
| 103 |
observer = commandDurationHistory |
| 104 |
case protocol.MethodTypePing: |
| 105 |
observer = commandDurationPing |
| 106 |
case protocol.MethodTypeSend: |
| 107 |
observer = commandDurationSend |
| 108 |
case protocol.MethodTypeRPC: |
| 109 |
observer = commandDurationRPC |
| 110 |
case protocol.MethodTypeRefresh: |
| 111 |
observer = commandDurationRefresh |
| 112 |
case protocol.MethodTypeSubRefresh: |
| 113 |
observer = commandDurationSubRefresh |
| 114 |
default: |
| 115 |
observer = commandDurationUnknown |
| 116 |
} |
| 117 |
observer.Observe(d.Seconds()) |
| 118 |
} |
| 119 |
|
| 120 |
func setBuildInfo(version string) { |
| 121 |
registryMu.RLock() |
| 122 |
defer registryMu.RUnlock() |
| 123 |
|
| 124 |
buildInfoGauge.WithLabelValues(version).Set(1) |
| 125 |
} |
| 126 |
|
| 127 |
func setNumClients(n float64) { |
| 128 |
registryMu.RLock() |
| 129 |
defer registryMu.RUnlock() |
| 130 |
|
| 131 |
numClientsGauge.Set(n) |
| 132 |
} |
| 133 |
|
| 134 |
func setNumUsers(n float64) { |
| 135 |
registryMu.RLock() |
| 136 |
defer registryMu.RUnlock() |
| 137 |
|
| 138 |
numUsersGauge.Set(n) |
| 139 |
} |
| 140 |
|
| 141 |
func setNumChannels(n float64) { |
| 142 |
registryMu.RLock() |
| 143 |
defer registryMu.RUnlock() |
| 144 |
|
| 145 |
numChannelsGauge.Set(n) |
| 146 |
} |
| 147 |
|
| 148 |
func setNumNodes(n float64) { |
| 149 |
registryMu.RLock() |
| 150 |
defer registryMu.RUnlock() |
| 151 |
|
| 152 |
numNodesGauge.Set(n) |
| 153 |
} |
| 154 |
|
| 155 |
func incReplyError(method protocol.MethodType, code uint32) { |
| 156 |
registryMu.RLock() |
| 157 |
defer registryMu.RUnlock() |
| 158 |
|
| 159 |
replyErrorCount.WithLabelValues(strings.ToLower(protocol.MethodType_name[int32(method)]), strconv.FormatUint(uint64(code), 10)).Inc() |
| 160 |
} |
| 161 |
|
| 162 |
func incRecover(success bool) { |
| 163 |
registryMu.RLock() |
| 164 |
defer registryMu.RUnlock() |
| 165 |
|
| 166 |
if success { |
| 167 |
recoverCountYes.Inc() |
| 168 |
} else { |
| 169 |
recoverCountNo.Inc() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func incTransportConnect(transport string) { |
| 174 |
registryMu.RLock() |
| 175 |
defer registryMu.RUnlock() |
| 176 |
|
| 177 |
switch transport { |
| 178 |
case transportWebsocket: |
| 179 |
transportConnectCountWebsocket.Inc() |
| 180 |
case transportSockJS: |
| 181 |
transportConnectCountSockJS.Inc() |
| 182 |
default: |
| 183 |
transportConnectCount.WithLabelValues(transport).Inc() |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func incTransportMessagesSent(transport string) { |
| 188 |
registryMu.RLock() |
| 189 |
defer registryMu.RUnlock() |
| 190 |
|
| 191 |
switch transport { |
| 192 |
case transportWebsocket: |
| 193 |
transportMessagesSentWebsocket.Inc() |
| 194 |
case transportSockJS: |
| 195 |
transportMessagesSentSockJS.Inc() |
| 196 |
default: |
| 197 |
transportMessagesSent.WithLabelValues(transport).Inc() |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func addTransportMessagesSent(transport string, n float64) { |
| 202 |
registryMu.RLock() |
| 203 |
defer registryMu.RUnlock() |
| 204 |
|
| 205 |
switch transport { |
| 206 |
case transportWebsocket: |
| 207 |
transportMessagesSentWebsocket.Add(n) |
| 208 |
case transportSockJS: |
| 209 |
transportMessagesSentSockJS.Add(n) |
| 210 |
default: |
| 211 |
transportMessagesSent.WithLabelValues(transport).Add(n) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func incServerDisconnect(code uint32) { |
| 216 |
registryMu.RLock() |
| 217 |
defer registryMu.RUnlock() |
| 218 |
|
| 219 |
serverDisconnectCount.WithLabelValues(strconv.FormatUint(uint64(code), 10)).Inc() |
| 220 |
} |
| 221 |
|
| 222 |
func incMessagesSent(msgType string) { |
| 223 |
registryMu.RLock() |
| 224 |
defer registryMu.RUnlock() |
| 225 |
|
| 226 |
switch msgType { |
| 227 |
case "publication": |
| 228 |
messagesSentCountPublication.Inc() |
| 229 |
case "join": |
| 230 |
messagesSentCountJoin.Inc() |
| 231 |
case "leave": |
| 232 |
messagesSentCountLeave.Inc() |
| 233 |
case "control": |
| 234 |
messagesSentCountControl.Inc() |
| 235 |
default: |
| 236 |
messagesSentCount.WithLabelValues(msgType).Inc() |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func incMessagesReceived(msgType string) { |
| 241 |
registryMu.RLock() |
| 242 |
defer registryMu.RUnlock() |
| 243 |
|
| 244 |
switch msgType { |
| 245 |
case "publication": |
| 246 |
messagesReceivedCountPublication.Inc() |
| 247 |
case "join": |
| 248 |
messagesReceivedCountJoin.Inc() |
| 249 |
case "leave": |
| 250 |
messagesReceivedCountLeave.Inc() |
| 251 |
case "control": |
| 252 |
messagesReceivedCountControl.Inc() |
| 253 |
default: |
| 254 |
messagesReceivedCount.WithLabelValues(msgType).Inc() |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
func incActionCount(action string) { |
| 259 |
registryMu.RLock() |
| 260 |
defer registryMu.RUnlock() |
| 261 |
|
| 262 |
switch action { |
| 263 |
case "add_client": |
| 264 |
actionCountAddClient.Inc() |
| 265 |
case "remove_client": |
| 266 |
actionCountRemoveClient.Inc() |
| 267 |
case "add_subscription": |
| 268 |
actionCountAddSub.Inc() |
| 269 |
case "remove_subscription": |
| 270 |
actionCountRemoveSub.Inc() |
| 271 |
case "add_presence": |
| 272 |
actionCountAddPresence.Inc() |
| 273 |
case "remove_presence": |
| 274 |
actionCountRemovePresence.Inc() |
| 275 |
case "presence": |
| 276 |
actionCountPresence.Inc() |
| 277 |
case "presence_stats": |
| 278 |
actionCountPresenceStats.Inc() |
| 279 |
case "history": |
| 280 |
actionCountHistory.Inc() |
| 281 |
case "history_recover": |
| 282 |
actionCountHistoryRecover.Inc() |
| 283 |
case "history_stream_top": |
| 284 |
actionCountHistoryStreamTop.Inc() |
| 285 |
case "history_remove": |
| 286 |
actionCountHistoryRemove.Inc() |
| 287 |
case "survey": |
| 288 |
actionCountSurvey.Inc() |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func initMetricsRegistry(registry prometheus.Registerer, metricsNamespace string) error { |
| 293 |
registryMu.Lock() |
| 294 |
defer registryMu.Unlock() |
| 295 |
|
| 296 |
if metricsNamespace == "" { |
| 297 |
metricsNamespace = defaultMetricsNamespace |
| 298 |
} |
| 299 |
if registry == nil { |
| 300 |
registry = prometheus.DefaultRegisterer |
| 301 |
} |
| 302 |
|
| 303 |
messagesSentCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 304 |
Namespace: metricsNamespace, |
| 305 |
Subsystem: "node", |
| 306 |
Name: "messages_sent_count", |
| 307 |
Help: "Number of messages sent.", |
| 308 |
}, []string{"type"}) |
| 309 |
|
| 310 |
messagesReceivedCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 311 |
Namespace: metricsNamespace, |
| 312 |
Subsystem: "node", |
| 313 |
Name: "messages_received_count", |
| 314 |
Help: "Number of messages received.", |
| 315 |
}, []string{"type"}) |
| 316 |
|
| 317 |
actionCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 318 |
Namespace: metricsNamespace, |
| 319 |
Subsystem: "node", |
| 320 |
Name: "action_count", |
| 321 |
Help: "Number of node actions called.", |
| 322 |
}, []string{"action"}) |
| 323 |
|
| 324 |
numClientsGauge = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 325 |
Namespace: metricsNamespace, |
| 326 |
Subsystem: "node", |
| 327 |
Name: "num_clients", |
| 328 |
Help: "Number of clients connected.", |
| 329 |
}) |
| 330 |
|
| 331 |
numUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 332 |
Namespace: metricsNamespace, |
| 333 |
Subsystem: "node", |
| 334 |
Name: "num_users", |
| 335 |
Help: "Number of unique users connected.", |
| 336 |
}) |
| 337 |
|
| 338 |
numNodesGauge = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 339 |
Namespace: metricsNamespace, |
| 340 |
Subsystem: "node", |
| 341 |
Name: "num_nodes", |
| 342 |
Help: "Number of nodes in cluster.", |
| 343 |
}) |
| 344 |
|
| 345 |
buildInfoGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 346 |
Namespace: metricsNamespace, |
| 347 |
Subsystem: "node", |
| 348 |
Name: "build", |
| 349 |
Help: "Node build info.", |
| 350 |
}, []string{"version"}) |
| 351 |
|
| 352 |
numChannelsGauge = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 353 |
Namespace: metricsNamespace, |
| 354 |
Subsystem: "node", |
| 355 |
Name: "num_channels", |
| 356 |
Help: "Number of channels with one or more subscribers.", |
| 357 |
}) |
| 358 |
|
| 359 |
replyErrorCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 360 |
Namespace: metricsNamespace, |
| 361 |
Subsystem: "client", |
| 362 |
Name: "num_reply_errors", |
| 363 |
Help: "Number of errors in replies sent to clients.", |
| 364 |
}, []string{"method", "code"}) |
| 365 |
|
| 366 |
serverDisconnectCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 367 |
Namespace: metricsNamespace, |
| 368 |
Subsystem: "client", |
| 369 |
Name: "num_server_disconnects", |
| 370 |
Help: "Number of server initiated disconnects.", |
| 371 |
}, []string{"code"}) |
| 372 |
|
| 373 |
commandDurationSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 374 |
Namespace: metricsNamespace, |
| 375 |
Subsystem: "client", |
| 376 |
Name: "command_duration_seconds", |
| 377 |
Objectives: map[float64]float64{0.5: 0.05, 0.99: 0.001, 0.999: 0.0001}, |
| 378 |
Help: "Client command duration summary.", |
| 379 |
}, []string{"method"}) |
| 380 |
|
| 381 |
recoverCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 382 |
Namespace: metricsNamespace, |
| 383 |
Subsystem: "client", |
| 384 |
Name: "recover", |
| 385 |
Help: "Count of recover operations.", |
| 386 |
}, []string{"recovered"}) |
| 387 |
|
| 388 |
transportConnectCount = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 389 |
Namespace: metricsNamespace, |
| 390 |
Subsystem: "transport", |
| 391 |
Name: "connect_count", |
| 392 |
Help: "Number of connections to specific transport.", |
| 393 |
}, []string{"transport"}) |
| 394 |
|
| 395 |
transportMessagesSent = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 396 |
Namespace: metricsNamespace, |
| 397 |
Subsystem: "transport", |
| 398 |
Name: "messages_sent", |
| 399 |
Help: "Number of messages sent over specific transport.", |
| 400 |
}, []string{"transport"}) |
| 401 |
|
| 402 |
messagesReceivedCountPublication = messagesReceivedCount.WithLabelValues("publication") |
| 403 |
messagesReceivedCountJoin = messagesReceivedCount.WithLabelValues("join") |
| 404 |
messagesReceivedCountLeave = messagesReceivedCount.WithLabelValues("leave") |
| 405 |
messagesReceivedCountControl = messagesReceivedCount.WithLabelValues("control") |
| 406 |
|
| 407 |
messagesSentCountPublication = messagesSentCount.WithLabelValues("publication") |
| 408 |
messagesSentCountJoin = messagesSentCount.WithLabelValues("join") |
| 409 |
messagesSentCountLeave = messagesSentCount.WithLabelValues("leave") |
| 410 |
messagesSentCountControl = messagesSentCount.WithLabelValues("control") |
| 411 |
|
| 412 |
actionCountAddClient = actionCount.WithLabelValues("add_client") |
| 413 |
actionCountRemoveClient = actionCount.WithLabelValues("remove_client") |
| 414 |
actionCountAddSub = actionCount.WithLabelValues("add_subscription") |
| 415 |
actionCountRemoveSub = actionCount.WithLabelValues("remove_subscription") |
| 416 |
actionCountAddPresence = actionCount.WithLabelValues("add_presence") |
| 417 |
actionCountRemovePresence = actionCount.WithLabelValues("remove_presence") |
| 418 |
actionCountPresence = actionCount.WithLabelValues("presence") |
| 419 |
actionCountPresenceStats = actionCount.WithLabelValues("presence_stats") |
| 420 |
actionCountHistory = actionCount.WithLabelValues("history") |
| 421 |
actionCountHistoryRecover = actionCount.WithLabelValues("history_recover") |
| 422 |
actionCountHistoryStreamTop = actionCount.WithLabelValues("history_stream_top") |
| 423 |
actionCountHistoryRemove = actionCount.WithLabelValues("history_remove") |
| 424 |
actionCountSurvey = actionCount.WithLabelValues("survey") |
| 425 |
|
| 426 |
recoverCountYes = recoverCount.WithLabelValues("yes") |
| 427 |
recoverCountNo = recoverCount.WithLabelValues("no") |
| 428 |
|
| 429 |
transportConnectCountWebsocket = transportConnectCount.WithLabelValues(transportWebsocket) |
| 430 |
transportConnectCountSockJS = transportConnectCount.WithLabelValues(transportSockJS) |
| 431 |
|
| 432 |
transportMessagesSentWebsocket = transportMessagesSent.WithLabelValues(transportWebsocket) |
| 433 |
transportMessagesSentSockJS = transportMessagesSent.WithLabelValues(transportSockJS) |
| 434 |
|
| 435 |
labelForMethod := func(methodType protocol.MethodType) string { |
| 436 |
return strings.ToLower(protocol.MethodType_name[int32(methodType)]) |
| 437 |
} |
| 438 |
|
| 439 |
commandDurationConnect = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeConnect)) |
| 440 |
commandDurationSubscribe = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeSubscribe)) |
| 441 |
commandDurationUnsubscribe = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeUnsubscribe)) |
| 442 |
commandDurationPublish = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypePublish)) |
| 443 |
commandDurationPresence = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypePresence)) |
| 444 |
commandDurationPresenceStats = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypePresenceStats)) |
| 445 |
commandDurationHistory = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeHistory)) |
| 446 |
commandDurationPing = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypePing)) |
| 447 |
commandDurationSend = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeSend)) |
| 448 |
commandDurationRPC = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeRPC)) |
| 449 |
commandDurationRefresh = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeRefresh)) |
| 450 |
commandDurationSubRefresh = commandDurationSummary.WithLabelValues(labelForMethod(protocol.MethodTypeSubRefresh)) |
| 451 |
commandDurationUnknown = commandDurationSummary.WithLabelValues("unknown") |
| 452 |
|
| 453 |
if err := registry.Register(messagesSentCount); err != nil { |
| 454 |
return err |
| 455 |
} |
| 456 |
if err := registry.Register(messagesReceivedCount); err != nil { |
| 457 |
return err |
| 458 |
} |
| 459 |
if err := registry.Register(actionCount); err != nil { |
| 460 |
return err |
| 461 |
} |
| 462 |
if err := registry.Register(numClientsGauge); err != nil { |
| 463 |
return err |
| 464 |
} |
| 465 |
if err := registry.Register(numUsersGauge); err != nil { |
| 466 |
return err |
| 467 |
} |
| 468 |
if err := registry.Register(numChannelsGauge); err != nil { |
| 469 |
return err |
| 470 |
} |
| 471 |
if err := registry.Register(numNodesGauge); err != nil { |
| 472 |
return err |
| 473 |
} |
| 474 |
if err := registry.Register(commandDurationSummary); err != nil { |
| 475 |
return err |
| 476 |
} |
| 477 |
if err := registry.Register(replyErrorCount); err != nil { |
| 478 |
return err |
| 479 |
} |
| 480 |
if err := registry.Register(serverDisconnectCount); err != nil { |
| 481 |
return err |
| 482 |
} |
| 483 |
if err := registry.Register(recoverCount); err != nil { |
| 484 |
return err |
| 485 |
} |
| 486 |
if err := registry.Register(transportConnectCount); err != nil { |
| 487 |
return err |
| 488 |
} |
| 489 |
if err := registry.Register(transportMessagesSent); err != nil { |
| 490 |
return err |
| 491 |
} |
| 492 |
if err := registry.Register(buildInfoGauge); err != nil { |
| 493 |
return err |
| 494 |
} |
| 495 |
|
| 496 |
return nil |
| 497 |
} |