Best K6 code snippet using metrics.Sample
compare-metrics_test.go
Source:compare-metrics_test.go
...21)22func TestMetricCompareSuccess(t *testing.T) {23 left := &e2e.MetricsForE2E{24 ApiServerMetrics: metrics.ApiServerMetrics{25 "apiserver_request_count": model.Samples{26 &model.Sample{27 Metric: model.Metric{28 "__name__": "apiserver_request_count",29 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",30 "code": "200",31 "resource": "events",32 "verb": "LIST",33 },34 Value: 2,35 },36 &model.Sample{37 Metric: model.Metric{38 "__name__": "apiserver_request_count",39 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",40 "code": "200",41 "resource": "events",42 "verb": "WATCHLIST",43 },44 Value: 1,45 },46 &model.Sample{47 Metric: model.Metric{48 "__name__": "apiserver_request_count",49 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",50 "code": "200",51 "resource": "nodes",52 "verb": "LIST",53 },54 Value: 4,55 },56 },57 },58 ControllerManagerMetrics: metrics.ControllerManagerMetrics{59 "etcd_helper_cache_entry_count": model.Samples{60 &model.Sample{61 Metric: model.Metric{62 "__name__": "etcd_helper_cache_entry_count",63 },64 Value: 0,65 },66 },67 "etcd_helper_cache_hit_count": model.Samples{68 &model.Sample{69 Metric: model.Metric{70 "__name__": "etcd_helper_cache_hit_count",71 },72 Value: 0,73 },74 },75 "etcd_helper_cache_miss_count": model.Samples{76 &model.Sample{77 Metric: model.Metric{78 "__name__": "etcd_helper_cache_miss_count",79 },80 Value: 0,81 },82 },83 "etcd_request_cache_add_latencies_summary": model.Samples{84 &model.Sample{85 Metric: model.Metric{86 "__name__": "etcd_request_cache_add_latencies_summary",87 "quantile": "0.5",88 },89 Value: model.SampleValue(math.NaN()),90 },91 &model.Sample{92 Metric: model.Metric{93 "__name__": "etcd_request_cache_add_latencies_summary",94 "quantile": "0.9",95 },96 Value: model.SampleValue(math.NaN()),97 },98 &model.Sample{99 Metric: model.Metric{100 "__name__": "etcd_request_cache_add_latencies_summary",101 "quantile": "0.99",102 },103 Value: model.SampleValue(math.NaN()),104 },105 },106 },107 KubeletMetrics: map[string]metrics.KubeletMetrics{108 "e2e-test-master": {109 "go_gc_duration_seconds": model.Samples{110 &model.Sample{111 Metric: model.Metric{112 "__name__": "go_gc_duration_seconds",113 "quantile": "0",114 },115 Value: 0.000158288,116 },117 &model.Sample{118 Metric: model.Metric{119 "__name__": "go_gc_duration_seconds",120 "quantile": "0.25",121 },122 Value: 0.002003092,123 },124 &model.Sample{125 Metric: model.Metric{126 "__name__": "go_gc_duration_seconds",127 "quantile": "0.5",128 },129 Value: 0.002483379,130 },131 &model.Sample{132 Metric: model.Metric{133 "__name__": "go_gc_duration_seconds",134 "quantile": "0.75",135 },136 Value: 0.002912684,137 },138 &model.Sample{139 Metric: model.Metric{140 "__name__": "go_gc_duration_seconds",141 "quantile": "1",142 },143 Value: 0.012803119,144 },145 },146 "go_goroutines": model.Samples{147 &model.Sample{148 Metric: model.Metric{149 "__name__": "go_goroutines",150 },151 Value: 119,152 },153 },154 },155 "e2e-test-minion": {156 "go_gc_duration_seconds": model.Samples{157 &model.Sample{158 Metric: model.Metric{159 "__name__": "go_gc_duration_seconds",160 "quantile": "0",161 },162 Value: 0.001158288,163 },164 &model.Sample{165 Metric: model.Metric{166 "__name__": "go_gc_duration_seconds",167 "quantile": "0.25",168 },169 Value: 0.012003092,170 },171 &model.Sample{172 Metric: model.Metric{173 "__name__": "go_gc_duration_seconds",174 "quantile": "0.5",175 },176 Value: 0.012483379,177 },178 &model.Sample{179 Metric: model.Metric{180 "__name__": "go_gc_duration_seconds",181 "quantile": "0.75",182 },183 Value: 0.012912684,184 },185 &model.Sample{186 Metric: model.Metric{187 "__name__": "go_gc_duration_seconds",188 "quantile": "1",189 },190 Value: 0.022803119,191 },192 },193 "go_goroutines": model.Samples{194 &model.Sample{195 Metric: model.Metric{196 "__name__": "go_goroutines",197 },198 Value: 219,199 },200 },201 },202 },203 SchedulerMetrics: metrics.SchedulerMetrics{204 "get_token_count": model.Samples{},205 "get_token_fail_count": model.Samples{},206 "go_gc_duration_seconds": model.Samples{207 &model.Sample{208 Metric: model.Metric{209 "__name__": "go_gc_duration_seconds",210 "quantile": "0",211 },212 Value: 0.00014963700000000002,213 },214 &model.Sample{215 Metric: model.Metric{216 "__name__": "go_gc_duration_seconds",217 "quantile": "0.25",218 },219 Value: 0.0005699380000000001,220 },221 &model.Sample{222 Metric: model.Metric{223 "__name__": "go_gc_duration_seconds",224 "quantile": "0.5",225 },226 Value: 0.001986198,227 },228 &model.Sample{229 Metric: model.Metric{230 "__name__": "go_gc_duration_seconds",231 "quantile": "0.75",232 },233 Value: 0.003013346,234 },235 &model.Sample{236 Metric: model.Metric{237 "__name__": "go_gc_duration_seconds",238 "quantile": "1",239 },240 Value: 0.007081308000000001,241 },242 },243 },244 }245 right := &e2e.MetricsForE2E{246 ApiServerMetrics: metrics.ApiServerMetrics{247 "apiserver_request_count": model.Samples{248 &model.Sample{249 Metric: model.Metric{250 "__name__": "apiserver_request_count",251 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",252 "code": "200",253 "resource": "events",254 "verb": "LIST",255 },256 Value: 3,257 },258 &model.Sample{259 Metric: model.Metric{260 "__name__": "apiserver_request_count",261 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",262 "code": "200",263 "resource": "events",264 "verb": "WATCHLIST",265 },266 Value: 1,267 },268 &model.Sample{269 Metric: model.Metric{270 "__name__": "apiserver_request_count",271 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",272 "code": "200",273 "resource": "nodes",274 "verb": "LIST",275 },276 Value: 3,277 },278 },279 },280 ControllerManagerMetrics: metrics.ControllerManagerMetrics{281 "etcd_helper_cache_entry_count": model.Samples{282 &model.Sample{283 Metric: model.Metric{284 "__name__": "etcd_helper_cache_entry_count",285 },286 Value: 0,287 },288 },289 "etcd_helper_cache_hit_count": model.Samples{290 &model.Sample{291 Metric: model.Metric{292 "__name__": "etcd_helper_cache_hit_count",293 },294 Value: 0,295 },296 },297 "etcd_helper_cache_miss_count": model.Samples{298 &model.Sample{299 Metric: model.Metric{300 "__name__": "etcd_helper_cache_miss_count",301 },302 Value: 0,303 },304 },305 "etcd_request_cache_add_latencies_summary": model.Samples{306 &model.Sample{307 Metric: model.Metric{308 "__name__": "etcd_request_cache_add_latencies_summary",309 "quantile": "0.5",310 },311 Value: model.SampleValue(math.NaN()),312 },313 &model.Sample{314 Metric: model.Metric{315 "__name__": "etcd_request_cache_add_latencies_summary",316 "quantile": "0.9",317 },318 Value: model.SampleValue(math.NaN()),319 },320 &model.Sample{321 Metric: model.Metric{322 "__name__": "etcd_request_cache_add_latencies_summary",323 "quantile": "0.99",324 },325 Value: model.SampleValue(math.NaN()),326 },327 },328 },329 KubeletMetrics: map[string]metrics.KubeletMetrics{330 "e2e-test-master": {331 "go_gc_duration_seconds": model.Samples{332 &model.Sample{333 Metric: model.Metric{334 "__name__": "go_gc_duration_seconds",335 "quantile": "0",336 },337 Value: 0.000158288,338 },339 &model.Sample{340 Metric: model.Metric{341 "__name__": "go_gc_duration_seconds",342 "quantile": "0.25",343 },344 Value: 0.002003092,345 },346 &model.Sample{347 Metric: model.Metric{348 "__name__": "go_gc_duration_seconds",349 "quantile": "0.5",350 },351 Value: 0.002483379,352 },353 &model.Sample{354 Metric: model.Metric{355 "__name__": "go_gc_duration_seconds",356 "quantile": "0.75",357 },358 Value: 0.002912684,359 },360 &model.Sample{361 Metric: model.Metric{362 "__name__": "go_gc_duration_seconds",363 "quantile": "1",364 },365 Value: 0.012803119,366 },367 },368 "go_goroutines": model.Samples{369 &model.Sample{370 Metric: model.Metric{371 "__name__": "go_goroutines",372 },373 Value: 119,374 },375 },376 },377 "e2e-test-minion": {378 "go_gc_duration_seconds": model.Samples{379 &model.Sample{380 Metric: model.Metric{381 "__name__": "go_gc_duration_seconds",382 "quantile": "0",383 },384 Value: 0.001158288,385 },386 &model.Sample{387 Metric: model.Metric{388 "__name__": "go_gc_duration_seconds",389 "quantile": "0.25",390 },391 Value: 0.012003092,392 },393 &model.Sample{394 Metric: model.Metric{395 "__name__": "go_gc_duration_seconds",396 "quantile": "0.5",397 },398 Value: 0.012483379,399 },400 &model.Sample{401 Metric: model.Metric{402 "__name__": "go_gc_duration_seconds",403 "quantile": "0.75",404 },405 Value: 0.012912684,406 },407 &model.Sample{408 Metric: model.Metric{409 "__name__": "go_gc_duration_seconds",410 "quantile": "1",411 },412 Value: 0.022803119,413 },414 },415 "go_goroutines": model.Samples{416 &model.Sample{417 Metric: model.Metric{418 "__name__": "go_goroutines",419 },420 Value: 219,421 },422 },423 },424 },425 SchedulerMetrics: metrics.SchedulerMetrics{426 "get_token_count": model.Samples{},427 "get_token_fail_count": model.Samples{},428 "go_gc_duration_seconds": model.Samples{429 &model.Sample{430 Metric: model.Metric{431 "__name__": "go_gc_duration_seconds",432 "quantile": "0",433 },434 Value: 0.00014963700000000002,435 },436 &model.Sample{437 Metric: model.Metric{438 "__name__": "go_gc_duration_seconds",439 "quantile": "0.25",440 },441 Value: 0.0005699380000000001,442 },443 &model.Sample{444 Metric: model.Metric{445 "__name__": "go_gc_duration_seconds",446 "quantile": "0.5",447 },448 Value: 0.001986198,449 },450 &model.Sample{451 Metric: model.Metric{452 "__name__": "go_gc_duration_seconds",453 "quantile": "0.75",454 },455 Value: 0.003013346,456 },457 &model.Sample{458 Metric: model.Metric{459 "__name__": "go_gc_duration_seconds",460 "quantile": "1",461 },462 Value: 0.007081308000000001,463 },464 },465 },466 }467 if violated := CompareMetrics(left, right); len(violated) != 0 {468 t.Errorf("Expected compare to return empty list, got %v.", violated)469 }470}471func TestMetricCompareFailure(t *testing.T) {472 left := &e2e.MetricsForE2E{473 ApiServerMetrics: metrics.ApiServerMetrics{474 "apiserver_request_count": model.Samples{475 &model.Sample{476 Metric: model.Metric{477 "__name__": "apiserver_request_count",478 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",479 "code": "200",480 "resource": "events",481 "verb": "LIST",482 },483 Value: 5,484 },485 &model.Sample{486 Metric: model.Metric{487 "__name__": "apiserver_request_count",488 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",489 "code": "200",490 "resource": "events",491 "verb": "WATCHLIST",492 },493 Value: 1,494 },495 &model.Sample{496 Metric: model.Metric{497 "__name__": "apiserver_request_count",498 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",499 "code": "200",500 "resource": "nodes",501 "verb": "LIST",502 },503 Value: 4,504 },505 },506 },507 ControllerManagerMetrics: metrics.ControllerManagerMetrics{508 "etcd_helper_cache_entry_count": model.Samples{509 &model.Sample{510 Metric: model.Metric{511 "__name__": "etcd_helper_cache_entry_count",512 },513 Value: 0,514 },515 },516 "etcd_helper_cache_hit_count": model.Samples{517 &model.Sample{518 Metric: model.Metric{519 "__name__": "etcd_helper_cache_hit_count",520 },521 Value: 0,522 },523 },524 "etcd_helper_cache_miss_count": model.Samples{525 &model.Sample{526 Metric: model.Metric{527 "__name__": "etcd_helper_cache_miss_count",528 },529 Value: 0,530 },531 },532 "etcd_request_cache_add_latencies_summary": model.Samples{533 &model.Sample{534 Metric: model.Metric{535 "__name__": "etcd_request_cache_add_latencies_summary",536 "quantile": "0.5",537 },538 Value: model.SampleValue(math.NaN()),539 },540 &model.Sample{541 Metric: model.Metric{542 "__name__": "etcd_request_cache_add_latencies_summary",543 "quantile": "0.9",544 },545 Value: model.SampleValue(math.NaN()),546 },547 &model.Sample{548 Metric: model.Metric{549 "__name__": "etcd_request_cache_add_latencies_summary",550 "quantile": "0.99",551 },552 Value: model.SampleValue(math.NaN()),553 },554 },555 },556 KubeletMetrics: map[string]metrics.KubeletMetrics{557 "e2e-test-master": {558 "go_gc_duration_seconds": model.Samples{559 &model.Sample{560 Metric: model.Metric{561 "__name__": "go_gc_duration_seconds",562 "quantile": "0",563 },564 Value: 0.000158288,565 },566 &model.Sample{567 Metric: model.Metric{568 "__name__": "go_gc_duration_seconds",569 "quantile": "0.25",570 },571 Value: 0.002003092,572 },573 &model.Sample{574 Metric: model.Metric{575 "__name__": "go_gc_duration_seconds",576 "quantile": "0.5",577 },578 Value: 0.002483379,579 },580 &model.Sample{581 Metric: model.Metric{582 "__name__": "go_gc_duration_seconds",583 "quantile": "0.75",584 },585 Value: 0.002912684,586 },587 &model.Sample{588 Metric: model.Metric{589 "__name__": "go_gc_duration_seconds",590 "quantile": "1",591 },592 Value: 0.012803119,593 },594 },595 "go_goroutines": model.Samples{596 &model.Sample{597 Metric: model.Metric{598 "__name__": "go_goroutines",599 },600 Value: 119,601 },602 },603 },604 "e2e-test-minion": {605 "go_gc_duration_seconds": model.Samples{606 &model.Sample{607 Metric: model.Metric{608 "__name__": "go_gc_duration_seconds",609 "quantile": "0",610 },611 Value: 0.001158288,612 },613 &model.Sample{614 Metric: model.Metric{615 "__name__": "go_gc_duration_seconds",616 "quantile": "0.25",617 },618 Value: 0.013003092,619 },620 &model.Sample{621 Metric: model.Metric{622 "__name__": "go_gc_duration_seconds",623 "quantile": "0.5",624 },625 Value: 0.012483379,626 },627 &model.Sample{628 Metric: model.Metric{629 "__name__": "go_gc_duration_seconds",630 "quantile": "0.75",631 },632 Value: 0.012912684,633 },634 &model.Sample{635 Metric: model.Metric{636 "__name__": "go_gc_duration_seconds",637 "quantile": "1",638 },639 Value: 0.032803119,640 },641 },642 "go_goroutines": model.Samples{643 &model.Sample{644 Metric: model.Metric{645 "__name__": "go_goroutines",646 },647 Value: 219,648 },649 },650 },651 },652 SchedulerMetrics: metrics.SchedulerMetrics{653 "get_token_count": model.Samples{},654 "get_token_fail_count": model.Samples{},655 "go_gc_duration_seconds": model.Samples{656 &model.Sample{657 Metric: model.Metric{658 "__name__": "go_gc_duration_seconds",659 "quantile": "0",660 },661 Value: 0.00014963700000000002,662 },663 &model.Sample{664 Metric: model.Metric{665 "__name__": "go_gc_duration_seconds",666 "quantile": "0.25",667 },668 Value: 0.0005699380000000001,669 },670 &model.Sample{671 Metric: model.Metric{672 "__name__": "go_gc_duration_seconds",673 "quantile": "0.5",674 },675 Value: 0.001986198,676 },677 &model.Sample{678 Metric: model.Metric{679 "__name__": "go_gc_duration_seconds",680 "quantile": "0.75",681 },682 Value: 0.003013446,683 },684 &model.Sample{685 Metric: model.Metric{686 "__name__": "go_gc_duration_seconds",687 "quantile": "1",688 },689 Value: 0.004081308000000001,690 },691 },692 },693 }694 right := &e2e.MetricsForE2E{695 ApiServerMetrics: metrics.ApiServerMetrics{696 "apiserver_request_count": model.Samples{697 &model.Sample{698 Metric: model.Metric{699 "__name__": "apiserver_request_count",700 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",701 "code": "200",702 "resource": "events",703 "verb": "LIST",704 },705 Value: 3,706 },707 &model.Sample{708 Metric: model.Metric{709 "__name__": "apiserver_request_count",710 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",711 "code": "200",712 "resource": "events",713 "verb": "WATCHLIST",714 },715 Value: 10,716 },717 &model.Sample{718 Metric: model.Metric{719 "__name__": "apiserver_request_count",720 "client": "e2e.test/v1.2.0 (linux/amd64) kubernetes/ba8efbd",721 "code": "200",722 "resource": "nodes",723 "verb": "LIST",724 },725 Value: 3,726 },727 },728 },729 ControllerManagerMetrics: metrics.ControllerManagerMetrics{730 "etcd_helper_cache_entry_count": model.Samples{731 &model.Sample{732 Metric: model.Metric{733 "__name__": "etcd_helper_cache_entry_count",734 },735 Value: 0,736 },737 },738 "etcd_helper_cache_hit_count": model.Samples{739 &model.Sample{740 Metric: model.Metric{741 "__name__": "etcd_helper_cache_hit_count",742 },743 Value: 0,744 },745 },746 "etcd_helper_cache_miss_count": model.Samples{747 &model.Sample{748 Metric: model.Metric{749 "__name__": "etcd_helper_cache_miss_count",750 },751 Value: 0,752 },753 },754 "etcd_request_cache_add_latencies_summary": model.Samples{755 &model.Sample{756 Metric: model.Metric{757 "__name__": "etcd_request_cache_add_latencies_summary",758 "quantile": "0.5",759 },760 Value: model.SampleValue(math.NaN()),761 },762 &model.Sample{763 Metric: model.Metric{764 "__name__": "etcd_request_cache_add_latencies_summary",765 "quantile": "0.9",766 },767 Value: model.SampleValue(math.NaN()),768 },769 &model.Sample{770 Metric: model.Metric{771 "__name__": "etcd_request_cache_add_latencies_summary",772 "quantile": "0.99",773 },774 Value: model.SampleValue(math.NaN()),775 },776 },777 },778 KubeletMetrics: map[string]metrics.KubeletMetrics{779 "e2e-test-master": {780 "go_gc_duration_seconds": model.Samples{781 &model.Sample{782 Metric: model.Metric{783 "__name__": "go_gc_duration_seconds",784 "quantile": "0",785 },786 Value: 0.000158288,787 },788 &model.Sample{789 Metric: model.Metric{790 "__name__": "go_gc_duration_seconds",791 "quantile": "0.25",792 },793 Value: 0.002003092,794 },795 &model.Sample{796 Metric: model.Metric{797 "__name__": "go_gc_duration_seconds",798 "quantile": "0.5",799 },800 Value: 0.012483379,801 },802 &model.Sample{803 Metric: model.Metric{804 "__name__": "go_gc_duration_seconds",805 "quantile": "0.75",806 },807 Value: 0.102912684,808 },809 &model.Sample{810 Metric: model.Metric{811 "__name__": "go_gc_duration_seconds",812 "quantile": "1",813 },814 Value: 0.012803119,815 },816 },817 "go_goroutines": model.Samples{818 &model.Sample{819 Metric: model.Metric{820 "__name__": "go_goroutines",821 },822 Value: 119,823 },824 },825 },826 "e2e-test-minion": {827 "go_gc_duration_seconds": model.Samples{828 &model.Sample{829 Metric: model.Metric{830 "__name__": "go_gc_duration_seconds",831 "quantile": "0",832 },833 Value: 0.001158288,834 },835 &model.Sample{836 Metric: model.Metric{837 "__name__": "go_gc_duration_seconds",838 "quantile": "0.25",839 },840 Value: 0.012003092,841 },842 &model.Sample{843 Metric: model.Metric{844 "__name__": "go_gc_duration_seconds",845 "quantile": "0.5",846 },847 Value: 0.012483379,848 },849 &model.Sample{850 Metric: model.Metric{851 "__name__": "go_gc_duration_seconds",852 "quantile": "0.75",853 },854 Value: 0.012912684,855 },856 &model.Sample{857 Metric: model.Metric{858 "__name__": "go_gc_duration_seconds",859 "quantile": "1",860 },861 Value: 0.022803119,862 },863 },864 "go_goroutines": model.Samples{865 &model.Sample{866 Metric: model.Metric{867 "__name__": "go_goroutines",868 },869 Value: 519,870 },871 },872 },873 },874 SchedulerMetrics: metrics.SchedulerMetrics{875 "get_token_count": model.Samples{},876 "get_token_fail_count": model.Samples{},877 "go_gc_duration_seconds": model.Samples{878 &model.Sample{879 Metric: model.Metric{880 "__name__": "go_gc_duration_seconds",881 "quantile": "0",882 },883 Value: 0.00014963700000000002,884 },885 &model.Sample{886 Metric: model.Metric{887 "__name__": "go_gc_duration_seconds",888 "quantile": "0.25",889 },890 Value: 0.0005699380000000001,891 },892 &model.Sample{893 Metric: model.Metric{894 "__name__": "go_gc_duration_seconds",895 "quantile": "0.5",896 },897 Value: 0.001986198,898 },899 &model.Sample{900 Metric: model.Metric{901 "__name__": "go_gc_duration_seconds",902 "quantile": "0.75",903 },904 Value: 0.103013346,905 },906 &model.Sample{907 Metric: model.Metric{908 "__name__": "go_gc_duration_seconds",909 "quantile": "1",910 },911 Value: 0.007081308000000001,912 },913 },914 },915 }916 expected := ViolatingMetricsArr{917 "apiserver_request_count": []ViolatingMetric{918 {919 labels: "client=e2e.test, code=200, resource=events, verb=LIST",920 component: "ApiServer",...
e2e_metrics.go
Source:e2e_metrics.go
...45 (*m).APIServerMetrics = apiServerMetrics46 (*m).ControllerManagerMetrics = controllerManagerMetrics47 (*m).KubeletMetrics = kubeletMetrics48}49func printSample(sample *model.Sample) string {50 buf := make([]string, 0)51 // Id is a VERY special label. For 'normal' container it's useless, but it's necessary52 // for 'system' containers (e.g. /docker-daemon, /kubelet, etc.). We know if that's the53 // case by checking if there's a label "kubernetes_container_name" present. It's hacky54 // but it works...55 _, normalContainer := sample.Metric["kubernetes_container_name"]56 for k, v := range sample.Metric {57 if strings.HasPrefix(string(k), "__") {58 continue59 }60 if string(k) == "id" && normalContainer {61 continue62 }63 buf = append(buf, fmt.Sprintf("%v=%v", string(k), v))64 }65 return fmt.Sprintf("[%v] = %v", strings.Join(buf, ","), sample.Value)66}67// PrintHumanReadable returns e2e metrics with JSON format.68func (m *MetricsForE2E) PrintHumanReadable() string {69 buf := bytes.Buffer{}70 for _, interestingMetric := range interestingAPIServerMetrics {71 buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))72 for _, sample := range (*m).APIServerMetrics[interestingMetric] {73 buf.WriteString(fmt.Sprintf("\t%v\n", printSample(sample)))74 }75 }76 for _, interestingMetric := range interestingControllerManagerMetrics {77 buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))78 for _, sample := range (*m).ControllerManagerMetrics[interestingMetric] {79 buf.WriteString(fmt.Sprintf("\t%v\n", printSample(sample)))80 }81 }82 for _, interestingMetric := range interestingClusterAutoscalerMetrics {83 buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))84 for _, sample := range (*m).ClusterAutoscalerMetrics[interestingMetric] {85 buf.WriteString(fmt.Sprintf("\t%v\n", printSample(sample)))86 }87 }88 for kubelet, grabbed := range (*m).KubeletMetrics {89 buf.WriteString(fmt.Sprintf("For %v:\n", kubelet))90 for _, interestingMetric := range interestingKubeletMetrics {91 buf.WriteString(fmt.Sprintf("\tFor %v:\n", interestingMetric))92 for _, sample := range grabbed[interestingMetric] {93 buf.WriteString(fmt.Sprintf("\t\t%v\n", printSample(sample)))94 }95 }96 }97 return buf.String()98}99// PrettyPrintJSON converts metrics to JSON format.100func PrettyPrintJSON(metrics interface{}) string {101 output := &bytes.Buffer{}102 if err := json.NewEncoder(output).Encode(metrics); err != nil {103 e2elog.Logf("Error building encoder: %v", err)104 return ""105 }106 formatted := &bytes.Buffer{}107 if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil {108 e2elog.Logf("Error indenting: %v", err)109 return ""110 }111 return string(formatted.Bytes())112}113// PrintJSON returns e2e metrics with JSON format.114func (m *MetricsForE2E) PrintJSON() string {115 m.filterMetrics()116 return PrettyPrintJSON(m)117}118// SummaryKind returns the summary of e2e metrics.119func (m *MetricsForE2E) SummaryKind() string {120 return "MetricsForE2E"121}122func makeKey(a, b model.LabelValue) string {123 return string(a) + "___" + string(b)124}125// ComputeClusterAutoscalerMetricsDelta computes the change in cluster126// autoscaler metrics.127func (m *MetricsForE2E) ComputeClusterAutoscalerMetricsDelta(before Collection) {128 if beforeSamples, found := before.ClusterAutoscalerMetrics[caFunctionMetric]; found {129 if afterSamples, found := m.ClusterAutoscalerMetrics[caFunctionMetric]; found {130 beforeSamplesMap := make(map[string]*model.Sample)131 for _, bSample := range beforeSamples {132 beforeSamplesMap[makeKey(bSample.Metric[caFunctionMetricLabel], bSample.Metric["le"])] = bSample133 }134 for _, aSample := range afterSamples {135 if bSample, found := beforeSamplesMap[makeKey(aSample.Metric[caFunctionMetricLabel], aSample.Metric["le"])]; found {136 aSample.Value = aSample.Value - bSample.Value137 }138 }139 }140 }141}...
multi_header_merger.go
Source:multi_header_merger.go
...10type MultiHeaderMerger struct {11 bitflow.NoopProcessor12 header *bitflow.Header13 metrics map[string][]bitflow.Value14 samples []*bitflow.SampleMetadata15}16func NewMultiHeaderMerger() *MultiHeaderMerger {17 return &MultiHeaderMerger{18 metrics: make(map[string][]bitflow.Value),19 }20}21func RegisterMergeHeaders(b reg.ProcessorRegistry) {22 b.RegisterStep("merge_headers",23 func(p *bitflow.SamplePipeline, _ map[string]interface{}) error {24 p.Add(NewMultiHeaderMerger())25 return nil26 },27 "Accept any number of changing headers and merge them into one output header when flushing the results")28}29func (p *MultiHeaderMerger) Sample(sample *bitflow.Sample, header *bitflow.Header) error {30 p.addSample(sample, header)31 return nil32}33func (p *MultiHeaderMerger) addSample(incomingSample *bitflow.Sample, header *bitflow.Header) {34 handledMetrics := make(map[string]bool, len(header.Fields))35 for i, field := range header.Fields {36 metrics, ok := p.metrics[field]37 if !ok {38 metrics = make([]bitflow.Value, len(p.samples)) // Filled up with zeroes39 }40 p.metrics[field] = append(metrics, incomingSample.Values[i])41 handledMetrics[field] = true42 }43 for field := range p.metrics {44 if ok := handledMetrics[field]; !ok {45 p.metrics[field] = append(p.metrics[field], 0) // Filled up with zeroes46 }47 }48 p.samples = append(p.samples, incomingSample.Metadata())49}50func (p *MultiHeaderMerger) Close() {51 defer p.CloseSink()52 defer func() {53 // Allow garbage collection54 p.metrics = nil55 p.samples = nil56 }()57 if len(p.samples) == 0 {58 log.Warnln(p.String(), "has no samples stored")59 return60 }61 log.Println(p, "reconstructing and flushing", len(p.samples), "samples with", len(p.metrics), "metrics")62 outHeader := p.reconstructHeader()63 for index := range p.samples {64 outSample := p.reconstructSample(index, outHeader)65 if err := p.NoopProcessor.Sample(outSample, outHeader); err != nil {66 err = fmt.Errorf("Error flushing reconstructed samples: %v", err)67 log.Errorln(err)68 p.Error(err)69 return70 }71 }72}73func (p *MultiHeaderMerger) OutputSampleSize(sampleSize int) int {74 if len(p.metrics) > sampleSize {75 sampleSize = len(p.metrics)76 }77 return sampleSize78}79func (p *MultiHeaderMerger) reconstructHeader() *bitflow.Header {80 fields := make([]string, 0, len(p.metrics))81 for field := range p.metrics {82 fields = append(fields, field)83 }84 sort.Strings(fields)85 return &bitflow.Header{Fields: fields}86}87func (p *MultiHeaderMerger) reconstructSample(num int, header *bitflow.Header) *bitflow.Sample {88 values := make([]bitflow.Value, len(p.metrics))89 for i, field := range header.Fields {90 slice := p.metrics[field]91 if len(slice) != len(p.samples) {92 // Should never happen93 panic(fmt.Sprintf("Have %v values for field %v, should be %v", len(slice), field, len(p.samples)))94 }95 values[i] = slice[num]96 }97 return p.samples[num].NewSample(values)98}99func (p *MultiHeaderMerger) String() string {100 return "MultiHeaderMerger"101}...
Sample
Using AI Code Generation
1import (2func main() {3 histogram := promauto.NewHistogram(prometheus.HistogramOpts{4 Buckets: prometheus.LinearBuckets(0.1, 0.1, 10),5 })6 counter := promauto.NewCounter(prometheus.CounterOpts{7 })8 gauge := promauto.NewGauge(prometheus.GaugeOpts{9 })10 summary := promauto.NewSummary(prometheus.SummaryOpts{11 })12 gauge.Set(float64(runtime.NumGoroutine()))13 counter.Inc()14 summary.Observe(42)15 histogram.Observe(0.75)16 time.Sleep(1 * time.Second)17 histogram.Observe(0.25)18 time.Sleep(1 * time.Second)19 histogram.Observe(0.1)20 time.Sleep(1 * time.Second)21 histogram.Observe(0.05)22 time.Sleep(1 * time.Second)23 histogram.Observe(0.01)24 time.Sleep(1 * time.Second)25 histogram.Observe(0.001)26 time.Sleep(1 * time.Second)27 histogram.Observe(0.0001
Sample
Using AI Code Generation
1import (2func main() {3 r := metrics.NewRegistry()4 counter := metrics.NewCounter()5 r.Register("counter", counter)6 go metrics.Log(r, time.Second, log.New(os.Stdout, "metrics: ", log.Lmicroseconds))7 go metrics.CaptureRuntimeMemStats(r, time.Second)8 for {9 counter.Inc(1)10 time.Sleep(time.Second)11 }12}
Sample
Using AI Code Generation
1import (2func main() {3 timer := metrics.NewTimer()4 sample := metrics.NewUniformSample(1028)5 rand.Seed(time.Now().UnixNano())6 for i := 0; i < 100; i++ {7 timer.Update(time.Duration(rand.Intn(1000)) * time.Millisecond)8 }9 fmt.Println("Count:", timer.Count())10 fmt.Println("Max:", timer.Max())11 fmt.Println("Mean:", timer.Mean())12 fmt.Println("Min:", timer.Min())13 fmt.Println("StdDev:", timer.StdDev())14 fmt.Println("Variance:", timer.Variance())15 fmt.Println("Percentiles:", timer.Percentiles([]float64{0.5, 0.95, 0.99}))16}
Sample
Using AI Code Generation
1import (2func main() {3 s := metrics.NewEWMA1()4 for i := 0; i < 1000000; i++ {5 s.Update(1)6 }7 time.Sleep(5 * time.Minute)8 s.Update(1)
Sample
Using AI Code Generation
1import (2func main() {3 r := metrics.NewRegistry()4 m := metrics.NewMeter()5 r.Register("meter", m)6 t := metrics.NewTimer()7 r.Register("timer", t)8 h := metrics.NewHistogram(metrics.NewUniformSample(1028))9 r.Register("histogram", h)10 c := metrics.NewCounter()11 r.Register("counter", c)12 g := metrics.NewGauge()13 r.Register("gauge", g)14 for i := 0; i < 100; i++ {15 m.Mark(1)16 }17 for i := 0; i < 100; i++ {18 t.Time(func() { time.Sleep(1 * time.Millisecond) })19 }20 for i := 0; i < 100; i++ {21 h.Update(1)22 }23 for i := 0; i < 100; i++ {24 c.Inc(1)25 }26 for i := 0; i < 100; i++ {27 g.Update(1)28 }29 fmt.Println(r)30}31{counter 100 100 100 100 0 0 0 0 0 0}32{gauge 100 100 100 100 0 0 0 0 0 0}33{histogram 100 100 100 100 0 0 0 0 0 0}34{meter 100 100 100 100 0 0 0
Sample
Using AI Code Generation
1import (2func main() {3 r := metrics.NewRegistry()4 h := metrics.NewHistogram(metrics.NewUniformSample(100))5 r.Register("myapp.histogram", h)6 h.Update(1)7 s := h.Snapshot()8 fmt.Println(s.Sample())9 fmt.Println(s.Count())10 fmt.Println(s.Mean())11 fmt.Println(s.Variance())12 fmt.Println(s.StdDev())13 fmt.Println(s.Min())14 fmt.Println(s.Max())15 fmt.Println(s.Median())16 fmt.Println(s.Percentile(0.75))17 fmt.Println(s.Percentile(0.95))18 fmt.Println(s.Percentile(0.99))19 fmt.Println(s.Percentile(0.999))20 fmt.Println(s.Percentile(0.9999))21}22import (23func main() {24 r := metrics.NewRegistry()25 h := metrics.NewHistogram(metrics.NewUniformSample(100))26 r.Register("myapp.histogram", h)
Sample
Using AI Code Generation
1func main() {2 metrics.Sample("some metric")3 metrics.Sample("another metric")4 metrics.Sample("some metric")5 metrics.Sample("some metric")6 metrics.Sample("some metric")7 metrics.Sample("another metric")8 metrics.Sample("another metric")
Sample
Using AI Code Generation
1import "metrics"2func main() {3 m := metrics.New()4 c := m.NewCounter("my_counter")5 c.Incr(1)6 c.Decr(1)7 c.Set(100)8 fmt.Println(c.Get())9}10import "metrics"11func main() {12 m := metrics.New()13 g := m.NewGauge("my_gauge")14 g.Set(100)15 fmt.Println(g.Get())16}17import "metrics"18func main() {19 m := metrics.New()20 t := m.NewTimer("my_timer")21 t.Start()22 t.Stop()23 fmt.Println(t.Get())24}25import "metrics"26func main() {27 m := metrics.New()28 h := m.NewHistogram("my_histogram")29 h.Add(10)30 fmt.Println(h.Get())31}32import "metrics"33func main() {34 m := metrics.New()35 mm := m.NewMeter("my_meter")36 mm.Mark(1)37 fmt.Println(mm.Get())38}
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!!