Skip to content

Commit

Permalink
Update infludb client to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sanathp committed Apr 29, 2016
1 parent d1617b2 commit 7e606e8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 59 deletions.
109 changes: 63 additions & 46 deletions database/influxdb.go
Expand Up @@ -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 {
Expand All @@ -19,7 +20,7 @@ type InfluxDb struct {
}

var (
influxDBcon *client.Client
influxDBcon client.Client
)

const (
Expand All @@ -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())
Expand All @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions sample_config.json
Expand Up @@ -13,7 +13,7 @@
"url":"http://mywebsite.com",
"requestType":"POST",
"headers":{
"Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f",
"Authorization":"Bearer ac2168444f4de69c27d6384ea2ccf61a49669be5a2fb037ccc1f"
}
},
"mail":{
Expand Down Expand Up @@ -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
},
{
Expand All @@ -72,7 +72,7 @@
"name":"statusok"
},
"checkEvery":300,
"responseCode":200,
"responseCode":200,
"responseTime":800
},
{
Expand All @@ -83,7 +83,7 @@
"params":{
},
"checkEvery":30,
"responseCode":200,
"responseCode":200,
"responseTime":800
}
]
Expand Down
14 changes: 7 additions & 7 deletions 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
}
]
Expand Down

0 comments on commit 7e606e8

Please sign in to comment.