Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 8c22b16

Browse files
Merge pull request #26 from candysmurf/big-chunck
SDI-2279: Plugin doesn't handle publishing big chunk
2 parents d4918ba + aa447e9 commit 8c22b16

File tree

6 files changed

+123
-206
lines changed

6 files changed

+123
-206
lines changed

Godeps/Godeps.json

Lines changed: 0 additions & 193 deletions
This file was deleted.

Godeps/Readme

Lines changed: 0 additions & 5 deletions
This file was deleted.

glide.lock

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package: github.com/intelsdi-x/snap-plugin-publisher-opentsdb
2+
import:
3+
- package: github.com/Sirupsen/logrus
4+
version: cd7d1bbe41066b6c1f19780f895901052150a575
5+
- package: github.com/gopherjs/gopherjs
6+
version: 4b53e1bddba0e2f734514aeb6c02db652f4c6fe8
7+
subpackages:
8+
- js
9+
- package: github.com/intelsdi-x/snap
10+
version: 005961655e59e33a1515a432b1c6e6074d152519
11+
subpackages:
12+
- control/plugin
13+
- control/plugin/cpolicy
14+
- control/plugin/encoding
15+
- control/plugin/encrypter
16+
- core
17+
- core/cdata
18+
- core/ctypes
19+
- core/serror
20+
- pkg/ctree
21+
- pkg/schedule
22+
- pkg/stringutils
23+
- scheduler/wmap
24+
- package: github.com/jtolds/gls
25+
version: 8ddce2a84170772b95dd5d576c48d517b22cac63
26+
- package: github.com/robfig/cron
27+
version: 32d9c273155a0506d27cf73dd1246e86a470997e
28+
- package: github.com/smartystreets/assertions
29+
version: 443d812296a84445c202c085f19e18fc238f8250
30+
subpackages:
31+
- internal/go-render/render
32+
- internal/oglematchers
33+
- package: github.com/smartystreets/goconvey
34+
version: 995f5b2e021c69b8b028ba6d0b05c1dd500783db
35+
subpackages:
36+
- convey
37+
- convey/gotest
38+
- convey/reporting
39+
- package: golang.org/x/sys
40+
version: 7f918dd405547ecb864d14a8ecbbfe205b5f930f
41+
subpackages:
42+
- unix
43+
- package: gopkg.in/yaml.v2
44+
version: c1cd2254a6dd314c9d73c338c12688c9325d85c6

opentsdb/httpclient.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import (
3232

3333
const (
3434
putEndPoint = "/api/put"
35-
contentTypeJson = "application/json"
35+
contentTypeJSON = "application/json"
3636
userAgent = "snap-publisher"
37+
maxChunkLength = 25
3738
)
3839

3940
type HttpClient struct {
@@ -58,7 +59,7 @@ func NewClient(url string, timeout time.Duration) *HttpClient {
5859
}
5960
}
6061

61-
func (hc *HttpClient) getUrl() string {
62+
func (hc *HttpClient) getURL() string {
6263
u := url.URL{
6364
Scheme: "http",
6465
Host: hc.url,
@@ -67,16 +68,40 @@ func (hc *HttpClient) getUrl() string {
6768
return u.String()
6869
}
6970

70-
// Post stores slides of Datapoint to OpenTSDB
71-
func (hc *HttpClient) Post(dps []DataPoint) error {
72-
url := hc.getUrl()
71+
// Save saves data points in maxChunkLength size.
72+
func (hc *HttpClient) Save(dps []DataPoint) error {
73+
url := hc.getURL()
74+
75+
loop := len(dps) / maxChunkLength
76+
start := 0
77+
end := start
78+
for i := 0; i < loop; i++ {
79+
end += maxChunkLength
80+
chunk := dps[start:end]
81+
start = end
82+
err := hc.post(url, chunk)
83+
if err != nil {
84+
return err
85+
}
86+
}
87+
88+
remainder := len(dps) % maxChunkLength
89+
if remainder > 0 {
90+
end = start + remainder
91+
chunk := dps[start:end]
92+
return hc.post(url, chunk)
93+
}
94+
return nil
95+
}
7396

97+
// post stores a slice of Datapoint to OpenTSDB
98+
func (hc *HttpClient) post(url string, dps []DataPoint) error {
7499
buf, err := json.Marshal(dps)
75100
if err != nil {
76101
return err
77102
}
78103

79-
resp, err := hc.httpClient.Post(url, contentTypeJson, bytes.NewReader(buf))
104+
resp, err := hc.httpClient.Post(url, contentTypeJSON, bytes.NewReader(buf))
80105
if err != nil {
81106
return err
82107
}

opentsdb/opentsdb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838

3939
const (
4040
name = "opentsdb"
41-
version = 8
41+
version = 9
4242
pluginType = plugin.PublisherPluginType
4343
timeout = 5
4444
host = "host"
@@ -154,7 +154,7 @@ func (p *opentsdbPublisher) Publish(contentType string, content []byte, config m
154154

155155
td := time.Duration(timeout * time.Second)
156156
con := NewClient(u.String(), td)
157-
err = con.Post(pts)
157+
err = con.Save(pts)
158158
if err != nil {
159159
logger.Printf("Error: '%s' posting metrics: %+v", err.Error(), metrics)
160160
return err

0 commit comments

Comments
 (0)