How to use NewRegistry method of metrics Package

Best K6 code snippet using metrics.NewRegistry

api_test.go

Source:api_test.go Github

copy

Full Screen

...9 clientmodel "github.com/prometheus/client_model/go"10 "github.com/stretchr/testify/assert"11)12func TestCounter(t *testing.T) {13 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "z"})14 doTestCounter(t, &metrics)15}16func doTestCounter(t *testing.T, metrics *PrometheusMetricsImpl) {17 c := metrics.Counter("Mine")18 c.Inc()19 metrics.Counter("Mine").Inc()20 c.IncBy(7)21 m := findMetric("z_mine", metrics.gatherOK(t))22 assert.Equal(t, 1, len(m.Metric))23 assert.Equal(t, "name:\"z_mine\" help:\"z_mine\" type:COUNTER metric:<counter:<value:9 > >", strings.TrimSpace(m.String()))24}25func TestCounterWithExplicitDescription(t *testing.T) {26 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah"})27 metrics.Counter("123", "MyDesc").Inc()28 m := findMetric("blah_123", metrics.gatherOK(t))29 assert.Contains(t, m.String(), "name:\"blah_123\" help:\"MyDesc\" type:COUNTER")30}31func TestCounterWithMappedDescription(t *testing.T) {32 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(),33 MetricNamePrefix: "a",34 Descriptions: MetricDescriptions{"mapped": "Description found", "mapped_blank": ""}})35 metrics.Counter("mapped").Inc()36 metrics.Counter("unmapped").Inc()37 metrics.Counter("mapped_blank").Inc()38 gathered := metrics.gatherOK(t)39 assert.Contains(t, findMetric("a_mapped", gathered).String(), "name:\"a_mapped\" help:\"Description found\" type:COUNTER")40 assert.Contains(t, findMetric("a_unmapped", gathered).String(), "name:\"a_unmapped\" help:\"a_unmapped\" type:COUNTER")41 assert.Contains(t, findMetric("a_mapped_blank", gathered).String(), "name:\"a_mapped_blank\" help:\"\" type:COUNTER")42}43func TestCounterCaseInsensitivity(t *testing.T) {44 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah"})45 metrics.Counter("abcd").Inc()46 metrics.Counter("AbCd").Inc()47 metrics.Counter("ABCD").Inc()48 m := findMetric("blah_abcd", metrics.gatherOK(t))49 assert.Equal(t, "name:\"blah_abcd\" help:\"blah_abcd\" type:COUNTER metric:<counter:<value:3 > >", strings.TrimSpace(m.String()))50}51func TestCounterCaseSensitivity(t *testing.T) {52 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah", CaseSensitiveMetricNames: true})53 metrics.Counter("abcd").Inc()54 metrics.Counter("AbCd").Inc()55 metrics.Counter("ABCD").Inc()56 gathered := metrics.gatherOK(t)57 assert.Equal(t, "name:\"blah_abcd\" help:\"blah_abcd\" type:COUNTER metric:<counter:<value:1 > >", strings.TrimSpace(findMetric("blah_abcd", gathered).String()))58 assert.Equal(t, "name:\"blah_AbCd\" help:\"blah_AbCd\" type:COUNTER metric:<counter:<value:1 > >", strings.TrimSpace(findMetric("blah_AbCd", gathered).String()))59 assert.Equal(t, "name:\"blah_ABCD\" help:\"blah_ABCD\" type:COUNTER metric:<counter:<value:1 > >", strings.TrimSpace(findMetric("blah_ABCD", gathered).String()))60}61func TestCounterWithAllDefaultOptions(t *testing.T) {62 metrics := NewMetrics(MetricOpts{})63 metrics.Counter("abcde").Inc()64 m := findMetric("abcde", metrics.gatherOK(t))65 assert.Equal(t, "name:\"abcde\" help:\"abcde\" type:COUNTER metric:<counter:<value:1 > >", strings.TrimSpace(m.String()))66}67func TestCounterWithBlankExplicitName(t *testing.T) {68 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah"})69 metrics.Counter("234", "").Inc()70 m := findMetric("blah_234", metrics.gatherOK(t))71 assert.Equal(t, "name:\"blah_234\" help:\"blah_234\" type:COUNTER metric:<counter:<value:1 > >", strings.TrimSpace(m.String()))72}73func TestCounterWithLabel(t *testing.T) {74 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "v"})75 c := metrics.CounterWithLabel("visitors", "country", "desc")76 c.IncLabel("uk")77 c.IncLabelBy("usa").Value(16)78 c.IncLabel("uk")79 c.IncLabel("usa")80 c.IncLabelBy("usa").Value(3)81 labels := metrics.TestHelper().GetMetricLabelValues("v_visitors")82 assertRegistryHasMetricWithConfigTypeCounts(t, labels, "v_visitors", map[string]map[string]float64{"country": {83 "uk": 2.0,84 "usa": 20.0,85 }})86 // For coverage87 assert.Nil(t, metrics.TestHelper().GetMetricLabelValues("_xxx"))88}89func TestCounterWithLabels(t *testing.T) {90 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "v", PrefixSeparator: ":"})91 c := metrics.CounterWithLabels("animals", []string{"animal", "breed"}, "desc")92 c.IncLabel("cat", "persian")93 c.IncLabelBy("dog", "spaniel").Value(16)94 c.IncLabel("cat", "black")95 c.IncLabel("dog", "greyhound")96 c.IncLabelBy("cat", "black").Value(3)97 m := findMetric("v:animals", metrics.gatherOK(t))98 assert.Equal(t, "name:\"v:animals\" help:\"desc\" type:COUNTER metric:<label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"black\" > counter:<value:4 > > metric:<label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"persian\" > counter:<value:1 > > metric:<label:<name:\"animal\" value:\"dog\" > label:<name:\"breed\" value:\"greyhound\" > counter:<value:1 > > metric:<label:<name:\"animal\" value:\"dog\" > label:<name:\"breed\" value:\"spaniel\" > counter:<value:16 > >", strings.TrimSpace(m.String()))99}100func TestBadNameReuse(t *testing.T) {101 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah"})102 metrics.Counter("a").Inc()103 assert.Panics(t, func() { metrics.Gauge("a").Inc() }, "The code did not panic")104}105func TestGauge(t *testing.T) {106 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "x-service#123"})107 g := metrics.Gauge("MyGauge")108 g.SetValue(100)109 g.Inc()110 g.IncBy(5)111 g.Dec()112 g.DecBy(4)113 metrics.Gauge("MyGauge").Inc()114 m := findMetric("x_service_123_mygauge", metrics.gatherOK(t))115 assert.Equal(t, 1, len(m.Metric))116 assert.Equal(t, "name:\"x_service_123_mygauge\" help:\"x_service_123_mygauge\" type:GAUGE metric:<gauge:<value:102 > >", strings.TrimSpace(m.String()))117}118func TestGaugeWithLabel(t *testing.T) {119 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "prefix"})120 g := metrics.GaugeWithLabel("current animals", "animal")121 g.SetLabels("fleas").Value(1000)122 g.IncLabels("dog")123 g.IncLabelsBy("cat").Value(5)124 g.DecLabels("cat")125 g.DecLabelsBy("fleas").Value(15)126 m := findMetric("prefix_current_animals", metrics.gatherOK(t))127 assert.Equal(t, 3, len(m.Metric))128 assert.Equal(t, "name:\"prefix_current_animals\" help:\"prefix_current_animals\" type:GAUGE metric:<label:<name:\"animal\" value:\"cat\" > gauge:<value:4 > > metric:<label:<name:\"animal\" value:\"dog\" > gauge:<value:1 > > metric:<label:<name:\"animal\" value:\"fleas\" > gauge:<value:985 > >", strings.TrimSpace(m.String()))129}130func TestGaugeWithLabels(t *testing.T) {131 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "prefix"})132 g := metrics.GaugeWithLabels("current animals", []string{"animal", "breed"})133 g.SetLabels("fleas", "plague").Value(500)134 g.SetLabels("fleas", "asian").Value(1000)135 g.IncLabels("dog", "borzoi")136 g.IncLabelsBy("cat", "black").Value(5)137 g.IncLabelsBy("cat", "white").Value(1)138 g.DecLabels("cat", "black")139 g.DecLabels("cat", "white")140 g.DecLabelsBy("fleas", "plague").Value(15)141 m := findMetric("prefix_current_animals", metrics.gatherOK(t))142 assert.Equal(t, 5, len(m.Metric))143 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"black\" > gauge:<value:4 >",144 strings.TrimSpace(m.Metric[0].String()))145 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"white\" > gauge:<value:0 >",146 strings.TrimSpace(m.Metric[1].String()))147 assert.Equal(t, "label:<name:\"animal\" value:\"dog\" > label:<name:\"breed\" value:\"borzoi\" > gauge:<value:1 >",148 strings.TrimSpace(m.Metric[2].String()))149 assert.Equal(t, "label:<name:\"animal\" value:\"fleas\" > label:<name:\"breed\" value:\"asian\" > gauge:<value:1000 >",150 strings.TrimSpace(m.Metric[3].String()))151 assert.Equal(t, "label:<name:\"animal\" value:\"fleas\" > label:<name:\"breed\" value:\"plague\" > gauge:<value:485 >",152 strings.TrimSpace(m.Metric[4].String()))153}154func TestSummary(t *testing.T) {155 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "BLAH"})156 s := metrics.Summary("MySummary")157 s.Observe(1.3)158 s.Observe(2.5)159 s.Observe(2.6)160 s.Observe(2.9)161 s.Observe(3.2)162 s.Observe(3.3)163 s.Observe(3.834344)164 m := findMetric("blah_mysummary", metrics.gatherOK(t))165 assert.Equal(t, 1, len(m.Metric))166 assert.Equal(t, "summary:<sample_count:7 sample_sum:19.634344000000002 quantile:<quantile:0.5 value:2.9 > quantile:<quantile:0.75 value:3.3 > quantile:<quantile:0.9 value:3.834344 > quantile:<quantile:0.95 value:3.834344 > quantile:<quantile:0.99 value:3.834344 > quantile:<quantile:0.999 value:3.834344 > >",167 strings.TrimSpace(m.Metric[0].String()))168}169func TestSummaryWithLabel(t *testing.T) {170 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "BLAH"})171 s := metrics.SummaryWithLabel("animal facts", "animal")172 s.Observe(1.0, "cat")173 s.Observe(2.5, "cat")174 s.Observe(2.6, "dog")175 s.Observe(2.0, "cat")176 s.Observe(3.2, "ant")177 s.Observe(3.3, "dog")178 s.Observe(3.834344, "bear")179 m := findMetric("blah_animal_facts", metrics.gatherOK(t))180 assert.Equal(t, 4, len(m.Metric))181 assert.Equal(t, "label:<name:\"animal\" value:\"ant\" > summary:<sample_count:1 sample_sum:3.2 quantile:<quantile:0.5 value:3.2 > quantile:<quantile:0.75 value:3.2 > quantile:<quantile:0.9 value:3.2 > quantile:<quantile:0.95 value:3.2 > quantile:<quantile:0.99 value:3.2 > quantile:<quantile:0.999 value:3.2 > >",182 strings.TrimSpace(m.Metric[0].String()))183 assert.Equal(t, "label:<name:\"animal\" value:\"bear\" > summary:<sample_count:1 sample_sum:3.834344 quantile:<quantile:0.5 value:3.834344 > quantile:<quantile:0.75 value:3.834344 > quantile:<quantile:0.9 value:3.834344 > quantile:<quantile:0.95 value:3.834344 > quantile:<quantile:0.99 value:3.834344 > quantile:<quantile:0.999 value:3.834344 > >",184 strings.TrimSpace(m.Metric[1].String()))185 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > summary:<sample_count:3 sample_sum:5.5 quantile:<quantile:0.5 value:2 > quantile:<quantile:0.75 value:2.5 > quantile:<quantile:0.9 value:2.5 > quantile:<quantile:0.95 value:2.5 > quantile:<quantile:0.99 value:2.5 > quantile:<quantile:0.999 value:2.5 > >",186 strings.TrimSpace(m.Metric[2].String()))187 assert.Equal(t, "label:<name:\"animal\" value:\"dog\" > summary:<sample_count:2 sample_sum:5.9 quantile:<quantile:0.5 value:2.6 > quantile:<quantile:0.75 value:3.3 > quantile:<quantile:0.9 value:3.3 > quantile:<quantile:0.95 value:3.3 > quantile:<quantile:0.99 value:3.3 > quantile:<quantile:0.999 value:3.3 > >",188 strings.TrimSpace(m.Metric[3].String()))189}190func TestSummaryWithLabels(t *testing.T) {191 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "BLAH"})192 s := metrics.SummaryWithLabels("animal breeds", []string{"animal", "breed"})193 s.Observe(1.0, "cat", "tabby")194 s.Observe(2.5, "cat", "siamese")195 s.Observe(2.6, "dog", "mutt")196 m := findMetric("blah_animal_breeds", metrics.gatherOK(t))197 assert.Equal(t, 3, len(m.Metric))198 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"siamese\" > summary:<sample_count:1 sample_sum:2.5 quantile:<quantile:0.5 value:2.5 > quantile:<quantile:0.75 value:2.5 > quantile:<quantile:0.9 value:2.5 > quantile:<quantile:0.95 value:2.5 > quantile:<quantile:0.99 value:2.5 > quantile:<quantile:0.999 value:2.5 > >",199 strings.TrimSpace(m.Metric[0].String()))200 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > label:<name:\"breed\" value:\"tabby\" > summary:<sample_count:1 sample_sum:1 quantile:<quantile:0.5 value:1 > quantile:<quantile:0.75 value:1 > quantile:<quantile:0.9 value:1 > quantile:<quantile:0.95 value:1 > quantile:<quantile:0.99 value:1 > quantile:<quantile:0.999 value:1 > >",201 strings.TrimSpace(m.Metric[1].String()))202 assert.Equal(t, "label:<name:\"animal\" value:\"dog\" > label:<name:\"breed\" value:\"mutt\" > summary:<sample_count:1 sample_sum:2.6 quantile:<quantile:0.5 value:2.6 > quantile:<quantile:0.75 value:2.6 > quantile:<quantile:0.9 value:2.6 > quantile:<quantile:0.95 value:2.6 > quantile:<quantile:0.99 value:2.6 > quantile:<quantile:0.999 value:2.6 > >",203 strings.TrimSpace(m.Metric[2].String()))204}205func TestRegisterUnderlyingMetric(t *testing.T) {206 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "unused"})207 metricName := "name_" + fmt.Sprint(rand.Intn(100000))208 metric := prometheus.NewCounter(prometheus.CounterOpts{Name: metricName, Help: "help"})209 metrics.MustRegister(metric)210 metric.Add(71)211 m := findMetric(metricName, metrics.gatherOK(t))212 assert.Equal(t, 1, len(m.Metric))213 assert.Equal(t, "counter:<value:71 >", strings.TrimSpace(m.Metric[0].String()))214}215func TestHistogramForResponseTime(t *testing.T) {216 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "A"})217 s := metrics.HistogramForResponseTime("MyHisto")218 s.Update(1.3)219 s.Update(2.5)220 s.Update(2.6)221 s.Update(2.9)222 s.Update(3.2)223 s.Update(3.3)224 s.Update(3.834344)225 m := findMetric("a_myhisto", metrics.gatherOK(t))226 assert.Equal(t, 1, len(m.Metric))227 assert.Equal(t, "histogram:<sample_count:7 sample_sum:19.634344000000002 bucket:<cumulative_count:0 upper_bound:0.005 > bucket:<cumulative_count:0 upper_bound:0.01 > bucket:<cumulative_count:0 upper_bound:0.025 > bucket:<cumulative_count:0 upper_bound:0.05 > bucket:<cumulative_count:0 upper_bound:0.1 > bucket:<cumulative_count:0 upper_bound:0.25 > bucket:<cumulative_count:0 upper_bound:0.5 > bucket:<cumulative_count:0 upper_bound:1 > bucket:<cumulative_count:2 upper_bound:2.5 > bucket:<cumulative_count:7 upper_bound:5 > bucket:<cumulative_count:7 upper_bound:10 > >",228 strings.TrimSpace(m.Metric[0].String()))229}230func TestHistogramCustomBuckets(t *testing.T) {231 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "A"})232 s := metrics.Histogram("MyHisto", []float64{2.0, 3.0, 3.5})233 s.Update(1.3)234 s.Update(2.5)235 s.Update(2.6)236 s.Update(2.9)237 s.Update(3.2)238 s.Update(3.3)239 s.Update(3.834344)240 m := findMetric("a_myhisto", metrics.gatherOK(t))241 assert.Equal(t, 1, len(m.Metric))242 assert.Equal(t, "histogram:<sample_count:7 sample_sum:19.634344000000002 bucket:<cumulative_count:1 upper_bound:2 > bucket:<cumulative_count:4 upper_bound:3 > bucket:<cumulative_count:6 upper_bound:3.5 > >",243 strings.TrimSpace(m.Metric[0].String()))244}245func TestErrors(t *testing.T) {246 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "z"})247 doTestErrors(t, &metrics)248 assert.Equal(t, []string{"z_errors"}, metrics.TestHelper().MetricNames())249}250func doTestErrors(t *testing.T, metrics *PrometheusMetricsImpl) {251 metrics.Error("bad")252 metrics.Error("generic")253 metrics.Error("generic")254 metrics.Error("worse")255 m := findMetric("z_errors", metrics.gatherOK(t))256 assert.Equal(t, 3, len(m.Metric))257 assert.Equal(t, "label:<name:\"error_type\" value:\"bad\" > counter:<value:1 >", strings.TrimSpace(m.Metric[0].String()))258 assert.Equal(t, "label:<name:\"error_type\" value:\"generic\" > counter:<value:2 >", strings.TrimSpace(m.Metric[1].String()))259 assert.Equal(t, "label:<name:\"error_type\" value:\"worse\" > counter:<value:1 >", strings.TrimSpace(m.Metric[2].String()))260}261func TestClear(t *testing.T) {262 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "z"})263 doTestCounter(t, &metrics)264 metrics.TestHelper().Clear()265 doTestErrors(t, &metrics)266 metrics.TestHelper().Clear()267 doTestCounter(t, &metrics)268}269func TestNames(t *testing.T) {270 metrics := NewMetrics(MetricOpts{Registry: prometheus.NewRegistry(), MetricNamePrefix: "blah"})271 metrics.Counter("c")272 metrics.Error("e")273 metrics.Gauge("g")274 metrics.HistogramForResponseTime("h")275 metrics.Summary("s")276 timedMethod(&metrics)277 assert.ElementsMatch(t, []string{"blah_c", "blah_g", "blah_h", "blah_s", "blah_timer", "blah_errors"}, metrics.TestHelper().MetricNames())278}279func TestTimersControlled(t *testing.T) {280 metrics := PrometheusMetricsImpl{registry: prometheus.NewRegistry(),281 metricNamePrefix: "xx_",282 registrations: newMetricRegistrations(),283 normalisedNames: newNormalisedNames(),284 timerFactory: &controlledTimerFactory{defaultExpectation: 2 * time.Second}}285 timedMethod(&metrics)286 timedMethod(&metrics)287 m := findMetric("xx_timer", metrics.gatherOK(t))288 assert.Equal(t, 1, len(m.Metric))289 assert.Equal(t, "summary:<sample_count:2 sample_sum:4 quantile:<quantile:0.5 value:2 > quantile:<quantile:0.75 value:2 > quantile:<quantile:0.9 value:2 > quantile:<quantile:0.95 value:2 > quantile:<quantile:0.99 value:2 > quantile:<quantile:0.999 value:2 > >",290 strings.TrimSpace(m.Metric[0].String()))291}292func TestTimersRealTime(t *testing.T) {293 metrics := PrometheusMetricsImpl{registry: prometheus.NewRegistry(),294 registrations: newMetricRegistrations(),295 normalisedNames: newNormalisedNames(),296 timerFactory: &defaultTimerFactory{}}297 timedMethod(&metrics)298 m := findMetric("timer", metrics.gatherOK(t))299 assert.Equal(t, 1, len(m.Metric))300 // Can't validate times301}302func TestLabelledTimersControlled(t *testing.T) {303 metrics := PrometheusMetricsImpl{registry: prometheus.NewRegistry(),304 metricNamePrefix: "xx_",305 registrations: newMetricRegistrations(),306 normalisedNames: newNormalisedNames(),307 timerFactory: &controlledTimerFactory{defaultExpectation: 2 * time.Second}}308 timedMethodWithLabel(&metrics)309 timedMethodWithLabel(&metrics)310 m := findMetric("xx_animal_timer", metrics.gatherOK(t))311 assert.Equal(t, 1, len(m.Metric))312 assert.Equal(t, "label:<name:\"animal\" value:\"cat\" > summary:<sample_count:2 sample_sum:4 quantile:<quantile:0.5 value:2 > quantile:<quantile:0.75 value:2 > quantile:<quantile:0.9 value:2 > quantile:<quantile:0.95 value:2 > quantile:<quantile:0.99 value:2 > quantile:<quantile:0.999 value:2 > >",313 strings.TrimSpace(m.Metric[0].String()))314}315func timedMethod(metrics PrometheusMetrics) {316 defer metrics.Timer("Timer")()317 fmt.Println("Whatever it is we're timing")...

