func TestMetricTransform(t *testing.T) { hSink := dummySink() l := make(map[string]string) l["spooky"] = "notvisible" l[core.LabelHostname.Key] = "localhost" l[core.LabelHostID.Key] = "localhost" l[core.LabelContainerName.Key] = "docker" l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" l[core.LabelNodename.Key] = "myNode" metricName := "test/metric/1" labeledMetricNameA := "test/labeledmetric/A" labeledMetricNameB := "test/labeledmetric/B" metricSet := core.MetricSet{ Labels: l, MetricValues: map[string]core.MetricValue{ metricName: { ValueType: core.ValueInt64, MetricType: core.MetricGauge, IntValue: 123456, }, }, LabeledMetrics: []core.LabeledMetric{ { Name: labeledMetricNameA, Labels: map[string]string{ core.LabelResourceID.Key: "XYZ", }, MetricValue: core.MetricValue{ MetricType: core.MetricGauge, FloatValue: 124.456, }, }, { Name: labeledMetricNameB, MetricValue: core.MetricValue{ MetricType: core.MetricGauge, FloatValue: 454, }, }, }, } metricSet.LabeledMetrics = append(metricSet.LabeledMetrics, metricValueToLabeledMetric(metricSet.MetricValues)...) now := time.Now() // m, err := hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[2], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelPodId.Key], metricName), m.Id) assert.Equal(t, 1, len(m.Data)) _, ok := m.Data[0].Value.(float64) assert.True(t, ok, "Value should have been converted to float64") delete(l, core.LabelPodId.Key) // m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[2], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelNodename.Key], metricName), m.Id) // m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelNodename.Key], labeledMetricNameA, metricSet.LabeledMetrics[0].Labels[core.LabelResourceID.Key]), m.Id) // m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[1], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelNodename.Key], labeledMetricNameB), m.Id) }
func TestMetricIds(t *testing.T) { hSink := dummySink() l := make(map[string]string) l["spooky"] = "notvisible" l[core.LabelHostname.Key] = "localhost" l[core.LabelHostID.Key] = "localhost" l[core.LabelContainerName.Key] = "docker" l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" l[core.LabelNodename.Key] = "myNode" l[core.LabelNamespaceName.Key] = "myNamespace" metricName := "test/metric/nodeType" metricSet := core.MetricSet{ Labels: l, MetricValues: map[string]core.MetricValue{ metricName: { ValueType: core.ValueInt64, MetricType: core.MetricGauge, IntValue: 123456, }, }, } metricSet.LabeledMetrics = metricValueToLabeledMetric(metricSet.MetricValues) now := time.Now() // m, err := hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelPodId.Key], metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypeNode m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", "machine", metricSet.Labels[core.LabelNodename.Key], metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypePod m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", core.MetricSetTypePod, metricSet.Labels[core.LabelPodId.Key], metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypePodContainer m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelPodId.Key], metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypeSystemContainer m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s/%s", core.MetricSetTypeSystemContainer, metricSet.Labels[core.LabelContainerName.Key], metricSet.Labels[core.LabelPodId.Key], metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypeCluster m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s", core.MetricSetTypeCluster, metricName), m.Id) // metricSet.Labels[core.LabelMetricSetType.Key] = core.MetricSetTypeNamespace m, err = hSink.pointToLabeledMetricHeader(&metricSet, metricSet.LabeledMetrics[0], now) assert.NoError(t, err) assert.Equal(t, fmt.Sprintf("%s/%s/%s", core.MetricSetTypeNamespace, metricSet.Labels[core.LabelNamespaceName.Key], metricName), m.Id) }