generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.html
6218 lines (4972 loc) · 219 KB
/
coverage.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>agent: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/ex0rcist/metflix/cmd/agent/main.go (0.0%)</option>
<option value="file1">github.com/ex0rcist/metflix/cmd/server/main.go (0.0%)</option>
<option value="file2">github.com/ex0rcist/metflix/cmd/staticlint/main.go (0.0%)</option>
<option value="file3">github.com/ex0rcist/metflix/docs/api/docs.go (100.0%)</option>
<option value="file4">github.com/ex0rcist/metflix/internal/agent/agent.go (69.7%)</option>
<option value="file5">github.com/ex0rcist/metflix/internal/agent/config.go (61.5%)</option>
<option value="file6">github.com/ex0rcist/metflix/internal/agent/exporter/exporter.go (42.9%)</option>
<option value="file7">github.com/ex0rcist/metflix/internal/agent/exporter/grpc.go (62.7%)</option>
<option value="file8">github.com/ex0rcist/metflix/internal/agent/exporter/http.go (67.0%)</option>
<option value="file9">github.com/ex0rcist/metflix/internal/agent/exporter/http_batch.go (64.1%)</option>
<option value="file10">github.com/ex0rcist/metflix/internal/agent/runtime.go (100.0%)</option>
<option value="file11">github.com/ex0rcist/metflix/internal/agent/stats.go (92.9%)</option>
<option value="file12">github.com/ex0rcist/metflix/internal/agent/system.go (83.3%)</option>
<option value="file13">github.com/ex0rcist/metflix/internal/compression/compression.go (66.7%)</option>
<option value="file14">github.com/ex0rcist/metflix/internal/compression/compressor.go (78.9%)</option>
<option value="file15">github.com/ex0rcist/metflix/internal/compression/decompressor.go (96.0%)</option>
<option value="file16">github.com/ex0rcist/metflix/internal/entities/address.go (100.0%)</option>
<option value="file17">github.com/ex0rcist/metflix/internal/entities/errors.go (0.0%)</option>
<option value="file18">github.com/ex0rcist/metflix/internal/entities/filepath.go (100.0%)</option>
<option value="file19">github.com/ex0rcist/metflix/internal/entities/secret.go (100.0%)</option>
<option value="file20">github.com/ex0rcist/metflix/internal/grpcserver/backend.go (91.3%)</option>
<option value="file21">github.com/ex0rcist/metflix/internal/grpcserver/converters.go (93.5%)</option>
<option value="file22">github.com/ex0rcist/metflix/internal/grpcserver/health.go (100.0%)</option>
<option value="file23">github.com/ex0rcist/metflix/internal/grpcserver/interceptors/helpers.go (70.0%)</option>
<option value="file24">github.com/ex0rcist/metflix/internal/grpcserver/interceptors/logging.go (100.0%)</option>
<option value="file25">github.com/ex0rcist/metflix/internal/grpcserver/interceptors/subnet.go (30.0%)</option>
<option value="file26">github.com/ex0rcist/metflix/internal/grpcserver/metrics.go (87.0%)</option>
<option value="file27">github.com/ex0rcist/metflix/internal/httpserver/backend.go (78.9%)</option>
<option value="file28">github.com/ex0rcist/metflix/internal/httpserver/converters.go (92.3%)</option>
<option value="file29">github.com/ex0rcist/metflix/internal/httpserver/health.go (100.0%)</option>
<option value="file30">github.com/ex0rcist/metflix/internal/httpserver/metric.go (77.1%)</option>
<option value="file31">github.com/ex0rcist/metflix/internal/httpserver/middleware/compression.go (96.8%)</option>
<option value="file32">github.com/ex0rcist/metflix/internal/httpserver/middleware/crypto.go (100.0%)</option>
<option value="file33">github.com/ex0rcist/metflix/internal/httpserver/middleware/request_logger.go (26.7%)</option>
<option value="file34">github.com/ex0rcist/metflix/internal/httpserver/middleware/signer.go (77.1%)</option>
<option value="file35">github.com/ex0rcist/metflix/internal/httpserver/middleware/subnet.go (100.0%)</option>
<option value="file36">github.com/ex0rcist/metflix/internal/logging/debug.go (100.0%)</option>
<option value="file37">github.com/ex0rcist/metflix/internal/logging/error.go (88.9%)</option>
<option value="file38">github.com/ex0rcist/metflix/internal/logging/fatal.go (0.0%)</option>
<option value="file39">github.com/ex0rcist/metflix/internal/logging/info.go (100.0%)</option>
<option value="file40">github.com/ex0rcist/metflix/internal/logging/logging.go (77.4%)</option>
<option value="file41">github.com/ex0rcist/metflix/internal/logging/warn.go (100.0%)</option>
<option value="file42">github.com/ex0rcist/metflix/internal/profiler/profiler.go (15.8%)</option>
<option value="file43">github.com/ex0rcist/metflix/internal/retrier/retrier.go (100.0%)</option>
<option value="file44">github.com/ex0rcist/metflix/internal/security/crypto.go (70.6%)</option>
<option value="file45">github.com/ex0rcist/metflix/internal/security/signer.go (76.9%)</option>
<option value="file46">github.com/ex0rcist/metflix/internal/server/config.go (63.9%)</option>
<option value="file47">github.com/ex0rcist/metflix/internal/server/grpc_server.go (15.4%)</option>
<option value="file48">github.com/ex0rcist/metflix/internal/server/http_server.go (22.2%)</option>
<option value="file49">github.com/ex0rcist/metflix/internal/server/profiler_server.go (41.7%)</option>
<option value="file50">github.com/ex0rcist/metflix/internal/server/server.go (31.9%)</option>
<option value="file51">github.com/ex0rcist/metflix/internal/services/health.go (100.0%)</option>
<option value="file52">github.com/ex0rcist/metflix/internal/services/metric.go (88.5%)</option>
<option value="file53">github.com/ex0rcist/metflix/internal/storage/file.go (68.3%)</option>
<option value="file54">github.com/ex0rcist/metflix/internal/storage/memory.go (93.8%)</option>
<option value="file55">github.com/ex0rcist/metflix/internal/storage/postgres.go (56.2%)</option>
<option value="file56">github.com/ex0rcist/metflix/internal/storage/postgres_migrator.go (0.0%)</option>
<option value="file57">github.com/ex0rcist/metflix/internal/storage/record.go (95.7%)</option>
<option value="file58">github.com/ex0rcist/metflix/internal/storage/storage.go (0.0%)</option>
<option value="file59">github.com/ex0rcist/metflix/internal/utils/file.go (0.0%)</option>
<option value="file60">github.com/ex0rcist/metflix/internal/utils/http.go (100.0%)</option>
<option value="file61">github.com/ex0rcist/metflix/internal/utils/net.go (0.0%)</option>
<option value="file62">github.com/ex0rcist/metflix/internal/utils/request_id.go (100.0%)</option>
<option value="file63">github.com/ex0rcist/metflix/internal/utils/time.go (100.0%)</option>
<option value="file64">github.com/ex0rcist/metflix/internal/validators/validators.go (100.0%)</option>
<option value="file65">github.com/ex0rcist/metflix/models/metrics_metric_exchange.go (0.0%)</option>
<option value="file66">github.com/ex0rcist/metflix/pkg/grpcapi/grpcapi.go (100.0%)</option>
<option value="file67">github.com/ex0rcist/metflix/pkg/metrics/metrics.go (100.0%)</option>
<option value="file68">github.com/ex0rcist/metflix/pkg/metrics/types.go (100.0%)</option>
<option value="file69">github.com/ex0rcist/metflix/pkg/noexit/analyzer.go (97.2%)</option>
<option value="file70">github.com/ex0rcist/metflix/pkg/staticlint/staticlint.go (0.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package main
import (
"fmt"
"github.com/ex0rcist/metflix/internal/agent"
"github.com/ex0rcist/metflix/internal/logging"
)
var (
buildVersion = "N/A"
buildDate = "N/A"
buildCommit = "N/A"
)
func main() <span class="cov0" title="0">{
logging.Setup()
fmt.Printf("Build version: %s\nBuild date: %s\nBuild commit: %s\n", buildVersion, buildDate, buildCommit)
agnt, err := agent.New()
if err != nil </span><span class="cov0" title="0">{
logging.LogFatal(err)
}</span>
<span class="cov0" title="0">err = agnt.Run()
if err != nil </span><span class="cov0" title="0">{
logging.LogFatal(err)
}</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package main
import (
"fmt"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/server"
)
var (
buildVersion = "N/A"
buildDate = "N/A"
buildCommit = "N/A"
)
func main() <span class="cov0" title="0">{
logging.Setup()
fmt.Printf("Build version: %s\nBuild date: %s\nBuild commit: %s\n", buildVersion, buildDate, buildCommit)
srv, err := server.New()
if err != nil </span><span class="cov0" title="0">{
logging.LogFatal(err)
}</span>
<span class="cov0" title="0">srv.Start()</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package main
import "github.com/ex0rcist/metflix/pkg/staticlint"
func main() <span class="cov0" title="0">{
lint := staticlint.New()
lint.Run()
}</span>
</pre>
<pre class="file" id="file3" style="display: none">// Package api Code generated by swaggo/swag. DO NOT EDIT
package api
import "github.com/swaggo/swag"
const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Evgeniy Shuvalov",
"email": "evshuvalov@yandex.ru"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/": {
"get": {
"produces": [
"text/html"
],
"tags": [
"Metrics"
],
"summary": "Yet another homepage",
"operationId": "homepage",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/ping": {
"get": {
"tags": [
"Healthcheck"
],
"summary": "Verify server up and running",
"operationId": "health_info",
"responses": {
"200": {
"description": "OK"
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
},
"501": {
"description": "Not Implemented",
"schema": {
"type": "string"
}
}
}
}
},
"/update": {
"post": {
"consumes": [
"application/json"
],
"tags": [
"Metrics"
],
"summary": "Push metric data as JSON",
"operationId": "metrics_json_update",
"parameters": [
{
"description": "Request parameters.",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/metrics.MetricExchange"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/metrics.MetricExchange"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/update/{type}/{name}/{value}": {
"post": {
"produces": [
"text/plain"
],
"tags": [
"Metrics"
],
"summary": "Push metric data.",
"operationId": "metrics_update",
"parameters": [
{
"type": "string",
"description": "Metrics type (e.g. ` + "`" + `counter` + "`" + `, ` + "`" + `gauge` + "`" + `).",
"name": "type",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Metrics name.",
"name": "name",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Metrics value, must be convertable to ` + "`" + `int64` + "`" + ` or ` + "`" + `float64` + "`" + `.",
"name": "value",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/updates": {
"post": {
"consumes": [
"application/json"
],
"tags": [
"Metrics"
],
"summary": "Push list of metrics data as JSON",
"operationId": "metrics_json_update_list",
"parameters": [
{
"description": "List of metrics to update.",
"name": "request",
"in": "body",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/metrics.MetricExchange"
}
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/metrics.MetricExchange"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/value": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Metrics"
],
"summary": "Get metrics value as JSON",
"operationId": "metrics_json_info",
"parameters": [
{
"description": "Request parameters: ` + "`" + `id` + "`" + ` and ` + "`" + `type` + "`" + ` are required.",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/metrics.MetricExchange"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/metrics.MetricExchange"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"404": {
"description": "Not Found",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/value/{type}/{name}": {
"get": {
"produces": [
"text/plain"
],
"tags": [
"Metrics"
],
"summary": "Get metric's value as string",
"operationId": "metrics_info",
"parameters": [
{
"type": "string",
"description": "Metrics type (e.g. ` + "`" + `counter` + "`" + `, ` + "`" + `gauge` + "`" + `).",
"name": "type",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Metrics name.",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"404": {
"description": "Not Found",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"metrics.MetricExchange": {
"type": "object",
"properties": {
"delta": {
"type": "integer"
},
"id": {
"type": "string"
},
"type": {
"type": "string"
},
"value": {
"type": "number"
}
}
}
},
"tags": [
{
"description": "\"Metrics API\"",
"name": "Metrics"
},
{
"description": "\"API to inspect service health state\"",
"name": "Healthcheck"
}
]
}`
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "Metrics collector API",
Description: "Service for storing metrics data.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}
func init() <span class="cov10" title="2">{
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package agent
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/ex0rcist/metflix/internal/agent/exporter"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/security"
"github.com/ex0rcist/metflix/internal/utils"
)
const shutdownTimeout = 60 * time.Second
// Metric collecting agent (mr. Bond?).
type Agent struct {
Config *Config
Stats *Stats
Exporter exporter.Exporter
interrupt chan os.Signal
wg sync.WaitGroup
}
// Constructor.
func New() (*Agent, error) <span class="cov1" title="1">{
config, err := NewConfig()
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">return &Agent{
Config: config,
Stats: NewStats(),
interrupt: make(chan os.Signal, 1),
}, nil</span>
}
// Run agent.
func (a *Agent) Run() error <span class="cov1" title="1">{
logging.LogInfo(a.Config.String())
logging.LogInfo("agent ready")
ctx, cancelBackgroundTasks := context.WithCancel(context.Background())
defer cancelBackgroundTasks()
publicKey, err := preparePublicKey(a.Config.PublicKeyPath)
if err != nil </span><span class="cov0" title="0">{
logging.LogInfo("error making public key")
}</span>
<span class="cov1" title="1">var signer security.Signer
if len(a.Config.Secret) > 0 </span><span class="cov0" title="0">{
signer = security.NewSignerService(a.Config.Secret)
}</span>
<span class="cov1" title="1">exporter, err := exporter.New(ctx, a.Config.Transport, &a.Config.Address, a.Config.RateLimit, signer, publicKey)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">a.Exporter = exporter
signal.Notify(a.interrupt, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
a.wg.Add(2)
go func() </span><span class="cov1" title="1">{
defer a.wg.Done()
a.startPolling(ctx)
}</span>()
<span class="cov1" title="1">go func() </span><span class="cov1" title="1">{
defer a.wg.Done()
a.startReporting(ctx)
}</span>()
<span class="cov1" title="1"><-a.interrupt
logging.LogInfo("shutting down agent...")
cancelBackgroundTasks()
stopped := make(chan struct{})
stopCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
go func() </span><span class="cov1" title="1">{
defer close(stopped)
a.wg.Wait()
}</span>()
<span class="cov1" title="1">select </span>{
case <-stopped:<span class="cov1" title="1">
logging.LogInfo("agent shutdown successful")</span>
case <-stopCtx.Done():<span class="cov0" title="0">
logging.LogWarn("exceeded shutdown timeout, force exit")</span>
}
<span class="cov1" title="1">return nil</span>
}
// Shutdown agent
func (a *Agent) Shutdown() <span class="cov1" title="1">{
a.interrupt <- os.Interrupt
}</span>
func (a *Agent) startPolling(ctx context.Context) <span class="cov1" title="1">{
ticker := time.NewTicker(utils.IntToDuration(a.Config.PollInterval))
defer ticker.Stop()
for </span><span class="cov10" title="2">{
select </span>{
case <-ticker.C:<span class="cov1" title="1">
func() </span><span class="cov1" title="1">{
err := a.Stats.Poll(ctx)
if err != nil </span><span class="cov0" title="0">{
logging.LogError(err)
}</span>
}()
case <-ctx.Done():<span class="cov1" title="1">
logging.LogInfo("shutting down metrics polling")
return</span>
}
}
}
func (a *Agent) startReporting(ctx context.Context) <span class="cov1" title="1">{
ticker := time.NewTicker(utils.IntToDuration(a.Config.ReportInterval))
defer ticker.Stop()
for </span><span class="cov1" title="1">{
select </span>{
case <-ticker.C:<span class="cov0" title="0">
a.reportStats()</span>
case <-ctx.Done():<span class="cov1" title="1">
logging.LogInfo("shutting down metrics reporting")
return</span>
}
}
}
func (a *Agent) reportStats() <span class="cov0" title="0">{
logging.LogInfo("reporting stats ... ")
snapshot := *a.Stats
a.Exporter.
Add("Alloc", snapshot.Runtime.Alloc).
Add("BuckHashSys", snapshot.Runtime.BuckHashSys).
Add("Frees", snapshot.Runtime.Frees).
Add("GCCPUFraction", snapshot.Runtime.GCCPUFraction).
Add("GCSys", snapshot.Runtime.GCSys).
Add("HeapAlloc", snapshot.Runtime.HeapAlloc).
Add("HeapIdle", snapshot.Runtime.HeapIdle).
Add("HeapInuse", snapshot.Runtime.HeapInuse).
Add("HeapObjects", snapshot.Runtime.HeapObjects).
Add("HeapReleased", snapshot.Runtime.HeapReleased).
Add("HeapSys", snapshot.Runtime.HeapSys).
Add("LastGC", snapshot.Runtime.LastGC).
Add("Lookups", snapshot.Runtime.Lookups).
Add("MCacheInuse", snapshot.Runtime.MCacheInuse).
Add("MCacheSys", snapshot.Runtime.MCacheSys).
Add("MSpanInuse", snapshot.Runtime.MSpanInuse).
Add("MSpanSys", snapshot.Runtime.MSpanSys).
Add("Mallocs", snapshot.Runtime.Mallocs).
Add("NextGC", snapshot.Runtime.NextGC).
Add("NumForcedGC", snapshot.Runtime.NumForcedGC).
Add("NumGC", snapshot.Runtime.NumGC).
Add("OtherSys", snapshot.Runtime.OtherSys).
Add("PauseTotalNs", snapshot.Runtime.PauseTotalNs).
Add("StackInuse", snapshot.Runtime.StackInuse).
Add("StackSys", snapshot.Runtime.StackSys).
Add("Sys", snapshot.Runtime.Sys).
Add("TotalAlloc", snapshot.Runtime.TotalAlloc)
a.Exporter.
Add("RandomValue", snapshot.RandomValue)
a.Exporter.
Add("PollCount", snapshot.PollCount)
a.Exporter.
Add("FreeMemory", snapshot.System.FreeMemory).
Add("TotalMemory", snapshot.System.TotalMemory)
for i, u := range snapshot.System.CPUutilization </span><span class="cov0" title="0">{
a.Exporter.Add(fmt.Sprintf("CPUutilization%d", i+1), u)
}</span>
<span class="cov0" title="0">err := a.Exporter.Send()
if err != nil </span><span class="cov0" title="0">{
logging.LogErrorF("error sending metrics: %w", err)
}</span>
<span class="cov0" title="0">a.Exporter.Reset()
// because metrics.Counter adds value to itself
a.Stats.PollCount -= snapshot.PollCount</span>
}
func preparePublicKey(path entities.FilePath) (security.PublicKey, error) <span class="cov1" title="1">{
var (
publicKey security.PublicKey
err error
)
if len(path) > 0 </span><span class="cov0" title="0">{
publicKey, err = security.NewPublicKey(path)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
<span class="cov1" title="1">return publicKey, err</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package agent
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/caarlos0/env/v11"
"github.com/ex0rcist/metflix/internal/entities"
"github.com/spf13/pflag"
)
// Agent config.
type Config struct {
Address entities.Address `env:"ADDRESS" json:"address"`
Transport string `env:"TRANSPORT" json:"transport"`
PollInterval int `env:"POLL_INTERVAL" json:"poll_interval"`
ReportInterval int `env:"REPORT_INTERVAL" json:"report_interval"`
RateLimit int `env:"RATE_LIMIT" json:"-"`
Secret entities.Secret `env:"KEY" json:"key"`
PublicKeyPath entities.FilePath `env:"CRYPTO_KEY" json:"crypto_key"`
}
func NewConfig() (*Config, error) <span class="cov1" title="1">{
var err error
config := &Config{
Address: "0.0.0.0:8080",
Transport: entities.TransportHTTP,
PollInterval: 2,
ReportInterval: 10,
RateLimit: -1,
}
err = config.parse()
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">return config, err</span>
}
// Stringer
func (c Config) String() string <span class="cov1" title="1">{
str := []string{
fmt.Sprintf("address=%s", c.Address),
fmt.Sprintf("poll-interval=%v", c.PollInterval),
fmt.Sprintf("report-interval=%v", c.ReportInterval),
fmt.Sprintf("rate-limit=%v", c.RateLimit),
fmt.Sprintf("transport=%v", c.Transport),
}
if len(c.Secret) > 0 </span><span class="cov0" title="0">{
str = append(str, fmt.Sprintf("secret=%v", c.Secret))
}</span>
<span class="cov1" title="1">if len(c.PublicKeyPath) > 0 </span><span class="cov0" title="0">{
str = append(str, fmt.Sprintf("public-key=%v", c.PublicKeyPath))
}</span>
<span class="cov1" title="1">return "agent config: " + strings.Join(str, "; ")</span>
}
func (c *Config) parse() error <span class="cov1" title="1">{
err := c.tryLoadJSONConfig()
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">address := c.Address
pflag.VarP(&address, "address", "a", "address:port for HTTP API requests")
secret := c.Secret
pflag.VarP(&secret, "secret", "k", "a key to sign outgoing data")
publicKeyPath := c.PublicKeyPath
pflag.VarP(&publicKeyPath, "crypto-key", "", "path to public key to encrypt agent -> server communications")
configPath := entities.FilePath("") // register var for compatibility
pflag.VarP(&configPath, "config", "c", "path to configuration file in JSON format")
pflag.IntVarP(&c.PollInterval, "poll-interval", "p", c.PollInterval, "interval (s) for polling stats")
pflag.IntVarP(&c.ReportInterval, "report-interval", "r", c.ReportInterval, "interval (s) for polling stats")
pflag.IntVarP(&c.RateLimit, "rate-limit", "l", c.RateLimit, "number of max simultaneous requests to server")
pflag.StringVarP(&c.Transport, "transport", "t", c.Transport, "transport to use: http/grpc")
pflag.Parse()
pflag.Visit(func(f *pflag.Flag) </span><span class="cov0" title="0">{
switch f.Name </span>{
case "address":<span class="cov0" title="0">
c.Address = address</span>
case "secret":<span class="cov0" title="0">
c.Secret = secret</span>
case "crypto-key":<span class="cov0" title="0">
c.PublicKeyPath = publicKeyPath</span>
}
})
<span class="cov1" title="1">if err := env.Parse(c); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Config) tryLoadJSONConfig() error <span class="cov1" title="1">{
configArg := os.Getenv("CONFIG")