agent_stats.go 3.5 KB

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