Hey all,
I'm ingesting JSON formatted logs from nginx. The JSON is all ECS style nested fields.
I've currently got a need to visualize the upstream response time, http.upstream.response.time. Fundamentally this is a float/double (0.166, 10.0, etc). In some simple tests I succeeded with the following.
PUT logs-my-datastream/_mapping
{
"runtime": {
"http.upstream.response.time_int": {
"type": "double",
"script": {
"source": "if (doc.containsKey('http.upstream.response.time')) { if (doc['http.upstream.response.time'].size() != 0 && doc['http.upstream.response.time'].value.length() > 0) {emit(Double.parseDouble(doc['http.upstream.response.time'].value)) } }"
}
}
}
}
The problem I've got is that http.upstream.response.time is quite literally a string, not a text representation of a number an actual string.
It is expected behavior for nginx to try more than one upstream, in such cases it returns a comma separated list of the time taken for each of the upstreams tried, i.e. "10.000, 0.166" (where 10 = the timeout communicating with the upstream).
My only sensible idea here to have a meaningful value is to convert each number then add them all up, as that is the time the client sees, but I'm struggling to understand how to do that. My best attempt so far results in;
class_cast_exception: Cannot cast from [java.lang.Double] to [double].
if (doc.containsKey('http.upstream.response.time')) {
if (doc['http.upstream.response.time'].size() != 0 && doc['http.upstream.response.time'].value.length() > 0) {
String[] times = doc['http.upstream.response.time'].value.split(", ");
def total=0.0;
for(Double time:times){
total = total + (double)time;
}
emit(total)
}
}
I am in no way a Java developer at all, today is literally the first day I have written any, so please don't be too horrified at my potentially horrible java.
I've tried various combinations of Double, double, (double), String and nothing, the best I can do is reverse the order of [java.lang.Double] and [double] in the cast exception.
If someone has a better idea for doing this I'd love to hear, if not I'd be super appreciative of help fixing the java/painless.
Thanks
Mike