-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Additional output plugin for writing to OpenTSDB using telnet mode #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0975bc1
fixing link: warning: option -X main.Version v0.1.8-2-g8c5e1ff may no…
rplessl 0b8e7cb
code improvements after running tests / compile step
rplessl 9af525c
change/fix expected test result
rplessl e2e1e31
adds opentsdb telnet output plugin
rplessl 66681a7
added opentsdb as sink
rplessl db57f2c
Rebase for merging
rplessl e75b7d2
added readme as suggested / whished in #177
rplessl fa037ad
fix spaces with gofmt
rplessl 58a2e5e
added docker image unit test with OpenTSDB
rplessl 33281ed
Merge pull request #1 from rplessl/opentsdb_output_plugin_telnet_unit…
rplessl 8a3b563
added prefix settings of the module and rearrange go test code
rplessl b4eb7d3
added more UNIT test cases for covering all parts of the code
rplessl da3a590
enhance coding style gofmt
rplessl 9e76c5d
Merge pull request #2 from rplessl/opentsdb_output_plugin_telnet_unit…
rplessl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package opentsdb | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/influxdb/influxdb/client" | ||
| "github.com/influxdb/telegraf/outputs" | ||
| ) | ||
|
|
||
| type OpenTSDB struct { | ||
| Prefix string | ||
|
|
||
| Host string | ||
| Port int | ||
| } | ||
|
|
||
| var sampleConfig = ` | ||
| # prefix for metrics keys | ||
| prefix = "my.specific.prefix." | ||
|
|
||
| ## Telnet Mode ## | ||
| # DNS name of the OpenTSDB server in telnet mode | ||
| host = "opentsdb.example.com" | ||
|
|
||
| # Port of the OpenTSDB server in telnet mode | ||
| port = 4242 | ||
| ` | ||
|
|
||
| type MetricLine struct { | ||
| Metric string | ||
| Timestamp int64 | ||
| Value string | ||
| Tags string | ||
| } | ||
|
|
||
| func (o *OpenTSDB) Connect() error { | ||
| // Test Connection to OpenTSDB Server | ||
| uri := fmt.Sprintf("%s:%d", o.Host, o.Port) | ||
| tcpAddr, err := net.ResolveTCPAddr("tcp", uri) | ||
| if err != nil { | ||
| return fmt.Errorf("OpenTSDB: TCP address cannot be resolved") | ||
| } | ||
| connection, err := net.DialTCP("tcp", nil, tcpAddr) | ||
| defer connection.Close() | ||
| if err != nil { | ||
| return fmt.Errorf("OpenTSDB: Telnet connect fail") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (o *OpenTSDB) Write(bp client.BatchPoints) error { | ||
| if len(bp.Points) == 0 { | ||
| return nil | ||
| } | ||
| var timeNow = time.Now() | ||
| // Send Data with telnet / socket communication | ||
| uri := fmt.Sprintf("%s:%d", o.Host, o.Port) | ||
| tcpAddr, _ := net.ResolveTCPAddr("tcp", uri) | ||
| connection, err := net.DialTCP("tcp", nil, tcpAddr) | ||
| if err != nil { | ||
| return fmt.Errorf("OpenTSDB: Telnet connect fail") | ||
| } | ||
| for _, pt := range bp.Points { | ||
| metric := &MetricLine{ | ||
| Metric: fmt.Sprintf("%s%s", o.Prefix, pt.Measurement), | ||
| Timestamp: timeNow.Unix(), | ||
| } | ||
| if metricValue, err := buildValue(bp, pt); err == nil { | ||
| metric.Value = metricValue | ||
| } | ||
|
|
||
| tagsSlice := buildTags(bp.Tags, pt.Tags) | ||
| metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " ")) | ||
|
|
||
| messageLine := fmt.Sprintf("put %s %v %s %s\n", metric.Metric, metric.Timestamp, metric.Value, metric.Tags) | ||
| fmt.Print(messageLine) | ||
| _, err := connection.Write([]byte(messageLine)) | ||
| if err != nil { | ||
| fmt.Errorf("OpenTSDB: Telnet writing error %s", err.Error()) | ||
| } | ||
| } | ||
| defer connection.Close() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func buildTags(bpTags map[string]string, ptTags map[string]string) []string { | ||
| tags := make([]string, (len(bpTags) + len(ptTags))) | ||
| index := 0 | ||
| for k, v := range bpTags { | ||
| tags[index] = fmt.Sprintf("%s=%s", k, v) | ||
| index += 1 | ||
| } | ||
| for k, v := range ptTags { | ||
| tags[index] = fmt.Sprintf("%s=%s", k, v) | ||
| index += 1 | ||
| } | ||
| sort.Strings(tags) | ||
| return tags | ||
| } | ||
|
|
||
| func buildValue(bp client.BatchPoints, pt client.Point) (string, error) { | ||
| var retv string | ||
| var v = pt.Fields["value"] | ||
| switch p := v.(type) { | ||
| case int64: | ||
| retv = IntToString(int64(p)) | ||
| case uint64: | ||
| retv = UIntToString(uint64(p)) | ||
| case float64: | ||
| retv = FloatToString(float64(p)) | ||
| default: | ||
| return retv, fmt.Errorf("undeterminable type for telegraf") | ||
| } | ||
| return retv, nil | ||
| } | ||
|
|
||
| func IntToString(input_num int64) string { | ||
| return strconv.FormatInt(input_num, 10) | ||
| } | ||
|
|
||
| func UIntToString(input_num uint64) string { | ||
| return strconv.FormatUint(input_num, 10) | ||
| } | ||
|
|
||
| func FloatToString(input_num float64) string { | ||
| return strconv.FormatFloat(input_num, 'f', 6, 64) | ||
| } | ||
|
|
||
| func (o *OpenTSDB) SampleConfig() string { | ||
| return sampleConfig | ||
| } | ||
|
|
||
| func (o *OpenTSDB) Description() string { | ||
| return "Configuration for OpenTSDB server to send metrics to" | ||
| } | ||
|
|
||
| func (o *OpenTSDB) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func init() { | ||
| outputs.Add("opentsdb", func() outputs.Output { | ||
| return &OpenTSDB{} | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package opentsdb | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| var ( | ||
| fakeHost = "metrics.example.com" | ||
| fakePort = 4242 | ||
| ) | ||
|
|
||
| func fakeOpenTSDB() *OpenTSDB { | ||
| var o OpenTSDB | ||
| o.Host = fakeHost | ||
| o.Port = fakePort | ||
| return &o | ||
| } | ||
|
|
||
| func TestBuildTagsTelnet(t *testing.T) { | ||
| var tagtests = []struct { | ||
| bpIn map[string]string | ||
| ptIn map[string]string | ||
| outTags []string | ||
| }{ | ||
| { | ||
| map[string]string{"one": "two"}, | ||
| map[string]string{"three": "four"}, | ||
| []string{"one=two", "three=four"}, | ||
| }, | ||
| { | ||
| map[string]string{"aaa": "bbb"}, | ||
| map[string]string{}, | ||
| []string{"aaa=bbb"}, | ||
| }, | ||
| { | ||
| map[string]string{"one": "two"}, | ||
| map[string]string{"aaa": "bbb"}, | ||
| []string{"aaa=bbb", "one=two"}, | ||
| }, | ||
| { | ||
| map[string]string{}, | ||
| map[string]string{}, | ||
| []string{}, | ||
| }, | ||
| } | ||
| for _, tt := range tagtests { | ||
| tags := buildTags(tt.bpIn, tt.ptIn) | ||
| if !reflect.DeepEqual(tags, tt.outTags) { | ||
| t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please run
go fmton your code, it looks like you have some space-indented lines hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed thanks for the hint