| 1 |
// Copyright 2016 Circonus, Inc. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package circonusgometrics |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io/ioutil" |
| 12 |
"net/http" |
| 13 |
"net/http/httptest" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
) |
| 18 |
|
| 19 |
func testServer() *httptest.Server { |
| 20 |
f := func(w http.ResponseWriter, r *http.Request) { |
| 21 |
// fmt.Printf("%s %s\n", r.Method, r.URL.String()) |
| 22 |
switch r.URL.Path { |
| 23 |
case "/metrics_endpoint": // submit metrics |
| 24 |
switch r.Method { |
| 25 |
case "POST": |
| 26 |
fallthrough |
| 27 |
case "PUT": |
| 28 |
defer r.Body.Close() |
| 29 |
b, err := ioutil.ReadAll(r.Body) |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
var ret []byte |
| 34 |
var r interface{} |
| 35 |
err = json.Unmarshal(b, &r) |
| 36 |
if err != nil { |
| 37 |
ret, err = json.Marshal(err) |
| 38 |
if err != nil { |
| 39 |
panic(err) |
| 40 |
} |
| 41 |
} else { |
| 42 |
ret, err = json.Marshal(r) |
| 43 |
if err != nil { |
| 44 |
panic(err) |
| 45 |
} |
| 46 |
} |
| 47 |
w.WriteHeader(200) |
| 48 |
w.Header().Set("Content-Type", "application/json") |
| 49 |
fmt.Fprintln(w, string(ret)) |
| 50 |
default: |
| 51 |
w.WriteHeader(500) |
| 52 |
fmt.Fprintln(w, "unsupported method") |
| 53 |
} |
| 54 |
default: |
| 55 |
msg := fmt.Sprintf("not found %s", r.URL.Path) |
| 56 |
w.WriteHeader(404) |
| 57 |
fmt.Fprintln(w, msg) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return httptest.NewServer(http.HandlerFunc(f)) |
| 62 |
} |
| 63 |
|
| 64 |
func TestNew(t *testing.T) { |
| 65 |
|
| 66 |
t.Log("invalid config (none)") |
| 67 |
{ |
| 68 |
expectedError := errors.New("invalid configuration (nil)") |
| 69 |
_, err := New(nil) |
| 70 |
if err == nil || err.Error() != expectedError.Error() { |
| 71 |
t.Fatalf("Expected an '%#v' error, got '%#v'", expectedError, err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
t.Log("no API token, no submission URL") |
| 76 |
{ |
| 77 |
cfg := &Config{} |
| 78 |
expectedError := errors.New("creating new check manager: invalid check manager configuration (no API token AND no submission url)") |
| 79 |
_, err := New(cfg) |
| 80 |
if err == nil || err.Error() != expectedError.Error() { |
| 81 |
t.Fatalf("Expected an '%#v' error, got '%#v'", expectedError, err) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
t.Log("no API token, submission URL only") |
| 86 |
{ |
| 87 |
cfg := &Config{} |
| 88 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 89 |
|
| 90 |
cm, err := New(cfg) |
| 91 |
if err != nil { |
| 92 |
t.Fatalf("Expected no error, got '%v'", err) |
| 93 |
} |
| 94 |
|
| 95 |
for !cm.check.IsReady() { |
| 96 |
t.Log("\twaiting for cm to init") |
| 97 |
time.Sleep(1 * time.Second) |
| 98 |
} |
| 99 |
|
| 100 |
trap, err := cm.check.GetSubmissionURL() |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("Expected no error, got '%v'", err) |
| 103 |
} |
| 104 |
|
| 105 |
if trap.URL.String() != cfg.CheckManager.Check.SubmissionURL { |
| 106 |
t.Fatalf("Expected '%s' == '%s'", trap.URL.String(), cfg.CheckManager.Check.SubmissionURL) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
t.Log("no Log, Debug = true") |
| 111 |
{ |
| 112 |
cfg := &Config{ |
| 113 |
Debug: true, |
| 114 |
} |
| 115 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 116 |
_, err := New(cfg) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("Expected no error, got '%v'", err) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
t.Log("flush interval [good]") |
| 123 |
{ |
| 124 |
cfg := &Config{ |
| 125 |
Interval: "30s", |
| 126 |
} |
| 127 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 128 |
_, err := New(cfg) |
| 129 |
if err != nil { |
| 130 |
t.Errorf("Expected no error, got '%v'", err) |
| 131 |
} |
| 132 |
} |
| 133 |
t.Log("flush interval [bad]") |
| 134 |
{ |
| 135 |
cfg := &Config{ |
| 136 |
Interval: "thirty seconds", |
| 137 |
} |
| 138 |
expectedError := errors.New("parsing flush interval: time: invalid duration thirty seconds") |
| 139 |
_, err := New(cfg) |
| 140 |
if err == nil { |
| 141 |
t.Fatal("expected error") |
| 142 |
} |
| 143 |
if err.Error() != expectedError.Error() { |
| 144 |
t.Fatalf("Expected %v got '%v'", expectedError, err) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
t.Log("reset counters [good(true)]") |
| 149 |
{ |
| 150 |
cfg := &Config{ |
| 151 |
ResetCounters: "true", |
| 152 |
} |
| 153 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 154 |
_, err := New(cfg) |
| 155 |
if err != nil { |
| 156 |
t.Errorf("Expected no error, got '%v'", err) |
| 157 |
} |
| 158 |
} |
| 159 |
t.Log("reset counters [good(1)]") |
| 160 |
{ |
| 161 |
cfg := &Config{ |
| 162 |
ResetCounters: "1", |
| 163 |
} |
| 164 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 165 |
_, err := New(cfg) |
| 166 |
if err != nil { |
| 167 |
t.Errorf("Expected no error, got '%v'", err) |
| 168 |
} |
| 169 |
} |
| 170 |
t.Log("reset counters [bad(yes)]") |
| 171 |
{ |
| 172 |
cfg := &Config{ |
| 173 |
ResetCounters: "yes", |
| 174 |
} |
| 175 |
expectedError := errors.New("parsing reset counters: strconv.ParseBool: parsing \"yes\": invalid syntax") |
| 176 |
_, err := New(cfg) |
| 177 |
if err == nil { |
| 178 |
t.Fatal("expected error") |
| 179 |
} |
| 180 |
if err.Error() != expectedError.Error() { |
| 181 |
t.Fatalf("Expected %v got '%v'", expectedError, err) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
t.Log("reset gauges [good(true)]") |
| 186 |
{ |
| 187 |
cfg := &Config{ |
| 188 |
ResetGauges: "true", |
| 189 |
} |
| 190 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 191 |
_, err := New(cfg) |
| 192 |
if err != nil { |
| 193 |
t.Errorf("Expected no error, got '%v'", err) |
| 194 |
} |
| 195 |
} |
| 196 |
t.Log("reset gauges [good(1)]") |
| 197 |
{ |
| 198 |
cfg := &Config{ |
| 199 |
ResetGauges: "1", |
| 200 |
} |
| 201 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 202 |
_, err := New(cfg) |
| 203 |
if err != nil { |
| 204 |
t.Errorf("Expected no error, got '%v'", err) |
| 205 |
} |
| 206 |
} |
| 207 |
t.Log("reset gauges [bad(yes)]") |
| 208 |
{ |
| 209 |
cfg := &Config{ |
| 210 |
ResetGauges: "yes", |
| 211 |
} |
| 212 |
expectedError := errors.New("parsing reset gauges: strconv.ParseBool: parsing \"yes\": invalid syntax") |
| 213 |
_, err := New(cfg) |
| 214 |
if err == nil { |
| 215 |
t.Fatal("expected error") |
| 216 |
} |
| 217 |
if err.Error() != expectedError.Error() { |
| 218 |
t.Fatalf("Expected %v got '%v'", expectedError, err) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
t.Log("reset histograms [good(true)]") |
| 223 |
{ |
| 224 |
cfg := &Config{ |
| 225 |
ResetHistograms: "true", |
| 226 |
} |
| 227 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 228 |
_, err := New(cfg) |
| 229 |
if err != nil { |
| 230 |
t.Errorf("Expected no error, got '%v'", err) |
| 231 |
} |
| 232 |
} |
| 233 |
t.Log("reset histograms [good(1)]") |
| 234 |
{ |
| 235 |
cfg := &Config{ |
| 236 |
ResetHistograms: "1", |
| 237 |
} |
| 238 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 239 |
_, err := New(cfg) |
| 240 |
if err != nil { |
| 241 |
t.Errorf("Expected no error, got '%v'", err) |
| 242 |
} |
| 243 |
} |
| 244 |
t.Log("reset histograms [bad(yes)]") |
| 245 |
{ |
| 246 |
cfg := &Config{ |
| 247 |
ResetHistograms: "yes", |
| 248 |
} |
| 249 |
expectedError := errors.New("parsing reset histograms: strconv.ParseBool: parsing \"yes\": invalid syntax") |
| 250 |
_, err := New(cfg) |
| 251 |
if err == nil { |
| 252 |
t.Fatal("expected error") |
| 253 |
} |
| 254 |
if err.Error() != expectedError.Error() { |
| 255 |
t.Fatalf("Expected %v got '%v'", expectedError, err) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
t.Log("reset text metrics [good(true)]") |
| 260 |
{ |
| 261 |
cfg := &Config{ |
| 262 |
ResetText: "true", |
| 263 |
} |
| 264 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 265 |
_, err := New(cfg) |
| 266 |
if err != nil { |
| 267 |
t.Errorf("Expected no error, got '%v'", err) |
| 268 |
} |
| 269 |
} |
| 270 |
t.Log("reset text metrics [good(1)]") |
| 271 |
{ |
| 272 |
cfg := &Config{ |
| 273 |
ResetText: "1", |
| 274 |
} |
| 275 |
cfg.CheckManager.Check.SubmissionURL = "http://127.0.0.1:56104/blah/blah" |
| 276 |
_, err := New(cfg) |
| 277 |
if err != nil { |
| 278 |
t.Errorf("Expected no error, got '%v'", err) |
| 279 |
} |
| 280 |
} |
| 281 |
t.Log("reset text metrics [bad(yes)]") |
| 282 |
{ |
| 283 |
cfg := &Config{ |
| 284 |
ResetText: "yes", |
| 285 |
} |
| 286 |
expectedError := errors.New("parsing reset text: strconv.ParseBool: parsing \"yes\": invalid syntax") |
| 287 |
_, err := New(cfg) |
| 288 |
if err == nil { |
| 289 |
t.Fatal("expected error") |
| 290 |
} |
| 291 |
if err.Error() != expectedError.Error() { |
| 292 |
t.Fatalf("Expected %v got '%v'", expectedError, err) |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func TestFlush(t *testing.T) { |
| 298 |
server := testServer() |
| 299 |
defer server.Close() |
| 300 |
|
| 301 |
submissionURL := server.URL + "/metrics_endpoint" |
| 302 |
|
| 303 |
t.Log("Already flushing") |
| 304 |
{ |
| 305 |
cfg := &Config{} |
| 306 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 307 |
cm, err := NewCirconusMetrics(cfg) |
| 308 |
if err != nil { |
| 309 |
t.Errorf("Expected no error, got '%v'", err) |
| 310 |
} |
| 311 |
|
| 312 |
cm.flushing = true |
| 313 |
cm.Flush() |
| 314 |
} |
| 315 |
|
| 316 |
t.Log("No metrics") |
| 317 |
{ |
| 318 |
cfg := &Config{} |
| 319 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 320 |
cm, err := NewCirconusMetrics(cfg) |
| 321 |
if err != nil { |
| 322 |
t.Errorf("Expected no error, got '%v'", err) |
| 323 |
} |
| 324 |
|
| 325 |
cm.Flush() |
| 326 |
} |
| 327 |
|
| 328 |
t.Log("counter") |
| 329 |
{ |
| 330 |
cfg := &Config{} |
| 331 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 332 |
cm, err := NewCirconusMetrics(cfg) |
| 333 |
if err != nil { |
| 334 |
t.Errorf("Expected no error, got '%v'", err) |
| 335 |
} |
| 336 |
|
| 337 |
cm.Set("foo", 30) |
| 338 |
|
| 339 |
cm.Flush() |
| 340 |
} |
| 341 |
|
| 342 |
t.Log("gauge") |
| 343 |
{ |
| 344 |
cfg := &Config{} |
| 345 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 346 |
cm, err := NewCirconusMetrics(cfg) |
| 347 |
if err != nil { |
| 348 |
t.Errorf("Expected no error, got '%v'", err) |
| 349 |
} |
| 350 |
|
| 351 |
cm.SetGauge("foo", 30) |
| 352 |
|
| 353 |
cm.Flush() |
| 354 |
} |
| 355 |
|
| 356 |
t.Log("histogram") |
| 357 |
{ |
| 358 |
cfg := &Config{} |
| 359 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 360 |
cm, err := NewCirconusMetrics(cfg) |
| 361 |
if err != nil { |
| 362 |
t.Errorf("Expected no error, got '%v'", err) |
| 363 |
} |
| 364 |
|
| 365 |
cm.Timing("foo", 30.28) |
| 366 |
|
| 367 |
cm.Flush() |
| 368 |
} |
| 369 |
|
| 370 |
t.Log("text") |
| 371 |
{ |
| 372 |
cfg := &Config{} |
| 373 |
cfg.CheckManager.Check.SubmissionURL = submissionURL |
| 374 |
cm, err := NewCirconusMetrics(cfg) |
| 375 |
if err != nil { |
| 376 |
t.Errorf("Expected no error, got '%v'", err) |
| 377 |
} |
| 378 |
|
| 379 |
cm.SetText("foo", "bar") |
| 380 |
|
| 381 |
cm.Flush() |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
func TestPackageMetrics(t *testing.T) { |
| 386 |
cfg := &Config{} |
| 387 |
cfg.CheckManager.Check.SubmissionURL = "none" |
| 388 |
cfg.Interval = "0" |
| 389 |
|
| 390 |
t.Log("Not flushing") |
| 391 |
{ |
| 392 |
cm, err := NewCirconusMetrics(cfg) |
| 393 |
if err != nil { |
| 394 |
t.Errorf("Expected no error, got '%v'", err) |
| 395 |
} |
| 396 |
|
| 397 |
cm.flushing = false |
| 398 |
newMetrics, output := cm.packageMetrics() |
| 399 |
if len(newMetrics) != 0 && len(output) != 0 { |
| 400 |
t.Fatal("expected 0 metrics") |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
func TestFlushMetrics(t *testing.T) { |
| 406 |
cfg := &Config{} |
| 407 |
cfg.CheckManager.Check.SubmissionURL = "none" |
| 408 |
cfg.Interval = "0" |
| 409 |
|
| 410 |
t.Log("Already flushing") |
| 411 |
{ |
| 412 |
cm, err := NewCirconusMetrics(cfg) |
| 413 |
if err != nil { |
| 414 |
t.Errorf("Expected no error, got '%v'", err) |
| 415 |
} |
| 416 |
|
| 417 |
cm.flushing = true |
| 418 |
metrics := cm.FlushMetrics() |
| 419 |
if len(*metrics) != 0 { |
| 420 |
t.Fatal("expected 0 metrics") |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
t.Log("No metrics") |
| 425 |
{ |
| 426 |
cm, err := NewCirconusMetrics(cfg) |
| 427 |
if err != nil { |
| 428 |
t.Errorf("Expected no error, got '%v'", err) |
| 429 |
} |
| 430 |
|
| 431 |
metrics := cm.FlushMetrics() |
| 432 |
if len(*metrics) != 0 { |
| 433 |
t.Fatal("expected 0 metrics") |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
t.Log("counter") |
| 438 |
{ |
| 439 |
cm, err := NewCirconusMetrics(cfg) |
| 440 |
if err != nil { |
| 441 |
t.Errorf("Expected no error, got '%v'", err) |
| 442 |
} |
| 443 |
|
| 444 |
cm.Set("foo", 30) |
| 445 |
|
| 446 |
metrics := cm.FlushMetrics() |
| 447 |
if len(*metrics) == 0 { |
| 448 |
t.Fatal("expected 1 metric") |
| 449 |
} |
| 450 |
|
| 451 |
if m, mok := (*metrics)["foo"]; !mok { |
| 452 |
t.Fatalf("'foo' not found in %v", metrics) |
| 453 |
} else if m.Type != "L" { |
| 454 |
t.Fatalf("'Type' not correct %v", m) |
| 455 |
} else if m.Value.(uint64) != 30 { |
| 456 |
t.Fatalf("'Value' not correct %v", m) |
| 457 |
} |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
t.Log("gauge") |
| 462 |
{ |
| 463 |
cm, err := NewCirconusMetrics(cfg) |
| 464 |
if err != nil { |
| 465 |
t.Errorf("Expected no error, got '%v'", err) |
| 466 |
} |
| 467 |
|
| 468 |
v := int64(30) |
| 469 |
cm.SetGauge("foo", v) |
| 470 |
|
| 471 |
metrics := cm.FlushMetrics() |
| 472 |
if len(*metrics) == 0 { |
| 473 |
t.Fatal("expected 1 metric") |
| 474 |
} |
| 475 |
|
| 476 |
if m, mok := (*metrics)["foo"]; !mok { |
| 477 |
t.Fatalf("'foo' not found in %v", metrics) |
| 478 |
} else if m.Type != "l" { |
| 479 |
t.Fatalf("'Type' not correct %v", m) |
| 480 |
} else if m.Value.(int64) != v { |
| 481 |
t.Fatalf("'Value' not correct, expected %v got %v", v, m.Value) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
t.Log("histogram") |
| 486 |
{ |
| 487 |
cm, err := NewCirconusMetrics(cfg) |
| 488 |
if err != nil { |
| 489 |
t.Errorf("Expected no error, got '%v'", err) |
| 490 |
} |
| 491 |
|
| 492 |
cm.Timing("foo", 30.28) |
| 493 |
|
| 494 |
metrics := cm.FlushMetrics() |
| 495 |
if len(*metrics) == 0 { |
| 496 |
t.Fatal("expected 1 metric") |
| 497 |
} |
| 498 |
|
| 499 |
if m, mok := (*metrics)["foo"]; !mok { |
| 500 |
t.Fatalf("'foo' not found in %v", metrics) |
| 501 |
} else if m.Type != "n" { |
| 502 |
t.Fatalf("'Type' not correct %v", m) |
| 503 |
} else if len(m.Value.([]string)) != 1 { |
| 504 |
t.Fatal("expected 1 value") |
| 505 |
} else if m.Value.([]string)[0] != "H[3.0e+01]=1" { |
| 506 |
t.Fatalf("'Value' not correct %v", m) |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
t.Log("text") |
| 511 |
{ |
| 512 |
cm, err := NewCirconusMetrics(cfg) |
| 513 |
if err != nil { |
| 514 |
t.Errorf("Expected no error, got '%v'", err) |
| 515 |
} |
| 516 |
|
| 517 |
cm.SetText("foo", "bar") |
| 518 |
|
| 519 |
metrics := cm.FlushMetrics() |
| 520 |
if len(*metrics) == 0 { |
| 521 |
t.Fatal("expected 1 metric") |
| 522 |
} |
| 523 |
|
| 524 |
if m, mok := (*metrics)["foo"]; !mok { |
| 525 |
t.Fatalf("'foo' not found in %v", metrics) |
| 526 |
} else if m.Type != "s" { |
| 527 |
t.Fatalf("'Type' not correct %v", m) |
| 528 |
} else if m.Value != "bar" { |
| 529 |
t.Fatalf("'Value' not correct %v", m) |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
func TestPromOutput(t *testing.T) { |
| 535 |
cfg := &Config{} |
| 536 |
cfg.CheckManager.Check.SubmissionURL = "none" |
| 537 |
cfg.Interval = "0" |
| 538 |
|
| 539 |
t.Log("No metrics") |
| 540 |
{ |
| 541 |
cm, err := NewCirconusMetrics(cfg) |
| 542 |
if err != nil { |
| 543 |
t.Errorf("Expected no error, got '%v'", err) |
| 544 |
} |
| 545 |
|
| 546 |
b, err := cm.PromOutput() |
| 547 |
if err == nil { |
| 548 |
t.Fatal("expected error") |
| 549 |
} |
| 550 |
if b != nil { |
| 551 |
t.Fatalf("expected nil, got (%v)", b.String()) |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
t.Log("counter") |
| 556 |
{ |
| 557 |
cm, err := NewCirconusMetrics(cfg) |
| 558 |
if err != nil { |
| 559 |
t.Errorf("Expected no error, got '%v'", err) |
| 560 |
} |
| 561 |
|
| 562 |
cm.Set("foo", 30) |
| 563 |
|
| 564 |
metrics := cm.FlushMetrics() |
| 565 |
if len(*metrics) == 0 { |
| 566 |
t.Fatal("expected 1 metric") |
| 567 |
} |
| 568 |
|
| 569 |
if m, mok := (*metrics)["foo"]; !mok { |
| 570 |
t.Fatalf("'foo' not found in %v", metrics) |
| 571 |
} else if m.Type != "L" { |
| 572 |
t.Fatalf("'Type' not correct %v", m) |
| 573 |
} else if m.Value.(uint64) != 30 { |
| 574 |
t.Fatalf("'Value' not correct %v", m) |
| 575 |
} |
| 576 |
|
| 577 |
b, err := cm.PromOutput() |
| 578 |
if err != nil { |
| 579 |
t.Fatalf("expected no error, got (%s)", err) |
| 580 |
} |
| 581 |
if b == nil { |
| 582 |
t.Fatal("expected not nil") |
| 583 |
} |
| 584 |
expect := "foo 30" |
| 585 |
if !strings.HasPrefix(b.String(), expect) { |
| 586 |
t.Fatalf("expected prefix (%s) got (%s)", expect, b.String()) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
t.Log("gauge") |
| 591 |
{ |
| 592 |
cm, err := NewCirconusMetrics(cfg) |
| 593 |
if err != nil { |
| 594 |
t.Errorf("Expected no error, got '%v'", err) |
| 595 |
} |
| 596 |
|
| 597 |
v := int(30) |
| 598 |
cm.SetGauge("foo", v) |
| 599 |
|
| 600 |
metrics := cm.FlushMetrics() |
| 601 |
if len(*metrics) == 0 { |
| 602 |
t.Fatal("expected 1 metric") |
| 603 |
} |
| 604 |
|
| 605 |
if m, mok := (*metrics)["foo"]; !mok { |
| 606 |
t.Fatalf("'foo' not found in %v", metrics) |
| 607 |
} else if m.Type != "i" { |
| 608 |
t.Fatalf("'Type' not correct %v", m) |
| 609 |
} else if m.Value.(int) != v { |
| 610 |
t.Fatalf("'Value' not correct, expected %v got %v", v, m.Value) |
| 611 |
} |
| 612 |
|
| 613 |
b, err := cm.PromOutput() |
| 614 |
if err != nil { |
| 615 |
t.Fatalf("expected no error, got (%s)", err) |
| 616 |
} |
| 617 |
if b == nil { |
| 618 |
t.Fatal("expected not nil") |
| 619 |
} |
| 620 |
expect := "foo 30" |
| 621 |
if !strings.HasPrefix(b.String(), expect) { |
| 622 |
t.Fatalf("expected prefix (%s) got (%s)", expect, b.String()) |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
t.Log("histogram") |
| 627 |
{ |
| 628 |
cm, err := NewCirconusMetrics(cfg) |
| 629 |
if err != nil { |
| 630 |
t.Errorf("Expected no error, got '%v'", err) |
| 631 |
} |
| 632 |
|
| 633 |
cm.Timing("foo", 30.28) |
| 634 |
|
| 635 |
metrics := cm.FlushMetrics() |
| 636 |
if len(*metrics) == 0 { |
| 637 |
t.Fatal("expected 1 metric") |
| 638 |
} |
| 639 |
|
| 640 |
if m, mok := (*metrics)["foo"]; !mok { |
| 641 |
t.Fatalf("'foo' not found in %v", metrics) |
| 642 |
} else if m.Type != "n" { |
| 643 |
t.Fatalf("'Type' not correct %v", m) |
| 644 |
} else if len(m.Value.([]string)) != 1 { |
| 645 |
t.Fatal("expected 1 value") |
| 646 |
} else if m.Value.([]string)[0] != "H[3.0e+01]=1" { |
| 647 |
t.Fatalf("'Value' not correct %v", m) |
| 648 |
} |
| 649 |
|
| 650 |
b, err := cm.PromOutput() |
| 651 |
if err != nil { |
| 652 |
t.Fatalf("expected no error, got (%s)", err) |
| 653 |
} |
| 654 |
if b == nil { |
| 655 |
t.Fatal("expected not nil") |
| 656 |
} |
| 657 |
expect := "" |
| 658 |
if b.String() != expect { |
| 659 |
t.Fatalf("expected prefix (%s) got (%s)", expect, b.String()) |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
t.Log("text") |
| 664 |
{ |
| 665 |
cm, err := NewCirconusMetrics(cfg) |
| 666 |
if err != nil { |
| 667 |
t.Errorf("Expected no error, got '%v'", err) |
| 668 |
} |
| 669 |
|
| 670 |
cm.SetText("foo", "bar") |
| 671 |
|
| 672 |
metrics := cm.FlushMetrics() |
| 673 |
if len(*metrics) == 0 { |
| 674 |
t.Fatal("expected 1 metric") |
| 675 |
} |
| 676 |
|
| 677 |
if m, mok := (*metrics)["foo"]; !mok { |
| 678 |
t.Fatalf("'foo' not found in %v", metrics) |
| 679 |
} else if m.Type != "s" { |
| 680 |
t.Fatalf("'Type' not correct %v", m) |
| 681 |
} else if m.Value != "bar" { |
| 682 |
t.Fatalf("'Value' not correct %v", m) |
| 683 |
} |
| 684 |
|
| 685 |
b, err := cm.PromOutput() |
| 686 |
if err != nil { |
| 687 |
t.Fatalf("expected no error, got (%s)", err) |
| 688 |
} |
| 689 |
if b == nil { |
| 690 |
t.Fatal("expected not nil") |
| 691 |
} |
| 692 |
expect := "" |
| 693 |
if b.String() != expect { |
| 694 |
t.Fatalf("expected prefix (%s) got (%s)", expect, b.String()) |
| 695 |
} |
| 696 |
} |
| 697 |
} |