Skip to content

Commit

Permalink
docs: Doc and examples for WriteApi.Errors()
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed May 5, 2020
1 parent 8e00135 commit 891a7a2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
35 changes: 35 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 891a7a2

Please sign in to comment.