github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/decoder/influxdb/decoder_test.go (about)

     1  // Copyright 2021 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 influxdb
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/alibaba/ilogtail/pkg/config"
    25  	"github.com/alibaba/ilogtail/pkg/models"
    26  	"github.com/alibaba/ilogtail/pkg/protocol"
    27  )
    28  
    29  var textFormat = `
    30  # integer value
    31  cpu value=1i
    32  
    33  # float value
    34  cpu_load value=1
    35  
    36  cpu_load value=1.0
    37  
    38  cpu_load value=1.2
    39  
    40  # boolean value
    41  error fatal=true
    42  
    43  # string value
    44  event msg="logged out"
    45  
    46  # multiple values
    47  cpu load=10,alert=true,reason="value above maximum threshold"
    48  
    49  cpu,host=server01,region=uswest value=1 1434055562000000000
    50  cpu,host=server02,region=uswest value=3 1434055562000010000
    51  temperature,machine=unit42,type=assembly internal=32,external=100 1434055562000000035
    52  temperature,machine=unit143,type=assembly internal=22,external=130 1434055562005000035
    53  cpu,host=server\ 01,region=uswest value=1,msg="all systems nominal"
    54  cpu,host=server\ 01,region=us\,west value_int=1i
    55  `
    56  
    57  var mySQLFormat = `
    58  
    59  cpu,host=server01,region=uswest value=1 1434055562000000000
    60  
    61  mysql,host=Vm-Req-170328120400894271-tianchi113855.tc,server=rm-bp1eomqfte2vj91tkjo.mysql.rds.aliyuncs.com:3306 bytes_sent=19815071437i,com_assign_to_keycache=0i,com_alter_event=0i,com_alter_function=0i,com_alter_server=0i,com_alter_table=0i,aborted_clients=7738i,binlog_cache_use=136756i,binlog_stmt_cache_use=136759i,com_alter_procedure=0i,binlog_stmt_cache_disk_use=0i,bytes_received=6811387420i,com_alter_db_upgrade=0i,com_alter_instance=0i,aborted_connects=7139i,binlog_cache_disk_use=0i,com_admin_commands=3478164i,com_alter_db=0i,com_alter_tablespace=0i,com_alter_user=0i 1595818360000000000
    62  
    63  mysql,host=Vm-Req-170328120400894271-tianchi113855.tc,server=rm-bp1eomqfte2vj91tkjo.mysql.rds.aliyuncs.com:3306 innodb_buffer_pool_read_ahead_rnd=0i,innodb_data_pending_fsyncs=0i,innodb_buffer_pool_bytes_dirty=4325376i,innodb_buffer_pool_pages_flushed=21810i,innodb_buffer_pool_pages_total=40960i,innodb_buffer_pool_read_ahead_evicted=0i,innodb_buffer_pool_reads=757i,innodb_buffer_pool_load_status="Buffer pool(s) load completed at 200702 21:33:49",innodb_buffer_pool_pages_data=846i,innodb_buffer_pool_read_ahead=0i,innodb_buffer_pool_write_requests=36830857i,innodb_data_fsyncs=344588i,innodb_buffer_pool_dump_status="Dumping of buffer pool not started",innodb_buffer_pool_pages_dirty=264i,innodb_buffer_pool_pages_misc=3i,innodb_buffer_pool_read_requests=45390218i,innodb_buffer_pool_wait_free=0i,innodb_buffer_pool_bytes_data=13860864i,innodb_buffer_pool_pages_free=40111i 1595406780000000000
    64  `
    65  
    66  var txtWithDotNames = `
    67  cpu.load,host=server01,region=uswest value=1 1434055562000000000
    68  cpu.load,host.dd=server02,region=uswest tt="xx",value=3 1434055562000010000
    69  `
    70  
    71  func TestFieldsExtend(t *testing.T) {
    72  	cases := []struct {
    73  		enableFieldsExtend     bool
    74  		enableSlsMetricsFormat bool
    75  		data                   string
    76  		wantLogs               []*protocol.Log
    77  		wantErr                bool
    78  	}{
    79  		{
    80  			enableFieldsExtend: true,
    81  			data:               txtWithDotNames,
    82  			wantErr:            false,
    83  			wantLogs: []*protocol.Log{
    84  				{
    85  					Contents: []*protocol.Log_Content{
    86  						{Key: "__name__", Value: "cpu.load"},
    87  						{Key: "__value__", Value: "1"},
    88  						{Key: "__labels__", Value: "host#$#server01|region#$#uswest"},
    89  						{Key: "__time_nano__", Value: "1434055562000000000"},
    90  						{Key: "__type__", Value: "float"},
    91  						{Key: "__field__", Value: "value"},
    92  					},
    93  				},
    94  				{
    95  					Contents: []*protocol.Log_Content{
    96  						{Key: "__name__", Value: "cpu.load:tt"},
    97  						{Key: "__value__", Value: "xx"},
    98  						{Key: "__labels__", Value: "host.dd#$#server02|region#$#uswest"},
    99  						{Key: "__time_nano__", Value: "1434055562000010000"},
   100  						{Key: "__type__", Value: "string"},
   101  						{Key: "__field__", Value: "tt"},
   102  					},
   103  				},
   104  				{
   105  					Contents: []*protocol.Log_Content{
   106  						{Key: "__name__", Value: "cpu.load"},
   107  						{Key: "__value__", Value: "3"},
   108  						{Key: "__labels__", Value: "host.dd#$#server02|region#$#uswest"},
   109  						{Key: "__time_nano__", Value: "1434055562000010000"},
   110  						{Key: "__type__", Value: "float"},
   111  						{Key: "__field__", Value: "value"},
   112  					},
   113  				},
   114  			},
   115  		},
   116  		{
   117  			enableFieldsExtend:     false,
   118  			enableSlsMetricsFormat: true,
   119  			data:                   txtWithDotNames,
   120  			wantErr:                false,
   121  			wantLogs: []*protocol.Log{
   122  				{
   123  					Contents: []*protocol.Log_Content{
   124  						{Key: "__name__", Value: "cpu_load"},
   125  						{Key: "__value__", Value: "1"},
   126  						{Key: "__labels__", Value: "host#$#server01|region#$#uswest"},
   127  						{Key: "__time_nano__", Value: "1434055562000000000"},
   128  					},
   129  				},
   130  				{
   131  					Contents: []*protocol.Log_Content{
   132  						{Key: "__name__", Value: "cpu_load"},
   133  						{Key: "__value__", Value: "3"},
   134  						{Key: "__labels__", Value: "host_dd#$#server02|region#$#uswest"},
   135  						{Key: "__time_nano__", Value: "1434055562000010000"},
   136  					},
   137  				},
   138  			},
   139  		},
   140  	}
   141  
   142  	for _, testCase := range cases {
   143  		decoder := &Decoder{FieldsExtend: testCase.enableFieldsExtend}
   144  		config.LoongcollectorGlobalConfig.EnableSlsMetricsFormat = testCase.enableSlsMetricsFormat
   145  		logs, err := decoder.Decode([]byte(txtWithDotNames), &http.Request{}, nil)
   146  		if testCase.wantErr {
   147  			assert.NotNil(t, err)
   148  			continue
   149  		}
   150  		assert.Nil(t, err)
   151  		assert.Len(t, logs, len(testCase.wantLogs))
   152  		assert.ElementsMatch(t, convertLog2Map(testCase.wantLogs), convertLog2Map(logs))
   153  	}
   154  }
   155  
   156  func convertLog2Map(logs []*protocol.Log) (mapLogs []map[string]string) {
   157  	for _, log := range logs {
   158  		m := map[string]string{}
   159  		for _, v := range log.Contents {
   160  			m[v.Key] = v.Value
   161  		}
   162  		mapLogs = append(mapLogs, m)
   163  	}
   164  	return mapLogs
   165  }
   166  
   167  func TestNormal(t *testing.T) {
   168  	decoder := &Decoder{}
   169  	req := &http.Request{}
   170  	logs, err := decoder.Decode([]byte(textFormat), req, nil)
   171  	assert.Nil(t, err)
   172  	assert.Equal(t, len(logs), 15)
   173  	for _, log := range logs {
   174  		fmt.Printf("%s \n", log.String())
   175  	}
   176  }
   177  
   178  func TestMySQL(t *testing.T) {
   179  	decoder := &Decoder{}
   180  	req := &http.Request{}
   181  	logs, err := decoder.Decode([]byte(mySQLFormat), req, nil)
   182  	assert.Nil(t, err)
   183  	assert.Equal(t, len(logs), 38)
   184  	for _, log := range logs {
   185  		fmt.Printf("%s \n", log.String())
   186  	}
   187  }
   188  
   189  func TestDecodeV2(t *testing.T) {
   190  	decoder := &Decoder{}
   191  	groups, err := decoder.DecodeV2([]byte(txtWithDotNames), &http.Request{Form: map[string][]string{"db": {"mydb"}}})
   192  	assert.Nil(t, err)
   193  	assert.Len(t, groups, 1) // should convert to one eventGroup
   194  
   195  	want := &models.PipelineGroupEvents{
   196  		Group: models.NewGroup(models.NewMetadataWithKeyValues("db", "mydb"), models.NewTags()),
   197  		Events: []models.PipelineEvent{
   198  			models.NewMetric("cpu.load",
   199  				models.MetricTypeUntyped,
   200  				models.NewTagsWithKeyValues("host", "server01", "region", "uswest"),
   201  				1434055562000000000,
   202  				models.NewMetricMultiValueWithMap(map[string]float64{"value": float64(1)}),
   203  				models.NewMetricTypedValues(),
   204  			),
   205  			models.NewMetric("cpu.load",
   206  				models.MetricTypeUntyped,
   207  				models.NewTagsWithKeyValues("host.dd", "server02", "region", "uswest"),
   208  				1434055562000010000,
   209  				models.NewMetricMultiValueWithMap(map[string]float64{"value": float64(3)}),
   210  				models.NewMetricTypedValueWithMap(map[string]*models.TypedValue{"tt": {Type: models.ValueTypeString, Value: "xx"}}),
   211  			),
   212  		},
   213  	}
   214  	assert.Equal(t, want, groups[0])
   215  }