Forum Discussion

RVKS's avatar
4 years ago

Place a file in cache or property source and read it in Groovy Script

Hello support team,

                   We are trying to place a JSON file in Cache or Property Source and trying to read the same.Can you please let us know how we can be able to achieve it? We are trying to load the class in Script using scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache(); but we are getting errors while doing the same.Can you please let us know,how we will be able to know the version of collector we are using ? Where can we find the same ?

                                                                                                                                                                Also,we are unable to read inputs from a JSON File present in the Property Source.Please assist us with the same.

  • Anonymous's avatar
    Anonymous

    If you need support, you should be reaching out to support through their channels. The community does not provide support on the product.

    That said, have you tried the method described here

  • Anonymous's avatar
    Anonymous

    Yes, you can write JSON content to a cached token since you can write any string object. I'm not sure what the character limit might be. 

    def scriptCache
    scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache();
    String token = scriptCache.get("myjson");
    if(token == null){ //the token is empty because it has not been set previously
      //generate the json and set it into a token
      def json = JsonOutput.toJson([name: 'John Doe', age: 42])
      assert json == '{"name":"John Doe","age":42}'
      scriptCache.set("myjson", json);
    } else{ //the json has been stored in the cache previously, so let's use it
      println(token)
    }

     

  • You can check your collector version under Support > Collectors. Appears this feature requires an EA (early release) Collector version.

     

  •  

    On 1/11/2021 at 8:36 AM, Stuart Weenig said:

    Yes, you can write JSON content to a cached token since you can write any string object. I'm not sure what the character limit might be. 

    def scriptCache
    scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache();
    String token = scriptCache.get("myjson");
    if(token == null){ //the token is empty because it has not been set previously
      //generate the json and set it into a token
      def json = JsonOutput.toJson([name: 'John Doe', age: 42])
      assert json == '{"name":"John Doe","age":42}'
      scriptCache.set("myjson", json);
    } else{ //the json has been stored in the cache previously, so let's use it
      println(token)
    }

     

     

    to avoid duplicates i'm writing a script that logs certain events and I want to cache the event ID#.  I will probably do something like: 

    def compare_event_Ids (eventId) {
    scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache();
    String lastEventId = scriptCache.get("lastEventId");
    if(lastEventId == null){ //the id is empty because it has not been set previously
      //generate the json and set it into a token
      lastEventId = eventId
      scriptCache.set("lastEventId", lastEventId);
      return true
    } else { //the json has been stored in the cache previously, so let's use it
        if (lastEventId < eventId) {
        	lastEventId = eventId
            scriptCash.set("lastEventId", lastEventId);
            return true   
    	}
        else {
        	return false
            }
    }

     

    (more or less pseudo code, just trying to get the concept to work)

    do you think that will work?

  • Anonymous's avatar
    Anonymous

    I think that's the exact use case for script caching.