當前位置: 首頁>>代碼示例>>Golang>>正文


Golang client.NewClient函數代碼示例

本文整理匯總了Golang中github.com/influxdb/influxdb/client.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: influxConnect

func influxConnect(config *StartupConfig, runningConfig *RunningConfig) (*influx.Client, error) {
	//Connect to InfluxDb
	activeServers := len(runningConfig.InfluxDbProps)
	rand.Seed(4200)
	//if there is only 1 active, use it
	if activeServers == 1 {
		u, err := url.Parse(fmt.Sprintf("http://%s:%d", runningConfig.InfluxDbProps[0].Fqdn, runningConfig.InfluxDbProps[0].Port))
		if err != nil {
			return nil, err
		}
		conf := influx.Config{
			URL:      *u,
			Username: config.InfluxUser,
			Password: config.InfluxPassword,
		}
		con, err := influx.NewClient(conf)
		if err != nil {
			return nil, err
		}
		_, _, err = con.Ping()
		if err != nil {
			return nil, err
		}
		return con, nil
	} else if activeServers > 1 {
		//try to connect to a random server until we find one that works.  if we dont find one in 20 tries, bail.
		for i := 0; i < 20; i++ {
			index := rand.Intn(activeServers)
			u, err := url.Parse(fmt.Sprintf("http://%s:%d", runningConfig.InfluxDbProps[index].Fqdn, runningConfig.InfluxDbProps[index].Port))
			if err != nil {
				errHndlr(err, ERROR)
				continue
			} else {
				conf := influx.Config{
					URL:      *u,
					Username: config.InfluxUser,
					Password: config.InfluxPassword,
				}
				con, err := influx.NewClient(conf)
				if err != nil {
					errHndlr(err, ERROR)
					continue
				} else {
					_, _, err = con.Ping()
					if err != nil {
						errHndlr(err, ERROR)
						continue
					} else {
						return con, nil
					}
				}
			}
		}
		err := errors.New("Could not connect to any of the InfluxDb servers that are ONLINE in traffic ops.")
		return nil, err
	} else {
		err := errors.New("No online InfluxDb servers could be found!")
		return nil, err
	}
}
開發者ID:jianli123,項目名稱:traffic_control,代碼行數:60,代碼來源:write_traffic_stats.go

示例2: TestClient_Timeout

func TestClient_Timeout(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1 * time.Second)
		var data client.Response
		w.WriteHeader(http.StatusOK)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u, Timeout: 500 * time.Millisecond}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	query := client.Query{}
	_, err = c.Query(query)
	if err == nil {
		t.Fatalf("unexpected success.  expected timeout error")
	} else if !strings.Contains(err.Error(), "use of closed network connection") {
		t.Fatalf("unexpected error.  expected 'use of closed network connection' error, got %v", err)
	}

	confignotimeout := client.Config{URL: *u}
	cnotimeout, err := client.NewClient(confignotimeout)
	_, err = cnotimeout.Query(query)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}
開發者ID:ASAPPinc,項目名稱:influxdb,代碼行數:31,代碼來源:influxdb_test.go

示例3: influxConnect

func influxConnect(config *StartupConfig, trafOps TrafOpsData) (*influx.Client, error) {
	//Connect to InfluxDb
	activeServers := len(trafOps.InfluxDbProps)
	rand.Seed(42)
	//if there is only 1 active, use it
	if activeServers == 1 {
		u, err := url.Parse(fmt.Sprintf("http://%s:%d", trafOps.InfluxDbProps[0].Fqdn, trafOps.InfluxDbProps[0].Port))
		if err != nil {
			return nil, err
		}
		conf := influx.Config{
			URL:      *u,
			Username: config.InfluxUser,
			Password: config.InfluxPassword,
		}
		con, err := influx.NewClient(conf)
		if err != nil {
			return nil, err
		}
		_, _, err = con.Ping()
		if err != nil {
			return nil, err
		}
		return con, nil
	} else if activeServers > 1 {
		//try to connect to all ONLINE servers until we find one that works
		for i := 0; i < activeServers; i++ {
			u, err := url.Parse(fmt.Sprintf("http://%s:%d", trafOps.InfluxDbProps[i].Fqdn, trafOps.InfluxDbProps[i].Port))
			if err != nil {
				errHndlr(err, ERROR)
			} else {
				conf := influx.Config{
					URL:      *u,
					Username: config.InfluxUser,
					Password: config.InfluxPassword,
				}
				con, err := influx.NewClient(conf)
				if err != nil {
					errHndlr(err, ERROR)
					continue
				} else {
					_, _, err = con.Ping()
					if err != nil {
						errHndlr(err, ERROR)
						continue
					} else {
						return con, nil
					}
				}
			}
		}
		err := errors.New("Could not connect to any of the InfluxDb servers that are ONLINE in traffic ops.")
		return nil, err
	} else {
		err := errors.New("No online InfluxDb servers could be found!")
		return nil, err
	}
}
開發者ID:kknet,項目名稱:traffic_control,代碼行數:58,代碼來源:ts_daily_summary.go

