func TestRespondToHttp(t *testing.T) {
	cfg := config.Config{}
	cfg.InternalServerConfig = map[string]interface{}{"port": 0}

	h1 := buildTestHandler(
		"firsthandler",
		map[string]float64{"somecounter": 12.3},
		map[string]float64{"somegauge": 432.3},
	)
	h2 := buildTestHandler(
		"secondhandler",
		map[string]float64{"secondcounter": 456.2},
		map[string]float64{"secondgauge": 890.2},
	)
	testHandlers := []handler.Handler{h1, h2}

	srv := New(cfg, &testHandlers)
	go srv.Run()

	time.Sleep(100 * time.Millisecond) // wait for server to bind on port
	rsp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", srv.port))
	assert.Nil(t, err)
	assert.Equal(t, 200, rsp.StatusCode)

	// get the body - make sure we can unmarshall it
	// then check the contents. The length of the memory parts and the
	// handlers that we put in

	txt, err := ioutil.ReadAll(rsp.Body)
	defer rsp.Body.Close()
	assert.Nil(t, err)

	var parsedResponse ResponseFormat
	err = json.Unmarshal(txt, &parsedResponse)
	assert.Nil(t, err)

	assert.Equal(t, 7, len(parsedResponse.Memory.Counters))
	assert.Equal(t, 19, len(parsedResponse.Memory.Gauges))

	// test that both handlers are present and have the right values
	assert.Equal(t, 2, len(parsedResponse.Handlers))

	handlerMetrics := parsedResponse.Handlers["firsthandler"]
	assert.Equal(t, 1, len(handlerMetrics.Counters))
	assert.Equal(t, 1, len(handlerMetrics.Gauges))
	assert.Equal(t, 12.3, handlerMetrics.Counters["somecounter"])
	assert.Equal(t, 432.3, handlerMetrics.Gauges["somegauge"])

	handlerMetrics = parsedResponse.Handlers["secondhandler"]
	assert.Equal(t, 1, len(handlerMetrics.Counters))
	assert.Equal(t, 1, len(handlerMetrics.Gauges))
	assert.Equal(t, 456.2, handlerMetrics.Counters["secondcounter"])
	assert.Equal(t, 890.2, handlerMetrics.Gauges["secondgauge"])
}
Ejemplo n.º 2
0
func getCollectorBatchSize(collectorName string,
	globalConfig config.Config,
	defaultBufSize int) (result int) {
	conf, err := globalConfig.GetCollectorConfig(collectorName)
	result = defaultBufSize
	if err != nil {
		return
	}

	if bufferSize, exists := conf["max_buffer_size"]; exists {
		result = config.GetAsInt(bufferSize, defaultBufSize)
	}
	return
}
Ejemplo n.º 3
0
func startCollectors(c config.Config) (collectors []collector.Collector) {
	log.Info("Starting collectors...")

	for _, name := range c.Collectors {
		conf, err := c.GetCollectorConfig(name)
		if err != nil {
			log.Error("Collector config failed to load for: ", name)
			continue
		}

		collectorInst := startCollector(name, c, conf)
		if collectorInst != nil {
			collectors = append(collectors, collectorInst)
		}
	}
	return collectors
}