Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a field mapping transformer #851

Closed
xcaptain opened this issue Sep 12, 2019 · 5 comments
Closed

Add a field mapping transformer #851

xcaptain opened this issue Sep 12, 2019 · 5 comments
Labels
type: feature A value-adding code addition that introduce new functionality.

Comments

@xcaptain
Copy link

A raw log may contain many fields but at last we may only care some of them and want them to have a different name, For example, a transformer produces log like

{
    "a": 1,
    "c": "some non-sence data"
}

we may want to rename field a to b and remove field c and get a result like

{
    "b": 1
}

currently the add_fields and remove_fields transformer can not achieve this goal.

@xcaptain
Copy link
Author

I made a simple attempt to implement this idea, not hard to implement

@xcaptain
Copy link
Author

I added config file parser for this map_fields transformer, unit test and function test passed. Below is a simple example:

vector_test.toml

[transforms.map_fields]
  type = "fieldmapper"
  inputs = ["transform_timestamp"]
[transforms.map_fields.fields]
  status = "http_status"

[sinks.my_sink1]
  type = "console"
  inputs = ["map_fields"]
  target = "stdout"
  encoding = "json"

[sinks.my_sink2]
  type = "console"
  inputs = ["transform_timestamp"]
  target = "stdout"
  encoding = "json"

图片

Using this transformer I filtered status field and then renamed it to http_status, the other fields are ignored.

@lukesteensen
Copy link
Member

Thanks for opening this @xcaptain! You're right that there is a gap in those transforms around renaming fields.

This is a good example of a situation where I'd recommend using the lua transform. Here's the equivalent of your example config:

[transforms.map_fields]
  type = "lua"
  inputs = ["transform_timestamp"]
  source = """
event["http_status"] = event["status"]
"""

[sinks.my_sink1]
  type = "console"
  inputs = ["map_fields"]
  target = "stdout"
  encoding = "json"

[sinks.my_sink2]
  type = "console"
  inputs = ["transform_timestamp"]
  target = "stdout"
  encoding = "json"

If you want to remove the old status field, you can add event["status"] = nil.

We are also in the process of planning a more general transform that will consolidate adding, removing, renaming, coercing, etc into a single more powerful transform (see #750).

@lukesteensen
Copy link
Member

Closing in favor of #750

@xcaptain
Copy link
Author

The lua transform is modify event inplace, if I want to remove other fields I have to use remove_fields, not very convenient but it's ok to use. looking forward to your new transform,and wish this project get better.

@binarylogic binarylogic added type: feature A value-adding code addition that introduce new functionality. and removed type: new feature labels Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A value-adding code addition that introduce new functionality.
Projects
None yet
Development

No branches or pull requests

3 participants