Ejemplo n.º 1
0
// Inserts into Elasticsearch the transactions from the channel
// Uses batches and the bulk API.
func InsertInEsFromChan(es *elasticsearch.Elasticsearch, index string,
	transactions chan TestTransaction) (int, error) {

	var buf bytes.Buffer

	enc := json.NewEncoder(&buf)

	var insOp struct {
		Index struct {
			Type string `json:"_type"`
		} `json:"index"`
	}
	insOp.Index.Type = "trans"

	flush := func() error {
		_, err := es.Bulk(index, &buf)
		if err != nil {
			return err
		}

		buf.Reset()
		enc = json.NewEncoder(&buf)
		return nil
	}

	i := 0
	for trans := range transactions {
		enc.Encode(insOp)
		enc.Encode(trans)

		if i%1000 == 0 {
			if err := flush(); err != nil {
				return i, err
			}
		}
		i++
	}

	if err := flush(); err != nil {
		return i, err
	}

	_, err := es.Refresh(index)
	if err != nil {
		return i, err
	}

	return i, nil
}
Ejemplo n.º 2
0
				}))
			})
		})
	})

	Context("With test ES", func() {
		var es *elasticsearch.Elasticsearch
		var index_name string
		var api *PerfDashApi
		BeforeEach(func() {
			index_name = fmt.Sprintf("packetbeat-unittest-%v", os.Getpid())
			es = elasticsearch.NewElasticsearch()

			_, err := es.DeleteIndex(index_name)
			Expect(err).To(BeNil())
			es.Refresh(index_name)

			ts1, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.000Z")
			ts2, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.001Z")

			transactions := []testdata.TestTransaction{
				testdata.TestTransaction{
					Timestamp:    ts1,
					Service:      "service1",
					Host:         "Host0",
					Count:        2,
					Responsetime: 2000,
					Status:       "ok",
				},
				testdata.TestTransaction{
					Timestamp:    ts2,