Skip to content

Commit

Permalink
common: Provide derived contexts to JS enrichment (close #769)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanch authored and spenes committed Apr 26, 2023
1 parent d217d78 commit 6144b5a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,12 @@ object EnrichmentManager {
javascriptScript: Option[JavascriptScriptEnrichment]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
case (event, derivedContexts) =>
javascriptScript match {
case Some(jse) => jse.process(event).leftMap(NonEmptyList.one(_))
case Some(jse) =>
if (derivedContexts.nonEmpty)
event.derived_contexts = ME.formatDerivedContexts(derivedContexts)
jse.process(event).leftMap(NonEmptyList.one(_))
case None => Nil.asRight
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.pii.{
import com.snowplowanalytics.snowplow.enrich.common.outputs.EnrichedEvent
import utils.Clock._
import utils.ConversionUtils
import enrichments.registry.{IabEnrichment, JavascriptScriptEnrichment, YauaaEnrichment}
import enrichments.registry.{HttpHeaderExtractorEnrichment, IabEnrichment, JavascriptScriptEnrichment, YauaaEnrichment}
import org.apache.commons.codec.digest.DigestUtils
import org.specs2.mutable.Specification
import org.specs2.matcher.EitherMatchers
Expand Down Expand Up @@ -780,6 +780,46 @@ class EnrichmentManagerSpec extends Specification with EitherMatchers {
enriched.value.map(_.useragent) must beRight(qs_ua)
enriched.value.map(_.derived_contexts) must beRight((_: String).contains("\"agentName\":\"%1$S\""))
}

"pass derived contexts generated by previous enrichments to the JavaScript enrichment" >> {
val script =
"""
function process(event) {
var derivedContexts = JSON.parse(event.getDerived_contexts());
var firstHeaderValue = derivedContexts.data[0].data.value;
event.setApp_id(firstHeaderValue);
return [];
}"""

val schemaKey = SchemaKey(
"com.snowplowanalytics.snowplow",
"javascript_script_config",
"jsonschema",
SchemaVer.Full(1, 0, 0)
)
val enrichmentReg = EnrichmentRegistry[Id](
javascriptScript = Some(JavascriptScriptEnrichment(schemaKey, script)),
httpHeaderExtractor = Some(HttpHeaderExtractorEnrichment(".*"))
)

val parameters = Map(
"e" -> "pp",
"tv" -> "js-0.13.1",
"p" -> "web"
).toOpt
val headerContext = context.copy(headers = List("X-Tract-Me: moo"))
val rawEvent = RawEvent(api, parameters, None, source, headerContext)
val enriched = EnrichmentManager.enrichEvent[Id](
enrichmentReg,
client,
processor,
timestamp,
rawEvent,
AcceptInvalid.featureFlags,
AcceptInvalid.countInvalid
)
enriched.value.map(_.app_id) must beRight("moo")
}
}

"getIabContext" should {
Expand Down

0 comments on commit 6144b5a

Please sign in to comment.