| 1 |
// Copyright 2018 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package graphite |
| 15 |
|
| 16 |
import ( |
| 17 |
"bufio" |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"log" |
| 24 |
"net" |
| 25 |
"os" |
| 26 |
"reflect" |
| 27 |
"regexp" |
| 28 |
"sort" |
| 29 |
"strings" |
| 30 |
"testing" |
| 31 |
"time" |
| 32 |
|
| 33 |
"github.com/prometheus/common/model" |
| 34 |
|
| 35 |
"github.com/prometheus/client_golang/prometheus" |
| 36 |
) |
| 37 |
|
| 38 |
func TestSanitize(t *testing.T) { |
| 39 |
testCases := []struct { |
| 40 |
in, out string |
| 41 |
}{ |
| 42 |
{in: "hello", out: "hello"}, |
| 43 |
{in: "hE/l1o", out: "hE_l1o"}, |
| 44 |
{in: "he,*ll(.o", out: "he_ll_o"}, |
| 45 |
{in: "hello_there%^&", out: "hello_there_"}, |
| 46 |
{in: "hell-.o", out: "hell-_o"}, |
| 47 |
} |
| 48 |
|
| 49 |
var buf bytes.Buffer |
| 50 |
w := bufio.NewWriter(&buf) |
| 51 |
|
| 52 |
for i, tc := range testCases { |
| 53 |
if err := writeSanitized(w, tc.in); err != nil { |
| 54 |
t.Fatalf("write failed: %v", err) |
| 55 |
} |
| 56 |
if err := w.Flush(); err != nil { |
| 57 |
t.Fatalf("flush failed: %v", err) |
| 58 |
} |
| 59 |
|
| 60 |
if want, got := tc.out, buf.String(); want != got { |
| 61 |
t.Fatalf("test case index %d: got sanitized string %s, want %s", i, got, want) |
| 62 |
} |
| 63 |
|
| 64 |
buf.Reset() |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestWriteSummary(t *testing.T) { |
| 69 |
testWriteSummary(t, false) |
| 70 |
testWriteSummary(t, true) |
| 71 |
} |
| 72 |
|
| 73 |
func testWriteSummary(t *testing.T, useTags bool) { |
| 74 |
sumVec := prometheus.NewSummaryVec( |
| 75 |
prometheus.SummaryOpts{ |
| 76 |
Name: "name", |
| 77 |
Help: "docstring", |
| 78 |
ConstLabels: prometheus.Labels{"constname": "constvalue"}, |
| 79 |
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 80 |
}, |
| 81 |
[]string{"labelname"}, |
| 82 |
) |
| 83 |
|
| 84 |
sumVec.WithLabelValues("val1").Observe(float64(10)) |
| 85 |
sumVec.WithLabelValues("val1").Observe(float64(20)) |
| 86 |
sumVec.WithLabelValues("val1").Observe(float64(30)) |
| 87 |
sumVec.WithLabelValues("val2").Observe(float64(20)) |
| 88 |
sumVec.WithLabelValues("val2").Observe(float64(30)) |
| 89 |
sumVec.WithLabelValues("val2").Observe(float64(40)) |
| 90 |
|
| 91 |
reg := prometheus.NewRegistry() |
| 92 |
reg.MustRegister(sumVec) |
| 93 |
|
| 94 |
mfs, err := reg.Gather() |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("error: %v", err) |
| 97 |
} |
| 98 |
|
| 99 |
testCases := []struct { |
| 100 |
prefix string |
| 101 |
}{ |
| 102 |
{prefix: "prefix"}, |
| 103 |
{prefix: "pre/fix"}, |
| 104 |
{prefix: "pre.fix"}, |
| 105 |
{prefix: ""}, |
| 106 |
} |
| 107 |
|
| 108 |
var ( |
| 109 |
want = `%s.name.constname.constvalue.labelname.val1.quantile.0_5 20 1477043 |
| 110 |
%s.name.constname.constvalue.labelname.val1.quantile.0_9 30 1477043 |
| 111 |
%s.name.constname.constvalue.labelname.val1.quantile.0_99 30 1477043 |
| 112 |
%s.name_sum.constname.constvalue.labelname.val1 60 1477043 |
| 113 |
%s.name_count.constname.constvalue.labelname.val1 3 1477043 |
| 114 |
%s.name.constname.constvalue.labelname.val2.quantile.0_5 30 1477043 |
| 115 |
%s.name.constname.constvalue.labelname.val2.quantile.0_9 40 1477043 |
| 116 |
%s.name.constname.constvalue.labelname.val2.quantile.0_99 40 1477043 |
| 117 |
%s.name_sum.constname.constvalue.labelname.val2 90 1477043 |
| 118 |
%s.name_count.constname.constvalue.labelname.val2 3 1477043 |
| 119 |
` |
| 120 |
wantTagged = `%s.name;constname=constvalue;labelname=val1;quantile=0.5 20 1477043 |
| 121 |
%s.name;constname=constvalue;labelname=val1;quantile=0.9 30 1477043 |
| 122 |
%s.name;constname=constvalue;labelname=val1;quantile=0.99 30 1477043 |
| 123 |
%s.name_sum;constname=constvalue;labelname=val1 60 1477043 |
| 124 |
%s.name_count;constname=constvalue;labelname=val1 3 1477043 |
| 125 |
%s.name;constname=constvalue;labelname=val2;quantile=0.5 30 1477043 |
| 126 |
%s.name;constname=constvalue;labelname=val2;quantile=0.9 40 1477043 |
| 127 |
%s.name;constname=constvalue;labelname=val2;quantile=0.99 40 1477043 |
| 128 |
%s.name_sum;constname=constvalue;labelname=val2 90 1477043 |
| 129 |
%s.name_count;constname=constvalue;labelname=val2 3 1477043 |
| 130 |
` |
| 131 |
) |
| 132 |
|
| 133 |
if useTags { |
| 134 |
want = wantTagged |
| 135 |
} |
| 136 |
|
| 137 |
for i, tc := range testCases { |
| 138 |
|
| 139 |
now := model.Time(1477043083) |
| 140 |
var buf bytes.Buffer |
| 141 |
err = writeMetrics(&buf, mfs, useTags, tc.prefix, now) |
| 142 |
if err != nil { |
| 143 |
t.Fatalf("error: %v", err) |
| 144 |
} |
| 145 |
|
| 146 |
var wantWithPrefix string |
| 147 |
if tc.prefix == "" { |
| 148 |
wantWithPrefix = strings.ReplaceAll(want, "%s.", "") |
| 149 |
} else { |
| 150 |
wantWithPrefix = fmt.Sprintf(want, |
| 151 |
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix, |
| 152 |
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix, |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
got := buf.String() |
| 157 |
|
| 158 |
if err := checkLinesAreEqual(wantWithPrefix, got, useTags); err != nil { |
| 159 |
t.Fatalf("test case index %d:\n%s", i, err.Error()) |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestWriteHistogram(t *testing.T) { |
| 165 |
testWriteHistogram(t, false) |
| 166 |
testWriteHistogram(t, true) |
| 167 |
} |
| 168 |
|
| 169 |
func testWriteHistogram(t *testing.T, useTags bool) { |
| 170 |
histVec := prometheus.NewHistogramVec( |
| 171 |
prometheus.HistogramOpts{ |
| 172 |
Name: "name", |
| 173 |
Help: "docstring", |
| 174 |
ConstLabels: prometheus.Labels{"constname": "constvalue"}, |
| 175 |
Buckets: []float64{0.01, 0.02, 0.05, 0.1}, |
| 176 |
}, |
| 177 |
[]string{"labelname"}, |
| 178 |
) |
| 179 |
|
| 180 |
histVec.WithLabelValues("val1").Observe(float64(10)) |
| 181 |
histVec.WithLabelValues("val1").Observe(float64(20)) |
| 182 |
histVec.WithLabelValues("val1").Observe(float64(30)) |
| 183 |
histVec.WithLabelValues("val2").Observe(float64(20)) |
| 184 |
histVec.WithLabelValues("val2").Observe(float64(30)) |
| 185 |
histVec.WithLabelValues("val2").Observe(float64(40)) |
| 186 |
|
| 187 |
reg := prometheus.NewRegistry() |
| 188 |
reg.MustRegister(histVec) |
| 189 |
|
| 190 |
mfs, err := reg.Gather() |
| 191 |
if err != nil { |
| 192 |
t.Fatalf("error: %v", err) |
| 193 |
} |
| 194 |
|
| 195 |
now := model.Time(1477043083) |
| 196 |
var buf bytes.Buffer |
| 197 |
err = writeMetrics(&buf, mfs, useTags, "prefix", now) |
| 198 |
if err != nil { |
| 199 |
t.Fatalf("error: %v", err) |
| 200 |
} |
| 201 |
|
| 202 |
var ( |
| 203 |
want = `prefix.name_bucket.constname.constvalue.labelname.val1.le.0_01 0 1477043 |
| 204 |
prefix.name_bucket.constname.constvalue.labelname.val1.le.0_02 0 1477043 |
| 205 |
prefix.name_bucket.constname.constvalue.labelname.val1.le.0_05 0 1477043 |
| 206 |
prefix.name_bucket.constname.constvalue.labelname.val1.le.0_1 0 1477043 |
| 207 |
prefix.name_sum.constname.constvalue.labelname.val1 60 1477043 |
| 208 |
prefix.name_count.constname.constvalue.labelname.val1 3 1477043 |
| 209 |
prefix.name_bucket.constname.constvalue.labelname.val1.le._Inf 3 1477043 |
| 210 |
prefix.name_bucket.constname.constvalue.labelname.val2.le.0_01 0 1477043 |
| 211 |
prefix.name_bucket.constname.constvalue.labelname.val2.le.0_02 0 1477043 |
| 212 |
prefix.name_bucket.constname.constvalue.labelname.val2.le.0_05 0 1477043 |
| 213 |
prefix.name_bucket.constname.constvalue.labelname.val2.le.0_1 0 1477043 |
| 214 |
prefix.name_sum.constname.constvalue.labelname.val2 90 1477043 |
| 215 |
prefix.name_count.constname.constvalue.labelname.val2 3 1477043 |
| 216 |
prefix.name_bucket.constname.constvalue.labelname.val2.le._Inf 3 1477043 |
| 217 |
` |
| 218 |
wantTagged = `prefix.name_bucket;constname=constvalue;labelname=val1;le=0.01 0 1477043 |
| 219 |
prefix.name_bucket;constname=constvalue;labelname=val1;le=0.02 0 1477043 |
| 220 |
prefix.name_bucket;constname=constvalue;labelname=val1;le=0.05 0 1477043 |
| 221 |
prefix.name_bucket;constname=constvalue;labelname=val1;le=0.1 0 1477043 |
| 222 |
prefix.name_sum;constname=constvalue;labelname=val1 60 1477043 |
| 223 |
prefix.name_count;constname=constvalue;labelname=val1 3 1477043 |
| 224 |
prefix.name_bucket;constname=constvalue;labelname=val1;le=+Inf 3 1477043 |
| 225 |
prefix.name_bucket;constname=constvalue;labelname=val2;le=0.01 0 1477043 |
| 226 |
prefix.name_bucket;constname=constvalue;labelname=val2;le=0.02 0 1477043 |
| 227 |
prefix.name_bucket;constname=constvalue;labelname=val2;le=0.05 0 1477043 |
| 228 |
prefix.name_bucket;constname=constvalue;labelname=val2;le=0.1 0 1477043 |
| 229 |
prefix.name_sum;constname=constvalue;labelname=val2 90 1477043 |
| 230 |
prefix.name_count;constname=constvalue;labelname=val2 3 1477043 |
| 231 |
prefix.name_bucket;constname=constvalue;labelname=val2;le=+Inf 3 1477043 |
| 232 |
` |
| 233 |
) |
| 234 |
|
| 235 |
if useTags { |
| 236 |
want = wantTagged |
| 237 |
} |
| 238 |
|
| 239 |
got := buf.String() |
| 240 |
|
| 241 |
if err := checkLinesAreEqual(want, got, useTags); err != nil { |
| 242 |
t.Fatal(err.Error()) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func TestToReader(t *testing.T) { |
| 247 |
testToReader(t, false) |
| 248 |
testToReader(t, true) |
| 249 |
} |
| 250 |
|
| 251 |
func testToReader(t *testing.T, useTags bool) { |
| 252 |
cntVec := prometheus.NewCounterVec( |
| 253 |
prometheus.CounterOpts{ |
| 254 |
Name: "name", |
| 255 |
Help: "docstring", |
| 256 |
ConstLabels: prometheus.Labels{"constname": "constvalue"}, |
| 257 |
}, |
| 258 |
[]string{"labelname"}, |
| 259 |
) |
| 260 |
cntVec.WithLabelValues("val1").Inc() |
| 261 |
cntVec.WithLabelValues("val2").Inc() |
| 262 |
|
| 263 |
reg := prometheus.NewRegistry() |
| 264 |
reg.MustRegister(cntVec) |
| 265 |
|
| 266 |
var ( |
| 267 |
want = `prefix.name.constname.constvalue.labelname.val1 1 1477043 |
| 268 |
prefix.name.constname.constvalue.labelname.val2 1 1477043 |
| 269 |
` |
| 270 |
wantTagged = `prefix.name;constname=constvalue;labelname=val1 1 1477043 |
| 271 |
prefix.name;constname=constvalue;labelname=val2 1 1477043 |
| 272 |
` |
| 273 |
) |
| 274 |
|
| 275 |
if useTags { |
| 276 |
want = wantTagged |
| 277 |
} |
| 278 |
|
| 279 |
mfs, err := reg.Gather() |
| 280 |
if err != nil { |
| 281 |
t.Fatalf("error: %v", err) |
| 282 |
} |
| 283 |
|
| 284 |
now := model.Time(1477043083) |
| 285 |
var buf bytes.Buffer |
| 286 |
err = writeMetrics(&buf, mfs, useTags, "prefix", now) |
| 287 |
if err != nil { |
| 288 |
t.Fatalf("error: %v", err) |
| 289 |
} |
| 290 |
|
| 291 |
got := buf.String() |
| 292 |
|
| 293 |
if err := checkLinesAreEqual(want, got, useTags); err != nil { |
| 294 |
t.Fatal(err.Error()) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func checkLinesAreEqual(w, g string, useTags bool) error { |
| 299 |
if useTags { |
| 300 |
taggedLineRegexp := regexp.MustCompile(`;| `) |
| 301 |
|
| 302 |
wantLines, err := stringToLines(w) |
| 303 |
if err != nil { |
| 304 |
return err |
| 305 |
} |
| 306 |
|
| 307 |
gotLines, err := stringToLines(g) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
|
| 312 |
for lineInd := range gotLines { |
| 313 |
var log string |
| 314 |
// Tagged metric, order of tags doesn't matter |
| 315 |
// m1 := "prefix.name;tag1=val1;tag2=val2 3 1477043" |
| 316 |
// m2 := "prefix.name;tag2=val2;tag1=val1 3 1477043" |
| 317 |
// m1 should be equal to m2 |
| 318 |
wantSplit := taggedLineRegexp.Split(wantLines[lineInd], -1) |
| 319 |
gotSplit := taggedLineRegexp.Split(gotLines[lineInd], -1) |
| 320 |
sort.Strings(wantSplit) |
| 321 |
sort.Strings(gotSplit) |
| 322 |
|
| 323 |
log += fmt.Sprintf("want: %v\ngot: %v\n\n", wantSplit, gotSplit) |
| 324 |
|
| 325 |
if !reflect.DeepEqual(wantSplit, gotSplit) { |
| 326 |
return errors.New(log) |
| 327 |
} |
| 328 |
} |
| 329 |
return nil |
| 330 |
} |
| 331 |
|
| 332 |
if w != g { |
| 333 |
return fmt.Errorf("wanted:\n\n%s\ngot:\n\n%s", w, g) |
| 334 |
} |
| 335 |
|
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
func stringToLines(s string) (lines []string, err error) { |
| 340 |
scanner := bufio.NewScanner(strings.NewReader(s)) |
| 341 |
for scanner.Scan() { |
| 342 |
lines = append(lines, scanner.Text()) |
| 343 |
} |
| 344 |
err = scanner.Err() |
| 345 |
return |
| 346 |
} |
| 347 |
|
| 348 |
func TestPush(t *testing.T) { |
| 349 |
reg := prometheus.NewRegistry() |
| 350 |
cntVec := prometheus.NewCounterVec( |
| 351 |
prometheus.CounterOpts{ |
| 352 |
Name: "name", |
| 353 |
Help: "docstring", |
| 354 |
ConstLabels: prometheus.Labels{"constname": "constvalue"}, |
| 355 |
}, |
| 356 |
[]string{"labelname"}, |
| 357 |
) |
| 358 |
cntVec.WithLabelValues("val1").Inc() |
| 359 |
cntVec.WithLabelValues("val2").Inc() |
| 360 |
reg.MustRegister(cntVec) |
| 361 |
|
| 362 |
host := "localhost" |
| 363 |
port := ":56789" |
| 364 |
b, err := NewBridge(&Config{ |
| 365 |
URL: host + port, |
| 366 |
Gatherer: reg, |
| 367 |
Prefix: "prefix", |
| 368 |
}) |
| 369 |
if err != nil { |
| 370 |
t.Fatalf("error creating bridge: %v", err) |
| 371 |
} |
| 372 |
|
| 373 |
nmg, err := newMockGraphite(port) |
| 374 |
if err != nil { |
| 375 |
t.Fatalf("error creating mock graphite: %v", err) |
| 376 |
} |
| 377 |
defer nmg.Close() |
| 378 |
|
| 379 |
err = b.Push() |
| 380 |
if err != nil { |
| 381 |
t.Fatalf("error pushing: %v", err) |
| 382 |
} |
| 383 |
|
| 384 |
wants := []string{ |
| 385 |
"prefix.name.constname.constvalue.labelname.val1 1", |
| 386 |
"prefix.name.constname.constvalue.labelname.val2 1", |
| 387 |
} |
| 388 |
|
| 389 |
select { |
| 390 |
case got := <-nmg.readc: |
| 391 |
for _, want := range wants { |
| 392 |
matched, err := regexp.MatchString(want, got) |
| 393 |
if err != nil { |
| 394 |
t.Fatalf("error pushing: %v", err) |
| 395 |
} |
| 396 |
if !matched { |
| 397 |
t.Fatalf("missing metric:\nno match for %s received by server:\n%s", want, got) |
| 398 |
} |
| 399 |
} |
| 400 |
return |
| 401 |
case err := <-nmg.errc: |
| 402 |
t.Fatalf("error reading push: %v", err) |
| 403 |
case <-time.After(50 * time.Millisecond): |
| 404 |
t.Fatalf("no result from graphite server") |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
func newMockGraphite(port string) (*mockGraphite, error) { |
| 409 |
readc := make(chan string) |
| 410 |
errc := make(chan error) |
| 411 |
ln, err := net.Listen("tcp", port) |
| 412 |
if err != nil { |
| 413 |
return nil, err |
| 414 |
} |
| 415 |
|
| 416 |
go func() { |
| 417 |
conn, err := ln.Accept() |
| 418 |
if err != nil { |
| 419 |
errc <- err |
| 420 |
} |
| 421 |
var b bytes.Buffer |
| 422 |
io.Copy(&b, conn) |
| 423 |
readc <- b.String() |
| 424 |
}() |
| 425 |
|
| 426 |
return &mockGraphite{ |
| 427 |
readc: readc, |
| 428 |
errc: errc, |
| 429 |
Listener: ln, |
| 430 |
}, nil |
| 431 |
} |
| 432 |
|
| 433 |
type mockGraphite struct { |
| 434 |
readc chan string |
| 435 |
errc chan error |
| 436 |
|
| 437 |
net.Listener |
| 438 |
} |
| 439 |
|
| 440 |
func ExampleBridge() { |
| 441 |
b, err := NewBridge(&Config{ |
| 442 |
URL: "graphite.example.org:3099", |
| 443 |
Gatherer: prometheus.DefaultGatherer, |
| 444 |
Prefix: "prefix", |
| 445 |
Interval: 15 * time.Second, |
| 446 |
Timeout: 10 * time.Second, |
| 447 |
ErrorHandling: AbortOnError, |
| 448 |
Logger: log.New(os.Stdout, "graphite bridge: ", log.Lshortfile), |
| 449 |
}) |
| 450 |
if err != nil { |
| 451 |
panic(err) |
| 452 |
} |
| 453 |
|
| 454 |
go func() { |
| 455 |
// Start something in a goroutine that uses metrics. |
| 456 |
}() |
| 457 |
|
| 458 |
// Push initial metrics to Graphite. Fail fast if the push fails. |
| 459 |
if err := b.Push(); err != nil { |
| 460 |
panic(err) |
| 461 |
} |
| 462 |
|
| 463 |
// Create a Context to control stopping the Run() loop that pushes |
| 464 |
// metrics to Graphite. |
| 465 |
ctx, cancel := context.WithCancel(context.Background()) |
| 466 |
defer cancel() |
| 467 |
|
| 468 |
// Start pushing metrics to Graphite in the Run() loop. |
| 469 |
b.Run(ctx) |
| 470 |
} |