Skip to content

Commit

Permalink
Add more tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kisieland committed Mar 22, 2021
1 parent 6c8a457 commit beb54b9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
9 changes: 5 additions & 4 deletions exporter/googlecloudexporter/googlecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ func (me *metricsExporter) pushMetrics(ctx context.Context, m pdata.Metrics) err

func exportAdditionalLabels(mds []internaldata.MetricsData) []internaldata.MetricsData {
for _, md := range mds {
if md.Resource == nil || md.Resource.Labels == nil {
continue
}
if md.Node == nil || md.Node.Identifier == nil || len(md.Node.Identifier.HostName) == 0 {
if md.Resource == nil ||
md.Resource.Labels == nil ||
md.Node == nil ||
md.Node.Identifier == nil ||
len(md.Node.Identifier.HostName) == 0 {
continue
}
// MetricsToOC removes `host.name` label and writes it to node indentifier, here we reintroduce it.
Expand Down
45 changes: 42 additions & 3 deletions exporter/googlecloudexporter/googlecloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ func TestGoogleCloudMetricExport(t *testing.T) {
time.Now(),
[]string{"v0", "v1", "v2"},
metricstestutil.Double(time.Now(), 123))),
metricstestutil.Gauge(
"test_gauge4",
[]string{"k0", "k1", "k2", "k3"},
metricstestutil.Timeseries(
time.Now(),
[]string{"v0", "v1", "v2", "v3"},
metricstestutil.Double(time.Now(), 1234))),
metricstestutil.Gauge(
"test_gauge5",
[]string{"k4", "k5"},
metricstestutil.Timeseries(
time.Now(),
[]string{"v4", "v5"},
metricstestutil.Double(time.Now(), 34))),
},
}
md.Metrics[2].Resource = &resourcepb.Resource{
Expand All @@ -247,15 +261,26 @@ func TestGoogleCloudMetricExport(t *testing.T) {
"contrib.opencensus.io/exporter/stackdriver/project_id": "1234567",
},
}
md.Metrics[3].Resource = &resourcepb.Resource{
Type: "host",
Labels: map[string]string{
"contrib.opencensus.io/exporter/stackdriver/project_id": "1234567",
},
}
md.Metrics[4].Resource = &resourcepb.Resource{
Type: "test",
}

assert.NoError(t, sde.ConsumeMetrics(context.Background(), internaldata.OCToMetrics(md)), err)

expectedNames := map[string]struct{}{
"projects/idk/metricDescriptors/custom.googleapis.com/opencensus/test_gauge1": {},
"projects/idk/metricDescriptors/custom.googleapis.com/opencensus/test_gauge2": {},
"projects/idk/metricDescriptors/custom.googleapis.com/opencensus/test_gauge3": {},
"projects/idk/metricDescriptors/custom.googleapis.com/opencensus/test_gauge4": {},
"projects/idk/metricDescriptors/custom.googleapis.com/opencensus/test_gauge5": {},
}
for i := 0; i < 3; i++ {
for i := 0; i < 5; i++ {
drm := <-descriptorReqCh
assert.Regexp(t, "MyAgent v0\\.0\\.1", drm.metadata["user-agent"])
dr := drm.req.(*cloudmonitoringpb.CreateMetricDescriptorRequest)
Expand All @@ -266,7 +291,7 @@ func TestGoogleCloudMetricExport(t *testing.T) {
trm := <-timeSeriesReqCh
assert.Regexp(t, "MyAgent v0\\.0\\.1", trm.metadata["user-agent"])
tr := trm.req.(*cloudmonitoringpb.CreateTimeSeriesRequest)
require.Len(t, tr.TimeSeries, 3)
require.Len(t, tr.TimeSeries, 5)

resourceFoo := map[string]string{
"node_name": "foo",
Expand All @@ -282,6 +307,10 @@ func TestGoogleCloudMetricExport(t *testing.T) {
"project_id": "1234567",
}

resourceProjectID := map[string]string{
"project_id": "1234567",
}

expectedTimeSeries := map[string]struct {
value float64
labels map[string]string
Expand All @@ -302,8 +331,18 @@ func TestGoogleCloudMetricExport(t *testing.T) {
labels: map[string]string{"k0": "v0", "k1": "v1", "k2": "v2"},
resourceLabels: resourceBar,
},
"custom.googleapis.com/opencensus/test_gauge4": {
value: float64(1234),
labels: map[string]string{"k0": "v0", "k1": "v1", "k2": "v2", "k3": "v3"},
resourceLabels: resourceProjectID,
},
"custom.googleapis.com/opencensus/test_gauge5": {
value: float64(34),
labels: map[string]string{"k4": "v4", "k5": "v5"},
resourceLabels: nil,
},
}
for i := 0; i < 3; i++ {
for i := 0; i < 5; i++ {
require.Contains(t, expectedTimeSeries, tr.TimeSeries[i].Metric.Type)
ts := expectedTimeSeries[tr.TimeSeries[i].Metric.Type]
assert.Equal(t, ts.labels, tr.TimeSeries[i].Metric.Labels)
Expand Down

0 comments on commit beb54b9

Please sign in to comment.