# Add new field based on future events

**URL:** <https://discuss.elastic.co/t/add-new-field-based-on-future-events/151601>\
**Category:** Logstash\
**Created:** [October 9, 2018, 9:58am UTC](https://discuss.elastic.co/t/add-new-field-based-on-future-events/151601 "2018-10-09T09:58:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![icking](https://avatars.discourse-cdn.com/v4/letter/i/848f3c/32.png) [@icking](https://discuss.elastic.co/u/icking)\
**Post date:** [October 9, 2018, 9:58am UTC](https://discuss.elastic.co/t/add-new-field-based-on-future-events/151601/1 "2018-10-09T09:58:33Z")

</div>

I have a log like this:  
id- API - logtype  
1 - login - process  
1 - logout - process  
1 - init - entry

I want to add a new field called parentAPI, based on the API with the same id and logtype "entry". The expected results are:

```
{
"id" => 1,
"API" => "login",
"logtype" => "process",
"parentAPI" => "init"
}
{
"id" => 1,
"API" => "logout",
"logtype" => "process",
"parentAPI" => "init"
}
{
"id" => 1,
"API" => "init",
"logtype" => "entry",
"parentAPI" => "init"
}

```

The challenge here is that the entry logs may come after other logs. I have a working solution for the case where entry logs always come first:

```
filter {
   grok {
     match => ["message", "%{WORD:id} - %{WORD:API} - %{WORD:logtype}"]
   }

   if [logtype] == "entry" {
     aggregate {
       task_id => "%{id}"
       code => "map['parentAPI'] = event.get('API'); event.set('parentAPI', map['parentAPI'])"
       map_action => "create"
     }
   }

  if [logtype] != "entry" {
     aggregate {
       task_id => "%{id}"
       code => "event.set('parentAPI', map['parentAPI'])"
       map_action => "update"
       timeout => 600
     }
   }
}
 
output {
  stdout { codec => rubydebug }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 6, 2018, 9:58am UTC](https://discuss.elastic.co/t/add-new-field-based-on-future-events/151601/2 "2018-11-06T09:58:37Z")

</div>

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