Exemplo n.º 1
0
func AppendSampleAsPureSparseAppendTests(p metric.Persistence, t test.Tester) {
	appendSample := func(x int) (success bool) {
		v := clientmodel.SampleValue(x)
		ts := clientmodel.TimestampFromUnix(int64(x))
		labelName := clientmodel.LabelName(x)
		labelValue := clientmodel.LabelValue(x)
		l := clientmodel.Metric{labelName: labelValue}

		sample := &clientmodel.Sample{
			Value:     v,
			Timestamp: ts,
			Metric:    l,
		}

		err := p.AppendSamples(clientmodel.Samples{sample})

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

		return
	}

	if err := quick.Check(appendSample, nil); err != nil {
		t.Error(err)
	}
}
Exemplo n.º 2
0
func AppendSampleAsSparseAppendWithReadsTests(p metric.Persistence, t test.Tester) {
	appendSample := func(x int) (success bool) {
		v := clientmodel.SampleValue(x)
		ts := clientmodel.TimestampFromUnix(int64(x))
		labelName := clientmodel.LabelName(x)
		labelValue := clientmodel.LabelValue(x)
		l := clientmodel.Metric{labelName: labelValue}

		sample := &clientmodel.Sample{
			Value:     v,
			Timestamp: ts,
			Metric:    l,
		}

		err := p.AppendSamples(clientmodel.Samples{sample})
		if err != nil {
			t.Error(err)
			return
		}

		values, err := p.GetLabelValuesForLabelName(labelName)
		if err != nil {
			t.Error(err)
			return
		}
		if len(values) != 1 {
			t.Errorf("expected label values count of %d, got %d", 1, len(values))
			return
		}

		fingerprints, err := p.GetFingerprintsForLabelMatchers(metric.LabelMatchers{{
			Type:  metric.Equal,
			Name:  labelName,
			Value: labelValue,
		}})
		if err != nil {
			t.Error(err)
			return
		}
		if len(fingerprints) != 1 {
			t.Errorf("expected fingerprint count of %d, got %d", 1, len(fingerprints))
			return
		}

		return true
	}

	if err := quick.Check(appendSample, nil); err != nil {
		t.Error(err)
	}
}
Exemplo n.º 3
0
func storeMatrix(storage metric.Persistence, matrix ast.Matrix) (err error) {
	pendingSamples := clientmodel.Samples{}
	for _, sampleSet := range matrix {
		for _, sample := range sampleSet.Values {
			pendingSamples = append(pendingSamples, &clientmodel.Sample{
				Metric:    sampleSet.Metric,
				Value:     sample.Value,
				Timestamp: sample.Timestamp,
			})
		}
	}
	err = storage.AppendSamples(pendingSamples)
	return
}
Exemplo n.º 4
0
func AppendSampleAsPureSingleEntityAppendTests(p metric.Persistence, t test.Tester) {
	appendSample := func(x int) bool {
		sample := &clientmodel.Sample{
			Value:     clientmodel.SampleValue(x),
			Timestamp: clientmodel.TimestampFromUnix(int64(x)),
			Metric:    clientmodel.Metric{clientmodel.MetricNameLabel: "my_metric"},
		}

		err := p.AppendSamples(clientmodel.Samples{sample})

		return err == nil
	}

	if err := quick.Check(appendSample, nil); err != nil {
		t.Error(err)
	}
}
Exemplo n.º 5
0
func AppendsRepeatingValuesTests(p metric.Persistence, t test.Tester) {
	m := clientmodel.Metric{
		clientmodel.MetricNameLabel: "errors_total",
		"controller":                "foo",
		"operation":                 "bar",
	}

	increments := 10
	repetitions := 500

	s := clientmodel.Samples{}
	for i := 0; i < increments; i++ {
		for j := 0; j < repetitions; j++ {
			time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
			s = append(s, &clientmodel.Sample{
				Value:     clientmodel.SampleValue(i),
				Timestamp: time,
				Metric:    m,
			})
		}
	}

	p.AppendSamples(s)

	v, ok := p.(metric.View)
	if !ok {
		// It's purely a benchmark for a MetricPersistance that is not viewable.
		return
	}

	matchers := labelMatchersFromLabelSet(clientmodel.LabelSet{
		clientmodel.MetricNameLabel: "errors_total",
		"controller":                "foo",
		"operation":                 "bar",
	})

	for i := 0; i < increments; i++ {
		for j := 0; j < repetitions; j++ {
			fingerprints, err := p.GetFingerprintsForLabelMatchers(matchers)
			if err != nil {
				t.Fatal(err)
			}
			if len(fingerprints) != 1 {
				t.Fatalf("expected %d fingerprints, got %d", 1, len(fingerprints))
			}

			time := clientmodel.Timestamp(0).Add(time.Duration(i) * time.Hour).Add(time.Duration(j) * time.Second)
			samples := v.GetValueAtTime(fingerprints[0], time)
			if len(samples) == 0 {
				t.Fatal("expected at least one sample.")
			}

			expected := clientmodel.SampleValue(i)

			for _, sample := range samples {
				if sample.Value != expected {
					t.Fatalf("expected %v value, got %v", expected, sample.Value)
				}
			}
		}
	}
}
Exemplo n.º 6
0
func testAppendSamples(p metric.Persistence, s *clientmodel.Sample, t test.Tester) {
	err := p.AppendSamples(clientmodel.Samples{s})
	if err != nil {
		t.Fatal(err)
	}
}