Skip to content

Commit

Permalink
feat: add ampli extra plugin for attaching ingestion metadata informa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
liuyang1520 committed Sep 26, 2022
1 parent b7cb125 commit fdb3259
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/com/amplitude/core/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.amplitude.core.platform.Plugin
import com.amplitude.core.platform.Timeline
import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.ContextPlugin
import com.amplitude.core.platform.plugins.GetAmpliExtrasPlugin
import com.amplitude.core.utilities.AnalyticsEventReceiver
import com.amplitude.core.utilities.AnalyticsIdentityListener
import com.amplitude.eventbridge.EventBridgeContainer
Expand Down Expand Up @@ -74,6 +75,7 @@ open class Amplitude internal constructor(
}
EventBridgeContainer.getInstance(configuration.instanceName).eventBridge.setEventReceiver(EventChannel.EVENT, AnalyticsEventReceiver(this))
add(ContextPlugin())
add(GetAmpliExtrasPlugin())
add(AmplitudeDestination())

isBuilt = amplitudeScope.async(amplitudeDispatcher) {
Expand Down
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
}
}
}
4 changes: 2 additions & 2 deletions core/src/test/kotlin/com/amplitude/core/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal class AmplitudeTest {
amplitude.add(middleware)
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.plugins?.let {
assertEquals(
1,
2,
it.size
)
} ?: fail()
Expand All @@ -295,7 +295,7 @@ internal class AmplitudeTest {
amplitude.remove(middleware)
amplitude.timeline.plugins[Plugin.Type.Enrichment]?.plugins?.let {
assertEquals(
0, // SegmentLog is the other added at startup
1, // SegmentLog is the other added at startup
it.size
)
} ?: fail()
Expand Down
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)
}
}

0 comments on commit fdb3259

Please sign in to comment.