The Minimal InfluxDB Client in Go

While you can get a proper InfluxDB client library for Go, sometime there's no substitution for rolling one yourself - especially since InfluxDB's Line Protocol is really easy.

It's just a matter of constructing a correct URL and setting up just two headers. Something like this:

Function
func sendInfluxData(line string, baseUrl string, org string, bucket string, token string) {
var url string
if len(org) > 0 {
url = fmt.Sprintf("%s/api/v2/write?org=%s&bucket=%s",
baseUrl, org, bucket)
} else {
url = fmt.Sprintf("%s/api/v2/write?bucket=%s",
baseUrl, bucket)
}

request, _ := http.NewRequest("POST", url, strings.NewReader(line))
request.Header.Set("Content-Type", "text/plain")
if len(token) > 0 {
request.Header.Set("Authorization", "Token " + token)
}

response, _ := http.DefaultClient.Do(request)
}

And yes, code doesn't do error checking nor it has any comments. Deal with it. ;)

Leave a Reply

Your email address will not be published. Required fields are marked *