How can I change a tag name with a tick script (Kapacitor)?

2.2k views Asked by At

Let's say I have a measurement in my Influx db called 'cpu':

name: cpu
time                  host   app             org          usage_idle                                                     ----                  -----  ------------    ------------  ----------       
1505758901462105873  serverA  applicationA    org1           23
1505758901462106873  serverB  applicationA    org2           42

I wrote tick script to query data from this measurement:

var data = batch
        |query('''select 100-min(usage_idle) as used_percent from "application"."autogen"."cpu"''')
            .period(1m)
            .every(10s)
            .groupBy(time(1s), 'app', 'org', 'host')
            .align()

// Here I want to write Lambda? to change tag names

    data|influxDBOut()
            .database('someDataBase')
            .measurement('someMeasurement')
            .precision('s')

I want the result of this query contains instead of 'app' - 'app_name' , and instead of 'org' - 'app_group'. What should I include in tick script in order to change tag names? Note! As a workaround I can insert into result new columns with name 'app_name' and 'app_group', but I don't want to add more columns in there and have duplicate data.

Looking for some advice. Thank you

1

There are 1 answers

0
Victoria Bunka On

Here is the solution! The idea is to add a new tag and delete the old one (instead of renaming it). The above tick script then looks as the following:

var data = batch
        |query('''select 100-min(usage_idle) as used_percent from "application"."autogen"."cpu"''')
            .period(1m)
            .every(10s)
            .groupBy(time(1s), 'app', 'org', 'host')
            .align()

|eval(lambda: "app") .as('app_name') .keep('used_percent') .tags('app_name') |delete() .tag('app') |eval(lambda: "org") .as('app_group') .keep('used_percent') .tags('app_group') |delete() .tag('org') data|influxDBOut() .database('someDataBase') .measurement('someMeasurement') .precision('s')