MetricStore指标字段追加内容并且排序

本文介绍如何使用数据加工对时序库(MetricStore)中的某个指标字段进行更新并排序。

升序排列

原始日志中指标字段__labels__已经是按照升序排列的,期望在__labels__后面追加一个指标app#$#--cd-app,并确保整体仍保持升序。

  • 原始日志

    {
      "__labels__": "hostname#$#prod-数据中心-集群-uewsd|instanceId#$#i-wet892djhhkbskd|ipgroup#$#10.10.0.0|regionId#$#cn-hangzhou|rmgroup_id#$#rg-acfm90ewnkw921|rmgroup_name#$#默认资源组|tag_env#$#prod|tag_f#$#user|tag_f_1#$#小明|tag_f_2#$#小亮|userId#$#18090223281935",
      "__name__": "AliyunEcs_usertestname",
      "__source__": "10.XX.XX.0",
      "__time__": 1717400670,
      "__time_nano__": "1717400670",
      "__topic__": "",
      "__value__": "49.5"
    }  
  • 加工规则

    str_split__labels__字段值解析成数组,lst_append追加数据,lst_sort对数组进行排序,str_join将排序后的数组用 | 连接成一个字符串。

    e_set("__labels__", str_split(v("__labels__"), sep="|"))
    e_set(
        "__labels__",
        str_join("|", lst_sort(lst_append(v("__labels__"), "app#$#--cd-app"))),
    )
  • 加工结果

    {
      "__labels__": "app#$#--cd-app|hostname#$#prod-数据中心-集群-uewsd|instanceId#$#i-wet892djhhkbskd|ipgroup#$#10.10.0.0|regionId#$#cn-hangzhou|rmgroup_id#$#rg-acfm90ewnkw921|rmgroup_name#$#默认资源组|tag_env#$#prod|tag_f#$#user|tag_f_1#$#小明|tag_f_2#$#小亮|userId#$#18090223281935",
      "__name__": "AliyunEcs_usertestname",
      "__source__": "10.XX.XX.0",
      "__time__": 1717400670,
      "__time_nano__": "1717400670",
      "__topic__": "",
      "__value__": "49.5"
    }  

降序排列

原始日志中指标字段__labels__已经是按照降序排列的,期望在__labels__后面追加一个指标group#$#group1,并确保整体仍保持降序。

  • 原始日志

    {
      "script_md5":"8c6aebe9****c27f",
      "remote_addr":"123.XX.XX.123",
      "remote_user":"-",
      "time_local":"15/Aug/2023:12:03:20",
      "method":"GET",
      "url":"/www.a.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"
    }
  • 加工规则

    str_split__labels__字段值解析成数组,lst_append追加数据,lst_sort对数组进行排序,str_join将排序后的数组用 | 连接成一个字符串。

    e_set("__labels__", str_split(v("__labels__"), sep="|"))
    e_set(
        "__labels__",
        str_join(
            "|", lst_sort(lst_append(v("__labels__"), "group#$#group1"), reverse=True),
        ),
    )
  • 加工结果

    {
      "script_md5":"8c6aebe9****c27f",
      "remote_addr":"123.XX.XX.123",
      "remote_user":"-",
      "time_local":"15/Aug/2023:12:03:20",
      "method":"GET",
      "url":"/www.a.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"
    }