Full Screen

Full Screen

metrics_set_test.go

Source:metrics_set_test.go Github

copy

Full Screen

...48 assert.Equal(t, "sub_sys", set.GetSubSystem())49 assert.Equal(t, prometheus.DefaultRegisterer, set.GetRegisterer())50}51func TestNewMetricsSet_HappyCase(t *testing.T) {52 registerer := prometheus.NewRegistry()53 set := NewMetricsSet("ns", "sub_sys", registerer)54 assert.NotNil(t, set, "metrics set should not be nil")55 assert.Equal(t, "ns", set.GetNamespace())56 assert.Equal(t, "sub_sys", set.GetSubSystem())57 assert.Equal(t, registerer, set.GetRegisterer())58}59func TestMetricsSet_GetNamespace_WithEmptyNamespace(t *testing.T) {60 set := NewMetricsSet("", "sub_sys", prometheus.NewRegistry())61 assert.Equal(t, namespaceDefault, set.GetNamespace())62}63func TestMetricsSet_GetNamespace_HappyCase(t *testing.T) {64 set := NewMetricsSet("ns", "sub_sys", prometheus.NewRegistry())65 assert.Equal(t, "ns", set.GetNamespace())66}67func TestMetricsSet_GetNamespace_WithNilRegisterer(t *testing.T) {68 set := NewMetricsSet("ns", "sub_sys", nil)69 assert.Equal(t, prometheus.DefaultRegisterer, set.GetRegisterer())70}71func TestMetricsSet_GetRegisterer_HappyCase(t *testing.T) {72 registerer := prometheus.NewRegistry()73 set := NewMetricsSet("ns", "sub_sys", registerer)74 assert.Equal(t, registerer, set.GetRegisterer())75}76func TestMetricsSet_GetSubSystem_WithEmptySubSystem(t *testing.T) {77 set := NewMetricsSet("ns", "", prometheus.NewRegistry())78 assert.Equal(t, subSystemDefault, set.GetSubSystem())79}80func TestMetricsSet_GetSubSystem_HappyCase(t *testing.T) {81 set := NewMetricsSet("ns", "sub_sys", prometheus.NewRegistry())82 assert.Equal(t, "sub_sys", set.GetSubSystem())83}84// register and unregister counter85func TestMetricsSet_RegisterCounter_WithEmptyName(t *testing.T) {86 set := NewMetricsSet("", "", prometheus.NewRegistry())87 err := set.RegisterCounter("")88 assert.NotNil(t, err)89 assert.Empty(t, set.ListCounters())90}91func TestMetricsSet_RegisterCounter_WithExceedNameLength(t *testing.T) {92 set := NewMetricsSet("", "", prometheus.NewRegistry())93 err := set.RegisterCounter(randStringBytes(257))94 assert.NotNil(t, err)95 assert.Empty(t, set.ListCounters())96}97func TestMetricsSet_RegisterCounter_WithDuplicate(t *testing.T) {98 set := NewMetricsSet("", "", prometheus.NewRegistry())99 err := set.RegisterCounter(counter)100 defer set.UnRegisterCounter(counter)101 assert.Nil(t, err)102 assert.NotEmpty(t, set.ListCounters())103 assert.NotNil(t, set.RegisterCounter(counter))104}105func TestMetricsSet_RegisterCounter_HappyCase(t *testing.T) {106 set := NewMetricsSet("", "", prometheus.NewRegistry())107 err := set.RegisterCounter(counter, label)108 defer set.UnRegisterCounter(counter)109 assert.Nil(t, err)110 assert.NotEmpty(t, set.ListCounters())111 assert.NotNil(t, set.GetCounter(counter))112}113func TestMetricsSet_UnRegisterCounter_WithNonExistKey(t *testing.T) {114 set := NewMetricsSet("", "", prometheus.NewRegistry())115 set.UnRegisterCounter(counter)116 assert.Empty(t, set.ListCounters())117}118func TestMetricsSet_UnRegisterCounter_HappyCase(t *testing.T) {119 set := NewMetricsSet("", "", prometheus.NewRegistry())120 // register counter121 err := set.RegisterCounter(counter, label)122 assert.Nil(t, err)123 assert.NotEmpty(t, set.ListCounters())124 assert.NotNil(t, set.GetCounter(counter))125 // unregister counter126 set.UnRegisterCounter(counter)127 assert.Empty(t, set.ListCounters())128 assert.Nil(t, set.GetCounter(counter))129}130// register and unregister gauge131func TestMetricsSet_RegisterGauge_WithEmptyName(t *testing.T) {132 set := NewMetricsSet("", "", prometheus.NewRegistry())133 err := set.RegisterGauge("", label)134 assert.NotNil(t, err)135 assert.Empty(t, set.ListGauges())136}137func TestMetricsSet_RegisterGauge_WithExceedNameLength(t *testing.T) {138 set := NewMetricsSet("", "", prometheus.NewRegistry())139 err := set.RegisterGauge(randStringBytes(257))140 assert.NotNil(t, err)141 assert.Empty(t, set.ListGauges())142}143func TestMetricsSet_RegisterGauge_WithDuplicate(t *testing.T) {144 set := NewMetricsSet("", "", prometheus.NewRegistry())145 err := set.RegisterGauge(gauge)146 defer set.UnRegisterGauge(gauge)147 assert.Nil(t, err)148 assert.NotEmpty(t, set.ListGauges())149 assert.NotNil(t, set.RegisterGauge(gauge))150}151func TestMetricsSet_RegisterGauge_HappyCase(t *testing.T) {152 set := NewMetricsSet("", "", prometheus.NewRegistry())153 err := set.RegisterGauge(gauge, label)154 defer set.UnRegisterGauge(gauge)155 assert.Nil(t, err)156 assert.NotEmpty(t, set.ListGauges())157 assert.NotNil(t, set.GetGauge(gauge))158}159func TestMetricsSet_UnRegisterGauge_WithNonExistKey(t *testing.T) {160 set := NewMetricsSet("", "", prometheus.NewRegistry())161 set.UnRegisterGauge(gauge)162 assert.Empty(t, set.ListGauges())163}164func TestMetricsSet_UnRegisterGauge_HappyCase(t *testing.T) {165 set := NewMetricsSet("", "", prometheus.NewRegistry())166 // register gauge167 err := set.RegisterGauge(gauge, label)168 assert.Nil(t, err)169 assert.NotEmpty(t, set.ListGauges())170 assert.NotNil(t, set.GetGauge(gauge))171 // unregister gauge172 set.UnRegisterGauge(gauge)173 assert.Empty(t, set.ListGauges())174 assert.Nil(t, set.GetGauge(gauge))175}176// register and unregister histogram177func TestMetricsSet_RegisterHistogram_WithEmptyName(t *testing.T) {178 set := NewMetricsSet("", "", prometheus.NewRegistry())179 err := set.RegisterHistogram("", []float64{}, label)180 assert.NotNil(t, err)181 assert.Empty(t, set.ListHistograms())182}183func TestMetricsSet_RegisterHistogram_WithExceedNameLength(t *testing.T) {184 set := NewMetricsSet("", "", prometheus.NewRegistry())185 err := set.RegisterHistogram(randStringBytes(257), []float64{}, label)186 assert.NotNil(t, err)187 assert.Empty(t, set.ListHistograms())188}189func TestMetricsSet_RegisterHistogram_WithNilBucket(t *testing.T) {190 set := NewMetricsSet("", "", prometheus.NewRegistry())191 err := set.RegisterHistogram(histogram, nil, label)192 defer set.UnRegisterHistogram(histogram)193 assert.Nil(t, err)194 assert.NotEmpty(t, set.ListHistograms())195}196func TestMetricsSet_RegisterHistogram_WithDuplicate(t *testing.T) {197 set := NewMetricsSet("", "", prometheus.NewRegistry())198 err := set.RegisterHistogram(histogram, []float64{}, label)199 defer set.UnRegisterHistogram(histogram)200 assert.Nil(t, err)201 assert.NotEmpty(t, set.ListHistograms())202 assert.NotNil(t, set.RegisterHistogram(histogram, []float64{}, label))203}204func TestMetricsSet_RegisterHistogram_HappyCase(t *testing.T) {205 set := NewMetricsSet("", "", prometheus.NewRegistry())206 err := set.RegisterHistogram(histogram, []float64{}, label)207 defer set.UnRegisterHistogram(histogram)208 assert.Nil(t, err)209 assert.NotEmpty(t, set.ListHistograms())210 assert.NotNil(t, set.GetHistogram(histogram))211}212func TestMetricsSet_UnRegisterHistogram_WithNonExistKey(t *testing.T) {213 set := NewMetricsSet("", "", prometheus.NewRegistry())214 set.UnRegisterHistogram(histogram)215 assert.Empty(t, set.ListHistograms())216}217func TestMetricsSet_UnRegisterHistogram_HappyCase(t *testing.T) {218 set := NewMetricsSet("", "", prometheus.NewRegistry())219 // register histogram220 err := set.RegisterHistogram(histogram, []float64{}, label)221 assert.Nil(t, err)222 assert.NotEmpty(t, set.ListHistograms())223 assert.NotNil(t, set.GetHistogram(histogram))224 // unregister histogram225 set.UnRegisterHistogram(histogram)226 assert.Empty(t, set.ListHistograms())227 assert.Nil(t, set.GetHistogram(histogram))228}229// register and unregister summary230func TestMetricsSet_RegisterSummary_WithEmptyName(t *testing.T) {231 set := NewMetricsSet("", "", prometheus.NewRegistry())232 err := set.RegisterSummary("", map[float64]float64{}, label)233 assert.NotNil(t, err)234 assert.Empty(t, set.ListSummaries())235}236func TestMetricsSet_RegisterSummary_WithExceedNameLength(t *testing.T) {237 set := NewMetricsSet("", "", prometheus.NewRegistry())238 err := set.RegisterSummary(randStringBytes(257), map[float64]float64{}, label)239 assert.NotNil(t, err)240 assert.Empty(t, set.ListSummaries())241}242func TestMetricsSet_RegisterSummary_WithNilObjective(t *testing.T) {243 set := NewMetricsSet("", "", prometheus.NewRegistry())244 err := set.RegisterSummary(summary, nil, label)245 defer set.UnRegisterSummary(summary)246 assert.Nil(t, err)247 assert.NotEmpty(t, set.ListSummaries())248}249func TestMetricsSet_RegisterSummary_WithDuplicate(t *testing.T) {250 set := NewMetricsSet("", "", prometheus.NewRegistry())251 err := set.RegisterSummary(summary, map[float64]float64{}, label)252 defer set.UnRegisterSummary(summary)253 assert.Nil(t, err)254 assert.NotEmpty(t, set.ListSummaries())255 assert.NotNil(t, set.RegisterSummary(summary, map[float64]float64{}, label))256}257func TestMetricsSet_RegisterSummary_HappyCase(t *testing.T) {258 set := NewMetricsSet("", "", prometheus.NewRegistry())259 err := set.RegisterSummary(summary, map[float64]float64{}, label)260 defer set.UnRegisterSummary(summary)261 assert.Nil(t, err)262 assert.NotEmpty(t, set.ListSummaries())263 assert.NotNil(t, set.GetSummary(summary))264}265func TestMetricsSet_UnRegisterSummary_WithNonExistKey(t *testing.T) {266 set := NewMetricsSet("", "", prometheus.NewRegistry())267 set.UnRegisterSummary(summary)268 assert.Empty(t, set.ListSummaries())269}270func TestMetricsSet_UnRegisterSummary_HappyCase(t *testing.T) {271 set := NewMetricsSet("", "", prometheus.NewRegistry())272 // register summary273 err := set.RegisterSummary(summary, map[float64]float64{}, label)274 assert.Nil(t, err)275 assert.NotEmpty(t, set.ListSummaries())276 assert.NotNil(t, set.GetSummary(summary))277 // unregister summary278 set.UnRegisterSummary(summary)279 assert.Empty(t, set.ListSummaries())280 assert.Nil(t, set.GetSummary(summary))281}282// get counter283func TestMetricsSet_GetCounter_ExpectNil(t *testing.T) {284 set := NewMetricsSet("", "", prometheus.NewRegistry())285 assert.Nil(t, set.GetCounter(counter))286}287func TestMetricsSet_GetCounter_HappyCase(t *testing.T) {288 set := NewMetricsSet("", "", prometheus.NewRegistry())289 assert.Nil(t, set.RegisterCounter(counter, label))290 defer set.UnRegisterCounter(counter)291 assert.NotNil(t, set.GetCounter(counter))292}293// get gauge294func TestMetricsSet_GetGauge_ExpectNil(t *testing.T) {295 set := NewMetricsSet("", "", prometheus.NewRegistry())296 assert.Nil(t, set.GetGauge(gauge))297}298func TestMetricsSet_GetGauge_HappyCase(t *testing.T) {299 set := NewMetricsSet("", "", prometheus.NewRegistry())300 assert.Nil(t, set.RegisterGauge(gauge, label))301 defer set.UnRegisterGauge(gauge)302 assert.NotNil(t, set.GetGauge(gauge))303}304// get histogram305func TestMetricsSet_GetHistogram_ExpectNil(t *testing.T) {306 set := NewMetricsSet("", "", prometheus.NewRegistry())307 assert.Nil(t, set.GetHistogram(histogram))308}309func TestMetricsSet_GetHistogram_HappyCase(t *testing.T) {310 set := NewMetricsSet("", "", prometheus.NewRegistry())311 assert.Nil(t, set.RegisterHistogram(histogram, []float64{}, label))312 defer set.UnRegisterHistogram(histogram)313 assert.NotNil(t, set.GetHistogram(histogram))314}315// get summary316func TestMetricsSet_GetSummary_ExpectNil(t *testing.T) {317 set := NewMetricsSet("", "", prometheus.NewRegistry())318 assert.Nil(t, set.GetSummary(summary))319}320func TestMetricsSet_GetSummary_HappyCase(t *testing.T) {321 set := NewMetricsSet("", "", prometheus.NewRegistry())322 assert.Nil(t, set.RegisterSummary(summary, map[float64]float64{}, label))323 defer set.UnRegisterSummary(summary)324 assert.NotNil(t, set.GetSummary(summary))325}326// list counters327func TestMetricsSet_ListCounters_ExpectEmpty(t *testing.T) {328 set := NewMetricsSet("", "", prometheus.NewRegistry())329 assert.Empty(t, set.ListCounters())330}331func TestMetricsSet_ListCounters_HappyCase(t *testing.T) {332 set := NewMetricsSet("", "", prometheus.NewRegistry())333 assert.Nil(t, set.RegisterCounter(counter, label))334 defer set.UnRegisterCounter(counter)335 assert.Len(t, set.ListCounters(), 1)336}337// list gauges338func TestMetricsSet_ListGauges_ExpectEmpty(t *testing.T) {339 set := NewMetricsSet("", "", prometheus.NewRegistry())340 assert.Empty(t, set.ListGauges())341}342func TestMetricsSet_ListGauges_HappyCase(t *testing.T) {343 set := NewMetricsSet("", "", prometheus.NewRegistry())344 assert.Nil(t, set.RegisterGauge(gauge, label))345 defer set.UnRegisterGauge(gauge)346 assert.Len(t, set.ListGauges(), 1)347}348// list histograms349func TestMetricsSet_ListHistograms_ExpectEmpty(t *testing.T) {350 set := NewMetricsSet("", "", prometheus.NewRegistry())351 assert.Empty(t, set.ListHistograms())352}353func TestMetricsSet_ListHistograms_HappyCase(t *testing.T) {354 set := NewMetricsSet("", "", prometheus.NewRegistry())355 assert.Nil(t, set.RegisterHistogram(histogram, []float64{}, label))356 defer set.UnRegisterHistogram(histogram)357 assert.Len(t, set.ListHistograms(), 1)358}359// list summaries360func TestMetricsSet_ListSummaries_ExpectEmpty(t *testing.T) {361 set := NewMetricsSet("", "", prometheus.NewRegistry())362 assert.Empty(t, set.ListSummaries())363}364func TestMetricsSet_ListSummaries_HappyCase(t *testing.T) {365 set := NewMetricsSet("", "", prometheus.NewRegistry())366 assert.Nil(t, set.RegisterSummary(summary, map[float64]float64{}, label))367 defer set.UnRegisterSummary(summary)368 assert.Len(t, set.ListSummaries(), 1)369}370// get counter with values371func TestMetricsSet_GetCounterWithValues_ExpectNil(t *testing.T) {372 set := NewMetricsSet("", "", prometheus.NewRegistry())373 assert.Nil(t, set.GetCounterWithValues(counter))374}375func TestMetricsSet_GetCounterWithValues_HappyCase(t *testing.T) {376 set := NewMetricsSet("", "", prometheus.NewRegistry())377 assert.Nil(t, set.RegisterCounter(counter, label))378 defer set.UnRegisterCounter(counter)379 assert.NotNil(t, set.GetCounterWithValues(counter, value))380}381// get gauge with values382func TestMetricsSet_GetGaugeWithValues_ExpectNil(t *testing.T) {383 set := NewMetricsSet("", "", prometheus.NewRegistry())384 assert.Nil(t, set.GetGaugeWithValues(gauge))385}386func TestMetricsSet_GetGaugeWithValues_HappyCase(t *testing.T) {387 set := NewMetricsSet("", "", prometheus.NewRegistry())388 assert.Nil(t, set.RegisterGauge(gauge, label))389 defer set.UnRegisterGauge(gauge)390 assert.NotNil(t, set.GetGaugeWithValues(gauge, value))391}392// get histogram with values393func TestMetricsSet_GetHistogramWithValues_ExpectNil(t *testing.T) {394 set := NewMetricsSet("", "", prometheus.NewRegistry())395 assert.Nil(t, set.GetHistogramWithValues(histogram))396}397func TestMetricsSet_GetHistogramWithValues_HappyCase(t *testing.T) {398 set := NewMetricsSet("", "", prometheus.NewRegistry())399 assert.Nil(t, set.RegisterHistogram(histogram, []float64{}, label))400 defer set.UnRegisterHistogram(histogram)401 assert.NotNil(t, set.GetHistogramWithValues(histogram, value))402}403// get summary with values404func TestMetricsSet_GetSummaryWithValues_ExpectNil(t *testing.T) {405 set := NewMetricsSet("", "", prometheus.NewRegistry())406 assert.Nil(t, set.GetSummaryWithValues(summary))407}408func TestMetricsSet_GetSummaryWithValues_HappyCase(t *testing.T) {409 set := NewMetricsSet("", "", prometheus.NewRegistry())410 assert.Nil(t, set.RegisterSummary(summary, map[float64]float64{}, label))411 defer set.UnRegisterSummary(summary)412 assert.NotNil(t, set.GetSummaryWithValues(summary, value))413}414// get counter with labels415func TestMetricsSet_GetCounterWithLabels_ExpectNil(t *testing.T) {416 set := NewMetricsSet("", "", prometheus.NewRegistry())417 assert.Nil(t, set.GetCounterWithLabels(counter, labelMap))418}419func TestMetricsSet_GetCounterWithLabels_HappyCase(t *testing.T) {420 set := NewMetricsSet("", "", prometheus.NewRegistry())421 assert.Nil(t, set.RegisterCounter(counter, label))422 defer set.UnRegisterCounter(counter)423 assert.NotNil(t, set.GetCounterWithLabels(counter, labelMap))424}425// get gauge with values426func TestMetricsSet_GetGaugeWithLabels_ExpectNil(t *testing.T) {427 set := NewMetricsSet("", "", prometheus.NewRegistry())428 assert.Nil(t, set.GetGaugeWithLabels(gauge, labelMap))429}430func TestMetricsSet_GetGaugeWithLabels_HappyCase(t *testing.T) {431 set := NewMetricsSet("", "", prometheus.NewRegistry())432 assert.Nil(t, set.RegisterGauge(gauge, label))433 defer set.UnRegisterGauge(gauge)434 assert.NotNil(t, set.GetGaugeWithLabels(gauge, labelMap))435}436// get histogram with values437func TestMetricsSet_GetHistogramWithLabels_ExpectNil(t *testing.T) {438 set := NewMetricsSet("", "", prometheus.NewRegistry())439 assert.Nil(t, set.GetHistogramWithLabels(histogram, labelMap))440}441func TestMetricsSet_GetHistogramWithLabels_HappyCase(t *testing.T) {442 set := NewMetricsSet("", "", prometheus.NewRegistry())443 assert.Nil(t, set.RegisterHistogram(histogram, []float64{}, label))444 defer set.UnRegisterHistogram(histogram)445 assert.NotNil(t, set.GetHistogramWithLabels(histogram, labelMap))446}447// get summary with values448func TestMetricsSet_GetSummaryWithLabels_ExpectNil(t *testing.T) {449 set := NewMetricsSet("", "", prometheus.NewRegistry())450 assert.Nil(t, set.GetSummaryWithLabels(summary, labelMap))451}452func TestMetricsSet_GetSummaryWithLabels_HappyCase(t *testing.T) {453 set := NewMetricsSet("", "", prometheus.NewRegistry())454 assert.Nil(t, set.RegisterSummary(summary, map[float64]float64{}, label))455 defer set.UnRegisterSummary(summary)456 assert.NotNil(t, set.GetSummaryWithLabels(summary, labelMap))457}458func TestMetricsSet_getKey_HappyCase(t *testing.T) {459 set := NewMetricsSet("", "", prometheus.NewRegistry())460 key := set.getKey(counter)461 tokens := strings.Split(key, separator)462 assert.Len(t, tokens, 3)463 assert.Equal(t, set.namespace, tokens[0])464 assert.Equal(t, set.subSystem, tokens[1])465 assert.Equal(t, counter, tokens[2])466}467func TestMetricsSet_containsKey_ExpectFalse(t *testing.T) {468 set := NewMetricsSet("", "", prometheus.NewRegistry())469 assert.False(t, set.containsKey(counter))470}471func TestMetricsSet_containsKey_ExpectTrue(t *testing.T) {472 set := NewMetricsSet("", "", prometheus.NewRegistry())473 assert.Nil(t, set.RegisterCounter(counter, label))474 assert.True(t, set.containsKey(set.getKey(counter)))475}476func TestMetricsSet_validateName_CheckTrimSpace(t *testing.T) {477 assert.Nil(t, validateName(counter+" "))478}479func TestMetricsSet_validateName_WithEmptyString(t *testing.T) {480 assert.NotNil(t, validateName(""))481}482func TestMetricsSet_validateName_WithExceedString(t *testing.T) {483 assert.NotNil(t, validateName(randStringBytes(257)))484}485func TestMetricsSet_validateName_HappyCase(t *testing.T) {486 assert.Nil(t, validateName(counter))...

