Skip to content

Commit

Permalink
Merge pull request #2436 from DataDog/tvaleev/rum-6454/lookup-logic-i…
Browse files Browse the repository at this point in the history
…mprovements

RUM-6454: fixing potential issues with subdomain host lookup
  • Loading branch information
satween authored Dec 5, 2024
2 parents db3e1d6 + 2a36396 commit 661033b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class DefaultFirstPartyHostHeaderTypeResolver(
/** @inheritdoc */
override fun headerTypesForUrl(url: HttpUrl): Set<TracingHeaderType> {
val host = url.host
val filteredHosts = knownHosts.filter {
it.key == "*" || it.key == host || host.endsWith(".${it.key}")
}
return filteredHosts.values.flatten().toSet()

return knownHosts[host]
?: knownHosts.entries.firstOrNull { host.endsWith(".${it.key}") }?.value
?: knownHosts["*"]
?: emptySet()
}

/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,22 @@ internal class DefaultFirstPartyHostHeaderTypeResolverTest {
}
}

@Test
fun `M return correct header type W headerTypesForUrl(String) {domain and subdomain has different types}`() {
val resolver = DefaultFirstPartyHostHeaderTypeResolver(
mapOf(
"bar.com" to setOf(TracingHeaderType.DATADOG),
"foo.bar.com" to setOf(TracingHeaderType.TRACECONTEXT)
)
)

assertThat(resolver.headerTypesForUrl("http://bar.com"))
.isEqualTo(setOf(TracingHeaderType.DATADOG))

assertThat(resolver.headerTypesForUrl("http://foo.bar.com"))
.isEqualTo(setOf(TracingHeaderType.TRACECONTEXT))
}

companion object {
private const val HOST_REGEX = "([a-z][a-z0-9_~-]{3,9}\\.){1,4}[a-z][a-z0-9]{2,3}"
}
Expand Down
1 change: 1 addition & 0 deletions detekt_custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ datadog:
- "kotlin.collections.Set.any(kotlin.Function1)"
- "kotlin.collections.Set.associate(kotlin.Function1)"
- "kotlin.collections.Set.contains(com.datadog.android.trace.TracingHeaderType)"
- "kotlin.collections.Set.firstOrNull(kotlin.Function1)"
- "kotlin.collections.Set.forEach(kotlin.Function1)"
- "kotlin.collections.Set.ifEmpty(kotlin.Function0)"
- "kotlin.collections.Set.isEmpty()"
Expand Down

0 comments on commit 661033b

Please sign in to comment.