You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

monitoring.go 2.8 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package event // import "go.mongodb.org/mongo-driver/event"
  7. import (
  8. "context"
  9. "go.mongodb.org/mongo-driver/bson"
  10. )
  11. // CommandStartedEvent represents an event generated when a command is sent to a server.
  12. type CommandStartedEvent struct {
  13. Command bson.Raw
  14. DatabaseName string
  15. CommandName string
  16. RequestID int64
  17. ConnectionID string
  18. }
  19. // CommandFinishedEvent represents a generic command finishing.
  20. type CommandFinishedEvent struct {
  21. DurationNanos int64
  22. CommandName string
  23. RequestID int64
  24. ConnectionID string
  25. }
  26. // CommandSucceededEvent represents an event generated when a command's execution succeeds.
  27. type CommandSucceededEvent struct {
  28. CommandFinishedEvent
  29. Reply bson.Raw
  30. }
  31. // CommandFailedEvent represents an event generated when a command's execution fails.
  32. type CommandFailedEvent struct {
  33. CommandFinishedEvent
  34. Failure string
  35. }
  36. // CommandMonitor represents a monitor that is triggered for different events.
  37. type CommandMonitor struct {
  38. Started func(context.Context, *CommandStartedEvent)
  39. Succeeded func(context.Context, *CommandSucceededEvent)
  40. Failed func(context.Context, *CommandFailedEvent)
  41. }
  42. // strings for pool command monitoring reasons
  43. const (
  44. ReasonIdle = "idle"
  45. ReasonPoolClosed = "poolClosed"
  46. ReasonStale = "stale"
  47. ReasonConnectionErrored = "connectionError"
  48. ReasonTimedOut = "timeout"
  49. )
  50. // strings for pool command monitoring types
  51. const (
  52. ConnectionClosed = "ConnectionClosed"
  53. PoolCreated = "ConnectionPoolCreated"
  54. ConnectionCreated = "ConnectionCreated"
  55. GetFailed = "ConnectionCheckOutFailed"
  56. GetSucceeded = "ConnectionCheckedOut"
  57. ConnectionReturned = "ConnectionCheckedIn"
  58. PoolCleared = "ConnectionPoolCleared"
  59. PoolClosedEvent = "ConnectionPoolClosed"
  60. )
  61. // MonitorPoolOptions contains pool options as formatted in pool events
  62. type MonitorPoolOptions struct {
  63. MaxPoolSize uint64 `json:"maxPoolSize"`
  64. MinPoolSize uint64 `json:"minPoolSize"`
  65. WaitQueueTimeoutMS uint64 `json:"maxIdleTimeMS"`
  66. }
  67. // PoolEvent contains all information summarizing a pool event
  68. type PoolEvent struct {
  69. Type string `json:"type"`
  70. Address string `json:"address"`
  71. ConnectionID uint64 `json:"connectionId"`
  72. PoolOptions *MonitorPoolOptions `json:"options"`
  73. Reason string `json:"reason"`
  74. }
  75. // PoolMonitor is a function that allows the user to gain access to events occurring in the pool
  76. type PoolMonitor struct {
  77. Event func(*PoolEvent)
  78. }