github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/librato/librato.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package librato 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "fmt" 21 "net" 22 "net/http" 23 "net/url" 24 "strings" 25 "time" 26 ) 27 28 type Measurement struct { 29 Name string `json:"name,omitempty"` 30 Value float64 `json:"value,omitempty"` 31 Tags map[string]string `json:"tags,omitempty"` 32 Time int64 `json:"time,omitempty"` 33 } 34 35 type request struct { 36 Tags map[string]string `json:"tags,omitempty"` 37 Measurements []Measurement `json:"measurements,omitempty"` 38 } 39 40 type Client interface { 41 Write([]Measurement) error 42 } 43 44 type LibratoClient struct { 45 httpClient *http.Client 46 config LibratoConfig 47 } 48 49 func (c *LibratoClient) Write(measurements []Measurement) error { 50 b, err := json.Marshal(&request{ 51 Measurements: measurements, 52 Tags: c.config.Tags, 53 }) 54 if nil != err { 55 return err 56 } 57 req, err := http.NewRequest( 58 "POST", 59 c.config.API+"/v1/measurements", 60 bytes.NewBuffer(b), 61 ) 62 if nil != err { 63 return err 64 } 65 req.Header.Add("Content-Type", "application/json") 66 req.Header.Set("User-Agent", "heapster") 67 req.SetBasicAuth(c.config.Username, c.config.Token) 68 _, err = c.httpClient.Do(req) 69 return err 70 } 71 72 type LibratoConfig struct { 73 Username string 74 Token string 75 API string 76 Prefix string 77 Tags map[string]string 78 } 79 80 func NewClient(c LibratoConfig) *LibratoClient { 81 var netTransport = &http.Transport{ 82 Dial: (&net.Dialer{ 83 Timeout: 5 * time.Second, 84 }).Dial, 85 TLSHandshakeTimeout: 5 * time.Second, 86 } 87 var httpClient = &http.Client{ 88 Timeout: time.Second * 10, 89 Transport: netTransport, 90 } 91 92 client := &LibratoClient{httpClient: httpClient, config: c} 93 return client 94 } 95 96 func BuildConfig(uri *url.URL) (*LibratoConfig, error) { 97 config := LibratoConfig{API: "https://metrics-api.librato.com", Prefix: ""} 98 99 opts := uri.Query() 100 if len(opts["username"]) >= 1 { 101 config.Username = opts["username"][0] 102 } else { 103 return nil, fmt.Errorf("no `username` flag specified") 104 } 105 // TODO: use more secure way to pass the password. 106 if len(opts["token"]) >= 1 { 107 config.Token = opts["token"][0] 108 } else { 109 return nil, fmt.Errorf("no `token` flag specified") 110 } 111 if len(opts["api"]) >= 1 { 112 config.API = opts["api"][0] 113 } 114 if len(opts["prefix"]) >= 1 { 115 config.Prefix = opts["prefix"][0] 116 117 if !strings.HasSuffix(config.Prefix, ".") { 118 config.Prefix = config.Prefix + "." 119 } 120 } 121 if len(opts["tags"]) >= 1 { 122 config.Tags = make(map[string]string) 123 124 tagNames := strings.Split(opts["tags"][0], ",") 125 126 for _, tagName := range tagNames { 127 if val, ok := opts["tag_"+tagName]; ok { 128 config.Tags[tagName] = val[0] 129 } 130 } 131 } 132 133 return &config, nil 134 }