github.com/thanos-io/thanos@v0.32.5/pkg/targets/targets_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package targets 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/efficientgo/core/testutil" 11 "github.com/thanos-io/thanos/pkg/store/labelpb" 12 "github.com/thanos-io/thanos/pkg/targets/targetspb" 13 ) 14 15 func TestDedupTargets(t *testing.T) { 16 for _, tc := range []struct { 17 name string 18 targets, want *targetspb.TargetDiscovery 19 replicaLabels []string 20 }{ 21 { 22 name: "nil slice", 23 targets: nil, 24 want: nil, 25 }, 26 { 27 name: "dropped", 28 replicaLabels: []string{"replica"}, 29 targets: &targetspb.TargetDiscovery{ 30 DroppedTargets: []*targetspb.DroppedTarget{ 31 { 32 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 33 {Name: "__address__", Value: "localhost:80"}, 34 {Name: "__metrics_path__", Value: "/metrics"}, 35 {Name: "__scheme__", Value: "http"}, 36 {Name: "job", Value: "myself"}, 37 {Name: "prometheus", Value: "ha"}, 38 {Name: "replica", Value: "0"}, 39 }}, 40 }, 41 { 42 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 43 {Name: "__address__", Value: "localhost:80"}, 44 {Name: "__metrics_path__", Value: "/metrics"}, 45 {Name: "__scheme__", Value: "http"}, 46 {Name: "job", Value: "myself"}, 47 {Name: "prometheus", Value: "ha"}, 48 {Name: "replica", Value: "1"}, 49 }}, 50 }, 51 }, 52 }, 53 want: &targetspb.TargetDiscovery{ 54 DroppedTargets: []*targetspb.DroppedTarget{ 55 { 56 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 57 {Name: "__address__", Value: "localhost:80"}, 58 {Name: "__metrics_path__", Value: "/metrics"}, 59 {Name: "__scheme__", Value: "http"}, 60 {Name: "job", Value: "myself"}, 61 {Name: "prometheus", Value: "ha"}, 62 }}, 63 }, 64 }, 65 }, 66 }, 67 { 68 name: "active simple", 69 replicaLabels: []string{"replica"}, 70 targets: &targetspb.TargetDiscovery{ 71 ActiveTargets: []*targetspb.ActiveTarget{ 72 { 73 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 74 {Name: "__address__", Value: "localhost:9090"}, 75 {Name: "__metrics_path__", Value: "/metrics"}, 76 {Name: "__scheme__", Value: "http"}, 77 {Name: "job", Value: "myself"}, 78 {Name: "prometheus", Value: "ha"}, 79 {Name: "replica", Value: "0"}, 80 }}, 81 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 82 {Name: "instance", Value: "localhost:9090"}, 83 {Name: "job", Value: "myself"}, 84 {Name: "prometheus", Value: "ha"}, 85 {Name: "replica", Value: "0"}, 86 }}, 87 ScrapePool: "myself", 88 ScrapeUrl: "http://localhost:9090/metrics", 89 Health: targetspb.TargetHealth_UP, 90 }, 91 { 92 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 93 {Name: "__address__", Value: "localhost:9090"}, 94 {Name: "__metrics_path__", Value: "/metrics"}, 95 {Name: "__scheme__", Value: "http"}, 96 {Name: "job", Value: "myself"}, 97 {Name: "prometheus", Value: "ha"}, 98 {Name: "replica", Value: "1"}, 99 }}, 100 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 101 {Name: "instance", Value: "localhost:9090"}, 102 {Name: "job", Value: "myself"}, 103 {Name: "prometheus", Value: "ha"}, 104 {Name: "replica", Value: "1"}, 105 }}, 106 ScrapePool: "myself", 107 ScrapeUrl: "http://localhost:9090/metrics", 108 Health: targetspb.TargetHealth_UP, 109 }, 110 }, 111 }, 112 want: &targetspb.TargetDiscovery{ 113 ActiveTargets: []*targetspb.ActiveTarget{ 114 { 115 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 116 {Name: "__address__", Value: "localhost:9090"}, 117 {Name: "__metrics_path__", Value: "/metrics"}, 118 {Name: "__scheme__", Value: "http"}, 119 {Name: "job", Value: "myself"}, 120 {Name: "prometheus", Value: "ha"}, 121 }}, 122 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 123 {Name: "instance", Value: "localhost:9090"}, 124 {Name: "job", Value: "myself"}, 125 {Name: "prometheus", Value: "ha"}, 126 }}, 127 ScrapePool: "myself", 128 ScrapeUrl: "http://localhost:9090/metrics", 129 Health: targetspb.TargetHealth_UP, 130 }, 131 }, 132 }, 133 }, 134 { 135 name: "active unhealth first", 136 replicaLabels: []string{"replica"}, 137 targets: &targetspb.TargetDiscovery{ 138 ActiveTargets: []*targetspb.ActiveTarget{ 139 { 140 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 141 {Name: "__address__", Value: "localhost:9090"}, 142 {Name: "__metrics_path__", Value: "/metrics"}, 143 {Name: "__scheme__", Value: "http"}, 144 {Name: "job", Value: "myself"}, 145 {Name: "prometheus", Value: "ha"}, 146 {Name: "replica", Value: "0"}, 147 }}, 148 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 149 {Name: "instance", Value: "localhost:9090"}, 150 {Name: "job", Value: "myself"}, 151 {Name: "prometheus", Value: "ha"}, 152 {Name: "replica", Value: "0"}, 153 }}, 154 ScrapePool: "myself", 155 ScrapeUrl: "http://localhost:9090/metrics", 156 Health: targetspb.TargetHealth_UP, 157 }, 158 { 159 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 160 {Name: "__address__", Value: "localhost:9090"}, 161 {Name: "__metrics_path__", Value: "/metrics"}, 162 {Name: "__scheme__", Value: "http"}, 163 {Name: "job", Value: "myself"}, 164 {Name: "prometheus", Value: "ha"}, 165 {Name: "replica", Value: "1"}, 166 }}, 167 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 168 {Name: "instance", Value: "localhost:9090"}, 169 {Name: "job", Value: "myself"}, 170 {Name: "prometheus", Value: "ha"}, 171 {Name: "replica", Value: "1"}, 172 }}, 173 ScrapePool: "myself", 174 ScrapeUrl: "http://localhost:9090/metrics", 175 Health: targetspb.TargetHealth_DOWN, 176 }, 177 }, 178 }, 179 want: &targetspb.TargetDiscovery{ 180 ActiveTargets: []*targetspb.ActiveTarget{ 181 { 182 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 183 {Name: "__address__", Value: "localhost:9090"}, 184 {Name: "__metrics_path__", Value: "/metrics"}, 185 {Name: "__scheme__", Value: "http"}, 186 {Name: "job", Value: "myself"}, 187 {Name: "prometheus", Value: "ha"}, 188 }}, 189 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 190 {Name: "instance", Value: "localhost:9090"}, 191 {Name: "job", Value: "myself"}, 192 {Name: "prometheus", Value: "ha"}, 193 }}, 194 ScrapePool: "myself", 195 ScrapeUrl: "http://localhost:9090/metrics", 196 Health: targetspb.TargetHealth_DOWN, 197 }, 198 }, 199 }, 200 }, 201 { 202 name: "active latest scrape first", 203 replicaLabels: []string{"replica"}, 204 targets: &targetspb.TargetDiscovery{ 205 ActiveTargets: []*targetspb.ActiveTarget{ 206 { 207 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 208 {Name: "__address__", Value: "localhost:9090"}, 209 {Name: "__metrics_path__", Value: "/metrics"}, 210 {Name: "__scheme__", Value: "http"}, 211 {Name: "job", Value: "myself"}, 212 {Name: "prometheus", Value: "ha"}, 213 {Name: "replica", Value: "0"}, 214 }}, 215 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 216 {Name: "instance", Value: "localhost:9090"}, 217 {Name: "job", Value: "myself"}, 218 {Name: "prometheus", Value: "ha"}, 219 {Name: "replica", Value: "0"}, 220 }}, 221 ScrapePool: "myself", 222 ScrapeUrl: "http://localhost:9090/metrics", 223 Health: targetspb.TargetHealth_UP, 224 LastScrape: time.Unix(1, 0), 225 }, 226 { 227 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 228 {Name: "__address__", Value: "localhost:9090"}, 229 {Name: "__metrics_path__", Value: "/metrics"}, 230 {Name: "__scheme__", Value: "http"}, 231 {Name: "job", Value: "myself"}, 232 {Name: "prometheus", Value: "ha"}, 233 {Name: "replica", Value: "1"}, 234 }}, 235 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 236 {Name: "instance", Value: "localhost:9090"}, 237 {Name: "job", Value: "myself"}, 238 {Name: "prometheus", Value: "ha"}, 239 {Name: "replica", Value: "1"}, 240 }}, 241 ScrapePool: "myself", 242 ScrapeUrl: "http://localhost:9090/metrics", 243 Health: targetspb.TargetHealth_UP, 244 LastScrape: time.Unix(2, 0), 245 }, 246 }, 247 }, 248 want: &targetspb.TargetDiscovery{ 249 ActiveTargets: []*targetspb.ActiveTarget{ 250 { 251 DiscoveredLabels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 252 {Name: "__address__", Value: "localhost:9090"}, 253 {Name: "__metrics_path__", Value: "/metrics"}, 254 {Name: "__scheme__", Value: "http"}, 255 {Name: "job", Value: "myself"}, 256 {Name: "prometheus", Value: "ha"}, 257 }}, 258 Labels: labelpb.ZLabelSet{Labels: []labelpb.ZLabel{ 259 {Name: "instance", Value: "localhost:9090"}, 260 {Name: "job", Value: "myself"}, 261 {Name: "prometheus", Value: "ha"}, 262 }}, 263 ScrapePool: "myself", 264 ScrapeUrl: "http://localhost:9090/metrics", 265 Health: targetspb.TargetHealth_UP, 266 LastScrape: time.Unix(2, 0), 267 }, 268 }, 269 }, 270 }, 271 } { 272 t.Run(tc.name, func(t *testing.T) { 273 replicaLabels := make(map[string]struct{}) 274 for _, lbl := range tc.replicaLabels { 275 replicaLabels[lbl] = struct{}{} 276 } 277 testutil.Equals(t, tc.want, dedupTargets(tc.targets, replicaLabels)) 278 }) 279 } 280 }