Append and sort a metric in a MetricStore

更新时间:
复制 MD 格式

You can use the data transformation feature to append a metric to the __labels__ field in a MetricStore and keep the field sorted.

Ascending order

The __labels__ field in the raw log is already sorted in ascending order. This example appends the metric app#$#--cd-app to __labels__ while preserving the ascending order.

  • Raw log

    {
      "__labels__": "hostname#$#prod-data center-cluster-uewsd|instanceId#$#i-wet892djhhk****|ipgroup#$#10.10.0.0|regionId#$#cn-hangzhou|rmgroup_id#$#rg-acfm90ewnkw921|rmgroup_name#$#default resource group|tag_env#$#prod|tag_f#$#user|tag_f_1#$#Alice|tag_f_2#$#Bob|userId#$#1809022328****",
      "__name__": "AliyunEcs_usertestname",
      "__source__": "10.XX.XX.0",
      "__time__": 1717400670,
      "__time_nano__": "1717400670",
      "__topic__": "",
      "__value__": "49.5"
    }  
  • Transformation rule

    The str_split function splits the value of __labels__ into an array. The lst_append function appends the new metric, the lst_sort function sorts the array, and the str_join function joins the sorted array back into a pipe-separated ( | ) string.

    e_set("__labels__", str_split(v("__labels__"), sep="|"))
    e_set(
        "__labels__",
        str_join("|", lst_sort(lst_append(v("__labels__"), "app#$#--cd-app"))),
    )
  • Transformation result

    {
      "__labels__": "app#$#--cd-app|hostname#$#prod-data center-cluster-uewsd|instanceId#$#i-wet892djhhk****|ipgroup#$#10.10.0.0|regionId#$#cn-hangzhou|rmgroup_id#$#rg-acfm90ewnkw921|rmgroup_name#$#default resource group|tag_env#$#prod|tag_f#$#user|tag_f_1#$#Alice|tag_f_2#$#Bob|userId#$#1809022328****",
      "__name__": "AliyunEcs_usertestname",
      "__source__": "10.XX.XX.0",
      "__time__": 1717400670,
      "__time_nano__": "1717400670",
      "__topic__": "",
      "__value__": "49.5"
    }  

Descending order

The __labels__ field in the raw log is pre-sorted in descending order. This example appends the metric group#$#group1 to __labels__ while preserving the descending order.

  • Raw log

    {
      "script_md5":"8c6aebe9****c27f",
      "remote_addr":"123.XX.XX.123",
      "remote_user":"-",
      "time_local":"15/Aug/2023:12:03:20",
      "method":"GET",
      "url":"/www.example.com",
      "protocol":"HTTP/1.1",
      "status":"404",
      "body_bytes_sent":"4146",
      "http_referer":"-",
      "http_user_agent":"curl/7.74.0",
      "http_x_forwarded_for":"-",
      "__labels__":"tag2#$#tag2|tag1#$#tag1|field2#$#field2|field1#$#field1",
      "body_bytes_sent_name":"body_bytes_sent",
      "status_name":"status",
      "httptime":"1692101000",
      "__time__":"1692101000"
    }
  • Transformation rule

    The str_split function splits the value of __labels__ into an array. The lst_append function appends the new metric, the lst_sort function sorts the array, and the str_join function joins the sorted array back into a pipe-separated ( | ) string.

    e_set("__labels__", str_split(v("__labels__"), sep="|"))
    e_set(
        "__labels__",
        str_join(
            "|", lst_sort(lst_append(v("__labels__"), "group#$#group1"), reverse=True),
        ),
    )
  • Transformation result

    {
      "script_md5":"8c6aebe9****c27f",
      "remote_addr":"123.XX.XX.123",
      "remote_user":"-",
      "time_local":"15/Aug/2023:12:03:20",
      "method":"GET",
      "url":"/www.example.com",
      "protocol":"HTTP/1.1",
      "status":"404",
      "body_bytes_sent":"4146",
      "http_referer":"-",
      "http_user_agent":"curl/7.74.0",
      "http_x_forwarded_for":"-",
      "__labels__":"tag2#$#tag2|tag1#$#tag1|group#$#group1|field2#$#field2|field1#$#field1",
      "body_bytes_sent_name":"body_bytes_sent",
      "status_name":"status",
      "httptime":"1692101000",
      "__time__":"1692101000"
    }