Skip to content

Commit

Permalink
Releases/v3.4.2 (#313)
Browse files Browse the repository at this point in the history
## Improvements

* feat: collect segment response headers beginning with x-litix (#312)



Co-authored-by: Emily Dixon <edixon@mux.com>
Co-authored-by: GitHub <noreply@github.com>
  • Loading branch information
daytime-em and web-flow authored Jun 7, 2023
1 parent bf5206e commit 4abd740
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.mux.stats.sdk.muxstats.internal.noneOf
import com.mux.stats.sdk.muxstats.internal.oneOf
import kotlinx.coroutines.*
import java.util.*
import java.util.regex.Pattern
import kotlin.properties.Delegates

/**
Expand Down Expand Up @@ -110,7 +111,7 @@ abstract class MuxStateCollectorBase(
/**
* Number of frames dropped per session.
*/
var numberOfDroppedFrames:Long = 0;
var numberOfDroppedFrames: Long = 0;

/**
* An asynchronous watcher for playback position. It waits for the given update interval, and
Expand Down Expand Up @@ -144,7 +145,7 @@ abstract class MuxStateCollectorBase(
* List of string patterns that will be used to determine if certain HTTP header will be
* reported to the backend.
* */
var allowedHeaders = ArrayList<String>()
var allowedHeaders = ArrayList<AllowedHeaderSpec>()


/**
Expand Down Expand Up @@ -180,10 +181,17 @@ abstract class MuxStateCollectorBase(
*/
fun allowHeaderToBeSentToBackend(headerName: String?) {
if (headerName != null) {
allowedHeaders.add(headerName)
allowedHeaders.add(AllowedHeaderSpec.ExactlyIgnoreCase(headerName))
}
}

/**
* Allow HTTP Response headers whose names match the given pattern to be sent to the backend
*/
fun allowHeaderToBeSentToBackend(headerPattern: Pattern) {
allowedHeaders.add(AllowedHeaderSpec.Matching(headerPattern))
}

/**
* Call when the player starts buffering. Buffering events after the player began playing are
* reported as rebuffering events
Expand Down Expand Up @@ -238,7 +246,7 @@ abstract class MuxStateCollectorBase(
play()
} else if (_playerState == MuxPlayerState.REBUFFERING) {
rebufferingEnded()
} else if( _playerState == MuxPlayerState.PLAYING) {
} else if (_playerState == MuxPlayerState.PLAYING) {
// No need to re-enter the playing state
return
}
Expand Down Expand Up @@ -482,9 +490,11 @@ abstract class MuxStateCollectorBase(
PlayEvent.TYPE -> {
playEventsSent++
}

PauseEvent.TYPE -> {
pauseEventsSent++
}

SeekingEvent.TYPE -> {
seekingEventsSent++
}
Expand Down Expand Up @@ -542,4 +552,26 @@ abstract class MuxStateCollectorBase(
}
}
}

sealed class AllowedHeaderSpec {

abstract fun isAllowed(headerName: String?): Boolean

class ExactlyIgnoreCase(private val name: String) : AllowedHeaderSpec() {
override fun isAllowed(headerName: String?): Boolean {
return headerName.contentEquals(name, true)
}
}

class Matching(private val pattern: Pattern) : AllowedHeaderSpec() {
override fun isAllowed(headerName: String?): Boolean {
return if (headerName != null) {
val matcher = pattern.matcher(headerName)
matcher.find()
} else {
false
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ internal class BandwidthMetricDispatcher(player: ExoPlayer,
var headerAllowed = false
synchronized (this) {
for (allowedHeader in collector!!.allowedHeaders) {
if (allowedHeader.contentEquals(headerName, true)) {
if (allowedHeader.isAllowed(headerName)) {
headerAllowed = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import com.mux.exoplayeradapter.double.FakeEventDispatcher
import com.mux.stats.sdk.core.events.playback.*
import com.mux.stats.sdk.muxstats.MuxPlayerState
import com.mux.stats.sdk.muxstats.MuxStateCollector
import com.mux.stats.sdk.muxstats.MuxStateCollectorBase
import com.mux.stats.sdk.muxstats.MuxStats
import io.mockk.mockk
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.util.regex.Pattern

class StateCollectorTests : AbsRobolectricTest() {

Expand Down Expand Up @@ -293,4 +297,40 @@ class StateCollectorTests : AbsRobolectricTest() {
)
)
}

@Test
fun testAllowedHeaderString() {
val headerNameMatches = "real-header"
val headerNameDoesntMatch = "not-the-right-header"
val spec = MuxStateCollectorBase.AllowedHeaderSpec.ExactlyIgnoreCase("real-header")

assertFalse(
"only exact matches should be allowed",
spec.isAllowed(headerNameDoesntMatch)
)
assertTrue(
"only exact matches should be allowed",
spec.isAllowed(headerNameMatches)
)
assertTrue(
"matching is case-insensitive",
spec.isAllowed(headerNameMatches.uppercase())
)
}

@Test
fun testAllowedHeaderPattern() {
val headerNameMatches = "x-litix-session-id"
val headerNameDoesntMatch = "fastcdn-log-tag-id"
val spec = MuxStateCollectorBase.AllowedHeaderSpec.Matching(Pattern.compile("^x-litix.*"))

assertFalse(
"only headers matching the regex are allowed",
spec.isAllowed(headerNameDoesntMatch)
)
assertTrue(
"only headers matching the regex are allowed",
spec.isAllowed(headerNameMatches)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.mux.stats.sdk.muxstats.internal.*
import com.mux.stats.sdk.muxstats.internal.weak
import java.lang.ref.WeakReference
import java.util.*
import java.util.regex.Pattern
import kotlin.math.ceil

/**
Expand Down Expand Up @@ -206,10 +207,12 @@ class MuxStatsExoPlayer @JvmOverloads constructor(
collector.buffering()
collector.playing()
}
// We always need these two values.

// These headers should be sent to the backend
collector.allowHeaderToBeSentToBackend("x-cdn")
collector.allowHeaderToBeSentToBackend("content-type")
collector.allowHeaderToBeSentToBackend("x-request-id")
collector.allowHeaderToBeSentToBackend(Pattern.compile("^x-litix-.*", Pattern.CASE_INSENSITIVE))
}

/**
Expand Down

0 comments on commit 4abd740

Please sign in to comment.