Exemplo n.º 1
0
func NewElasticConnTo(ip, port, indexName, configPath string) (EConn, error) {
	log.Debug("")
	var outerr error
	clientInit.Do(func() {
		logTimename := time.Now().Format(logNameTimeFormat)
		fo, err := os.Create(os.TempDir() + "/docker-collector-out-" + logTimename + ".log")
		if err != nil {
			l.Fatalf("Error while creating a log file: %s", err)
		}
		fe, err := os.Create(os.TempDir() + "/docker-collector-error-" + logTimename + ".log")
		if err != nil {
			l.Fatalf("Error while creating a log file: %s", err)
		}
		//		ft, err := os.Create(os.TempDir() + "/docker-collector-trace-" + logTimename + ".log")
		//		if err != nil {
		//			l.Fatalf("Error while creating a log file: %s", err)
		//		}
		l.Printf("connecting to %s, %s", ip, port)

		//Waiting for this to be solved: https://github.com/olivere/elastic/issues/124
		wait := time.Second * 1
		retries := 10
		l.Printf("Trying to connect to ElasticSearch\n")
		for {
			ec.Client, err = elastic.NewClient(
				elastic.SetURL("http://"+ip+":"+port),
				elastic.SetMaxRetries(10),
				elastic.SetHealthcheckTimeoutStartup(10*time.Second),
				elastic.SetSniff(false),
				elastic.SetErrorLog(l.New(fe, "", l.LstdFlags)),
				elastic.SetInfoLog(l.New(fo, "", l.LstdFlags)),
				//elastic.SetTraceLog(l.New(ft, "", l.LstdFlags)),
			)
			l.Printf("Attempt %d...\n", 11-retries)
			if err == elastic.ErrNoClient {
				retries--
				if retries < 0 {
					break
				}
				time.Sleep(wait)
				wait += wait
			}
			if err == nil {
				l.Printf("Success!")
				break
			}
		}
		ec.indexName = indexName
		ec.configPath = configPath
		outerr = err
	})
	return ec, outerr
}
Exemplo n.º 2
0
func ExampleClient_NewClient_manyOptions() {
	// Obtain a client for an Elasticsearch cluster of two nodes,
	// running on 10.0.1.1 and 10.0.1.2. Do not run the sniffer.
	// Set the healthcheck interval to 10s. When requests fail,
	// retry 5 times. Print error messages to os.Stderr and informational
	// messages to os.Stdout.
	client, err := elastic.NewClient(
		elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"),
		elastic.SetSniff(false),
		elastic.SetHealthcheckInterval(10*time.Second),
		elastic.SetMaxRetries(5),
		elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
		elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
	if err != nil {
		// Handle error
		panic(err)
	}
	_ = client
}
Exemplo n.º 3
0
func (t *TestCase) setup() error {
	var errorlogger *log.Logger
	if t.errorlogfile != "" {
		f, err := os.OpenFile(t.errorlogfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
		if err != nil {
			return err
		}
		errorlogger = log.New(f, "", log.Ltime|log.Lmicroseconds|log.Lshortfile)
	}

	var infologger *log.Logger
	if t.infologfile != "" {
		f, err := os.OpenFile(t.infologfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
		if err != nil {
			return err
		}
		infologger = log.New(f, "", log.LstdFlags)
	}

	// Trace request and response details like this
	var tracelogger *log.Logger
	if t.tracelogfile != "" {
		f, err := os.OpenFile(t.tracelogfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
		if err != nil {
			return err
		}
		tracelogger = log.New(f, "", log.LstdFlags)
	}

	client, err := elastic.NewClient(
		elastic.SetURL(t.nodes...),
		elastic.SetErrorLog(errorlogger),
		elastic.SetInfoLog(infologger),
		elastic.SetTraceLog(tracelogger),
		elastic.SetMaxRetries(t.maxRetries),
		elastic.SetSniff(t.sniff),
		elastic.SetSnifferInterval(t.snifferInterval),
		elastic.SetHealthcheck(t.healthcheck),
		elastic.SetHealthcheckInterval(t.healthcheckInterval))
	if err != nil {
		// Handle error
		return err
	}
	t.client = client

	// Use the IndexExists service to check if a specified index exists.
	exists, err := t.client.IndexExists(t.index).Do()
	if err != nil {
		return err
	}
	if exists {
		deleteIndex, err := t.client.DeleteIndex(t.index).Do()
		if err != nil {
			return err
		}
		if !deleteIndex.Acknowledged {
			return errors.New("delete index not acknowledged")
		}
	}

	// Create a new index.
	createIndex, err := t.client.CreateIndex(t.index).Do()
	if err != nil {
		return err
	}
	if !createIndex.Acknowledged {
		return errors.New("create index not acknowledged")
	}

	// Index a tweet (using JSON serialization)
	tweet1 := Tweet{User: "******", Message: "Take Five", Retweets: 0}
	_, err = t.client.Index().
		Index(t.index).
		Type("tweet").
		Id("1").
		BodyJson(tweet1).
		Do()
	if err != nil {
		return err
	}

	// Index a second tweet (by string)
	tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
	_, err = t.client.Index().
		Index(t.index).
		Type("tweet").
		Id("2").
		BodyString(tweet2).
		Do()
	if err != nil {
		return err
	}

	// Flush to make sure the documents got written.
	_, err = t.client.Flush().Index(t.index).Do()
	if err != nil {
		return err
	}

	return nil
}