agent_stats.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package ice
  2. import (
  3. "context"
  4. "time"
  5. )
  6. // GetCandidatePairsStats returns a list of candidate pair stats
  7. func (a *Agent) GetCandidatePairsStats() []CandidatePairStats {
  8. var res []CandidatePairStats
  9. err := a.run(a.context(), func(ctx context.Context, agent *Agent) {
  10. result := make([]CandidatePairStats, 0, len(agent.checklist))
  11. for _, cp := range agent.checklist {
  12. stat := CandidatePairStats{
  13. Timestamp: time.Now(),
  14. LocalCandidateID: cp.Local.ID(),
  15. RemoteCandidateID: cp.Remote.ID(),
  16. State: cp.state,
  17. Nominated: cp.nominated,
  18. // PacketsSent uint32
  19. // PacketsReceived uint32
  20. // BytesSent uint64
  21. // BytesReceived uint64
  22. // LastPacketSentTimestamp time.Time
  23. // LastPacketReceivedTimestamp time.Time
  24. // FirstRequestTimestamp time.Time
  25. // LastRequestTimestamp time.Time
  26. // LastResponseTimestamp time.Time
  27. // TotalRoundTripTime float64
  28. // CurrentRoundTripTime float64
  29. // AvailableOutgoingBitrate float64
  30. // AvailableIncomingBitrate float64
  31. // CircuitBreakerTriggerCount uint32
  32. // RequestsReceived uint64
  33. // RequestsSent uint64
  34. // ResponsesReceived uint64
  35. // ResponsesSent uint64
  36. // RetransmissionsReceived uint64
  37. // RetransmissionsSent uint64
  38. // ConsentRequestsSent uint64
  39. // ConsentExpiredTimestamp time.Time
  40. }
  41. result = append(result, stat)
  42. }
  43. res = result
  44. })
  45. if err != nil {
  46. a.log.Errorf("error getting candidate pairs stats %v", err)
  47. return []CandidatePairStats{}
  48. }
  49. return res
  50. }
  51. // GetLocalCandidatesStats returns a list of local candidates stats
  52. func (a *Agent) GetLocalCandidatesStats() []CandidateStats {
  53. var res []CandidateStats
  54. err := a.run(a.context(), func(ctx context.Context, agent *Agent) {
  55. result := make([]CandidateStats, 0, len(agent.localCandidates))
  56. for networkType, localCandidates := range agent.localCandidates {
  57. for _, c := range localCandidates {
  58. relayProtocol := ""
  59. if c.Type() == CandidateTypeRelay {
  60. relayProtocol = c.(*CandidateRelay).RelayProtocol()
  61. }
  62. stat := CandidateStats{
  63. Timestamp: time.Now(),
  64. ID: c.ID(),
  65. NetworkType: networkType,
  66. IP: c.Address(),
  67. Port: c.Port(),
  68. CandidateType: c.Type(),
  69. Priority: c.Priority(),
  70. // URL string
  71. RelayProtocol: relayProtocol,
  72. // Deleted bool
  73. }
  74. result = append(result, stat)
  75. }
  76. }
  77. res = result
  78. })
  79. if err != nil {
  80. a.log.Errorf("error getting candidate pairs stats %v", err)
  81. return []CandidateStats{}
  82. }
  83. return res
  84. }
  85. // GetRemoteCandidatesStats returns a list of remote candidates stats
  86. func (a *Agent) GetRemoteCandidatesStats() []CandidateStats {
  87. var res []CandidateStats
  88. err := a.run(a.context(), func(ctx context.Context, agent *Agent) {
  89. result := make([]CandidateStats, 0, len(agent.remoteCandidates))
  90. for networkType, localCandidates := range agent.remoteCandidates {
  91. for _, c := range localCandidates {
  92. stat := CandidateStats{
  93. Timestamp: time.Now(),
  94. ID: c.ID(),
  95. NetworkType: networkType,
  96. IP: c.Address(),
  97. Port: c.Port(),
  98. CandidateType: c.Type(),
  99. Priority: c.Priority(),
  100. // URL string
  101. RelayProtocol: "",
  102. }
  103. result = append(result, stat)
  104. }
  105. }
  106. res = result
  107. })
  108. if err != nil {
  109. a.log.Errorf("error getting candidate pairs stats %v", err)
  110. return []CandidateStats{}
  111. }
  112. return res
  113. }