Ejemplo n.º 1
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))
			}
		}
	}
}
Ejemplo n.º 2
0
func main() {
	flag.Parse()
	conf, err := config.LoadFromFile(*configFile)
	if err != nil {
		log.Fatalf("Error loading configuration from %s: %v", configFile, err)
	}

	persistence, err := leveldb.NewLevelDBMetricPersistence(*metricsStoragePath)
	if err != nil {
		log.Print(err)
		os.Exit(1)
	}

	go func() {
		notifier := make(chan os.Signal)
		signal.Notify(notifier, os.Interrupt)
		<-notifier
		persistence.Close()
		os.Exit(0)
	}()

	defer persistence.Close()

	scrapeResults := make(chan retrieval.Result, 4096)

	targetManager := retrieval.NewTargetManager(scrapeResults, 1)
	targetManager.AddTargetsFromConfig(conf)

	ruleResults := make(chan *rules.Result, 4096)

	ast.SetPersistence(persistence)
	ruleManager := rules.NewRuleManager(ruleResults, conf.Global.EvaluationInterval)
	err = ruleManager.AddRulesFromConfig(conf)
	if err != nil {
		log.Fatalf("Error loading rule files: %v", err)
	}

	go func() {
		gorest.RegisterService(new(api.MetricsService))
		exporter := registry.DefaultRegistry.YieldExporter()

		http.Handle("/", gorest.Handle())
		http.Handle("/metrics.json", exporter)
		http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
		http.ListenAndServe(":9090", nil)
	}()

	for {
		select {
		case scrapeResult := <-scrapeResults:
			//fmt.Printf("scrapeResult -> %s\n", scrapeResult)
			for _, sample := range scrapeResult.Samples {
				persistence.AppendSample(&sample)
			}
		case ruleResult := <-ruleResults:
			//fmt.Printf("ruleResult -> %s\n", ruleResult)
			for _, sample := range ruleResult.Samples {
				persistence.AppendSample(sample)
			}
		}
	}
}