Example #1
0
func GetMetricForFingerprintTests(p metric.Persistence, t test.Tester) {
	testAppendSamples(p, &clientmodel.Sample{
		Value:     0,
		Timestamp: 0,
		Metric: clientmodel.Metric{
			"request_type": "your_mom",
		},
	}, t)

	testAppendSamples(p, &clientmodel.Sample{
		Value:     0,
		Timestamp: 0,
		Metric: clientmodel.Metric{
			"request_type": "your_dad",
			"one-off":      "value",
		},
	}, t)

	result, err := p.GetFingerprintsForLabelMatchers(metric.LabelMatchers{{
		Type:  metric.Equal,
		Name:  "request_type",
		Value: "your_mom",
	}})

	if err != nil {
		t.Error(err)
	}

	if len(result) != 1 {
		t.Errorf("Expected one element.")
	}

	m, err := p.GetMetricForFingerprint(result[0])
	if err != nil {
		t.Error(err)
	}

	if m == nil {
		t.Fatal("Did not expect nil.")
	}

	if len(m) != 1 {
		t.Errorf("Expected one-dimensional metric.")
	}

	if m["request_type"] != "your_mom" {
		t.Errorf("Expected metric to match.")
	}

	result, err = p.GetFingerprintsForLabelMatchers(metric.LabelMatchers{{
		Type:  metric.Equal,
		Name:  "request_type",
		Value: "your_dad",
	}})

	if err != nil {
		t.Error(err)
	}

	if len(result) != 1 {
		t.Errorf("Expected one element.")
	}

	m, err = p.GetMetricForFingerprint(result[0])

	if m == nil {
		t.Fatal("Did not expect nil.")
	}

	if err != nil {
		t.Error(err)
	}

	if len(m) != 2 {
		t.Errorf("Expected two-dimensional metric.")
	}

	if m["request_type"] != "your_dad" {
		t.Errorf("Expected metric to match.")
	}

	if m["one-off"] != "value" {
		t.Errorf("Expected metric to match.")
	}

	// Verify that mutating a returned metric does not result in the mutated
	// metric to be returned at the next GetMetricForFingerprint() call.
	m["one-off"] = "new value"
	m, err = p.GetMetricForFingerprint(result[0])

	if m == nil {
		t.Fatal("Did not expect nil.")
	}

	if err != nil {
		t.Error(err)
	}

	if len(m) != 2 {
		t.Errorf("Expected two-dimensional metric.")
	}

	if m["request_type"] != "your_dad" {
		t.Errorf("Expected metric to match.")
	}

	if m["one-off"] != "value" {
		t.Errorf("Expected metric to match.")
	}
}