Visulaise response time for number of elements passed

I have an API which I collect metric data for and I want to visualise how long a perticular endpoint takes to respond based on the nubmer of unique ids have been passed.

I have two fields that come into this, one which gives me the response time in ms (so 1000 is a second) and one which indludes the unique ids that were passed with the request as a comma seperated list. so if the unique ids are "ABC, CDE, EFG" I want to take that as the numerical value "3"

Ideally I'd like some way to do this in the visualisation itself however if that isn't possible can I do it as a scripted field in the index pattern to generate the count which I can then reference? I know that will come with a perf hit and that I ideally if I choose to do this that way I'd then want to store it in the data set rather than calculate it over and over but I can look to that later.

You can use Java code in scripted fields, so you can count for the number of commas in the unique_ids field and add 1. But doing string operations like this in scripted fields isn't a really good idea. What I would suggest is to read up on Ingest pipelines and use that to create a field with the count of unique_ids. https://www.elastic.co/blog/new-way-to-ingest-part-1

2 Likes

Hi Marius,

I totally agree that this is a case where an ingestion pipeline would be a better long term approach but I need to look up how to do that for both future and past data and I'm happy to take the cycles hit to validate this is going to show me something useful before I increase my data storage requirements.

I have tried to look but I can't find it, I don't suppose you can give me a syntax example of how I might do this? I know to go to management> Index Patterns, then select the pattern to modify, select scripted fields and click "add scripted field". Just not sure how to fill that out for the purpose of getting this value avaliable to query. For the purpose of the example the field to be analysed is api.request.content.uniqieIds

many thanks
Ant

String str;
str = doc['api.request.content.uniqieIds.keyword'].value;
String findStr = ",";
int lastIndex = 0;
int count = 0;

while (lastIndex != -1) {
    lastIndex = str.indexOf(findStr,lastIndex);

    if( lastIndex != -1){
        count++;
        lastIndex += findStr.length();
}
}
return count

This works for me. Depending on how your data is, try it with and without .keyword. If you get an error about fielddata, use the .keyword version of the field.
But I'm gonna say it again, use this only for testing, as that loop will run on every single document in your index.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.