Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SCTPTransportStats #2112

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sctptransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,20 @@ func (r *SCTPTransport) State() SCTPTransportState {
func (r *SCTPTransport) collectStats(collector *statsReportCollector) {
collector.Collecting()

stats := TransportStats{
stats := SCTPTransportStats{
Timestamp: statsTimestampFrom(time.Now()),
Type: StatsTypeTransport,
Type: StatsTypeSCTPTransport,
ID: "sctpTransport",
}

association := r.association()
if association != nil {
stats.BytesSent = association.BytesSent()
stats.BytesReceived = association.BytesReceived()
stats.SmoothedRoundTripTime = association.SRTT() * 0.001 // convert milliseconds to seconds
stats.CongestionWindow = association.CWND()
stats.ReceiverWindow = association.RWND()
stats.MTU = association.MTU()
}

collector.Collect(stats.ID, stats)
Expand Down
55 changes: 55 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
return unmarshalICECandidateStats(b)
case StatsTypeCertificate:
return unmarshalCertificateStats(b)
case StatsTypeSCTPTransport:
return unmarshalSCTPTransportStats(b)

Check warning on line 71 in stats.go

View check run for this annotation

Codecov / codecov/patch

stats.go#L70-L71

Added lines #L70 - L71 were not covered by tests
default:
return nil, fmt.Errorf("type: %w", ErrUnknownType)
}
Expand Down Expand Up @@ -132,6 +134,9 @@

// StatsTypeCertificate is used by CertificateStats.
StatsTypeCertificate StatsType = "certificate"

// StatsTypeSCTPTransport is used by SCTPTransportStats
StatsTypeSCTPTransport StatsType = "sctp-transport"
)

// MediaKind indicates the kind of media (audio or video)
Expand Down Expand Up @@ -1980,3 +1985,53 @@
}
return certificateStats, nil
}

// SCTPTransportStats contains information about a certificate used by an SCTPTransport.
type SCTPTransportStats struct {
// Timestamp is the timestamp associated with this object.
Timestamp StatsTimestamp `json:"timestamp"`

// Type is the object's StatsType
Type StatsType `json:"type"`

// ID is a unique id that is associated with the component inspected to produce
// this Stats object. Two Stats objects will have the same ID if they were produced
// by inspecting the same underlying object.
ID string `json:"id"`

// TransportID is the identifier of the object that was inspected to produce the
// RTCTransportStats for the DTLSTransport and ICETransport supporting the SCTP transport.
TransportID string `json:"transportId"`

// SmoothedRoundTripTime is the latest smoothed round-trip time value, corresponding to spinfo_srtt defined in [RFC6458]
// but converted to seconds. If there has been no round-trip time measurements yet, this value is undefined.
SmoothedRoundTripTime float64 `json:"smoothedRoundTripTime"`

// CongestionWindow is the latest congestion window, corresponding to spinfo_cwnd defined in [RFC6458].
CongestionWindow uint32 `json:"congestionWindow"`

// ReceiverWindow is the latest receiver window, corresponding to sstat_rwnd defined in [RFC6458].
ReceiverWindow uint32 `json:"receiverWindow"`

// MTU is the latest maximum transmission unit, corresponding to spinfo_mtu defined in [RFC6458].
MTU uint32 `json:"mtu"`

// UNACKData is the number of unacknowledged DATA chunks, corresponding to sstat_unackdata defined in [RFC6458].
UNACKData uint32 `json:"unackData"`

// BytesSent represents the total number of bytes sent on this SCTPTransport
BytesSent uint64 `json:"bytesSent"`

// BytesReceived represents the total number of bytes received on this SCTPTransport
BytesReceived uint64 `json:"bytesReceived"`
}

func (s SCTPTransportStats) statsMarker() {}

Check warning on line 2029 in stats.go

View check run for this annotation

Codecov / codecov/patch

stats.go#L2029

Added line #L2029 was not covered by tests

func unmarshalSCTPTransportStats(b []byte) (SCTPTransportStats, error) {
var sctpTransportStats SCTPTransportStats
if err := json.Unmarshal(b, &sctpTransportStats); err != nil {
return SCTPTransportStats{}, fmt.Errorf("unmarshal sctp transport stats: %w", err)
}
return sctpTransportStats, nil

Check warning on line 2036 in stats.go

View check run for this annotation

Codecov / codecov/patch

stats.go#L2031-L2036

Added lines #L2031 - L2036 were not covered by tests
}
13 changes: 11 additions & 2 deletions stats_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,15 @@ func getTransportStats(t *testing.T, report StatsReport, statsID string) Transpo
return transportStats
}

func getSctpTransportStats(t *testing.T, report StatsReport) SCTPTransportStats {
stats, ok := report["sctpTransport"]
assert.True(t, ok)
transportStats, ok := stats.(SCTPTransportStats)
assert.True(t, ok)
assert.Equal(t, transportStats.Type, StatsTypeSCTPTransport)
return transportStats
}

func getCertificateStats(t *testing.T, report StatsReport, certificate *Certificate) CertificateStats {
certificateStats, ok := report.GetCertificateStats(certificate)
assert.True(t, ok)
Expand Down Expand Up @@ -1314,8 +1323,8 @@ func TestPeerConnection_GetStats(t *testing.T) {
assert.GreaterOrEqual(t, offerICETransportStats.BytesSent, answerICETransportStats.BytesReceived)
assert.GreaterOrEqual(t, answerICETransportStats.BytesSent, offerICETransportStats.BytesReceived)

answerSCTPTransportStats := getTransportStats(t, reportPCAnswer, "sctpTransport")
offerSCTPTransportStats := getTransportStats(t, reportPCOffer, "sctpTransport")
answerSCTPTransportStats := getSctpTransportStats(t, reportPCAnswer)
offerSCTPTransportStats := getSctpTransportStats(t, reportPCOffer)
assert.GreaterOrEqual(t, offerSCTPTransportStats.BytesSent, answerSCTPTransportStats.BytesReceived)
assert.GreaterOrEqual(t, answerSCTPTransportStats.BytesSent, offerSCTPTransportStats.BytesReceived)

Expand Down
Loading