Exemple #1
0
func generateMetricSet(objectType string, labels []core.LabelDescriptor) *core.MetricSet {
	ms := &core.MetricSet{
		CreateTime:     time.Now().Add(-time.Hour),
		ScrapeTime:     time.Now(),
		Labels:         make(map[string]string),
		MetricValues:   make(map[string]core.MetricValue),
		LabeledMetrics: make([]core.LabeledMetric, len(labels)),
	}
	// Add all necessary labels
	for _, label := range labels {
		ms.Labels[label.Key] = "test-value"
	}
	ms.Labels[core.LabelMetricSetType.Key] = objectType
	// Add all standard metrics
	for _, metric := range core.StandardMetrics {
		ms.MetricValues[metric.Name] = core.MetricValue{
			MetricType: core.MetricCumulative,
			ValueType:  core.ValueInt64,
			IntValue:   -1,
		}
	}
	// Add all labeled metrics
	for _, metric := range core.LabeledMetrics {
		lm := core.LabeledMetric{
			Name: metric.Name,
			MetricValue: core.MetricValue{
				MetricType: core.MetricCumulative,
				ValueType:  core.ValueInt64,
				IntValue:   -1,
			},
			Labels: make(map[string]string),
		}
		for _, label := range core.MetricLabels() {
			lm.Labels[label.Key] = "test-value"
		}
		ms.LabeledMetrics = append(ms.LabeledMetrics, lm)
	}
	return ms
}
Exemple #2
0
func TestRealInput(t *testing.T) {
	api := NewApi(false, nil, nil)
	dataBatch := []*core.DataBatch{
		{
			Timestamp:  time.Now(),
			MetricSets: map[string]*core.MetricSet{},
		},
		{
			Timestamp:  time.Now().Add(-time.Minute),
			MetricSets: map[string]*core.MetricSet{},
		},
	}
	labels := append(core.CommonLabels(), core.ContainerLabels()...)
	labels = append(labels, core.PodLabels()...)
	for _, entry := range dataBatch {
		// Add a pod, container, node, systemcontainer
		entry.MetricSets[core.MetricSetTypePod] = generateMetricSet(core.MetricSetTypePod, labels)
		entry.MetricSets[core.MetricSetTypeNode] = generateMetricSet(core.MetricSetTypeNode, labels)
		entry.MetricSets[core.MetricSetTypePodContainer] = generateMetricSet(core.MetricSetTypePodContainer, labels)
		entry.MetricSets[core.MetricSetTypeSystemContainer] = generateMetricSet(core.MetricSetTypeSystemContainer, labels)
	}
	ts := api.processMetricsRequest(dataBatch)
	type expectation struct {
		count       int
		extraLabels bool
	}
	expectedMetrics := make(map[string]*expectation)
	for _, metric := range core.StandardMetrics {
		expectedMetrics[metric.Name] = &expectation{
			count:       4,
			extraLabels: false,
		}
	}
	for _, metric := range core.LabeledMetrics {
		expectedMetrics[metric.Name] = &expectation{
			count:       4,
			extraLabels: true,
		}
	}
	as := assert.New(t)
	for _, elem := range ts {
		// validate labels
		for _, label := range labels {
			val, exists := elem.Labels[label.Key]
			as.True(exists, "%q label does not exist", label.Key)
			if label.Key == core.LabelMetricSetType.Key {
				continue
			}
			if label.Key == core.LabelContainerName.Key && val != "machine" && val != "/pod" {
				as.Equal(val, "test-value", "%q label's value is %q, expected 'test-value'", label.Key, val)
			}
		}
		for mname, points := range elem.Metrics {
			ex := expectedMetrics[mname]
			require.NotNil(t, ex)
			as.NotEqual(ex, 0)
			ex.count--
			for _, point := range points {
				as.Equal(point.Value, -1)
				if !ex.extraLabels {
					continue
				}
				as.Equal(len(core.MetricLabels()), len(point.Labels))
				for _, label := range core.MetricLabels() {
					val, exists := point.Labels[label.Key]
					as.True(exists, "expected label %q to be found - %+v", label.Key, point.Labels)
					as.Equal(val, "test-value")
				}
			}
		}

	}
}