How to use MakeClient method of influxdb Package

Best K6 code snippet using influxdb.MakeClient


Source:influxdb.go Github


Full Screen

1package influxdb2import (3 "fmt"4 uurl "net/url"5 "time"6 ""7 ""8 ""9)10type reporter struct {11 reg metrics.Registry12 interval time.Duration13 url uurl.URL14 database string15 username string16 password string17 namespace string18 tags map[string]string19 client *client.Client20 cache map[string]int6421}22// InfluxDB starts a InfluxDB reporter which will post the from the given metrics.Registry at each d interval.23func InfluxDB(r metrics.Registry, d time.Duration, url, database, username, password, namespace string) {24 InfluxDBWithTags(r, d, url, database, username, password, namespace, nil)25}26// InfluxDBWithTags starts a InfluxDB reporter which will post the from the given metrics.Registry at each d interval with the specified tags27func InfluxDBWithTags(r metrics.Registry, d time.Duration, url, database, username, password, namespace string, tags map[string]string) {28 u, err := uurl.Parse(url)29 if err != nil {30 log.Warn("Unable to parse InfluxDB", "url", url, "err", err)31 return32 }33 rep := &reporter{34 reg: r,35 interval: d,36 url: *u,37 database: database,38 username: username,39 password: password,40 namespace: namespace,41 tags: tags,42 cache: make(map[string]int64),43 }44 if err := rep.makeClient(); err != nil {45 log.Warn("Unable to make InfluxDB client", "err", err)46 return47 }48}50// InfluxDBWithTagsOnce runs once an InfluxDB reporter and post the given metrics.Registry with the specified tags51func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error {52 u, err := uurl.Parse(url)53 if err != nil {54 return fmt.Errorf("unable to parse InfluxDB. url: %s, err: %v", url, err)55 }56 rep := &reporter{57 reg: r,58 url: *u,59 database: database,60 username: username,61 password: password,62 namespace: namespace,63 tags: tags,64 cache: make(map[string]int64),65 }66 if err := rep.makeClient(); err != nil {67 return fmt.Errorf("unable to make InfluxDB client. err: %v", err)68 }69 if err := rep.send(); err != nil {70 return fmt.Errorf("unable to send to InfluxDB. err: %v", err)71 }72 return nil73}74func (r *reporter) makeClient() (err error) {75 r.client, err = client.NewClient(client.Config{76 URL: r.url,77 Username: r.username,78 Password: r.password,79 Timeout: 10 * time.Second,80 })81 return82}83func (r *reporter) run() {84 intervalTicker := time.Tick(r.interval)85 pingTicker := time.Tick(time.Second * 5)86 for {87 select {88 case <-intervalTicker:89 if err := r.send(); err != nil {90 log.Warn("Unable to send to InfluxDB", "err", err)91 }92 case <-pingTicker:93 _, _, err := r.client.Ping()94 if err != nil {95 log.Warn("Got error while sending a ping to InfluxDB, trying to recreate client", "err", err)96 if err = r.makeClient(); err != nil {97 log.Warn("Unable to make InfluxDB client", "err", err)98 }99 }100 }101 }102}103func (r *reporter) send() error {104 var pts []client.Point105 r.reg.Each(func(name string, i interface{}) {106 now := time.Now()107 namespace := r.namespace108 switch metric := i.(type) {109 case metrics.Counter:110 v := metric.Count()111 l := r.cache[name]112 pts = append(pts, client.Point{113 Measurement: fmt.Sprintf("%s%s.count", namespace, name),114 Tags: r.tags,115 Fields: map[string]interface{}{116 "value": v - l,117 },118 Time: now,119 })120 r.cache[name] = v121 case metrics.Gauge:122 ms := metric.Snapshot()123 pts = append(pts, client.Point{124 Measurement: fmt.Sprintf("%s%s.gauge", namespace, name),125 Tags: r.tags,126 Fields: map[string]interface{}{127 "value": ms.Value(),128 },129 Time: now,130 })131 case metrics.GaugeFloat64:132 ms := metric.Snapshot()133 pts = append(pts, client.Point{134 Measurement: fmt.Sprintf("%s%s.gauge", namespace, name),135 Tags: r.tags,136 Fields: map[string]interface{}{137 "value": ms.Value(),138 },139 Time: now,140 })141 case metrics.Histogram:142 ms := metric.Snapshot()143 ps := ms.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})144 pts = append(pts, client.Point{145 Measurement: fmt.Sprintf("%s%s.histogram", namespace, name),146 Tags: r.tags,147 Fields: map[string]interface{}{148 "count": ms.Count(),149 "max": ms.Max(),150 "mean": ms.Mean(),151 "min": ms.Min(),152 "stddev": ms.StdDev(),153 "variance": ms.Variance(),154 "p50": ps[0],155 "p75": ps[1],156 "p95": ps[2],157 "p99": ps[3],158 "p999": ps[4],159 "p9999": ps[5],160 },161 Time: now,162 })163 case metrics.Meter:164 ms := metric.Snapshot()165 pts = append(pts, client.Point{166 Measurement: fmt.Sprintf("%s%s.meter", namespace, name),167 Tags: r.tags,168 Fields: map[string]interface{}{169 "count": ms.Count(),170 "m1": ms.Rate1(),171 "m5": ms.Rate5(),172 "m15": ms.Rate15(),173 "mean": ms.RateMean(),174 },175 Time: now,176 })177 case metrics.Timer:178 ms := metric.Snapshot()179 ps := ms.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})180 pts = append(pts, client.Point{181 Measurement: fmt.Sprintf("%s%s.timer", namespace, name),182 Tags: r.tags,183 Fields: map[string]interface{}{184 "count": ms.Count(),185 "max": ms.Max(),186 "mean": ms.Mean(),187 "min": ms.Min(),188 "stddev": ms.StdDev(),189 "variance": ms.Variance(),190 "p50": ps[0],191 "p75": ps[1],192 "p95": ps[2],193 "p99": ps[3],194 "p999": ps[4],195 "p9999": ps[5],196 "m1": ms.Rate1(),197 "m5": ms.Rate5(),198 "m15": ms.Rate15(),199 "meanrate": ms.RateMean(),200 },201 Time: now,202 })203 case metrics.ResettingTimer:204 t := metric.Snapshot()205 if len(t.Values()) > 0 {206 ps := t.Percentiles([]float64{50, 95, 99})207 val := t.Values()208 pts = append(pts, client.Point{209 Measurement: fmt.Sprintf("%s%s.span", namespace, name),210 Tags: r.tags,211 Fields: map[string]interface{}{212 "count": len(val),213 "max": val[len(val)-1],214 "mean": t.Mean(),215 "min": val[0],216 "p50": ps[0],217 "p95": ps[1],218 "p99": ps[2],219 },220 Time: now,221 })222 }223 }224 })225 bps := client.BatchPoints{226 Points: pts,227 Database: r.database,228 }229 _, err := r.client.Write(bps)230 return err231}...

Full Screen

Full Screen


Using AI Code Generation


Full Screen

1import (2func main() {3 c, err := client.NewHTTPClient(client.HTTPConfig{4 })5 if err != nil {6 log.Fatal(err)7 }8 defer c.Close()9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?