Ejemplo n.º 1
0
func TestNerveHTTPDCollectWhiteList(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		fmt.Fprint(w, string(getRawApacheStat()))
	}))
	defer server.Close()
	ip, port := parseURL(server.URL)

	server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		fmt.Fprint(w, string(getRawApacheStat()))
	}))
	defer server2.Close()
	ip2, port2 := parseURL(server2.URL)
	minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
		"test_service.namespace1.and.stuff": util.EndPoint{ip, port},
		"test_service.namespace2.and.stuff": util.EndPoint{ip2, port2},
	})

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	defer os.Remove(tmpFile.Name())
	assert.Nil(t, err)

	marshalled, err := json.Marshal(minimalNerveConfig)
	assert.Nil(t, err)

	_, err = tmpFile.Write(marshalled)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath":    tmpFile.Name(),
		"queryPath":         "",
		"servicesWhitelist": []string{"test_service.namespace2"},
	}

	inst := getNerveHTTPDCollector()
	inst.Configure(cfg)

	inst.Collect()
	actual := []metric.Metric{}
	flag := true
	for flag == true {
		select {
		case metric := <-inst.Channel():
			actual = append(actual, metric)
		case <-time.After(2 * time.Second):
			flag = false
			break
		}
	}
	assert.Equal(t, 18, len(actual))

	metricMap := map[string]metric.Metric{}
	for _, m := range actual {
		metricMap[m.Name] = m
	}
	assert.Equal(t, port2, metricMap["TotalAccesses"].Dimensions["port"])
	assert.Equal(t, "test_service", metricMap["TotalAccesses"].Dimensions["service_name"])
	assert.Equal(t, "namespace2", metricMap["TotalAccesses"].Dimensions["service_namespace"])
}
Ejemplo n.º 2
0
func TestNerveHTTPDCollect(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		fmt.Fprint(w, string(getRawApacheStat()))
	}))
	defer server.Close()
	ip, port := parseURL(server.URL)
	minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
		"test_service.things.and.stuff": util.EndPoint{ip, port},
	})

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	defer os.Remove(tmpFile.Name())
	assert.Nil(t, err)

	marshalled, err := json.Marshal(minimalNerveConfig)
	assert.Nil(t, err)

	_, err = tmpFile.Write(marshalled)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath":    tmpFile.Name(),
		"queryPath":         "",
		"servicesWhitelist": []string{"test_service.things"},
	}

	inst := getNerveHTTPDCollector()
	inst.Configure(cfg)

	inst.Collect()
	actual := []metric.Metric{}
	for i := 0; i < 17; i++ {
		actual = append(actual, <-inst.Channel())
	}
	metricMap := map[string]metric.Metric{}
	for _, m := range actual {
		metricMap[m.Name] = m
	}
	assert.Equal(t, 99.0, metricMap["TotalAccesses"].Value)
	assert.Equal(t, 34.0, metricMap["WritingWorkers"].Value)
	assert.Equal(t, 6.0, metricMap["IdleWorkers"].Value)
	assert.Equal(t, 6.0, metricMap["StandbyWorkers"].Value)
	assert.Equal(t, 901.485, metricMap["CPULoad"].Value)
	assert.Equal(t, 1.45588, metricMap["ReqPerSec"].Value)
	assert.Equal(t, 1626.35, metricMap["BytesPerSec"].Value)

	assert.Equal(t, port, metricMap["TotalAccesses"].Dimensions["port"])
	assert.Equal(t, "test_service", metricMap["TotalAccesses"].Dimensions["service_name"])
	assert.Equal(t, "things", metricMap["TotalAccesses"].Dimensions["service_namespace"])
}
Ejemplo n.º 3
0
func TestNonConflictingServiceQueries(t *testing.T) {
	goodServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		fmt.Fprint(w, getTestUWSGIResponse())
	}))
	defer goodServer.Close()

	badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		return // no response
	}))
	defer badServer.Close()

	goodIP, goodPort := parseURL(goodServer.URL)
	badIP, badPort := parseURL(badServer.URL)
	minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
		"test_service.things.and.stuff":    util.EndPoint{goodIP, goodPort},
		"other_service.does.lots.of.stuff": util.EndPoint{badIP, badPort},
	})

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	defer os.Remove(tmpFile.Name())
	assert.Nil(t, err)

	marshalled, err := json.Marshal(minimalNerveConfig)
	assert.Nil(t, err)

	_, err = tmpFile.Write(marshalled)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath": tmpFile.Name(),
		"queryPath":      "",
	}

	inst := getTestNerveUWSGI()
	inst.Configure(cfg)

	go inst.Collect()

	actual := []metric.Metric{}
	for i := 0; i < 5; i++ {
		actual = append(actual, <-inst.Channel())
	}

	validateUWSGIResults(t, actual)
	validateFullDimensions(t, actual, "test_service", goodPort)
	validateEmptyChannel(t, inst.Channel())
}
Ejemplo n.º 4
0
func TestNerveUWSGICollectWithSchema(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		w.Header().Set("Metrics-Schema", "uwsgi.1.1")
		fmt.Fprint(w, getTestSchemaUWSGIResponse())
	}))
	defer server.Close()

	// assume format is http://ipaddr:port
	ip, port := parseURL(server.URL)

	minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
		"test_service.things.and.stuff": util.EndPoint{ip, port},
	})

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	defer os.Remove(tmpFile.Name())
	assert.Nil(t, err)

	marshalled, err := json.Marshal(minimalNerveConfig)
	assert.Nil(t, err)

	_, err = tmpFile.Write(marshalled)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath": tmpFile.Name(),
		"queryPath":      "",
	}

	inst := getTestNerveUWSGI()
	inst.Configure(cfg)

	go inst.Collect()

	actual := []metric.Metric{}
	for i := 0; i < 5; i++ {
		actual = append(actual, <-inst.Channel())
	}

	validateUWSGIResults(t, actual)
	validateFullSchemaDimensions(t, actual, "test_service", port)
	validateEmptyChannel(t, inst.Channel())
}
Ejemplo n.º 5
0
func TestNerveJavaCollectWithSchemaCumulativeCountersEnabled(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		w.Header().Set("Metrics-Schema", "java-1.1")
		fmt.Fprint(w, getTestJavaResponse())
	}))
	defer server.Close()

	// assume format is http://ipaddr:port
	ip, port := parseURL(server.URL)

	minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
		"test_service.namespace": util.EndPoint{ip, port},
	})

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	defer os.Remove(tmpFile.Name())
	assert.Nil(t, err)

	marshalled, err := json.Marshal(minimalNerveConfig)
	assert.Nil(t, err)

	_, err = tmpFile.Write(marshalled)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath":    tmpFile.Name(),
		"queryPath":         "",
		"servicesWhitelist": []string{"test_service"},
	}

	inst := getTestNerveUWSGI()
	inst.Configure(cfg)

	go inst.Collect()

	actual := []metric.Metric{}
	flag := true
	for flag == true {
		select {
		case metric := <-inst.Channel():
			actual = append(actual, metric)
		case <-time.After(2 * time.Second):
			flag = false
			break
		}
	}
	assert.Equal(t, 7, len(actual))

	for _, m := range actual {
		switch m.Name {
		case "Acounter.firstrollup":
			assert.Equal(t, 134.0, m.Value)
		case "Acounter.count":
			assert.Equal(t, 100.0, m.Value)
		case "Acounter.secondrollup":
			assert.Equal(t, 89.0, m.Value)
		case "some_timer":
			assert.Equal(t, 123.0, m.Value)
		case "some_timer.count":
			assert.Equal(t, 200.0, m.Value)
			assert.Equal(t, metric.CumulativeCounter, m.MetricType)
		case "othertimer.mean":
			assert.Equal(t, 345.0, m.Value)
		case "some_random_metric.rollup1":
			assert.Equal(t, 12.0, m.Value)
		default:
			t.Fatalf("unknown metric name %s", m.Name)
		}
	}
}
func DoTesting(t *testing.T, firstResponse string, secondResponse string, results [6]float64) {
	goodServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
		fmt.Fprint(w, firstResponse)
	}))
	defer goodServer.Close()
	// assume format is http://ipaddr:port
	goodIP, goodPort := convertURL(goodServer.URL)
	badIP, badPort, content := "", "", []byte{}

	if secondResponse != "" {
		badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, rsp *http.Request) {
			if secondResponse == "none" {
				return // no response
			}
			fmt.Fprint(w, secondResponse)
		}))
		defer badServer.Close()
		badIP, badPort = convertURL(badServer.URL)
	}

	if badIP == "" {
		// If whitelisting fails, non_whitelisted_service will send on a closed channel
		minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
			"test_service.things.and.stuff":            util.EndPoint{goodIP, goodPort},
			"non_whitelisted_service.things.and.stuff": util.EndPoint{goodIP, goodPort},
		})
		marshalled, err := json.Marshal(minimalNerveConfig)
		assert.Nil(t, err)
		content = marshalled
	} else {
		minimalNerveConfig := util.CreateMinimalNerveConfig(map[string]util.EndPoint{
			"test_service.things.and.stuff":    util.EndPoint{goodIP, goodPort},
			"other_service.does.lots.of.stuff": util.EndPoint{badIP, badPort},
		})
		marshalled, err := json.Marshal(minimalNerveConfig)
		assert.Nil(t, err)
		content = marshalled
	}

	tmpFile, err := ioutil.TempFile("", "fullerite_testing")
	assert.Nil(t, err)
	defer os.Remove(tmpFile.Name())

	_, err = tmpFile.Write(content)
	assert.Nil(t, err)

	cfg := map[string]interface{}{
		"configFilePath":    tmpFile.Name(),
		"queryPath":         "",
		"servicesWhitelist": []string{"test_service"},
	}

	inst := getTestNerveUWSGIWorkerStats()
	inst.Configure(cfg)

	go inst.Collect()

	actual := []metric.Metric{}
	for i := 0; i < 6; i++ {
		actual = append(actual, <-inst.Channel())
	}
	validateUWSGIWorkerStatsResults(t, actual, results)
	validateStatsDimensions(t, actual, "test_service", goodPort)
	validateStatsEmptyChannel(t, inst.Channel())
}