github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/log_helper_test.go (about) 1 // Copyright 2023 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package helper 16 17 import ( 18 "github.com/alibaba/ilogtail/pkg/config" 19 "github.com/alibaba/ilogtail/pkg/protocol" 20 "github.com/alibaba/ilogtail/pkg/selfmonitor" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "testing" 26 ) 27 28 func TestMetricLabels_Append(t *testing.T) { 29 var ml MetricLabels 30 ml.Append("key2", "val") 31 ml.Append("key", "val") 32 log := NewMetricLog("name", 1691646109945, 1, &ml) 33 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key#$#val|key2#$#val" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 34 35 ml.Replace("key", "val2") 36 37 log = NewMetricLog("name", 1691646109945, 1, &ml) 38 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key#$#val2|key2#$#val" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 39 40 ml.Replace("key3", "val3") 41 log = NewMetricLog("name", 1691646109945, 1, &ml) 42 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key#$#val2|key2#$#val|key3#$#val3" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 43 44 cloneLabel := ml.Clone() 45 cloneLabel.Replace("key3", "val4") 46 log = NewMetricLog("name", 1691646109945, 1, cloneLabel) 47 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key#$#val2|key2#$#val|key3#$#val4" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 48 49 log = NewMetricLog("name", 1691646109945, 1, &ml) 50 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key#$#val2|key2#$#val|key3#$#val3" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 51 52 log = NewMetricLog("name", 1691646109945, 1, nil) 53 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 54 55 log = NewMetricLog("name", 1691646109945, 1, nil) 56 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 57 58 var ml2 MetricLabels 59 config.LoongcollectorGlobalConfig.EnableSlsMetricsFormat = true 60 ml2.Append("key@", "val|") 61 62 log = NewMetricLog("name@", 1691646109945, 1, &ml2) 63 require.Equal(t, `Time:1691646109 Contents:<Key:"__name__" Value:"name_" > Contents:<Key:"__time_nano__" Value:"1691646109945000000" > Contents:<Key:"__labels__" Value:"key_#$#val_" > Contents:<Key:"__value__" Value:"1" > Time_ns:945000000 `, log.String()) 64 65 } 66 67 func Test_GetMetricName(t *testing.T) { 68 type args struct { 69 log *protocol.Log 70 } 71 tests := []struct { 72 name string 73 args args 74 want string 75 }{ 76 { 77 name: "test1", 78 args: struct { 79 log *protocol.Log 80 }{log: &protocol.Log{ 81 Contents: []*protocol.Log_Content{{Key: selfmonitor.SelfMetricNameKey, Value: "metric1"}}}, 82 }, 83 want: "metric1", 84 }, 85 86 { 87 name: "test2", 88 args: struct { 89 log *protocol.Log 90 }{log: &protocol.Log{ 91 Contents: []*protocol.Log_Content{ 92 {Key: "first", Value: "metric1"}, 93 {Key: selfmonitor.SelfMetricNameKey, Value: "metric2"}, 94 }}, 95 }, 96 want: "metric2", 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 assert.Equalf(t, tt.want, GetMetricName(tt.args.log), "GetMetricName(%v, %v)", tt.args.log) 102 }) 103 } 104 }