示例4: main

func main() {
	numberOfSeries := 50000
	if len(os.Args) > 1 {
		numberOfSeries, _ = strconv.Atoi(os.Args[1])
	}
	fmt.Printf("Benchmarking writing %d series\n", numberOfSeries)

	c, err := client.NewClient(&client.ClientConfig{})
	if err != nil {
		panic(err)
	}

	before := time.Now()
	c.DeleteDatabase("performance")
	fmt.Printf("Deleting took %s\n", time.Now().Sub(before))
	os.Exit(0)
	if err := c.CreateDatabase("performance"); err != nil {
		panic(err)
	}

	c, err = client.NewClient(&client.ClientConfig{
		Database: "performance",
	})
	if err != nil {
		panic(err)
	}

	before = time.Now()

	for i := 0; i < 10; i++ {
		series := []*client.Series{}
		for i := 0; i < numberOfSeries; i++ {
			name := fmt.Sprintf("series_%d", i+1)
			series = append(series, &client.Series{
				Name:    name,
				Columns: []string{"value"},
				Points: [][]interface{}{
					{rand.Float64()},
				},
			})
		}
		if err := c.WriteSeries(series); err != nil {
			panic(err)
		}
	}

	fmt.Printf("Writing took %s\n", time.Now().Sub(before))
}
開發者ID:ericcapricorn,項目名稱:influxdb,代碼行數:48,代碼來源:main.go

示例5: TestParseCommand_Insert

func TestParseCommand_Insert(t *testing.T) {
	t.Parallel()
	ts := emptyTestServer()
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := cli.CommandLine{Client: c}

	tests := []struct {
		cmd string
	}{
		{cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: " INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "INSERT   cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "insert cpu,host=serverA,region=us-west    value=1.0    "},
		{cmd: "insert"},
		{cmd: "Insert "},
		{cmd: "insert c"},
		{cmd: "insert int"},
	}

	for _, test := range tests {
		if err := m.ParseCommand(test.cmd); err != nil {
			t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd)
		}
	}
}
開發者ID:bwolf,項目名稱:influxdb,代碼行數:32,代碼來源:cli_test.go

示例6: TestSetWriteConsistency

func TestSetWriteConsistency(t *testing.T) {
	t.Parallel()
	c := cli.New(CLIENT_VERSION)
	config := client.NewConfig()
	client, _ := client.NewClient(config)
	c.Client = client

	// set valid write consistency
	consistency := "all"
	c.SetWriteConsistency("consistency " + consistency)
	if c.WriteConsistency != consistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}

	// set different valid write consistency and validate change
	consistency = "quorum"
	c.SetWriteConsistency("consistency " + consistency)
	if c.WriteConsistency != consistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}

	// set invalid write consistency and verify there was no change
	invalidConsistency := "invalid_consistency"
	c.SetWriteConsistency("consistency " + invalidConsistency)
	if c.WriteConsistency == invalidConsistency {
		t.Fatalf("WriteConsistency is %s but should be %s", c.WriteConsistency, consistency)
	}
}
開發者ID:bwolf,項目名稱:influxdb,代碼行數:28,代碼來源:cli_test.go

示例7: setupInflux

func setupInflux() {
	influx_port, _ := strconv.ParseInt(os.Getenv("INFLUX_PORT"), 10, 0)

	u, err := url.Parse(fmt.Sprintf("http://%s:%d", os.Getenv("INFLUX_HOST"), influx_port))
	if err != nil {
		log.Fatal(err)
	}

	conf := client.Config{
		URL:      *u,
		Username: os.Getenv("INFLUX_USER"),
		Password: os.Getenv("INFLUX_PASS"),
	}

	con, err = client.NewClient(conf)
	if err != nil {
		log.Fatal(err)
	}

	dur, ver, err := con.Ping()
	if err != nil {
		log.Fatal(err)
	}
	if os.Getenv("DEBUG") == "true" {
		log.Printf("Connected in %v | Version: %s", dur, ver)
	}
}
開發者ID:joshrendek,項目名稱:influx-alert,代碼行數:27,代碼來源:influx.go

示例8: query

func (s *influxdbSource) query(query string, precision ...influxdb.TimePrecision) ([]*influxdb.Series, error) {
	client, err := influxdb.NewClient(s.conf)
	if err != nil {
		return nil, err
	}
	return client.Query(query, precision...)
}
開發者ID:MikaelCluseau,項目名稱:kubernetes,代碼行數:7,代碼來源:data_source.go

示例9: NewInfluxDBSink

