Logstash filter for renaming based on a substring

I can't figure out how to rename fileds based on the pattern.

At the moment, in Logstash (6.5) I have a banch of filters:

mutate { rename => { "sensor_source_a" => "sensor.labels.a" } }
mutate { rename => { "sensor_source_b" => "sensor.labels.b" } }
mutate { rename => { "sensor_source_c" => "sensor.labels.c" } }

I need to make some more automated filter which independently of the last element will always rename the pattern:
"sensor_source_xxx" into "sensor.labels.xxx"
I am trying different approaches together with ruby code but can't find simple solution.
What would be the most siutable filter for such opearation

1 Like

Something like:

ruby {
    code => '
        event.to_hash.each_pair { |k, v|
            k.match(/^sensor_source_(.*)/) { |m|
                i = "[sensor][labels][#{m[1][0]}]"
                event.set(i, v)
                event.remove(k)
            }
        }
    '
}
1 Like

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