| 
									
										
										
										
											2017-10-24 04:39:40 +08:00
										 |  |  | // Copyright 2017 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | package remote | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2020-10-22 17:00:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 22:23:17 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/model/labels" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/model/textparse" | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/prompb" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/storage" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | var writeRequestFixture = &prompb.WriteRequest{ | 
					
						
							|  |  |  | 	Timeseries: []prompb.TimeSeries{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Labels: []prompb.Label{ | 
					
						
							|  |  |  | 				{Name: "__name__", Value: "test_metric1"}, | 
					
						
							|  |  |  | 				{Name: "b", Value: "c"}, | 
					
						
							|  |  |  | 				{Name: "baz", Value: "qux"}, | 
					
						
							|  |  |  | 				{Name: "d", Value: "e"}, | 
					
						
							|  |  |  | 				{Name: "foo", Value: "bar"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2021-09-22 04:53:27 +08:00
										 |  |  | 			Samples:   []prompb.Sample{{Value: 1, Timestamp: 0}}, | 
					
						
							|  |  |  | 			Exemplars: []prompb.Exemplar{{Labels: []prompb.Label{{Name: "f", Value: "g"}}, Value: 1, Timestamp: 0}}, | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Labels: []prompb.Label{ | 
					
						
							|  |  |  | 				{Name: "__name__", Value: "test_metric1"}, | 
					
						
							|  |  |  | 				{Name: "b", Value: "c"}, | 
					
						
							|  |  |  | 				{Name: "baz", Value: "qux"}, | 
					
						
							|  |  |  | 				{Name: "d", Value: "e"}, | 
					
						
							|  |  |  | 				{Name: "foo", Value: "bar"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2021-09-22 04:53:27 +08:00
										 |  |  | 			Samples:   []prompb.Sample{{Value: 2, Timestamp: 1}}, | 
					
						
							|  |  |  | 			Exemplars: []prompb.Exemplar{{Labels: []prompb.Label{{Name: "h", Value: "i"}}, Value: 2, Timestamp: 1}}, | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | func TestValidateLabelsAndMetricName(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		input       labels.Labels | 
					
						
							|  |  |  | 		expectedErr string | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 		description string | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"labelName", "labelValue", | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			expectedErr: "", | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			description: "regular labels", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"_labelName", "labelValue", | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			expectedErr: "", | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			description: "label name with _", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"@labelName", "labelValue", | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			expectedErr: "invalid label name: @labelName", | 
					
						
							|  |  |  | 			description: "label name with @", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"123labelName", "labelValue", | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			expectedErr: "invalid label name: 123labelName", | 
					
						
							|  |  |  | 			description: "label name starts with numbers", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"", "labelValue", | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			expectedErr: "invalid label name: ", | 
					
						
							|  |  |  | 			description: "label name is empty string", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name", | 
					
						
							|  |  |  | 				"labelName", string([]byte{0xff}), | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			expectedErr: "invalid label value: " + string([]byte{0xff}), | 
					
						
							|  |  |  | 			description: "label value is an invalid UTF-8 value", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "@invalid_name", | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 			expectedErr: "invalid metric name: @invalid_name", | 
					
						
							|  |  |  | 			description: "metric name starts with @", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"__name__", "name1", | 
					
						
							|  |  |  | 				"__name__", "name2", | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			expectedErr: "duplicate label with name: __name__", | 
					
						
							|  |  |  | 			description: "duplicate label names", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"label1", "name", | 
					
						
							|  |  |  | 				"label2", "name", | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			expectedErr: "", | 
					
						
							|  |  |  | 			description: "duplicate label values", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: labels.FromStrings( | 
					
						
							|  |  |  | 				"", "name", | 
					
						
							|  |  |  | 				"label2", "name", | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			expectedErr: "invalid label name: ", | 
					
						
							|  |  |  | 			description: "don't report as duplicate label name", | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 		t.Run(test.description, func(t *testing.T) { | 
					
						
							|  |  |  | 			err := validateLabelsAndMetricName(test.input) | 
					
						
							| 
									
										
										
										
											2019-10-08 01:35:53 +08:00
										 |  |  | 			if test.expectedErr != "" { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 				require.Error(t, err) | 
					
						
							|  |  |  | 				require.Equal(t, test.expectedErr, err.Error()) | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestConcreteSeriesSet(t *testing.T) { | 
					
						
							|  |  |  | 	series1 := &concreteSeries{ | 
					
						
							|  |  |  | 		labels:  labels.FromStrings("foo", "bar"), | 
					
						
							| 
									
										
										
										
											2019-01-17 06:28:08 +08:00
										 |  |  | 		samples: []prompb.Sample{{Value: 1, Timestamp: 2}}, | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	series2 := &concreteSeries{ | 
					
						
							|  |  |  | 		labels:  labels.FromStrings("foo", "baz"), | 
					
						
							| 
									
										
										
										
											2019-01-17 06:28:08 +08:00
										 |  |  | 		samples: []prompb.Sample{{Value: 3, Timestamp: 4}}, | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	c := &concreteSeriesSet{ | 
					
						
							|  |  |  | 		series: []storage.Series{series1, series2}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.True(t, c.Next(), "Expected Next() to be true.") | 
					
						
							|  |  |  | 	require.Equal(t, series1, c.At(), "Unexpected series returned.") | 
					
						
							|  |  |  | 	require.True(t, c.Next(), "Expected Next() to be true.") | 
					
						
							|  |  |  | 	require.Equal(t, series2, c.At(), "Unexpected series returned.") | 
					
						
							|  |  |  | 	require.False(t, c.Next(), "Expected Next() to be false.") | 
					
						
							| 
									
										
										
										
											2017-10-24 04:28:17 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-12 07:47:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestConcreteSeriesClonesLabels(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-07-22 00:41:20 +08:00
										 |  |  | 	lbls := labels.FromStrings("a", "b", "c", "d") | 
					
						
							| 
									
										
										
										
											2017-11-12 07:47:47 +08:00
										 |  |  | 	cs := concreteSeries{ | 
					
						
							| 
									
										
										
										
											2022-07-22 00:41:20 +08:00
										 |  |  | 		labels: lbls, | 
					
						
							| 
									
										
										
										
											2017-11-12 07:47:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gotLabels := cs.Labels() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, lbls, gotLabels) | 
					
						
							| 
									
										
										
										
											2017-11-12 07:47:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	gotLabels[0].Value = "foo" | 
					
						
							|  |  |  | 	gotLabels[1].Value = "bar" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gotLabels = cs.Labels() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, lbls, gotLabels) | 
					
						
							| 
									
										
										
										
											2017-11-12 07:47:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:47:56 +08:00
										 |  |  | func TestConcreteSeriesIterator(t *testing.T) { | 
					
						
							|  |  |  | 	series := &concreteSeries{ | 
					
						
							|  |  |  | 		labels: labels.FromStrings("foo", "bar"), | 
					
						
							|  |  |  | 		samples: []prompb.Sample{ | 
					
						
							|  |  |  | 			{Value: 1, Timestamp: 1}, | 
					
						
							|  |  |  | 			{Value: 1.5, Timestamp: 1}, | 
					
						
							|  |  |  | 			{Value: 2, Timestamp: 2}, | 
					
						
							|  |  |  | 			{Value: 3, Timestamp: 3}, | 
					
						
							|  |  |  | 			{Value: 4, Timestamp: 4}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	it := series.Iterator() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Seek to the first sample with ts=1.
 | 
					
						
							|  |  |  | 	require.True(t, it.Seek(1)) | 
					
						
							|  |  |  | 	ts, v := it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(1), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 1., v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Seek one further, next sample still has ts=1.
 | 
					
						
							|  |  |  | 	require.True(t, it.Next()) | 
					
						
							|  |  |  | 	ts, v = it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(1), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 1.5, v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Seek again to 1 and make sure we stay where we are.
 | 
					
						
							|  |  |  | 	require.True(t, it.Seek(1)) | 
					
						
							|  |  |  | 	ts, v = it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(1), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 1.5, v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Another seek.
 | 
					
						
							|  |  |  | 	require.True(t, it.Seek(3)) | 
					
						
							|  |  |  | 	ts, v = it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(3), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 3., v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// And we don't go back.
 | 
					
						
							|  |  |  | 	require.True(t, it.Seek(2)) | 
					
						
							|  |  |  | 	ts, v = it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(3), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 3., v) | 
					
						
							| 
									
										
										
										
											2021-12-16 19:01:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Seek beyond the end.
 | 
					
						
							|  |  |  | 	require.False(t, it.Seek(5)) | 
					
						
							|  |  |  | 	// And we don't go back. (This exposes issue #10027.)
 | 
					
						
							|  |  |  | 	require.False(t, it.Seek(2)) | 
					
						
							| 
									
										
										
										
											2021-11-29 17:47:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | func TestFromQueryResultWithDuplicates(t *testing.T) { | 
					
						
							|  |  |  | 	ts1 := prompb.TimeSeries{ | 
					
						
							|  |  |  | 		Labels: []prompb.Label{ | 
					
						
							| 
									
										
										
										
											2020-04-15 18:17:41 +08:00
										 |  |  | 			{Name: "foo", Value: "bar"}, | 
					
						
							|  |  |  | 			{Name: "foo", Value: "def"}, | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Samples: []prompb.Sample{ | 
					
						
							| 
									
										
										
										
											2020-04-15 18:17:41 +08:00
										 |  |  | 			{Value: 0.0, Timestamp: 0}, | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res := prompb.QueryResult{ | 
					
						
							|  |  |  | 		Timeseries: []*prompb.TimeSeries{ | 
					
						
							|  |  |  | 			&ts1, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:36:09 +08:00
										 |  |  | 	series := FromQueryResult(false, &res) | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	errSeries, isErrSeriesSet := series.(errSeriesSet) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.True(t, isErrSeriesSet, "Expected resulting series to be an errSeriesSet") | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | 	errMessage := errSeries.Err().Error() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Equal(t, "duplicate label with name: foo", errMessage, fmt.Sprintf("Expected error to be from duplicate label, but got: %s", errMessage)) | 
					
						
							| 
									
										
										
										
											2019-08-07 23:13:10 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-20 04:16:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestNegotiateResponseType(t *testing.T) { | 
					
						
							|  |  |  | 	r, err := NegotiateResponseType([]prompb.ReadRequest_ResponseType{ | 
					
						
							|  |  |  | 		prompb.ReadRequest_STREAMED_XOR_CHUNKS, | 
					
						
							|  |  |  | 		prompb.ReadRequest_SAMPLES, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, prompb.ReadRequest_STREAMED_XOR_CHUNKS, r) | 
					
						
							| 
									
										
										
										
											2019-08-20 04:16:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r2, err := NegotiateResponseType([]prompb.ReadRequest_ResponseType{ | 
					
						
							|  |  |  | 		prompb.ReadRequest_SAMPLES, | 
					
						
							|  |  |  | 		prompb.ReadRequest_STREAMED_XOR_CHUNKS, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, prompb.ReadRequest_SAMPLES, r2) | 
					
						
							| 
									
										
										
										
											2019-08-20 04:16:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	r3, err := NegotiateResponseType([]prompb.ReadRequest_ResponseType{}) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, prompb.ReadRequest_SAMPLES, r3) | 
					
						
							| 
									
										
										
										
											2019-08-20 04:16:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_, err = NegotiateResponseType([]prompb.ReadRequest_ResponseType{20}) | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.Error(t, err, "expected error due to not supported requested response types") | 
					
						
							|  |  |  | 	require.Equal(t, "server does not support any of the requested response types: [20]; supported: map[SAMPLES:{} STREAMED_XOR_CHUNKS:{}]", err.Error()) | 
					
						
							| 
									
										
										
										
											2019-08-20 04:16:10 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-07-05 12:17:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMergeLabels(t *testing.T) { | 
					
						
							|  |  |  | 	for _, tc := range []struct { | 
					
						
							|  |  |  | 		primary, secondary, expected []prompb.Label | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			primary:   []prompb.Label{{Name: "aaa", Value: "foo"}, {Name: "bbb", Value: "foo"}, {Name: "ddd", Value: "foo"}}, | 
					
						
							|  |  |  | 			secondary: []prompb.Label{{Name: "bbb", Value: "bar"}, {Name: "ccc", Value: "bar"}}, | 
					
						
							|  |  |  | 			expected:  []prompb.Label{{Name: "aaa", Value: "foo"}, {Name: "bbb", Value: "foo"}, {Name: "ccc", Value: "bar"}, {Name: "ddd", Value: "foo"}}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			primary:   []prompb.Label{{Name: "bbb", Value: "bar"}, {Name: "ccc", Value: "bar"}}, | 
					
						
							|  |  |  | 			secondary: []prompb.Label{{Name: "aaa", Value: "foo"}, {Name: "bbb", Value: "foo"}, {Name: "ddd", Value: "foo"}}, | 
					
						
							|  |  |  | 			expected:  []prompb.Label{{Name: "aaa", Value: "foo"}, {Name: "bbb", Value: "bar"}, {Name: "ccc", Value: "bar"}, {Name: "ddd", Value: "foo"}}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, tc.expected, MergeLabels(tc.primary, tc.secondary)) | 
					
						
							| 
									
										
										
										
											2020-07-05 12:17:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-11-19 23:23:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMetricTypeToMetricTypeProto(t *testing.T) { | 
					
						
							|  |  |  | 	tc := []struct { | 
					
						
							|  |  |  | 		desc     string | 
					
						
							|  |  |  | 		input    textparse.MetricType | 
					
						
							|  |  |  | 		expected prompb.MetricMetadata_MetricType | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "with a single-word metric", | 
					
						
							|  |  |  | 			input:    textparse.MetricTypeCounter, | 
					
						
							|  |  |  | 			expected: prompb.MetricMetadata_COUNTER, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "with a two-word metric", | 
					
						
							|  |  |  | 			input:    textparse.MetricTypeStateset, | 
					
						
							|  |  |  | 			expected: prompb.MetricMetadata_STATESET, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "with an unknown metric", | 
					
						
							|  |  |  | 			input:    "not-known", | 
					
						
							|  |  |  | 			expected: prompb.MetricMetadata_UNKNOWN, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tt := range tc { | 
					
						
							|  |  |  | 		t.Run(tt.desc, func(t *testing.T) { | 
					
						
							|  |  |  | 			m := metricTypeToMetricTypeProto(tt.input) | 
					
						
							|  |  |  | 			require.Equal(t, tt.expected, m) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestDecodeWriteRequest(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-10-30 05:44:40 +08:00
										 |  |  | 	buf, _, err := buildWriteRequest(writeRequestFixture.Timeseries, nil, nil, nil) | 
					
						
							| 
									
										
										
										
											2021-01-30 19:04:48 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	actual, err := DecodeWriteRequest(bytes.NewReader(buf)) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, writeRequestFixture, actual) | 
					
						
							|  |  |  | } |