@@ -16,8 +16,6 @@ import (
16
16
"github.com/influxdata/telegraf/plugins/serializers/influx"
17
17
)
18
18
19
- var localhost = "localhost"
20
-
21
19
const (
22
20
DefaultDelta = 0.001
23
21
DefaultEpsilon = 0.1
@@ -26,21 +24,22 @@ const (
26
24
// GetLocalHost returns the DOCKER_HOST environment variable, parsing
27
25
// out any scheme or ports so that only the IP address is returned.
28
26
func GetLocalHost () string {
29
- if dockerHostVar := os .Getenv ("DOCKER_HOST" ); dockerHostVar != "" {
30
- u , err := url . Parse ( dockerHostVar )
31
- if err != nil {
32
- return dockerHostVar
33
- }
34
-
35
- // split out the ip addr from the port
36
- host , _ , err := net . SplitHostPort ( u . Host )
37
- if err != nil {
38
- return dockerHostVar
39
- }
40
-
41
- return host
27
+ dockerHostVar := os .Getenv ("DOCKER_HOST" )
28
+ if dockerHostVar == "" {
29
+ return "localhost"
30
+ }
31
+
32
+ u , err := url . Parse ( dockerHostVar )
33
+ if err != nil {
34
+ return dockerHostVar
35
+ }
36
+
37
+ host , _ , err := net . SplitHostPort ( u . Host )
38
+ if err != nil {
39
+ return dockerHostVar
42
40
}
43
- return localhost
41
+
42
+ return host
44
43
}
45
44
46
45
// GetRandomString returns a random alphanumerical string of the given length.
@@ -49,32 +48,23 @@ func GetLocalHost() string {
49
48
// host which might be drained e.g. in CI pipelines. This is useful to e.g.
50
49
// create random passwords for tests where security is not a concern.
51
50
func GetRandomString (chars int ) string {
52
- charset := []byte ("abcdefghijklmnopqrstABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
53
-
54
- nchars := len (charset )
51
+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
55
52
buffer := make ([]byte , chars )
56
- for i := range chars {
53
+ for i := range buffer {
57
54
//nolint:gosec // Using a weak random number generator on purpose to not drain entropy
58
- buffer [i ] = charset [rand .Intn (nchars )]
55
+ buffer [i ] = charset [rand .Intn (len ( charset ) )]
59
56
}
60
-
61
57
return string (buffer )
62
58
}
63
59
64
60
// MockMetrics returns a mock []telegraf.Metric object for using in unit tests
65
61
// of telegraf output sinks.
66
62
func MockMetrics () []telegraf.Metric {
67
- metrics := make ([]telegraf.Metric , 0 )
68
- // Create a new point batch
69
- metrics = append (metrics , TestMetric (1.0 ))
70
- return metrics
63
+ return []telegraf.Metric {TestMetric (1.0 )}
71
64
}
72
65
73
66
func MockMetricsWithValue (value float64 ) []telegraf.Metric {
74
- metrics := make ([]telegraf.Metric , 0 )
75
- // Create a new point batch
76
- metrics = append (metrics , TestMetric (value ))
77
- return metrics
67
+ return []telegraf.Metric {TestMetric (value )}
78
68
}
79
69
80
70
// TestMetric Returns a simple test point:
@@ -91,20 +81,21 @@ func TestMetric(value interface{}, name ...string) telegraf.Metric {
91
81
if len (name ) > 0 {
92
82
measurement = name [0 ]
93
83
}
94
- tags := map [ string ] string { "tag1" : "value1" }
95
- pt := metric .New (
84
+
85
+ return metric .New (
96
86
measurement ,
97
- tags ,
87
+ map [ string ] string { "tag1" : "value1" } ,
98
88
map [string ]interface {}{"value" : value },
99
89
time .Date (2009 , time .November , 10 , 23 , 0 , 0 , 0 , time .UTC ),
100
90
)
101
- return pt
102
91
}
103
92
104
93
// OnlyTags returns an option for keeping only "Tags" for a given Metric
105
94
func OnlyTags () cmp.Option {
106
- f := func (p cmp.Path ) bool { return p .String () != "Tags" && p .String () != "" }
107
- return cmp .FilterPath (f , cmp .Ignore ())
95
+ return cmp .FilterPath (func (p cmp.Path ) bool {
96
+ path := p .String ()
97
+ return path != "Tags" && path != ""
98
+ }, cmp .Ignore ())
108
99
}
109
100
110
101
func PrintMetrics (m []telegraf.Metric ) {
@@ -130,8 +121,5 @@ func DefaultSampleConfig(sampleConfig string) []byte {
130
121
}
131
122
132
123
func WithinDefaultDelta (dt float64 ) bool {
133
- if dt < - DefaultDelta || dt > DefaultDelta {
134
- return false
135
- }
136
- return true
124
+ return dt >= - DefaultDelta && dt <= DefaultDelta
137
125
}
0 commit comments