-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ampli extra plugin for attaching ingestion metadata informa…
…tion
- Loading branch information
1 parent
b7cb125
commit fdb3259
Showing
4 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
core/src/main/java/com/amplitude/core/platform/plugins/GetAmpliExtrasPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.amplitude.core.platform.plugins | ||
|
||
import com.amplitude.core.Amplitude | ||
import com.amplitude.core.events.BaseEvent | ||
import com.amplitude.core.events.IngestionMetadata | ||
import com.amplitude.core.platform.Plugin | ||
|
||
class GetAmpliExtrasPlugin : Plugin { | ||
override val type: Plugin.Type = Plugin.Type.Enrichment | ||
override lateinit var amplitude: Amplitude | ||
companion object { | ||
const val AMP_AMPLI = "ampli" | ||
} | ||
|
||
override fun setup(amplitude: Amplitude) { | ||
super.setup(amplitude) | ||
} | ||
|
||
override fun execute(event: BaseEvent): BaseEvent { | ||
val ampliExtra = event.extra?.get(AMP_AMPLI) ?: return event | ||
try { | ||
val ingestionMetadataMap = (ampliExtra as Map<String, Any>).get("ingestionMetadata") as Map<String, String> | ||
val ingestionMetadata = IngestionMetadata( | ||
ingestionMetadataMap?.get("sourceName"), | ||
ingestionMetadataMap?.get("sourceVersion") | ||
) | ||
event.ingestionMetadata = ingestionMetadata | ||
} finally { | ||
return event | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/test/kotlin/com/amplitude/core/platform/plugins/GetAmpliExtrasPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.amplitude.core.platform.plugins | ||
|
||
import com.amplitude.core.Amplitude | ||
import com.amplitude.core.Configuration | ||
import com.amplitude.core.events.BaseEvent | ||
import com.amplitude.core.utils.testAmplitude | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class GetAmpliExtrasPluginTest { | ||
private lateinit var amplitude: Amplitude | ||
|
||
@ExperimentalCoroutinesApi | ||
@BeforeEach | ||
fun setup() { | ||
val testApiKey = "test-123" | ||
amplitude = testAmplitude(Configuration(testApiKey)) | ||
} | ||
|
||
@Test | ||
fun `test missing ampli extra in event`() { | ||
val getAmpliExtrasPlugin = GetAmpliExtrasPlugin() | ||
getAmpliExtrasPlugin.setup(amplitude) | ||
val event = BaseEvent() | ||
getAmpliExtrasPlugin.execute(event) | ||
Assertions.assertEquals(event.ingestionMetadata, null) | ||
} | ||
|
||
@Test | ||
fun `test ingestion metadata in event`() { | ||
val getAmpliExtrasPlugin = GetAmpliExtrasPlugin() | ||
getAmpliExtrasPlugin.setup(amplitude) | ||
val event = BaseEvent() | ||
val sourceName = "test-source-name" | ||
val sourceVersion = "test-source-version" | ||
event.extra = mapOf( | ||
"ampli" to mapOf( | ||
"ingestionMetadata" to mapOf( | ||
"sourceName" to sourceName, | ||
"sourceVersion" to sourceVersion | ||
) | ||
) | ||
) | ||
getAmpliExtrasPlugin.execute(event) | ||
Assertions.assertEquals(event.ingestionMetadata?.sourceName, sourceName) | ||
Assertions.assertEquals(event.ingestionMetadata?.sourceVersion, sourceVersion) | ||
} | ||
|
||
@Test | ||
fun `test ingeston metadata in event with null value`() { | ||
val getAmpliExtrasPlugin = GetAmpliExtrasPlugin() | ||
getAmpliExtrasPlugin.setup(amplitude) | ||
val event = BaseEvent() | ||
val sourceName = "test-source-name" | ||
val sourceVersion = null | ||
event.extra = mapOf( | ||
"ampli" to mapOf( | ||
"ingestionMetadata" to mapOf( | ||
"sourceName" to sourceName, | ||
"sourceVersion" to sourceVersion | ||
) | ||
) | ||
) | ||
getAmpliExtrasPlugin.execute(event) | ||
Assertions.assertEquals(event.ingestionMetadata?.sourceName, sourceName) | ||
Assertions.assertEquals(event.ingestionMetadata?.sourceVersion, sourceVersion) | ||
} | ||
|
||
@Test | ||
fun `test empty ampli extra in event`() { | ||
val getAmpliExtrasPlugin = GetAmpliExtrasPlugin() | ||
getAmpliExtrasPlugin.setup(amplitude) | ||
val event = BaseEvent() | ||
event.extra = mapOf( | ||
"ampli" to mapOf( | ||
"ingestionMetadata" to null | ||
) | ||
) | ||
getAmpliExtrasPlugin.execute(event) | ||
Assertions.assertEquals(event.ingestionMetadata, null) | ||
} | ||
|
||
@Test | ||
fun `test wrong ampli extra in event`() { | ||
val getAmpliExtrasPlugin = GetAmpliExtrasPlugin() | ||
getAmpliExtrasPlugin.setup(amplitude) | ||
val event = BaseEvent() | ||
event.extra = mapOf("ampli" to "string") | ||
getAmpliExtrasPlugin.execute(event) | ||
Assertions.assertEquals(event.ingestionMetadata, null) | ||
} | ||
} |