# Safely remove tcp input plugin's "host" and "port" fields

**URL:** <https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492>\
**Category:** Logstash\
**Created:** [April 7, 2021, 3:00pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492 "2021-04-07T15:00:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Supermathie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/supermathie/32/44936_2.png) [@Supermathie](https://discuss.elastic.co/u/Supermathie)\
**Post date:** [April 7, 2021, 3:00pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492/1 "2021-04-07T15:00:26Z")

</div>

The [tcp input plugin](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-tcp.html) adds `host` and `port` fields to received events.

Ideally we don't want to add these but since this isn't documented, there doesn't seem to be a way to remove them.

What is the best filter to safely remove them? For example, we can't blindly:

```auto
filter {
  mutate {
    remove_field => [
      "host",
      "port"
    ]
  }
}

```

since some of our received events are using ECS and this will destroy anything under `host` (e.g. `host.hostname`)

If there a way to test in logstash for this? Or should we use a ruby filter?

EDIT:

I would _like_ to do something like:

```auto
filter {
   ruby {
      code => "event.delete('host') if String === event.get('host')"
   }  
}

```

but the ruby API only provides `get` and `set`; you can't delete a field.

EDIT:

unless I hear "there's a better way to do this" here's what I'm settling on:

```auto
filter {
  ruby {
    code => "event.set('host', {}) if String === event.get('host')" # set by input plugins, we don't care
    id => "remove_input_noise"
    remove_field => [
      "port" # set by input plugins, we don't care
    ]
  }
}

```

if elastic receives the blank hash under `host`, it won't index anything so there's no extra noise in the event. It's still in the original document though.

---

<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:** [April 7, 2021, 4:15pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492/2 "2021-04-07T16:15:48Z")

</div>

> [@Supermathie](#):
>
> the ruby API only provides `get` and `set` ; you can't delete a field

Not so, there is an event.remove method that deletes a field.

---

<div class="post-metadata">

**Author:** ![Supermathie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/supermathie/32/44936_2.png) [@Supermathie](https://discuss.elastic.co/u/Supermathie)\
**Post date:** [April 7, 2021, 4:21pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492/3 "2021-04-07T16:21:30Z")

</div>

Is it unsupported? It's not in the [ruby API documentation](https://www.elastic.co/guide/en/logstash/current/event-api.html).

---

<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:** [April 7, 2021, 4:44pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492/4 "2021-04-07T16:44:56Z")

</div>

They may be undocumented but event.remove or event.cancel are both used a lot.

---

<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:** [May 5, 2021, 4:45pm UTC](https://discuss.elastic.co/t/safely-remove-tcp-input-plugins-host-and-port-fields/269492/5 "2021-05-05T16:45:33Z")

</div>

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