github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/converter/influxdb_metric_test.go (about)

     1  // Copyright 2022 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 protocol
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/smartystreets/goconvey/convey"
    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  func TestConvertToInfluxdbProtocolStream(t *testing.T) {
    30  	convey.Convey("Given protocol.LogGroup", t, func() {
    31  		cases := []struct {
    32  			name       string
    33  			logGroup   *protocol.LogGroup
    34  			wantStream interface{}
    35  			wantErr    bool
    36  		}{
    37  			{
    38  				name: "contains metrics without labels or timestamp part",
    39  				logGroup: &protocol.LogGroup{
    40  					Logs: []*protocol.Log{
    41  						{
    42  							Contents: []*protocol.Log_Content{
    43  								{Key: metricNameKey, Value: "metric:field"},
    44  								{Key: metricLabelsKey, Value: ""},
    45  								{Key: metricValueKey, Value: "1"},
    46  							},
    47  						},
    48  						{
    49  							Contents: []*protocol.Log_Content{
    50  								{Key: metricNameKey, Value: "metric:field"},
    51  								{Key: metricLabelsKey, Value: "aa#$#bb"},
    52  								{Key: metricValueKey, Value: "1"},
    53  							},
    54  						},
    55  						{
    56  							Contents: []*protocol.Log_Content{
    57  								{Key: metricNameKey, Value: "metric:field"},
    58  								{Key: metricValueKey, Value: "1"},
    59  								{Key: metricTimeNanoKey, Value: "1667615389000000000"},
    60  							},
    61  						},
    62  					},
    63  				},
    64  				wantStream: [][]byte{[]byte("metric field=1\nmetric,aa=bb field=1\nmetric field=1 1667615389000000000\n")},
    65  			},
    66  		}
    67  
    68  		converter, err := NewConverter("influxdb", "custom", nil, nil, &config.GlobalConfig{})
    69  		convey.So(err, convey.ShouldBeNil)
    70  
    71  		for _, test := range cases {
    72  			convey.Convey(fmt.Sprintf("When %s, result should match", test.name), func() {
    73  
    74  				stream, _, err := converter.ConvertToInfluxdbProtocolStream(test.logGroup, nil)
    75  				if test.wantErr {
    76  					convey.So(err, convey.ShouldNotBeNil)
    77  				} else {
    78  					convey.So(err, convey.ShouldBeNil)
    79  					convey.So(stream, convey.ShouldResemble, test.wantStream)
    80  				}
    81  			})
    82  		}
    83  	})
    84  }
    85  
    86  func TestConverter_ConvertToInfluxdbProtocolStreamV2(t *testing.T) {
    87  	date := time.Now()
    88  	convey.Convey("Given a PipelineGroupEvents", t, func() {
    89  		cases := []struct {
    90  			name        string
    91  			groupEvents *models.PipelineGroupEvents
    92  			wantStream  interface{}
    93  			wantErr     bool
    94  		}{
    95  			{
    96  				name: "contains none MetricsEvents type",
    97  				groupEvents: &models.PipelineGroupEvents{
    98  					Events: []models.PipelineEvent{
    99  						models.NewMetric("cpu", models.MetricTypeCounter, models.NewTags(), time.Now().UnixNano(), &models.MetricSingleValue{Value: 1}, nil),
   100  						models.NewByteArray([]byte("test")),
   101  					},
   102  				},
   103  				wantErr: true,
   104  			},
   105  			{
   106  				name: "contains single value metric only",
   107  				groupEvents: &models.PipelineGroupEvents{
   108  					Events: []models.PipelineEvent{
   109  						models.NewMetric("cpu1", models.MetricTypeCounter, models.NewTagsWithKeyValues("k2", "v2", "k1", "v1"), date.Add(1).UnixNano(), &models.MetricSingleValue{Value: 1}, nil),
   110  						models.NewMetric("cpu2", models.MetricTypeCounter, models.NewTagsWithKeyValues("k3", "v3", "k4", "v4"), date.Add(2).UnixNano(), &models.MetricSingleValue{Value: 10}, nil),
   111  						models.NewMetric("cpu3", models.MetricTypeCounter, models.NewTagsWithKeyValues("k5", "v5", "k6", "v6"), date.Add(3).UnixNano(), &models.MetricSingleValue{Value: 20}, nil),
   112  					},
   113  				},
   114  				wantStream: [][]byte{[]byte(fmt.Sprintf("cpu1,k1=v1,k2=v2 value=1 %d\ncpu2,k3=v3,k4=v4 value=10 %d\ncpu3,k5=v5,k6=v6 value=20 %d\n",
   115  					date.Add(1).UnixNano(), date.Add(2).UnixNano(), date.Add(3).UnixNano()))},
   116  			},
   117  			{
   118  				name: "contains multi fields value metric only",
   119  				groupEvents: &models.PipelineGroupEvents{
   120  					Events: []models.PipelineEvent{
   121  						models.NewMetric("cpu1", models.MetricTypeCounter, models.NewTagsWithKeyValues("k2", "v2", "k1", "v1"), date.Add(1).UnixNano(), models.NewMetricMultiValueWithMap(map[string]float64{"f1": 1}), nil),
   122  						models.NewMetric("cpu2", models.MetricTypeCounter, models.NewTagsWithKeyValues("k3", "v3", "k4", "v4"), date.Add(2).UnixNano(), models.NewMetricMultiValueWithMap(map[string]float64{"f1": 3}), nil),
   123  						models.NewMetric("cpu3", models.MetricTypeCounter, models.NewTagsWithKeyValues("k5", "v5", "k6", "v6"), date.Add(3).UnixNano(), models.NewMetricMultiValueWithMap(map[string]float64{"f1": 5}), nil),
   124  					},
   125  				},
   126  				wantStream: [][]byte{[]byte(fmt.Sprintf("cpu1,k1=v1,k2=v2 f1=1 %d\ncpu2,k3=v3,k4=v4 f1=3 %d\ncpu3,k5=v5,k6=v6 f1=5 %d\n",
   127  					date.Add(1).UnixNano(), date.Add(2).UnixNano(), date.Add(3).UnixNano()))},
   128  			},
   129  			{
   130  				name: "contains multi fields value metric and type value",
   131  				groupEvents: &models.PipelineGroupEvents{
   132  					Events: []models.PipelineEvent{
   133  						models.NewMetric("cpu1", models.MetricTypeCounter, models.NewTagsWithKeyValues("k2", "v2", "k1", "v1"), date.Add(1).UnixNano(), models.NewMetricMultiValueWithMap(map[string]float64{"f1": 1}), nil),
   134  						models.NewMetric("cpu2", models.MetricTypeCounter, models.NewTagsWithKeyValues("k3", "v3", "k4", "v4"), date.Add(2).UnixNano(), &models.MetricSingleValue{Value: 1},
   135  							models.NewMetricTypedValueWithMap(map[string]*models.TypedValue{"f1": {Type: models.ValueTypeString, Value: "f1v"}})),
   136  						models.NewMetric("cpu3", models.MetricTypeCounter, models.NewTagsWithKeyValues("k3", "v3", "k4", "v4"), date.Add(2).UnixNano(), nil,
   137  							models.NewMetricTypedValueWithMap(map[string]*models.TypedValue{"f2": {Type: models.ValueTypeBoolean, Value: true}})),
   138  					},
   139  				},
   140  				wantStream: [][]byte{[]byte(fmt.Sprintf("cpu1,k1=v1,k2=v2 f1=1 %d\ncpu2,k3=v3,k4=v4 value=1,f1=\"f1v\" %d\ncpu3,k3=v3,k4=v4 f2=true %d\n",
   141  					date.Add(1).UnixNano(), date.Add(2).UnixNano(), date.Add(2).UnixNano()))},
   142  			},
   143  		}
   144  
   145  		converter, err := NewConverter("influxdb", "custom", nil, nil, &config.GlobalConfig{})
   146  		convey.So(err, convey.ShouldBeNil)
   147  
   148  		for _, test := range cases {
   149  			convey.Convey(fmt.Sprintf("When %s, result should match", test.name), func() {
   150  				stream, _, err := converter.ConvertToInfluxdbProtocolStreamV2(test.groupEvents, nil)
   151  				if test.wantErr {
   152  					convey.So(err, convey.ShouldNotBeNil)
   153  				} else {
   154  					convey.So(err, convey.ShouldBeNil)
   155  					convey.So(stream, convey.ShouldResemble, test.wantStream)
   156  				}
   157  			})
   158  		}
   159  	})
   160  }