From 728e41baf49f679ab711dfabe6e7e2d91e5703fb Mon Sep 17 00:00:00 2001 From: dengliming Date: Sat, 15 Aug 2020 23:32:24 +0800 Subject: [PATCH 1/6] Integration tests for OTel Collector Attributes. --- exporters/otlp/otlp_integration_test.go | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/exporters/otlp/otlp_integration_test.go b/exporters/otlp/otlp_integration_test.go index cf767d48e00..011dba6d62d 100644 --- a/exporters/otlp/otlp_integration_test.go +++ b/exporters/otlp/otlp_integration_test.go @@ -28,6 +28,8 @@ import ( commonpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/common/v1" metricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/metrics/v1" + "go.opentelemetry.io/otel/exporters/otlp/internal/transform" + "go.opentelemetry.io/otel/api/kv" "go.opentelemetry.io/otel/api/metric" metricapi "go.opentelemetry.io/otel/api/metric" @@ -426,3 +428,85 @@ func TestNewExporter_withHeaders(t *testing.T) { require.Len(t, headers.Get("header1"), 1) assert.Equal(t, "value1", headers.Get("header1")[0]) } + +func TestNewExporter_withMultipleAttributeTypes(t *testing.T) { + mc := runMockCol(t) + + defer func() { + _ = mc.stop() + }() + + <-time.After(5 * time.Millisecond) + + exp, _ := otlp.NewExporter( + otlp.WithInsecure(), + otlp.WithReconnectionPeriod(50*time.Millisecond), + otlp.WithAddress(mc.address), + ) + + defer func() { + _ = exp.Stop() + }() + + tp, err := sdktrace.NewProvider( + sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}), + sdktrace.WithBatcher(exp, // add following two options to ensure flush + sdktrace.WithBatchTimeout(15), + sdktrace.WithMaxExportBatchSize(10), + )) + assert.NoError(t, err) + + tr := tp.Tracer("test-tracer") + testKvs := []kv.KeyValue{ + kv.Int("k1", 1), + kv.Int32("k2", int32(1)), + kv.Int64("k3", int64(1)), + kv.Float32("k4", float32(1.11)), + kv.Float64("k5", 2.22), + kv.Bool("k6", true), + kv.String("k7", "test"), + } + _, span := tr.Start(context.Background(), "AlwaysSample") + span.SetAttributes(testKvs...) + span.End() + + selector := simple.NewWithExactDistribution() + processor := processor.New(selector, metricsdk.PassThroughExporter) + pusher := push.New(processor, exp) + pusher.Start() + + // Flush and close. + pusher.Stop() + + // Wait >2 cycles. + <-time.After(40 * time.Millisecond) + + // Now shutdown the exporter + if err := exp.Stop(); err != nil { + t.Fatalf("failed to stop the exporter: %v", err) + } + + // Shutdown the collector too so that we can begin + // verification checks of expected data back. + _ = mc.stop() + + // Now verify that we only got one span + rss := mc.getSpans() + if got, want := len(rss), 1; got != want { + t.Fatalf("resource span count: got %d, want %d\n", got, want) + } + + expecteds := transform.Attributes(testKvs) + got := map[string]*commonpb.KeyValue{} + for _, atts := range rss[0].Attributes { + got[atts.Key] = atts + } + + // Verify attributes + if !assert.Len(t, got, len(expecteds)) { + t.Fatalf("attributes count: got %d, want %d\n", len(got), len(expecteds)) + } + for _, expected := range expecteds { + assert.Equal(t, expected, got[expected.Key]) + } +} From 63ff9f7262780f144a664b0f34f486af9ac24d7e Mon Sep 17 00:00:00 2001 From: dengliming Date: Mon, 17 Aug 2020 00:36:32 +0800 Subject: [PATCH 2/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83b42e1dd77..609466b0a58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The environment variable used for resource detection has been changed from `OTEL_RESOURCE_LABELS` to `OTEL_RESOURCE_ATTRIBUTES` (#1042) - Replace `WithSyncer` with `WithBatcher` in examples. (#1044) - Replace the `google.golang.org/grpc/codes` dependency in the API with an equivalent `go.opentelemetry.io/otel/codes` package. (#1046) +- Integration tests for OTel Collector Attributes. (#1062) ### Removed From eb1cc75208558be976dd0a6f6d152d7afd9534cc Mon Sep 17 00:00:00 2001 From: dengliming Date: Mon, 17 Aug 2020 00:47:37 +0800 Subject: [PATCH 3/6] Fix review --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 609466b0a58..46dca43d916 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- Integration tests for more OTel Collector Attribute types. (#1062) + ### Changed - Rename `sdk/metric/processor/test` to `sdk/metric/processor/processortest` @@ -20,7 +24,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The environment variable used for resource detection has been changed from `OTEL_RESOURCE_LABELS` to `OTEL_RESOURCE_ATTRIBUTES` (#1042) - Replace `WithSyncer` with `WithBatcher` in examples. (#1044) - Replace the `google.golang.org/grpc/codes` dependency in the API with an equivalent `go.opentelemetry.io/otel/codes` package. (#1046) -- Integration tests for OTel Collector Attributes. (#1062) ### Removed From 504d41ef07cbca211cd608122aa1d55721d98750 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 18 Aug 2020 21:12:58 +0800 Subject: [PATCH 4/6] Fix review. --- exporters/otlp/otlp_integration_test.go | 96 ++++++++++++++++++++----- 1 file changed, 79 insertions(+), 17 deletions(-) diff --git a/exporters/otlp/otlp_integration_test.go b/exporters/otlp/otlp_integration_test.go index 011dba6d62d..d3304c51e6d 100644 --- a/exporters/otlp/otlp_integration_test.go +++ b/exporters/otlp/otlp_integration_test.go @@ -28,8 +28,6 @@ import ( commonpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/common/v1" metricpb "go.opentelemetry.io/otel/exporters/otlp/internal/opentelemetry-proto-gen/metrics/v1" - "go.opentelemetry.io/otel/exporters/otlp/internal/transform" - "go.opentelemetry.io/otel/api/kv" "go.opentelemetry.io/otel/api/metric" metricapi "go.opentelemetry.io/otel/api/metric" @@ -458,13 +456,13 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) { tr := tp.Tracer("test-tracer") testKvs := []kv.KeyValue{ - kv.Int("k1", 1), - kv.Int32("k2", int32(1)), - kv.Int64("k3", int64(1)), - kv.Float32("k4", float32(1.11)), - kv.Float64("k5", 2.22), - kv.Bool("k6", true), - kv.String("k7", "test"), + kv.Int("Int", 1), + kv.Int32("Int32", int32(2)), + kv.Int64("Int64", int64(3)), + kv.Float32("Float32", float32(1.11)), + kv.Float64("Float64", 2.22), + kv.Bool("Bool", true), + kv.String("String", "test"), } _, span := tr.Start(context.Background(), "AlwaysSample") span.SetAttributes(testKvs...) @@ -496,17 +494,81 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) { t.Fatalf("resource span count: got %d, want %d\n", got, want) } - expecteds := transform.Attributes(testKvs) - got := map[string]*commonpb.KeyValue{} - for _, atts := range rss[0].Attributes { - got[atts.Key] = atts + expected := []*commonpb.KeyValue{ + { + Key: "Int", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_IntValue{ + IntValue: 1, + }, + }, + }, + { + Key: "Int32", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_IntValue{ + IntValue: 2, + }, + }, + }, + { + Key: "Int64", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_IntValue{ + IntValue: 3, + }, + }, + }, + { + Key: "Float32", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_DoubleValue{ + DoubleValue: 1.11, + }, + }, + }, + { + Key: "Float64", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_DoubleValue{ + DoubleValue: 2.22, + }, + }, + }, + { + Key: "Bool", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_BoolValue{ + BoolValue: true, + }, + }, + }, + { + Key: "String", + Value: &commonpb.AnyValue{ + Value: &commonpb.AnyValue_StringValue{ + StringValue: "test", + }, + }, + }, } // Verify attributes - if !assert.Len(t, got, len(expecteds)) { - t.Fatalf("attributes count: got %d, want %d\n", len(got), len(expecteds)) + if !assert.Len(t, rss[0].Attributes, len(expected)) { + t.Fatalf("attributes count: got %d, want %d\n", len(rss[0].Attributes), len(expected)) } - for _, expected := range expecteds { - assert.Equal(t, expected, got[expected.Key]) + for i, actual := range rss[0].Attributes { + if a, ok := actual.Value.Value.(*commonpb.AnyValue_DoubleValue); ok { + e, ok := expected[i].Value.Value.(*commonpb.AnyValue_DoubleValue) + if !ok { + t.Errorf("expected AnyValue_DoubleValue, got %T", expected[i].Value.Value) + continue + } + if !assert.InDelta(t, e.DoubleValue, a.DoubleValue, 0.01) { + continue + } + e.DoubleValue = a.DoubleValue + } + assert.Equal(t, expected[i], actual) } } From 6d5e566e287700112da0a61c1afe704e58260f69 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 18 Aug 2020 23:27:19 +0800 Subject: [PATCH 5/6] Fix test. --- exporters/otlp/otlp_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlp_integration_test.go b/exporters/otlp/otlp_integration_test.go index d3304c51e6d..d6232e2b9d7 100644 --- a/exporters/otlp/otlp_integration_test.go +++ b/exporters/otlp/otlp_integration_test.go @@ -449,7 +449,7 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) { tp, err := sdktrace.NewProvider( sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}), sdktrace.WithBatcher(exp, // add following two options to ensure flush - sdktrace.WithBatchTimeout(15), + sdktrace.WithBatchTimeout(15*time.Millisecond), sdktrace.WithMaxExportBatchSize(10), )) assert.NoError(t, err) From 588fa3fcff6888284b03fca4d17878e3f708e9d7 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 18 Aug 2020 23:39:52 +0800 Subject: [PATCH 6/6] Fix test. --- exporters/otlp/otlp_integration_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/exporters/otlp/otlp_integration_test.go b/exporters/otlp/otlp_integration_test.go index 381ae3913a4..36e23226ee5 100644 --- a/exporters/otlp/otlp_integration_test.go +++ b/exporters/otlp/otlp_integration_test.go @@ -455,14 +455,14 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) { assert.NoError(t, err) tr := tp.Tracer("test-tracer") - testKvs := []kv.KeyValue{ - kv.Int("Int", 1), - kv.Int32("Int32", int32(2)), - kv.Int64("Int64", int64(3)), - kv.Float32("Float32", float32(1.11)), - kv.Float64("Float64", 2.22), - kv.Bool("Bool", true), - kv.String("String", "test"), + testKvs := []label.KeyValue{ + label.Int("Int", 1), + label.Int32("Int32", int32(2)), + label.Int64("Int64", int64(3)), + label.Float32("Float32", float32(1.11)), + label.Float64("Float64", 2.22), + label.Bool("Bool", true), + label.String("String", "test"), } _, span := tr.Start(context.Background(), "AlwaysSample") span.SetAttributes(testKvs...)