# Aggregate filter - push on event size limit

**URL:** https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875
**Category:** Logstash
**Created:** [May 13, 2019, 7:53pm UTC](https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875 "2019-05-13T19:53:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bosand](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@bosand](https://discuss.elastic.co/u/bosand)
#### Post date: [May 13, 2019, 7:53pm UTC](https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875/1 "2019-05-13T19:53:04Z")

</div>

Hi there,

I would like to aggregate events until the aggregated event reaches a certain size limit (e.g. 256kb) at which point I would like to push the event to the queue. There are no start/end events.  
Push on timeout does not solve my problem as hundreds of events may unpredictably arrive in a very short time interval. Is there a way to do that?

Thanks,  
bosand

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [May 13, 2019, 8:58pm UTC](https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875/2 "2019-05-13T20:58:08Z")

</div>

At first I thought not, but there is a distinctly non-scaleable way to do it. In addition to the usual requirement of "--pipeline.workers 1" you need "--pipeline.batch.size 1" so that every event goes through the second aggregate filter before the first aggregate filter processes another event.

This is just a proof-of-concept that demonstrates how it could be done.

```
filter { json { source => "message" } }

input { stdin {} }
input { generator { count => 10 lines => ['{ "id": "a", "data": "123456789012345678901234567890123456789012345678901234567890"}'] } }
filter {
    aggregate {
        task_id => "%{id}"
        code => '
            map["task"] ||= ""
            map["task"] += event.get("data")
            if map["task"].bytesize > 200
                event.set("[@metadata][timeToFlush]", true)
            end
        '
        push_map_as_event_on_timeout => true
        timeout => 10
        timeout_task_id_field => "id"
        timeout_code => '
            event.set("[@metadata][timeToFlush]", true)
        '
    }
    if [@metadata][timeToFlush] {
        aggregate {
            task_id => "%{id}"
            code => '
                event.set("task", map["task"])
                map["task"] = ""
            '
            map_action => "update"
            end_of_task => true
        }
    } else {
        drop {}
    }
}

```

The stdin generator is just there to prevent logstash shutting down the pipeline when the generator input finishes. If you remove that when using a generator input you would not get the timeout. For almost any other input it is not needed or useful.

---

<div class="post-metadata">

### Author: ![bosand](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@bosand](https://discuss.elastic.co/u/bosand)
#### Post date: [May 13, 2019, 9:23pm UTC](https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875/3 "2019-05-13T21:23:09Z")

</div>

This is a nice trick.  
Thanks Badger!

---

<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: [June 10, 2019, 9:23pm UTC](https://discuss.elastic.co/t/aggregate-filter-push-on-event-size-limit/180875/4 "2019-06-10T21:23:12Z")

</div>

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