From 7e606e8a3d5c9cc21d522dfad02b866db646f8f4 Mon Sep 17 00:00:00 2001 From: Sanath Pasumarthy Date: Fri, 29 Apr 2016 16:28:34 -0700 Subject: [PATCH] Update infludb client to v2 --- database/influxdb.go | 109 ++++++++++++++++++++++----------------- sample_config.json | 12 ++--- sample_config_slack.json | 14 ++--- 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/database/influxdb.go b/database/influxdb.go index 3a8ac04..17a59e8 100644 --- a/database/influxdb.go +++ b/database/influxdb.go @@ -4,10 +4,11 @@ import ( "encoding/json" "errors" "fmt" - "github.com/influxdb/influxdb/client" "net/url" "strconv" "time" + + "github.com/influxdata/influxdb/client/v2" ) type InfluxDb struct { @@ -19,7 +20,7 @@ type InfluxDb struct { } var ( - influxDBcon *client.Client + influxDBcon client.Client ) const ( @@ -41,20 +42,20 @@ func (influxDb InfluxDb) Initialize() error { return err } - conf := client.Config{ - URL: *u, + conf := client.HTTPConfig{ + Addr: u.String(), Username: influxDb.Username, Password: influxDb.Password, } - influxDBcon, err = client.NewClient(conf) + influxDBcon, err = client.NewHTTPClient(conf) if err != nil { println("InfluxDB : Failed to connect to Database . Please check the details entered in the config file\nError Details: ", err.Error()) return err } - _, ver, err := influxDBcon.Ping() + _, ver, err := influxDBcon.Ping(10 * time.Second) if err != nil { println("InfluxDB : Failed to connect to Database . Please check the details entered in the config file\nError Details: ", err.Error()) @@ -79,71 +80,87 @@ func (influxDb InfluxDb) Initialize() error { //Add request information to database func (influxDb InfluxDb) AddRequestInfo(requestInfo RequestInfo) error { - var pts = make([]client.Point, 0) - - point := client.Point{ - Measurement: requestInfo.Url, - Tags: map[string]string{ - "requestId": strconv.Itoa(requestInfo.Id), - "requestType": requestInfo.RequestType, - }, - Fields: map[string]interface{}{ - "responseTime": requestInfo.ResponseTime, - "responseCode": requestInfo.ResponseCode, - }, - Time: time.Now(), + tags := map[string]string{ + "requestId": strconv.Itoa(requestInfo.Id), + "requestType": requestInfo.RequestType, + } + fields := map[string]interface{}{ + "responseTime": requestInfo.ResponseTime, + "responseCode": requestInfo.ResponseCode, + } + + bps, err := client.NewBatchPoints(client.BatchPointsConfig{ + Database: influxDb.DatabaseName, Precision: "ms", + }) + + if err != nil { + return err } - pts = append(pts, point) + point, err := client.NewPoint( + requestInfo.Url, + tags, + fields, + time.Now(), + ) - bps := client.BatchPoints{ - Points: pts, - Database: influxDb.DatabaseName, - RetentionPolicy: "default", + if err != nil { + return err } - _, err := influxDBcon.Write(bps) + bps.AddPoint(point) + + err = influxDBcon.Write(bps) if err != nil { return err } + return nil } //Add Error information to database func (influxDb InfluxDb) AddErrorInfo(errorInfo ErrorInfo) error { - var pts = make([]client.Point, 0) - point := client.Point{ - Measurement: errorInfo.Url, - Tags: map[string]string{ - "requestId": strconv.Itoa(errorInfo.Id), - "requestType": errorInfo.RequestType, - "reason": errorInfo.Reason.Error(), - }, - Fields: map[string]interface{}{ - "responseBody": errorInfo.ResponseBody, - "responseCode": errorInfo.ResponseCode, - "otherInfo": errorInfo.OtherInfo, - }, - Time: time.Now(), - Precision: "ms", + tags := map[string]string{ + "requestId": strconv.Itoa(errorInfo.Id), + "requestType": errorInfo.RequestType, + "reason": errorInfo.Reason.Error(), + } + fields := map[string]interface{}{ + "responseBody": errorInfo.ResponseBody, + "responseCode": errorInfo.ResponseCode, + "otherInfo": errorInfo.OtherInfo, } - pts = append(pts, point) + point, err := client.NewPoint( + errorInfo.Url, + tags, + fields, + time.Now(), + ) - bps := client.BatchPoints{ - Points: pts, - Database: influxDb.DatabaseName, - RetentionPolicy: "default", + if err != nil { + return err } + bps, err := client.NewBatchPoints(client.BatchPointsConfig{ + Database: influxDb.DatabaseName, + Precision: "ms", + }) - _, err := influxDBcon.Write(bps) + if err != nil { + return err + } + + bps.AddPoint(point) + + err = influxDBcon.Write(bps) if err != nil { return err } + return nil } diff --git a/sample_config.json b/sample_config.json index e8e9614..ea60022 100644 --- a/sample_config.json +++ b/sample_config.json @@ -13,7 +13,7 @@ "url":"http://mywebsite.com", "requestType":"POST", "headers":{ - "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f", + "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f" } }, "mail":{ @@ -49,20 +49,20 @@ "url":"http://google.com" }, "checkEvery":30, - "responseCode":200, + "responseCode":200, "responseTime":800 }, { "url":"http://mywebsite.com/v1/data", "requestType":"GET", "headers":{ - "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f", + "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f" }, "urlParams":{ "name":"statusok" }, "checkEvery":300, - "responseCode":200, + "responseCode":200, "responseTime":800 }, { @@ -72,7 +72,7 @@ "name":"statusok" }, "checkEvery":300, - "responseCode":200, + "responseCode":200, "responseTime":800 }, { @@ -83,7 +83,7 @@ "params":{ }, "checkEvery":30, - "responseCode":200, + "responseCode":200, "responseTime":800 } ] diff --git a/sample_config_slack.json b/sample_config_slack.json index 7a03d08..2ef3c18 100644 --- a/sample_config_slack.json +++ b/sample_config_slack.json @@ -1,23 +1,23 @@ { "notifications":{ "slack":{ - "channel":"#general", - "username":"statusok", - "channelWebhookURL":"https://hooks.slack.com/services/T09ZQZhET2E5Tl7" + "channel":"#chanelName", + "username":"user-name", + "channelWebhookURL":"https://hooks.slack.com/services/hook-url" } }, "requests":[ { - "url":"http://mywebsite.com/v1/data", + "url":"http://google.com", "requestType":"GET", "headers":{ - "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f", + "Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f" }, "urlParams":{ "name":"statusok" }, - "checkEvery":300, - "responseCode":200, + "checkEvery":30, + "responseCode":200, "responseTime":800 } ]