From 891a7a2a8dca0ef624ba769aac3c49211cc0a55e Mon Sep 17 00:00:00 2001 From: vlastahajek Date: Tue, 5 May 2020 13:44:36 +0200 Subject: [PATCH] docs: Doc and examples for WriteApi.Errors() --- CHANGELOG.md | 2 +- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ examples_test.go | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a378ec8..d1c01e48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ 1. [#108](https://github.com/influxdata/influxdb-client-go/issues/108) Fix default retry interval doc 1. [#110](https://github.com/influxdata/influxdb-client-go/issues/110) Allowing empty (nil) values - ### Documentation - [#112](https://github.com/influxdata/influxdb-client-go/pull/112) Clarify how to use client with InfluxDB 1.8+ + - [#115](https://github.com/influxdata/influxdb-client-go/pull/115) Doc and examples for reading write api errors ## 1.1.0 [2020-04-24] ### Features diff --git a/README.md b/README.md index 9677fed0..2a10fd19 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,58 @@ func main() { client.Close() } ``` + +### Reading async errors +[Error()](https://github.com/influxdata/influxdb-client-go/blob/master/write.go#L24) method returns a channel for reading errors which occurs during async writes. This channel is unbuffered and it +must be read asynchronously otherwise will block write procedure: + +```go +package main + +import ( + "fmt" + "math/rand" + "time" + + "github.com/influxdata/influxdb-client-go" +) + +func main() { + // Create client + client := influxdb2.NewClient("http://localhost:9999", "my-token") + // Get non-blocking write client + writeApi := client.WriteApi("my-org", "my-bucket") + // Get errors channel + errorsCh := writeApi.Errors() + // Create go proc for reading and logging errors + go func() { + for err := range errorsCh { + fmt.Printf("write error: %s\n", err.Error()) + } + }() + // write some points + for i := 0; i < 100; i++ { + // create point + p := influxdb2.NewPointWithMeasurement("stat"). + AddTag("id", fmt.Sprintf("rack_%v", i%10)). + AddTag("vendor", "AWS"). + AddTag("hostname", fmt.Sprintf("host_%v", i%100)). + AddField("temperature", rand.Float64()*80.0). + AddField("disk_free", rand.Float64()*1000.0). + AddField("disk_total", (i/10+1)*1000000). + AddField("mem_total", (i/100+1)*10000000). + AddField("mem_free", rand.Uint64()). + SetTime(time.Now()) + // write asynchronously + writeApi.WritePoint(p) + } + // Force all unwritten data to be sent + writeApi.Flush() + // Ensures background processes finishes + client.Close() +} +``` + ### Blocking write client Blocking write client writes given point(s) synchronously. It doesn't have implicit batching. Batch is created from given set of points. diff --git a/examples_test.go b/examples_test.go index c6635059..2885249a 100644 --- a/examples_test.go +++ b/examples_test.go @@ -75,6 +75,41 @@ func ExampleWriteApi() { client.Close() } +func ExampleWriteApi_errors() { + // Create client + client := influxdb2.NewClient("http://localhost:9999", "my-token") + // Get non-blocking write client + writeApi := client.WriteApi("my-org", "my-bucket") + // Get errors channel + errorsCh := writeApi.Errors() + // Create go proc for reading and logging errors + go func() { + for err := range errorsCh { + fmt.Printf("write error: %s\n", err.Error()) + } + }() + // write some points + for i := 0; i < 100; i++ { + // create point + p := influxdb2.NewPointWithMeasurement("stat"). + AddTag("id", fmt.Sprintf("rack_%v", i%10)). + AddTag("vendor", "AWS"). + AddTag("hostname", fmt.Sprintf("host_%v", i%100)). + AddField("temperature", rand.Float64()*80.0). + AddField("disk_free", rand.Float64()*1000.0). + AddField("disk_total", (i/10+1)*1000000). + AddField("mem_total", (i/100+1)*10000000). + AddField("mem_free", rand.Uint64()). + SetTime(time.Now()) + // write asynchronously + writeApi.WritePoint(p) + } + // Force all unwritten data to be sent + writeApi.Flush() + // Ensures background processes finishes + client.Close() +} + func ExampleQueryApi_query() { // Create client client := influxdb2.NewClient("http://localhost:9999", "my-token")