Skip to content

Commit

Permalink
Allow multiple javascript enrichments
Browse files Browse the repository at this point in the history
  • Loading branch information
istreeter committed Dec 7, 2023
1 parent 1259430 commit 8d54aa6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ object EnrichmentManager {
_ <- getHttpHeaderContexts // Execute header extractor enrichment
_ <- getYauaaContext[F](registry.yauaa, raw.context.headers) // Runs YAUAA enrichment (gets info thanks to user agent)
_ <- extractSchemaFields[F](unstructEvent) // Extract the event vendor/name/format/version
_ <- getJsScript[F](registry.javascriptScript) // Execute the JavaScript scripting enrichment
_ <- registry.javascriptScript.traverse(getJsScript[F](_)) // Execute the JavaScript scripting enrichment
_ <- getCurrency[F](raw.context.timestamp, registry.currencyConversion) // Finalize the currency conversion
_ <- getWeatherContext[F](registry.weather) // Fetch weather context
_ <- geoLocation[F](registry.ipLookups) // Execute IP lookup enrichment
Expand Down Expand Up @@ -240,7 +240,7 @@ object EnrichmentManager {
_ <- getYauaaContext[F](registry.yauaa, raw.context.headers) // Runs YAUAA enrichment (gets info thanks to user agent)
_ <- extractSchemaFields[F](unstructEvent) // Extract the event vendor/name/format/version
_ <- geoLocation[F](registry.ipLookups) // Execute IP lookup enrichment
_ <- getJsScript[F](registry.javascriptScript) // Execute the JavaScript scripting enrichment
_ <- registry.javascriptScript.traverse(getJsScript[F](_)) // Execute the JavaScript scripting enrichment
_ <- sqlContexts // Derive some contexts with custom SQL Query enrichment
_ <- apiContexts // Derive some contexts with custom API Request enrichment
// format: on
Expand Down Expand Up @@ -659,16 +659,12 @@ object EnrichmentManager {

// Execute the JavaScript scripting enrichment
def getJsScript[F[_]: Applicative](
javascriptScript: Option[JavascriptScriptEnrichment]
javascriptScript: JavascriptScriptEnrichment
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, derivedContexts) =>
javascriptScript match {
case Some(jse) =>
ME.formatContexts(derivedContexts).foreach(c => event.derived_contexts = c)
jse.process(event).leftMap(NonEmptyList.one)
case None => Nil.asRight
}
ME.formatContexts(derivedContexts).foreach(c => event.derived_contexts = c)
javascriptScript.process(event).leftMap(NonEmptyList.one)
}

def headerContexts[F[_]: Applicative, A](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ object EnrichmentRegistry {
enrichment <- EitherT.right(c.enrichment[F](blocker))
registry <- er
} yield registry.copy(ipLookups = enrichment.some)
case c: JavascriptScriptConf => er.map(_.copy(javascriptScript = c.enrichment.some))
case c: JavascriptScriptConf => er.map(v => v.copy(javascriptScript = v.javascriptScript :+ c.enrichment))
case c: RefererParserConf =>
for {
enrichment <- c.enrichment[F]
Expand Down Expand Up @@ -245,7 +245,7 @@ final case class EnrichmentRegistry[F[_]](
httpHeaderExtractor: Option[HttpHeaderExtractorEnrichment] = None,
iab: Option[IabEnrichment] = None,
ipLookups: Option[IpLookupsEnrichment[F]] = None,
javascriptScript: Option[JavascriptScriptEnrichment] = None,
javascriptScript: List[JavascriptScriptEnrichment] = Nil,
refererParser: Option[RefererParserEnrichment] = None,
uaParser: Option[UaParserEnrichment[F]] = None,
userAgentUtils: Option[UserAgentUtilsEnrichment] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class EnrichmentManagerSpec extends Specification with EitherMatchers {
val jsEnrichConf =
JavascriptScriptEnrichment.parse(config, schemaKey).toOption.get
val jsEnrich = JavascriptScriptEnrichment(jsEnrichConf.schemaKey, jsEnrichConf.rawFunction)
val enrichmentReg = EnrichmentRegistry[Id](javascriptScript = Some(jsEnrich))
val enrichmentReg = EnrichmentRegistry[Id](javascriptScript = List(jsEnrich))

val parameters = Map(
"e" -> "pp",
Expand Down Expand Up @@ -201,7 +201,7 @@ class EnrichmentManagerSpec extends Specification with EitherMatchers {
val jsEnrichConf =
JavascriptScriptEnrichment.parse(config, schemaKey).toOption.get
val jsEnrich = JavascriptScriptEnrichment(jsEnrichConf.schemaKey, jsEnrichConf.rawFunction)
val enrichmentReg = EnrichmentRegistry[Id](javascriptScript = Some(jsEnrich))
val enrichmentReg = EnrichmentRegistry[Id](javascriptScript = List(jsEnrich))

val parameters = Map(
"e" -> "pp",
Expand Down Expand Up @@ -799,7 +799,7 @@ class EnrichmentManagerSpec extends Specification with EitherMatchers {
SchemaVer.Full(1, 0, 0)
)
val enrichmentReg = EnrichmentRegistry[Id](
javascriptScript = Some(JavascriptScriptEnrichment(schemaKey, script)),
javascriptScript = List(JavascriptScriptEnrichment(schemaKey, script)),
httpHeaderExtractor = Some(HttpHeaderExtractorEnrichment(".*"))
)

Expand Down

0 comments on commit 8d54aa6

Please sign in to comment.