github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/selfmonitor/metrics_record_test.go (about)

     1  // Copyright 2025 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 selfmonitor
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestExportMetricRecords(t *testing.T) {
    24  	metricRecord := MetricsRecord{}
    25  	addEventCount := NewCounterMetricAndRegister(&metricRecord, MetricRunnerK8sMetaAddEventTotal)
    26  	updateEventCount := NewCounterMetricAndRegister(&metricRecord, MetricRunnerK8sMetaUpdateEventTotal)
    27  	deleteEventCount := NewCounterMetricAndRegister(&metricRecord, MetricRunnerK8sMetaDeleteEventTotal)
    28  	cacheResourceGauge := NewGaugeMetricAndRegister(&metricRecord, MetricRunnerK8sMetaCacheSize)
    29  	queueSizeGauge := NewGaugeMetricAndRegister(&metricRecord, MetricRunnerK8sMetaQueueSize)
    30  	httpRequestCount := NewCounterMetricAndRegister(&metricRecord, MetricRunnerK8sMetaHTTPRequestTotal)
    31  	httpAvgDelayMs := NewAverageMetricAndRegister(&metricRecord, MetricRunnerK8sMetaHTTPAvgDelayMs)
    32  	httpMaxDelayMs := NewMaxMetricAndRegister(&metricRecord, MetricRunnerK8sMetaHTTPMaxDelayMs)
    33  
    34  	metricRecord.Labels = []LabelPair{
    35  		{
    36  			Key:   MetricLabelKeyMetricCategory,
    37  			Value: MetricLabelValueMetricCategoryRunner,
    38  		},
    39  		{
    40  			Key:   MetricLabelKeyClusterID,
    41  			Value: "test-cluster-id",
    42  		},
    43  		{
    44  			Key:   MetricLabelKeyRunnerName,
    45  			Value: MetricLabelValueRunnerNameK8sMeta,
    46  		},
    47  		{
    48  			Key:   MetricLabelKeyProject,
    49  			Value: "test-project",
    50  		},
    51  	}
    52  
    53  	addEventCount.Add(1)
    54  	updateEventCount.Add(2)
    55  	deleteEventCount.Add(3)
    56  	cacheResourceGauge.Set(4)
    57  	queueSizeGauge.Set(5)
    58  	httpRequestCount.Add(6)
    59  	httpAvgDelayMs.Add(7)
    60  	httpMaxDelayMs.Set(8)
    61  
    62  	result := metricRecord.ExportMetricRecords()
    63  	assert.Equal(t, 3, len(result))
    64  	assert.Equal(t, "{\"add_event_total\":\"1.0000\",\"delete_event_total\":\"3.0000\",\"http_request_total\":\"6.0000\",\"update_event_total\":\"2.0000\"}", result["counters"])
    65  	assert.Equal(t, "{\"avg_delay_ms\":\"7.0000\",\"cache_size\":\"4.0000\",\"max_delay_ms\":\"8.0000\",\"queue_size\":\"5.0000\"}", result["gauges"])
    66  	assert.Equal(t, "{\"cluster_id\":\"test-cluster-id\",\"metric_category\":\"runner\",\"project\":\"test-project\",\"runner_name\":\"k8s_meta\"}", result["labels"])
    67  }