| 
									
										
										
										
											2013-02-07 18:49:04 +08:00
										 |  |  | // Copyright 2013 Prometheus Team
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | package rules | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	clientmodel "github.com/prometheus/client_golang/model" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-28 01:49:45 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/rules/ast" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/storage/metric" | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:51:40 +08:00
										 |  |  | var testSampleInterval = time.Duration(5) * time.Minute | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | var testStartTime = time.Time{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | func getTestValueStream(startVal clientmodel.SampleValue, endVal clientmodel.SampleValue, stepVal clientmodel.SampleValue, startTime time.Time) (resultValues metric.Values) { | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 	currentTime := startTime | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	for currentVal := startVal; currentVal <= endVal; currentVal += stepVal { | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		sample := &metric.SamplePair{ | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 			Value:     currentVal, | 
					
						
							|  |  |  | 			Timestamp: currentTime, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		resultValues = append(resultValues, sample) | 
					
						
							| 
									
										
										
										
											2013-04-24 17:51:40 +08:00
										 |  |  | 		currentTime = currentTime.Add(testSampleInterval) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return resultValues | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getTestVectorFromTestMatrix(matrix ast.Matrix) ast.Vector { | 
					
						
							|  |  |  | 	vector := ast.Vector{} | 
					
						
							|  |  |  | 	for _, sampleSet := range matrix { | 
					
						
							|  |  |  | 		lastSample := sampleSet.Values[len(sampleSet.Values)-1] | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		vector = append(vector, &clientmodel.Sample{ | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 			Metric:    sampleSet.Metric, | 
					
						
							|  |  |  | 			Value:     lastSample.Value, | 
					
						
							|  |  |  | 			Timestamp: lastSample.Timestamp, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return vector | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 00:27:12 +08:00
										 |  |  | func storeMatrix(storage metric.TieredStorage, matrix ast.Matrix) (err error) { | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 	pendingSamples := clientmodel.Samples{} | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	for _, sampleSet := range matrix { | 
					
						
							|  |  |  | 		for _, sample := range sampleSet.Values { | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 			pendingSamples = append(pendingSamples, &clientmodel.Sample{ | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 				Metric:    sampleSet.Metric, | 
					
						
							|  |  |  | 				Value:     sample.Value, | 
					
						
							|  |  |  | 				Timestamp: sample.Timestamp, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-29 22:55:18 +08:00
										 |  |  | 	err = storage.AppendSamples(pendingSamples) | 
					
						
							|  |  |  | 	return | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var testMatrix = ast.Matrix{ | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "api-server", | 
					
						
							|  |  |  | 			"instance":                  "0", | 
					
						
							|  |  |  | 			"group":                     "production", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 100, 10, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "api-server", | 
					
						
							|  |  |  | 			"instance":                  "1", | 
					
						
							|  |  |  | 			"group":                     "production", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 200, 20, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "api-server", | 
					
						
							|  |  |  | 			"instance":                  "0", | 
					
						
							|  |  |  | 			"group":                     "canary", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 300, 30, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "api-server", | 
					
						
							|  |  |  | 			"instance":                  "1", | 
					
						
							|  |  |  | 			"group":                     "canary", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 400, 40, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "app-server", | 
					
						
							|  |  |  | 			"instance":                  "0", | 
					
						
							|  |  |  | 			"group":                     "production", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 500, 50, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "app-server", | 
					
						
							|  |  |  | 			"instance":                  "1", | 
					
						
							|  |  |  | 			"group":                     "production", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 600, 60, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "app-server", | 
					
						
							|  |  |  | 			"instance":                  "0", | 
					
						
							|  |  |  | 			"group":                     "canary", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 700, 70, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "http_requests", | 
					
						
							|  |  |  | 			clientmodel.JobLabel:        "app-server", | 
					
						
							|  |  |  | 			"instance":                  "1", | 
					
						
							|  |  |  | 			"group":                     "canary", | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 800, 80, testStartTime), | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2013-03-29 01:14:41 +08:00
										 |  |  | 	// Single-letter metric and label names.
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "x", | 
					
						
							| 
									
										
										
										
											2013-05-14 18:39:08 +08:00
										 |  |  | 			"y": "testvalue", | 
					
						
							| 
									
										
										
										
											2013-03-29 01:14:41 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		Values: getTestValueStream(0, 100, 10, testStartTime), | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2013-05-28 23:36:53 +08:00
										 |  |  | 	// Counter reset in the middle of range.
 | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "testcounter_reset_middle", | 
					
						
							| 
									
										
										
										
											2013-05-12 08:03:16 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Values: append(getTestValueStream(0, 40, 10, testStartTime), getTestValueStream(0, 50, 10, testStartTime.Add(testSampleInterval*5))...), | 
					
						
							| 
									
										
										
										
											2013-03-29 01:14:41 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2013-05-28 23:36:53 +08:00
										 |  |  | 	// Counter reset at the end of range.
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-06-25 20:02:27 +08:00
										 |  |  | 		Metric: clientmodel.Metric{ | 
					
						
							|  |  |  | 			clientmodel.MetricNameLabel: "testcounter_reset_end", | 
					
						
							| 
									
										
										
										
											2013-05-28 23:36:53 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Values: append(getTestValueStream(0, 90, 10, testStartTime), getTestValueStream(0, 0, 10, testStartTime.Add(testSampleInterval*10))...), | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var testVector = getTestVectorFromTestMatrix(testMatrix) |