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

RUMM-2448: Add variant tag to logs #1025

Merged
merged 1 commit into from
Aug 31, 2022
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
1 change: 1 addition & 0 deletions dd-sdk-android/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ object com.datadog.android.log.LogAttributes
const val USR_EMAIL: String
const val USR_ID: String
const val USR_NAME: String
const val VARIANT: String
class com.datadog.android.log.Logger
fun v(String, Throwable? = null, Map<String, Any?> = emptyMap())
fun d(String, Throwable? = null, Map<String, Any?> = emptyMap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ internal object CoreFeature {
timeProvider,
sdkVersion,
envName,
packageVersion
packageVersion,
variant
),
NdkCrashLogDeserializer(sdkLogger),
RumEventDeserializer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ internal object CrashReportsFeature : SdkFeature<LogEvent, Configuration.Feature
CoreFeature.timeProvider,
CoreFeature.sdkVersion,
CoreFeature.envName,
CoreFeature.packageVersion
CoreFeature.packageVersion,
CoreFeature.variant
),
writer = persistenceStrategy.getWriter(),
appContext = appContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,10 @@ object LogAttributes {
* @see [Datadog.setUserInfo]
*/
const val USR_NAME: String = "$USR_ATTRIBUTES_GROUP.name"

/**
* The application variant. (String)
* This value is filled automatically by the [Logger].
*/
const val VARIANT: String = "variant"
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ internal constructor(internal var handler: LogHandler) {
CoreFeature.timeProvider,
CoreFeature.sdkVersion,
CoreFeature.envName,
CoreFeature.packageVersion
CoreFeature.packageVersion,
CoreFeature.variant
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ internal class LogGenerator(
internal val timeProvider: TimeProvider,
internal val sdkVersion: String,
envName: String,
appVersion: String
appVersion: String,
variant: String
) {

private val simpleDateFormat = buildLogDateFormat()
Expand All @@ -43,6 +44,12 @@ internal class LogGenerator(
null
}

private val variantTag = if (variant.isNotEmpty()) {
"${LogAttributes.VARIANT}:$variant"
} else {
null
}

@Suppress("LongParameterList")
fun generateLog(
level: Int,
Expand Down Expand Up @@ -124,6 +131,9 @@ internal class LogGenerator(
appVersionTag?.let {
combinedTags.add(it)
}
variantTag?.let {
combinedTags.add(it)
}

return combinedTags
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ internal class DatadogExceptionHandlerTest {
@StringForgery(regex = "[a-zA-Z0-9_:./-]{0,195}[a-zA-Z0-9_./-]")
lateinit var fakeEnvName: String

@StringForgery
lateinit var fakeVariant: String

@BeforeEach
fun `set up`() {
mockChoreographerInstance()
Expand All @@ -132,7 +135,7 @@ internal class DatadogExceptionHandlerTest {

Datadog.initialize(
appContext.mockInstance,
Credentials(fakeToken, fakeEnvName, Credentials.NO_VARIANT, null),
Credentials(fakeToken, fakeEnvName, fakeVariant, null),
Configuration.Builder(
logsEnabled = true,
tracesEnabled = true,
Expand All @@ -153,7 +156,8 @@ internal class DatadogExceptionHandlerTest {
mockTimeProvider,
CoreFeature.sdkVersion,
CoreFeature.envName,
CoreFeature.packageVersion
CoreFeature.packageVersion,
CoreFeature.variant
),
writer = mockLogWriter,
appContext = appContext.mockInstance
Expand Down Expand Up @@ -191,7 +195,8 @@ internal class DatadogExceptionHandlerTest {
.hasExactlyTags(
listOf(
"${LogAttributes.ENV}:$fakeEnvName",
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}"
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}",
"${LogAttributes.VARIANT}:$fakeVariant"
)
)
.hasExactlyAttributes(
Expand Down Expand Up @@ -291,7 +296,8 @@ internal class DatadogExceptionHandlerTest {
.hasExactlyTags(
listOf(
"${LogAttributes.ENV}:$fakeEnvName",
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}"
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}",
"${LogAttributes.VARIANT}:$fakeVariant"
)
)
.hasExactlyAttributes(
Expand Down Expand Up @@ -327,7 +333,8 @@ internal class DatadogExceptionHandlerTest {
.hasExactlyTags(
listOf(
"${LogAttributes.ENV}:$fakeEnvName",
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}"
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}",
"${LogAttributes.VARIANT}:$fakeVariant"
)
)
.hasExactlyAttributes(
Expand Down Expand Up @@ -365,7 +372,8 @@ internal class DatadogExceptionHandlerTest {
.hasExactlyTags(
listOf(
"${LogAttributes.ENV}:$fakeEnvName",
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}"
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}",
"${LogAttributes.VARIANT}:$fakeVariant"
)
)
.hasExactlyAttributes(
Expand Down Expand Up @@ -409,7 +417,8 @@ internal class DatadogExceptionHandlerTest {
.hasExactlyTags(
listOf(
"${LogAttributes.ENV}:$fakeEnvName",
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}"
"${LogAttributes.APPLICATION_VERSION}:${appContext.fakeVersionName}",
"${LogAttributes.VARIANT}:$fakeVariant"
)
)
.hasExactlyAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ internal class LogGeneratorTest {
lateinit var fakeTags: Set<String>
lateinit var fakeAppVersion: String
lateinit var fakeEnvName: String
lateinit var fakeVariant: String
lateinit var fakeLogMessage: String
lateinit var fakeThrowable: Throwable

Expand Down Expand Up @@ -114,6 +115,7 @@ internal class LogGeneratorTest {
fakeTags = forge.aList { anAlphabeticalString() }.toSet()
fakeAppVersion = forge.aStringMatching("^[0-9]\\.[0-9]\\.[0-9]")
fakeEnvName = forge.aStringMatching("[a-zA-Z0-9_:./-]{0,195}[a-zA-Z0-9_./-]")
fakeVariant = forge.anAlphabeticalString()
fakeThrowable = forge.aThrowable()
fakeTimestamp = System.currentTimeMillis()
fakeThreadName = forge.anAlphabeticalString()
Expand All @@ -138,7 +140,8 @@ internal class LogGeneratorTest {
mockTimeProvider,
fakeSdkVersion,
fakeEnvName,
fakeAppVersion
fakeAppVersion,
fakeVariant
)
}

Expand Down Expand Up @@ -384,7 +387,8 @@ internal class LogGeneratorTest {
mockTimeProvider,
fakeSdkVersion,
fakeEnvName,
fakeAppVersion
fakeAppVersion,
fakeVariant
)
// WHEN
val log = testedLogGenerator.generateLog(
Expand Down Expand Up @@ -413,7 +417,8 @@ internal class LogGeneratorTest {
mockTimeProvider,
fakeSdkVersion,
fakeEnvName,
fakeAppVersion
fakeAppVersion,
fakeVariant
)
// WHEN
val log = testedLogGenerator.generateLog(
Expand Down Expand Up @@ -458,7 +463,8 @@ internal class LogGeneratorTest {
mockTimeProvider,
fakeSdkVersion,
"",
fakeAppVersion
fakeAppVersion,
fakeVariant
)

// WHEN
Expand All @@ -472,7 +478,9 @@ internal class LogGeneratorTest {
)

// THEN
val expectedTags = fakeTags + "${LogAttributes.APPLICATION_VERSION}:$fakeAppVersion"
val expectedTags = fakeTags +
"${LogAttributes.APPLICATION_VERSION}:$fakeAppVersion" +
"${LogAttributes.VARIANT}:$fakeVariant"
assertThat(log).hasExactlyTags(expectedTags)
}

Expand All @@ -495,7 +503,7 @@ internal class LogGeneratorTest {
}

@Test
fun `M not add the appVersionTag W not empty`() {
fun `M not add the appVersionTag W empty`() {
// GIVEN
testedLogGenerator = LogGenerator(
fakeServiceName,
Expand All @@ -505,6 +513,57 @@ internal class LogGeneratorTest {
mockTimeProvider,
fakeSdkVersion,
fakeEnvName,
"",
fakeVariant
)

// WHEN
val log = testedLogGenerator.generateLog(
fakeLevel,
fakeLogMessage,
fakeThrowable,
fakeAttributes,
fakeTags,
fakeTimestamp
)

// THEN
val expectedTags = fakeTags +
"${LogAttributes.ENV}:$fakeEnvName" +
"${LogAttributes.VARIANT}:$fakeVariant"
assertThat(log).hasExactlyTags(expectedTags)
}

@Test
fun `M add the variantTag W not empty`() {
// WHEN
val log = testedLogGenerator.generateLog(
fakeLevel,
fakeLogMessage,
fakeThrowable,
fakeAttributes,
fakeTags,
fakeTimestamp
)

// THEN
val deserializedTags = log.ddtags.split(",")
Assertions.assertThat(deserializedTags)
.contains("${LogAttributes.VARIANT}:$fakeVariant")
}

@Test
fun `M not add the variantTag W empty`() {
// GIVEN
testedLogGenerator = LogGenerator(
fakeServiceName,
fakeLoggerName,
mockNetworkInfoProvider,
mockUserInfoProvider,
mockTimeProvider,
fakeSdkVersion,
fakeEnvName,
fakeAppVersion,
""
)

Expand All @@ -519,7 +578,9 @@ internal class LogGeneratorTest {
)

// THEN
val expectedTags = fakeTags + "${LogAttributes.ENV}:$fakeEnvName"
val expectedTags = fakeTags +
"${LogAttributes.ENV}:$fakeEnvName" +
"${LogAttributes.APPLICATION_VERSION}:$fakeAppVersion"
assertThat(log).hasExactlyTags(expectedTags)
}

Expand Down
Loading