github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/integrations/prometheus/ingest/putmetrics_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package writer
    18  
    19  import (
    20  	"os"
    21  	"sync/atomic"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/gogo/protobuf/proto"
    26  	"github.com/golang/snappy"
    27  	"github.com/prometheus/common/model"
    28  	"github.com/prometheus/prometheus/prompb"
    29  	"github.com/siglens/siglens/pkg/config"
    30  	"github.com/siglens/siglens/pkg/segment/writer"
    31  	log "github.com/sirupsen/logrus"
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  // FixtureSamplePayload returns a Snappy-compressed TimeSeries
    36  func FixtureSamplePayload() []byte {
    37  	nameLabelPair := prompb.Label{Name: model.MetricNameLabel, Value: "mah-test-metric"}
    38  	stubLabelPair := prompb.Label{Name: "environment", Value: "production"}
    39  	stubSample := prompb.Sample{Value: 123.45, Timestamp: time.Now().UTC().Unix()}
    40  	stubTimeSeries := prompb.TimeSeries{
    41  		Labels:  []prompb.Label{stubLabelPair, nameLabelPair},
    42  		Samples: []prompb.Sample{stubSample},
    43  	}
    44  
    45  	writeRequest := prompb.WriteRequest{Timeseries: []prompb.TimeSeries{stubTimeSeries}}
    46  
    47  	protoBytes, _ := proto.Marshal(&writeRequest)
    48  	compressedBytes := snappy.Encode(nil, protoBytes)
    49  	return compressedBytes
    50  }
    51  
    52  func Test_PutMetrics(t *testing.T) {
    53  	config.InitializeTestingConfig()
    54  	writer.InitWriterNode()
    55  	postData := FixtureSamplePayload()
    56  
    57  	sTime := time.Now()
    58  	totalSuccess := uint64(0)
    59  	for i := 0; i < 100; i++ {
    60  		success, fail, err := HandlePutMetrics(postData)
    61  		assert.NoError(t, err)
    62  		assert.Equal(t, success, uint64(1))
    63  		assert.Equal(t, fail, uint64(0))
    64  		atomic.AddUint64(&totalSuccess, success)
    65  	}
    66  	log.Infof("Ingested %+v metrics in %+v", totalSuccess, time.Since(sTime))
    67  	err := os.RemoveAll(config.GetDataPath())
    68  	assert.NoError(t, err)
    69  }