Exemplo n.º 1
0
func (this *NamespaceAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) {
	namespaces := make(map[string]*core.MetricSet)
	for key, metricSet := range batch.MetricSets {
		if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePod {
			// Aggregating pods
			if namespaceName, found := metricSet.Labels[core.LabelNamespaceName.Key]; found {
				namespaceKey := core.NamespaceKey(namespaceName)
				namespace, found := namespaces[namespaceKey]
				if !found {
					if nsFromBatch, found := batch.MetricSets[namespaceKey]; found {
						namespace = nsFromBatch
					} else {
						namespace = namespaceMetricSet(namespaceName, metricSet.Labels[core.LabelPodNamespaceUID.Key])
						namespaces[namespaceKey] = namespace
					}
				}
				if err := aggregate(metricSet, namespace, this.MetricsToAggregate); err != nil {
					return nil, err
				}
			} else {
				glog.Errorf("No namespace info in pod %s: %v", key, metricSet.Labels)
			}
		}
	}
	for key, val := range namespaces {
		batch.MetricSets[key] = val
	}
	return batch, nil
}
func TestNamespaceAggregate(t *testing.T) {
	batch := core.DataBatch{
		Timestamp: time.Now(),
		MetricSets: map[string]*core.MetricSet{
			core.PodKey("ns1", "pod1"): {
				Labels: map[string]string{
					core.LabelMetricSetType.Key: core.MetricSetTypePod,
					core.LabelNamespaceName.Key: "ns1",
				},
				MetricValues: map[string]core.MetricValue{
					"m1": {
						ValueType:  core.ValueInt64,
						MetricType: core.MetricGauge,
						IntValue:   10,
					},
					"m2": {
						ValueType:  core.ValueInt64,
						MetricType: core.MetricGauge,
						IntValue:   222,
					},
				},
			},

			core.PodKey("ns1", "pod2"): {
				Labels: map[string]string{
					core.LabelMetricSetType.Key: core.MetricSetTypePod,
					core.LabelNamespaceName.Key: "ns1",
				},
				MetricValues: map[string]core.MetricValue{
					"m1": {
						ValueType:  core.ValueInt64,
						MetricType: core.MetricGauge,
						IntValue:   100,
					},
					"m3": {
						ValueType:  core.ValueInt64,
						MetricType: core.MetricGauge,
						IntValue:   30,
					},
				},
			},
		},
	}
	processor := NamespaceAggregator{
		MetricsToAggregate: []string{"m1", "m3"},
	}
	result, err := processor.Process(&batch)
	assert.NoError(t, err)
	namespace, found := result.MetricSets[core.NamespaceKey("ns1")]
	assert.True(t, found)

	m1, found := namespace.MetricValues["m1"]
	assert.True(t, found)
	assert.Equal(t, 110, m1.IntValue)

	m3, found := namespace.MetricValues["m3"]
	assert.True(t, found)
	assert.Equal(t, 30, m3.IntValue)
}
Exemplo n.º 3
0
// namespaceMetrics returns a metric timeseries for a metric of the Namespace entity.
func (a *Api) namespaceMetrics(request *restful.Request, response *restful.Response) {
	a.processMetricRequest(core.NamespaceKey(request.PathParameter("namespace-name")),
		request, response)
}