Best K6 code snippet using metrics.add
accumulator_test.go
Source:accumulator_test.go
...30 actual)31}32func TestAddDefaultTags(t *testing.T) {33 a := accumulator{}34 a.addDefaultTag("default", "tag")35 now := time.Now()36 a.metrics = make(chan telegraf.Metric, 10)37 defer close(a.metrics)38 a.inputConfig = &internal_models.InputConfig{}39 a.Add("acctest", float64(101), map[string]string{})40 a.Add("acctest", float64(101), map[string]string{"acc": "test"})41 a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now)42 testm := <-a.metrics43 actual := testm.String()44 assert.Contains(t, actual, "acctest,default=tag value=101")45 testm = <-a.metrics46 actual = testm.String()47 assert.Contains(t, actual, "acctest,acc=test,default=tag value=101")48 testm = <-a.metrics49 actual = testm.String()50 assert.Equal(t,51 fmt.Sprintf("acctest,acc=test,default=tag value=101 %d", now.UnixNano()),52 actual)53}54func TestAddFields(t *testing.T) {55 a := accumulator{}56 now := time.Now()57 a.metrics = make(chan telegraf.Metric, 10)58 defer close(a.metrics)59 a.inputConfig = &internal_models.InputConfig{}60 fields := map[string]interface{}{61 "usage": float64(99),62 }63 a.AddFields("acctest", fields, map[string]string{})64 a.AddFields("acctest", fields, map[string]string{"acc": "test"})65 a.AddFields("acctest", fields, map[string]string{"acc": "test"}, now)66 testm := <-a.metrics67 actual := testm.String()68 assert.Contains(t, actual, "acctest usage=99")69 testm = <-a.metrics70 actual = testm.String()71 assert.Contains(t, actual, "acctest,acc=test usage=99")72 testm = <-a.metrics73 actual = testm.String()74 assert.Equal(t,75 fmt.Sprintf("acctest,acc=test usage=99 %d", now.UnixNano()),76 actual)77}78// Test that all Inf fields get dropped, and not added to metrics channel79func TestAddInfFields(t *testing.T) {80 inf := math.Inf(1)81 ninf := math.Inf(-1)82 a := accumulator{}83 now := time.Now()84 a.metrics = make(chan telegraf.Metric, 10)85 defer close(a.metrics)86 a.inputConfig = &internal_models.InputConfig{}87 fields := map[string]interface{}{88 "usage": inf,89 "nusage": ninf,90 }91 a.AddFields("acctest", fields, map[string]string{})92 a.AddFields("acctest", fields, map[string]string{"acc": "test"})93 a.AddFields("acctest", fields, map[string]string{"acc": "test"}, now)94 assert.Len(t, a.metrics, 0)95 // test that non-inf fields are kept and not dropped96 fields["notinf"] = float64(100)97 a.AddFields("acctest", fields, map[string]string{})98 testm := <-a.metrics99 actual := testm.String()100 assert.Contains(t, actual, "acctest notinf=100")101}102// Test that nan fields are dropped and not added103func TestAddNaNFields(t *testing.T) {104 nan := math.NaN()105 a := accumulator{}106 now := time.Now()107 a.metrics = make(chan telegraf.Metric, 10)108 defer close(a.metrics)109 a.inputConfig = &internal_models.InputConfig{}110 fields := map[string]interface{}{111 "usage": nan,112 }113 a.AddFields("acctest", fields, map[string]string{})114 a.AddFields("acctest", fields, map[string]string{"acc": "test"})115 a.AddFields("acctest", fields, map[string]string{"acc": "test"}, now)116 assert.Len(t, a.metrics, 0)117 // test that non-nan fields are kept and not dropped118 fields["notnan"] = float64(100)119 a.AddFields("acctest", fields, map[string]string{})120 testm := <-a.metrics121 actual := testm.String()122 assert.Contains(t, actual, "acctest notnan=100")123}124func TestAddUint64Fields(t *testing.T) {125 a := accumulator{}126 now := time.Now()127 a.metrics = make(chan telegraf.Metric, 10)128 defer close(a.metrics)129 a.inputConfig = &internal_models.InputConfig{}130 fields := map[string]interface{}{131 "usage": uint64(99),132 }133 a.AddFields("acctest", fields, map[string]string{})134 a.AddFields("acctest", fields, map[string]string{"acc": "test"})135 a.AddFields("acctest", fields, map[string]string{"acc": "test"}, now)136 testm := <-a.metrics137 actual := testm.String()138 assert.Contains(t, actual, "acctest usage=99i")139 testm = <-a.metrics140 actual = testm.String()141 assert.Contains(t, actual, "acctest,acc=test usage=99i")142 testm = <-a.metrics143 actual = testm.String()144 assert.Equal(t,145 fmt.Sprintf("acctest,acc=test usage=99i %d", now.UnixNano()),146 actual)147}148func TestAddUint64Overflow(t *testing.T) {149 a := accumulator{}150 now := time.Now()151 a.metrics = make(chan telegraf.Metric, 10)152 defer close(a.metrics)153 a.inputConfig = &internal_models.InputConfig{}154 fields := map[string]interface{}{155 "usage": uint64(9223372036854775808),156 }157 a.AddFields("acctest", fields, map[string]string{})158 a.AddFields("acctest", fields, map[string]string{"acc": "test"})159 a.AddFields("acctest", fields, map[string]string{"acc": "test"}, now)160 testm := <-a.metrics161 actual := testm.String()162 assert.Contains(t, actual, "acctest usage=9223372036854775807i")163 testm = <-a.metrics164 actual = testm.String()165 assert.Contains(t, actual, "acctest,acc=test usage=9223372036854775807i")166 testm = <-a.metrics167 actual = testm.String()168 assert.Equal(t,169 fmt.Sprintf("acctest,acc=test usage=9223372036854775807i %d", now.UnixNano()),170 actual)171}172func TestAddInts(t *testing.T) {173 a := accumulator{}174 a.addDefaultTag("default", "tag")175 now := time.Now()176 a.metrics = make(chan telegraf.Metric, 10)177 defer close(a.metrics)178 a.inputConfig = &internal_models.InputConfig{}179 a.Add("acctest", int(101), map[string]string{})180 a.Add("acctest", int32(101), map[string]string{"acc": "test"})181 a.Add("acctest", int64(101), map[string]string{"acc": "test"}, now)182 testm := <-a.metrics183 actual := testm.String()184 assert.Contains(t, actual, "acctest,default=tag value=101i")185 testm = <-a.metrics186 actual = testm.String()187 assert.Contains(t, actual, "acctest,acc=test,default=tag value=101i")188 testm = <-a.metrics189 actual = testm.String()190 assert.Equal(t,191 fmt.Sprintf("acctest,acc=test,default=tag value=101i %d", now.UnixNano()),192 actual)193}194func TestAddFloats(t *testing.T) {195 a := accumulator{}196 a.addDefaultTag("default", "tag")197 now := time.Now()198 a.metrics = make(chan telegraf.Metric, 10)199 defer close(a.metrics)200 a.inputConfig = &internal_models.InputConfig{}201 a.Add("acctest", float32(101), map[string]string{"acc": "test"})202 a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now)203 testm := <-a.metrics204 actual := testm.String()205 assert.Contains(t, actual, "acctest,acc=test,default=tag value=101")206 testm = <-a.metrics207 actual = testm.String()208 assert.Equal(t,209 fmt.Sprintf("acctest,acc=test,default=tag value=101 %d", now.UnixNano()),210 actual)211}212func TestAddStrings(t *testing.T) {213 a := accumulator{}214 a.addDefaultTag("default", "tag")215 now := time.Now()216 a.metrics = make(chan telegraf.Metric, 10)217 defer close(a.metrics)218 a.inputConfig = &internal_models.InputConfig{}219 a.Add("acctest", "test", map[string]string{"acc": "test"})220 a.Add("acctest", "foo", map[string]string{"acc": "test"}, now)221 testm := <-a.metrics222 actual := testm.String()223 assert.Contains(t, actual, "acctest,acc=test,default=tag value=\"test\"")224 testm = <-a.metrics225 actual = testm.String()226 assert.Equal(t,227 fmt.Sprintf("acctest,acc=test,default=tag value=\"foo\" %d", now.UnixNano()),228 actual)229}230func TestAddBools(t *testing.T) {231 a := accumulator{}232 a.addDefaultTag("default", "tag")233 now := time.Now()234 a.metrics = make(chan telegraf.Metric, 10)235 defer close(a.metrics)236 a.inputConfig = &internal_models.InputConfig{}237 a.Add("acctest", true, map[string]string{"acc": "test"})238 a.Add("acctest", false, map[string]string{"acc": "test"}, now)239 testm := <-a.metrics240 actual := testm.String()241 assert.Contains(t, actual, "acctest,acc=test,default=tag value=true")242 testm = <-a.metrics243 actual = testm.String()244 assert.Equal(t,245 fmt.Sprintf("acctest,acc=test,default=tag value=false %d", now.UnixNano()),246 actual)...
builtin_metrics.go
Source:builtin_metrics.go
1// Licensed to Elasticsearch B.V. under one or more contributor2// license agreements. See the NOTICE file distributed with3// this work for additional information regarding copyright4// ownership. Elasticsearch B.V. licenses this file to you under5// the Apache License, Version 2.0 (the "License"); you may6// not use this file except in compliance with the License.7// You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package apm18import (19 "context"20 "runtime"21 sysinfo "github.com/elastic/go-sysinfo"22 "github.com/elastic/go-sysinfo/types"23)24// builtinMetricsGatherer is an MetricsGatherer which gathers builtin metrics:25// - goroutines26// - memstats (allocations, usage, GC, etc.)27// - system and process CPU and memory usage28type builtinMetricsGatherer struct {29 tracer *Tracer30 lastSysMetrics sysMetrics31}32func newBuiltinMetricsGatherer(t *Tracer) *builtinMetricsGatherer {33 g := &builtinMetricsGatherer{tracer: t}34 if metrics, err := gatherSysMetrics(); err == nil {35 g.lastSysMetrics = metrics36 }37 return g38}39// GatherMetrics gathers mem metrics into m.40func (g *builtinMetricsGatherer) GatherMetrics(ctx context.Context, m *Metrics) error {41 m.Add("golang.goroutines", nil, float64(runtime.NumGoroutine()))42 g.gatherSystemMetrics(m)43 g.gatherMemStatsMetrics(m)44 return nil45}46func (g *builtinMetricsGatherer) gatherSystemMetrics(m *Metrics) {47 metrics, err := gatherSysMetrics()48 if err != nil {49 return50 }51 systemCPU, processCPU := calculateCPUUsage(metrics.cpu, g.lastSysMetrics.cpu)52 m.Add("system.cpu.total.norm.pct", nil, systemCPU)53 m.Add("system.process.cpu.total.norm.pct", nil, processCPU)54 m.Add("system.memory.total", nil, float64(metrics.mem.system.Total))55 m.Add("system.memory.actual.free", nil, float64(metrics.mem.system.Available))56 m.Add("system.process.memory.size", nil, float64(metrics.mem.process.Virtual))57 m.Add("system.process.memory.rss.bytes", nil, float64(metrics.mem.process.Resident))58 g.lastSysMetrics = metrics59}60func (g *builtinMetricsGatherer) gatherMemStatsMetrics(m *Metrics) {61 var mem runtime.MemStats62 runtime.ReadMemStats(&mem)63 addUint64 := func(name string, v uint64) {64 m.Add(name, nil, float64(v))65 }66 add := func(name string, v float64) {67 m.Add(name, nil, v)68 }69 addUint64("golang.heap.allocations.mallocs", mem.Mallocs)70 addUint64("golang.heap.allocations.frees", mem.Frees)71 addUint64("golang.heap.allocations.objects", mem.HeapObjects)72 addUint64("golang.heap.allocations.total", mem.TotalAlloc)73 addUint64("golang.heap.allocations.allocated", mem.HeapAlloc)74 addUint64("golang.heap.allocations.idle", mem.HeapIdle)75 addUint64("golang.heap.allocations.active", mem.HeapInuse)76 addUint64("golang.heap.system.total", mem.Sys)77 addUint64("golang.heap.system.obtained", mem.HeapSys)78 addUint64("golang.heap.system.stack", mem.StackSys)79 addUint64("golang.heap.system.released", mem.HeapReleased)80 addUint64("golang.heap.gc.next_gc_limit", mem.NextGC)81 addUint64("golang.heap.gc.total_count", uint64(mem.NumGC))82 addUint64("golang.heap.gc.total_pause.ns", mem.PauseTotalNs)83 add("golang.heap.gc.cpu_fraction", mem.GCCPUFraction)84}85func calculateCPUUsage(current, last cpuMetrics) (systemUsage, processUsage float64) {86 idleDelta := current.system.Idle + current.system.IOWait - last.system.Idle - last.system.IOWait87 systemTotalDelta := current.system.Total() - last.system.Total()88 if systemTotalDelta <= 0 {89 return 0, 090 }91 idlePercent := float64(idleDelta) / float64(systemTotalDelta)92 systemUsage = 1 - idlePercent93 processTotalDelta := current.process.Total() - last.process.Total()94 processUsage = float64(processTotalDelta) / float64(systemTotalDelta)95 return systemUsage, processUsage96}97type sysMetrics struct {...
add
Using AI Code Generation
1import (2func main() {3 reg := prometheus.NewRegistry()4 counter := promauto.With(reg).NewCounter(prometheus.CounterOpts{5 })6 counter.Add(1)7 reg.MustRegister(prometheus.NewBuildInfoCollector())8 reg.MustRegister(prometheus.NewGoCollector())9 http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))10 http.ListenAndServe(":2112", nil)11}
add
Using AI Code Generation
1import (2func main() {3 http.Handle("/metrics", promhttp.Handler())4 log.Fatal(http.ListenAndServe(":8080", nil))5}6import (7var (8 metrics = promauto.NewCounterVec(prometheus.CounterOpts{9 },10 []string{"method", "endpoint"},11}
add
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 m.Add(1)5 fmt.Println(m.Value())6}7import (8func main() {9 fmt.Println("Hello, playground")10 m.Add(1)11 fmt.Println(m.Value())12}13import (14func main() {15 fmt.Println("Hello, playground")16 m.Add(1)17 fmt.Println(m.Value())18}19import (20func main() {21 fmt.Println("Hello, playground")22 m.Add(1)23 fmt.Println(m.Value())24}25import (26func main() {27 fmt.Println("Hello, playground")28 m.Add(1)29 fmt.Println(m.Value())30}31import (32func main() {33 fmt.Println("Hello, playground")34 m.Add(1)35 fmt.Println(m.Value())36}37import (38func main() {39 fmt.Println("Hello, playground")40 m.Add(1)41 fmt.Println(m.Value())42}43import (44func main() {45 fmt.Println("Hello, playground")46 m.Add(1)47 fmt.Println(m.Value())48}49import (50func main() {
add
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4}5import (6func main() {7 fmt.Println("Hello World")8}9import (10func main() {11 fmt.Println("Hello World")12}13import (14func main() {15 fmt.Println("Hello World")16}17import (18func main() {19 fmt.Println("Hello World")20}21import (22func main() {23 fmt.Println("Hello World")24}25import (26func main() {27 fmt.Println("Hello World")28}29import (30func main() {31 fmt.Println("Hello World")32}33import (34func add(a int, b int) int {35}36import (
add
Using AI Code Generation
1import (2func main() {3 m := metrics.New()4 m.Add(1)5 fmt.Println(m.Value())6}7import "fmt"8type Metrics struct {9}10func (m *Metrics) Add(i int) {11}12func (m *Metrics) Value() int {13}14func main() {15 m := Metrics{}16 m.Add(1)17 fmt.Println(m.Value())18}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!