Full Screen

Full Screen

prometheusmetrics_test.go

Source:prometheusmetrics_test.go Github

copy

Full Screen

...13 t.Fatalf("Failed to pass prometheus registry to go-metrics provider")14 }15}16func TestUpdatePrometheusMetricsOnce(t *testing.T) {17 prometheusRegistry := prometheus.NewRegistry()18 metricsRegistry := metrics.NewRegistry()19 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)20 metricsRegistry.Register("counter", metrics.NewCounter())21 pClient.UpdatePrometheusMetricsOnce()22 gauge := prometheus.NewGauge(prometheus.GaugeOpts{23 Namespace: "test",24 Subsystem: "subsys",25 Name: "counter",26 Help: "counter",27 })28 err := prometheusRegistry.Register(gauge)29 if err == nil {30 t.Fatalf("Go-metrics registry didn't get registered to prometheus registry")31 }32}33func TestUpdatePrometheusMetrics(t *testing.T) {34 prometheusRegistry := prometheus.NewRegistry()35 metricsRegistry := metrics.NewRegistry()36 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)37 metricsRegistry.Register("counter", metrics.NewCounter())38 go pClient.UpdatePrometheusMetrics()39 time.Sleep(2 * time.Second)40 gauge := prometheus.NewGauge(prometheus.GaugeOpts{41 Namespace: "test",42 Subsystem: "subsys",43 Name: "counter",44 Help: "counter",45 })46 err := prometheusRegistry.Register(gauge)47 if err == nil {48 t.Fatalf("Go-metrics registry didn't get registered to prometheus registry")49 }50}51func TestPrometheusCounterGetUpdated(t *testing.T) {52 prometheusRegistry := prometheus.NewRegistry()53 metricsRegistry := metrics.NewRegistry()54 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)55 cntr := metrics.NewCounter()56 metricsRegistry.Register("counter", cntr)57 cntr.Inc(2)58 go pClient.UpdatePrometheusMetrics()59 cntr.Inc(13)60 time.Sleep(5 * time.Second)61 metrics, _ := prometheusRegistry.Gather()62 serialized := fmt.Sprint(metrics[0])63 expected := fmt.Sprintf("name:\"test_subsys_counter\" help:\"counter\" type:GAUGE metric:<gauge:<value:%d > > ", cntr.Count())64 if serialized != expected {65 t.Fatalf("Go-metrics value and prometheus metrics value do not match")66 }67}68func TestPrometheusGaugeGetUpdated(t *testing.T) {69 prometheusRegistry := prometheus.NewRegistry()70 metricsRegistry := metrics.NewRegistry()71 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)72 gm := metrics.NewGauge()73 metricsRegistry.Register("gauge", gm)74 gm.Update(2)75 go pClient.UpdatePrometheusMetrics()76 gm.Update(13)77 time.Sleep(5 * time.Second)78 metrics, _ := prometheusRegistry.Gather()79 if len(metrics) == 0 {80 t.Fatalf("prometheus was unable to register the metric")81 }82 serialized := fmt.Sprint(metrics[0])83 expected := fmt.Sprintf("name:\"test_subsys_gauge\" help:\"gauge\" type:GAUGE metric:<gauge:<value:%d > > ", gm.Value())84 if serialized != expected {85 t.Fatalf("Go-metrics value and prometheus metrics value do not match")86 }87}88func TestPrometheusMeterGetUpdated(t *testing.T) {89 prometheusRegistry := prometheus.NewRegistry()90 metricsRegistry := metrics.NewRegistry()91 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)92 gm := metrics.NewMeter()93 metricsRegistry.Register("meter", gm)94 gm.Mark(2)95 go pClient.UpdatePrometheusMetrics()96 gm.Mark(13)97 time.Sleep(5 * time.Second)98 metrics, _ := prometheusRegistry.Gather()99 if len(metrics) == 0 {100 t.Fatalf("prometheus was unable to register the metric")101 }102 serialized := fmt.Sprint(metrics[0])103 expected := fmt.Sprintf("name:\"test_subsys_meter\" help:\"meter\" type:GAUGE metric:<gauge:<value:%.16f > > ", gm.Rate1())104 if serialized != expected {105 t.Fatalf("Go-metrics value and prometheus metrics value do not match")106 }107}108func TestPrometheusHistogramGetUpdated(t *testing.T) {109 prometheusRegistry := prometheus.NewRegistry()110 metricsRegistry := metrics.NewRegistry()111 pClient := NewPrometheusProvider(metricsRegistry, "test", "subsys", prometheusRegistry, 1*time.Second)112 // values := make([]int64, 0)113 //sample := metrics.HistogramSnapshot{metrics.NewSampleSnapshot(int64(len(values)), values)}114 gm := metrics.NewHistogram(metrics.NewUniformSample(1028))115 metricsRegistry.Register("metric", gm)116 for ii := 0; ii < 94; ii++ {117 gm.Update(1)118 }119 for ii := 0; ii < 5; ii++ {120 gm.Update(5)121 }122 gm.Update(10)123 go pClient.UpdatePrometheusMetrics()124 time.Sleep(5 * time.Second)...

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 registry := prometheus.NewRegistry()4 counter := promauto.With(registry).NewCounter(prometheus.CounterOpts{5 })6 gauge := promauto.With(registry).NewGauge(prometheus.GaugeOpts{7 })8 histogram := promauto.With(registry).NewHistogram(prometheus.HistogramOpts{9 })10 summary := promauto.With(registry).NewSummary(prometheus.SummaryOpts{11 })12 prometheus.MustRegister(registry)13 http.HandleFunc("/inc", func(w http.ResponseWriter, r *http.Request) {14 counter.Inc()15 gauge.Inc()16 histogram.Observe(10)17 summary.Observe(10)18 })19 http.Handle("/metrics", promhttp.Handler())20 log.Fatal(http.ListenAndServe(":8080", nil))21}22import (23func main() {24 registry := prometheus.NewPedanticRegistry()

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 customRegistry := prometheus.NewRegistry()4 counter := promauto.With(customRegistry).NewCounter(prometheus.CounterOpts{5 })6 counter.Inc()7 gauge := promauto.With(customRegistry).NewGauge(prometheus.GaugeOpts{8 })9 gauge.Set(42)10 histogram := promauto.With(customRegistry).NewHistogram(prometheus.HistogramOpts{11 })12 histogram.Observe(3.14)13 prometheus.MustRegister(customRegistry)14 http.Handle("/metrics", promhttp.Handler())15 log.Fatal(http.ListenAndServe(":8080", nil))16}17net/http.(*conn).serve.func1(0xc0000b8000)18panic(0x5b5a40, 0x6b0e20)19github.com/prometheus/client_golang/prometheus.(*Registry).MustRegister(0xc0000c4000, 0xc0000ac0c0, 0x1, 0x1)

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2var (3 registry = prometheus.NewRegistry()4 cpuTemp = promauto.With(registry).NewGauge(prometheus.GaugeOpts{5 })6func main() {7 registry.MustRegister(cpuTemp)8 http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))9 fmt.Println("Now serving metrics on :2112/metrics")10 http.ListenAndServe(":2112", nil)11}12import (13var (14 registry = prometheus.NewRegistry()15 cpuTemp = promauto.With(registry).NewGauge(prometheus.GaugeOpts{16 })17func main() {18 registry.MustRegister(cpuTemp)19 http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))20 fmt.Println("Now serving metrics on :2112/metrics")21 http.ListenAndServe(":2112", nil)22}23import (24var (25 registry = prometheus.NewRegistry()

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 reg := prometheus.NewRegistry()4 reg.MustRegister(prometheus.NewGoCollector())5 reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))6 http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))7 log.Println("Listening on :8080")8 log.Fatal(http.ListenAndServe(":8080", nil))9}10go_gc_duration_seconds{quantile="0"} 011go_gc_duration_seconds{quantile="0.25"} 012go_gc_duration_seconds{quantile="0.5"} 013go_gc_duration_seconds{quantile="0.75"} 014go_gc_duration_seconds{quantile="1"} 015go_info{version="go1.12.6"} 1

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2var (3 requestCount = prometheus.NewCounterVec(4 prometheus.CounterOpts{5 },6 []string{"code", "method"},7 requestLatency = prometheus.NewHistogramVec(8 prometheus.HistogramOpts{9 },10 []string{"handler", "method"},11func init() {12 prometheus.MustRegister(requestCount)13 prometheus.MustRegister(requestLatency)14}15func main() {16 if fromEnv := os.Getenv("PORT"); fromEnv != "" {17 }18 http.Handle("/metrics", promhttp.Handler())19 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {20 requestCount.WithLabelValues("200", r.Method).Inc()21 start := time.Now()22 time.Sleep(100 * time.Millisecond)23 elapsed := float64(time.Since(start)) / float64(time.Second)24 requestLatency.WithLabelValues(r.URL.Path, r.Method).Observe(elapsed)25 w.WriteHeader(200)26 fmt.Fprintf(w, "Hello from example application.")27 })28 log.Printf("Server listening on port %s", port)29 log.Fatal(http.ListenAndServe(":"+port, nil))30}31request_count{code="200",method="GET"} 1

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := prometheus.NewRegistry()4 counter := promauto.With(r).NewCounter(prometheus.CounterOpts{5 })6 gauge := promauto.With(r).NewGauge(prometheus.GaugeOpts{7 })8 histogram := promauto.With(r).NewHistogram(prometheus.HistogramOpts{9 Buckets: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9},10 })11 counter.Inc()12 gauge.Inc()13 histogram.Observe(2.5)14 mfs, err := r.Gather()15 if err != nil {16 panic(err)17 }18 for _, mf := range mfs {19 fmt.Println(mf)20 }21}22myapp_event_latency_seconds_bucket{le="1"} 023myapp_event_latency_seconds_bucket{le="2"} 024myapp_event_latency_seconds_bucket{le="3"} 025myapp_event_latency_seconds_bucket{le="4"} 026myapp_event_latency_seconds_bucket{le="5"} 027myapp_event_latency_seconds_bucket{le="6"} 028myapp_event_latency_seconds_bucket{le="7"} 029myapp_event_latency_seconds_bucket{le

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1registry := metrics.NewRegistry()2counter := registry.NewCounter("my_counter")3counter.Inc(1)4counter := registry.Get("my_counter")5counter.Inc(1)6counter := registry.Get("my_counter")7counter.Inc(1)8counter := registry.Get("my_counter")9counter.Inc(1)10counter := registry.Get("my_counter")11counter.Inc(1)12counter := registry.Get("my_counter")13counter.Inc(1)14counter := registry.Get("my_counter")15counter.Inc(1)16counter := registry.Get("my_counter")17counter.Inc(1)18counter := registry.Get("my_counter")19counter.Inc(1)20counter := registry.Get("my_counter")21counter.Inc(1)

Full Screen

Full Screen

NewRegistry

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 registry := prometheus.NewRegistry()4 gauge := prometheus.NewGauge(prometheus.GaugeOpts{5 })6 registry.MustRegister(gauge)7 gauge.Set(1)8 counter := prometheus.NewCounter(prometheus.CounterOpts{9 })10 registry.MustRegister(counter)11 counter.Inc()12 histogram := prometheus.NewHistogram(prometheus.HistogramOpts{13 })14 registry.MustRegister(histogram)15 histogram.Observe(1)16 summary := prometheus.NewSummary(prometheus.SummaryOpts{17 })18 registry.MustRegister(summary)19 summary.Observe(1)20 gaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{21 }, []string{"label"})22 registry.MustRegister(gaugeVec)23 gaugeVec.WithLabelValues("test").Set(1)24 counterVec := prometheus.NewCounterVec(prometheus.CounterOpts{25 }, []string{"label"})26 registry.MustRegister(counterVec)27 counterVec.WithLabelValues("test").Inc()

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:

YouTube

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

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful