Package list golang-github-dcso-fluxline / 070559d
Merge tag 'upstream/0.0_git20181026.4f8ed83' Upstream version 0.0~git20181026.4f8ed83 Sascha Steinbiss 2 years ago
4 changed file(s) with 56 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
0 # Golang CircleCI 2.0 configuration file
1 #
2 # Check https://circleci.com/docs/2.0/language-go/ for more details
3 version: 2
4 jobs:
5 build:
6 docker:
7 - image: circleci/golang:1.10-stretch
8
9 working_directory: /go/src/github.com/DCSO/fluxline
10 steps:
11 - checkout
12 - run: go get -v -t -d ./...
13 - run: go test -v ./...
0 # fluxline
0 # fluxline [![CircleCI](https://circleci.com/gh/DCSO/fluxline.svg?style=shield)](https://circleci.com/gh/DCSO/fluxline) [![Documentation](https://godoc.org/github.com/DCSO/fluxline?status.svg)](http://godoc.org/github.com/DCSO/fluxline)
11
22 Encoder for Golang to prepare sets of metrics in [InfluxDB's Line Protocol](https://docs.influxdata.com/influxdb/v1.4/write_protocols/line_protocol_reference) format. As input, we use structs annotated with the `influx` tag, similar to how `encoding/json` works.
33
2727 return str
2828 }
2929
30 func toInfluxRepr(tag string, val interface{}) (string, error) {
30 func toInfluxRepr(tag string, val interface{}, nostatictypes bool) (string, error) {
3131 switch v := val.(type) {
3232 case string:
3333 if len(v) > 64000 {
3434 return "", fmt.Errorf("%s: string too long (%d characters, max. 64K)", tag, len(v))
3535 }
3636 return fmt.Sprintf("%q", v), nil
37 case int32, int64, int16, int8, int:
38 return fmt.Sprintf("%di", v), nil
39 case uint32, uint64, uint16, uint8, uint:
37 case int32, int64, int16, int8, int, uint32, uint64, uint16, uint8, uint:
38 if nostatictypes {
39 return fmt.Sprintf("%d", v), nil
40 }
4041 return fmt.Sprintf("%di", v), nil
4142 case float64, float32:
4243 return fmt.Sprintf("%g", v), nil
5051 }
5152
5253 func recordFields(val interface{},
53 fieldSet map[string]string) (map[string]string, error) {
54 fieldSet map[string]string, nostatictypes bool) (map[string]string, error) {
5455 t := reflect.TypeOf(val)
5556 v := reflect.ValueOf(val)
5657
6061 if tag == "" {
6162 continue
6263 }
63 repr, err := toInfluxRepr(tag, v.Field(i).Interface())
64 repr, err := toInfluxRepr(tag, v.Field(i).Interface(), nostatictypes)
6465 if err != nil {
6566 return nil, err
6667 }
115116
116117 // Encode writes the line protocol representation for a given measurement
117118 // name, data struct and tag map to the io.Writer specified on encoder creation.
118 func (a *Encoder) Encode(prefix string, val interface{},
119 tags map[string]string) error {
119 func (a *Encoder) encodeGeneric(prefix string, val interface{},
120 tags map[string]string, nostatictypes bool) error {
120121 fieldSet := make(map[string]string)
121 fieldSet, err := recordFields(val, fieldSet)
122 fieldSet, err := recordFields(val, fieldSet, nostatictypes)
122123 if err != nil {
123124 return err
124125 }
125126 _, err = a.Writer.Write([]byte(a.formatLineProtocol(prefix, tags, fieldSet)))
126127 return err
128 }
129
130 // Encode writes the line protocol representation for a given measurement
131 // name, data struct and tag map to the io.Writer specified on encoder creation.
132 func (a *Encoder) Encode(prefix string, val interface{},
133 tags map[string]string) error {
134 return a.encodeGeneric(prefix, val, tags, false)
135 }
136
137 // EncodeWithoutTypes writes the line protocol representation for a given measurement
138 // name, data struct and tag map to the io.Writer specified on encoder creation.
139 // In contrast to Encode(), this method never appends type suffixes to values.
140 func (a *Encoder) EncodeWithoutTypes(prefix string, val interface{},
141 tags map[string]string) error {
142 return a.encodeGeneric(prefix, val, tags, true)
127143 }
128144
129145 // EncodeMap writes the line protocol representation for a given measurement
101101 }
102102 }
103103
104 func TestEncoderNoTypes(t *testing.T) {
105 var b bytes.Buffer
106
107 ile := NewEncoder(&b)
108 tags := make(map[string]string)
109 err := ile.EncodeWithoutTypes("mytool", testStruct, tags)
110 if err != nil {
111 t.Fatal(err)
112 }
113 out := b.String()
114
115 if match, _ := regexp.Match(`^mytool,host=[^,]+ testval=1,testvalue=2,testvalue2=-3,testvalue3=\"foobar\\\"baz\",testvaluebool=false,testvalueflt32=3.1415927,testvalueflt64=1.29e-24,testvaluetime=`, []byte(out)); !match {
116 t.Fatalf("unexpected match content: %s", out)
117 }
118 }
119
104120 func TestEncoderStringTooLongFail(t *testing.T) {
105121 var b bytes.Buffer
106122