Exemplo n.º 1
0
func (serv MetricsService) Query(Expr string, Json string) (result string) {
	exprNode, err := rules.LoadExprFromString(Expr)
	if err != nil {
		return ast.ErrorToJSON(err)
	}

	timestamp := time.Now()

	rb := serv.ResponseBuilder()
	var format ast.OutputFormat
	if Json != "" {
		format = ast.JSON
		rb.SetContentType(gorest.Application_Json)
	} else {
		format = ast.TEXT
		rb.SetContentType(gorest.Text_Plain)
	}

	return ast.EvalToString(exprNode, &timestamp, format)
}
Exemplo n.º 2
0
func TestExpressions(t *testing.T) {
	temporaryDirectory, err := ioutil.TempDir("", "leveldb_metric_persistence_test")
	if err != nil {
		t.Errorf("Could not create temporary directory: %q\n", err)
		return
	}
	defer func() {
		if err = os.RemoveAll(temporaryDirectory); err != nil {
			t.Errorf("Could not remove temporary directory: %q\n", err)
		}
	}()
	persistence, err := leveldb.NewLevelDBMetricPersistence(temporaryDirectory)
	if err != nil {
		t.Errorf("Could not create LevelDB Metric Persistence: %q\n", err)
		return
	}
	if persistence == nil {
		t.Errorf("Received nil LevelDB Metric Persistence.\n")
		return
	}
	defer func() {
		persistence.Close()
	}()

	storeMatrix(persistence, testMatrix)
	ast.SetPersistence(persistence)

	for _, exprTest := range expressionTests {
		expectedLines := annotateWithTime(exprTest.output)

		testExpr, err := LoadExprFromString(exprTest.expr)

		if err != nil {
			if exprTest.shouldFail {
				continue
			}
			t.Errorf("Error during parsing: %v", err)
			t.Errorf("Expression: %v", exprTest.expr)
		} else {
			failed := false
			resultStr := ast.EvalToString(testExpr, &testEvalTime, ast.TEXT)
			resultLines := strings.Split(resultStr, "\n")

			if len(exprTest.output) != len(resultLines) {
				t.Errorf("Number of samples in expected and actual output don't match")
				failed = true
			}
			for _, expectedSample := range expectedLines {
				found := false
				for _, actualSample := range resultLines {
					if actualSample == expectedSample {
						found = true
					}
				}
				if !found {
					t.Errorf("Couldn't find expected sample in output: '%v'",
						expectedSample)
					failed = true
				}
			}
			if failed {
				t.Errorf("Expression: %v\n%v", exprTest.expr, vectorComparisonString(expectedLines, resultLines))
			}
		}
	}
}