We are trying to implement custom javascript enrichment to the enrichment process hosted in a GCP pipeline.
Here is a verbal eg for what we are trying to achieve.
We would like to modify user_ipaddress field in 2 ways based on the value of another field of the event.
Similar to ip_annon enrichment, we want to mask the last n octets of the user_ipaddress.
Mask the entire user_ipaddress .
Is it possible to achieve this by modifying the user_ipaddress field in place like we have for ip_annon without having to add a custom schema for the updated value in bigquery?
Hey @ashish_george. Yes, it’s totally doable. You would need to use the setter to amend the canonical field, namely setUser_ipaddress(). That is, depending on the value of user_ipaddress, which you can obtain with the getter getUser_ipaddress(), you would set the new value like this:
where some_modified_value is your new value you want to assign to user_ipaddress depending on its original value.
Note that you do not need to return anything in process(event) function as you do not want any derived context. Using setters overrides the original values in event’s TSV. Thus, be extra careful as you won’t be able to restore the original values.
Hi @ashish_george , there are 2 issues that I could spot. I can see wrong quotes in your configuration, which could be simply copy/paste problem in your comment than the actual content of the configuration file.
For example, you are using “curly” quotes instead of straight quotes around schema and data properties. For better visibility, here’s the image that demonstrates it better
Secondly, your base64 encoded string needs to end in one or two equal sign (it least that was an issue in the past to my knowledge). That is, if you add a new line to your unencoded function, the encoded version will get it with an equal sign (or two). Something like this
@ihor I was trying out a different way to hash the IP.
Was not able to get the below code working, is it because of the md5 usage?
function process(event) {
var ipAddress = event.getUser_ipaddress();
let saltStr = '.125.078';
var saltedIp = md5(ipAddress + saltStr);
event.setUser_ipaddress(new String(saltedIp));
}
We are working on a new guide for writing and testing JavaScript enrichments. It’s still in review, but as a quick way of unblocking you, I think this section may help: Writing your enrichment | Snowplow Documentation. (You can use md5 hashing from the Apache library.)
What version of Enrich are you using? The only thing I can think of is if your Enrich is below 3.0.0, you wouldn’t get ES6 syntax. You can try changing const to var though.
@stanch Enrich version was 2.0.5, i made the changes as you said and it worked. Thanks a lot Nick. really appreciate the quick response and resolution.