func NewInfluxDBSink(rawUrl, username, password, database string) (*InfluxDBSink, error) {
	url, err := url.Parse(fmt.Sprintf(rawUrl))
	if err != nil {
		return nil, err
	}

	config := influxdb.Config{
		URL:      *url,
		Username: username,
		Password: password,
	}

	client, err := influxdb.NewClient(config)
	if err != nil {
		return nil, err
	}

	_, _, err = client.Ping()
	if err != nil {
		return nil, err
	}

	return &InfluxDBSink{
		Client:         client,
		Database:       database,
		MetricsToFlush: []influxdb.Point{},
		LastFlush:      time.Now(),
	}, nil
}
開發者ID:snowsnail,項目名稱:merd,代碼行數:29,代碼來源:influxdb_sink.go

示例10: TestClient_NoTimeout

func TestClient_NoTimeout(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(1 * time.Second)
		var data client.Response
		w.WriteHeader(http.StatusOK)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}

	query := client.Query{}
	_, err = c.Query(query)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
}
開發者ID:bwolf,項目名稱:influxdb,代碼行數:25,代碼來源:influxdb_test.go

示例11: NewInfluxSinker

func NewInfluxSinker(conf InfluxConfig) (Sinker, error) {

	u, err := url.Parse("http://" + conf.Address)
	if err != nil {
		return nil, err
	}
	cli, err := influx.NewClient(influx.Config{
		URL:      *u,
		Username: conf.Username,
		Password: conf.Password,
	})

	if err != nil {
		return nil, err
	}

	host, err := os.Hostname()
	if err != nil {
		return nil, err
	}

	return &influxSinker{
		influx:   cli,
		hostName: host,
		database: conf.Database,
	}, nil
}
開發者ID:wangtuanjie,項目名稱:cfs,代碼行數:27,代碼來源:report.go

示例12: init_fluxdb

func init_fluxdb(conf InfluxdbConf) (pcon *client.Client, perr error) {
	u, err := url.Parse(fmt.Sprintf("http://%s:%d", conf.Host, conf.Port))
	if err != nil {
		log.Fatal(err)
		return nil, err
	}

	influxconf := client.Config{
		URL:      *u,
		Username: os.Getenv(conf.User),
		Password: os.Getenv(conf.Pwd),
	}

	con, err := client.NewClient(influxconf)
	if err != nil {
		log.Fatal(err)
		return nil, err
	}

	dur, ver, err := con.Ping()
	if err != nil {
		log.Fatal(err)
		return nil, err
	}
	log.Printf("Happy as a Hippo! %v, %s", dur, ver)
	return con, nil
}
開發者ID:harryshayne,項目名稱:cloudeye,代碼行數:27,代碼來源:parse_log.go

示例13: NewClient

func NewClient(c Config) (*MonitorInfo, error) {
	monClient := MonitorInfo{
		host:      c.Host,
		port:      c.Port,
		db:        c.DB,
		metricMap: make(map[string]time.Time),
	}

	u, err := url.Parse(fmt.Sprintf("http://%s:%d", monClient.host, monClient.port))
	if err != nil {
		log.Fatal(err)
	}

	conf := client.Config{
		URL:      *u,
		Username: os.Getenv("INFLUX_USER"),
		Password: os.Getenv("INFLUX_PWD"),
	}

	con, err := client.NewClient(conf)
	if err != nil {
		log.Fatal(err)
	}

	monClient.influxClient = con

	return &monClient, nil
}
開發者ID:nickrobinson,項目名稱:funcmon,代碼行數:28,代碼來源:funcmon.go

示例14: TestParseCommand_Insert

func TestParseCommand_Insert(t *testing.T) {
	t.Parallel()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var data client.Response
		w.WriteHeader(http.StatusNoContent)
		_ = json.NewEncoder(w).Encode(data)
	}))
	defer ts.Close()

	u, _ := url.Parse(ts.URL)
	config := client.Config{URL: *u}
	c, err := client.NewClient(config)
	if err != nil {
		t.Fatalf("unexpected error.  expected %v, actual %v", nil, err)
	}
	m := main.CommandLine{Client: c}

	tests := []struct {
		cmd string
	}{
		{cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"},
		{cmd: " INSERT test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "INSERT   test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "Insert test.test cpu,host=serverA,region=us-west value=1.0"},
		{cmd: "insert test.test cpu,host=serverA,region=us-west value=1.0"},
	}

	for _, test := range tests {
		if !m.ParseCommand(test.cmd) {
			t.Fatalf(`Command "insert" failed for %q.`, test.cmd)
		}
	}
}
開發者ID:nolenroyalty,項目名稱:bangarang,代碼行數:33,代碼來源:main_test.go

示例15: RunInvalidQuery

func (self *DataTestClient) RunInvalidQuery(query string, c *C, timePrecision ...influxdb.TimePrecision) []*influxdb.Series {
	client, err := influxdb.NewClient(&influxdb.ClientConfig{Database: self.db})
	c.Assert(err, IsNil)
	_, err = client.Query(query, timePrecision...)
	c.Assert(err, NotNil)
	return nil
}
開發者ID:Kenterfie,項目名稱:influxdb,代碼行數:7,代碼來源:data_test_client.go


注:本文中的github.com/influxdb/influxdb/client.NewClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。