From dcc01deec2b6f27a5af3e5e3ecbb7f93f5fb0f8a Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Thu, 4 Feb 2021 16:21:40 -0800 Subject: [PATCH 01/18] added failure scenario when getting container fails --- detectors/aws/ecs/ecs.go | 7 ++++++- detectors/aws/ecs/ecs_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index dcc11955b91..82eb201f142 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -37,6 +37,7 @@ const ( var ( empty = resource.Empty() errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile") + errCannotReadContainerName = errors.New("failed to read hostname") errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile") errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS") ) @@ -102,5 +103,9 @@ func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) { // returns host name reported by the kernel func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) { - return os.Hostname() + hostName, err := os.Hostname() + if err != nil { + return "", errCannotReadContainerName + } + return hostName, nil } diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 85cbb131288..7d667c35f3b 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -81,6 +81,23 @@ func TestDetectCannotReadContainerID(t *testing.T) { assert.Equal(t, 0, len(resource.Attributes())) } +//returns empty resource when detector cannot read container Name +func TestDetectCannotReadContainerName(t *testing.T) { + os.Clearenv() + os.Setenv(metadataV3EnvVar, "3") + os.Setenv(metadataV4EnvVar, "4") + detectorUtils := new(MockDetectorUtils) + + detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) + detectorUtils.On("getContainerID").Return("0123456789A", nil) + + detector := ResourceDetector{detectorUtils} + resource, err := detector.Detect(context.Background()) + + assert.Equal(t, errCannotReadContainerName, err) + assert.Equal(t, 0, len(resource.Attributes())) +} + //returns empty resource when process is not running ECS func TestReturnsIfNoEnvVars(t *testing.T) { os.Clearenv() From 1dd54d977a8dce0027f0ff4a79afa1aa9c49eaaf Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Thu, 4 Feb 2021 16:57:07 -0800 Subject: [PATCH 02/18] fix test case failure --- detectors/aws/ecs/ecs.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 82eb201f142..1bf477345d1 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -35,11 +35,11 @@ const ( ) var ( - empty = resource.Empty() - errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile") - errCannotReadContainerName = errors.New("failed to read hostname") - errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile") - errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS") + empty = resource.Empty() + errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile") + errCannotReadContainerName = errors.New("failed to read hostname") + errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile") + errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS") ) // Create interface for methods needing to be mocked From 03f6fb4f99601c371279378a4c005dc4bf78867e Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Fri, 5 Feb 2021 10:45:07 -0800 Subject: [PATCH 03/18] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91b685de7d9..39bba122bb9 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] +### Fixed + +- Added failure message for AWS ECS resource detector for better debugging (#568) + ## [0.16.0] - 2021-01-13 ### Fixed From e1ff7d0404815b18be13c39521405cc7d731bfe1 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Sat, 6 Feb 2021 17:04:11 -0800 Subject: [PATCH 04/18] fix ecs resource detector bug --- detectors/aws/ecs/ecs.go | 18 ++++++------------ detectors/aws/ecs/ecs_test.go | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 1bf477345d1..02c2718d78a 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -49,19 +49,13 @@ type detectorUtils interface { } // struct implements detectorUtils interface -type ecsDetectorUtils struct{} +type EcsDetectorUtils struct{} // resource detector collects resource information from Elastic Container Service environment type ResourceDetector struct { - utils detectorUtils + Utils detectorUtils } -// compile time assertion that ecsDetectorUtils implements detectorUtils interface -var _ detectorUtils = (*ecsDetectorUtils)(nil) - -// compile time assertion that resource detector implements the resource.Detector interface. -var _ resource.Detector = (*ResourceDetector)(nil) - // Detect finds associated resources when running on ECS environment. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { metadataURIV3 := os.Getenv(metadataV3EnvVar) @@ -70,11 +64,11 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc if len(metadataURIV3) == 0 && len(metadataURIV4) == 0 { return empty, errNotOnECS } - hostName, err := detector.utils.getContainerName() + hostName, err := detector.Utils.getContainerName() if err != nil { return empty, err } - containerID, err := detector.utils.getContainerID() + containerID, err := detector.Utils.getContainerID() if err != nil { return empty, err } @@ -87,7 +81,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc } // returns docker container ID from default c group path -func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) { +func (ecsUtils EcsDetectorUtils) getContainerID() (string, error) { fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { return "", errCannotReadCGroupFile @@ -102,7 +96,7 @@ func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) { } // returns host name reported by the kernel -func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) { +func (ecsUtils EcsDetectorUtils) getContainerName() (string, error) { hostName, err := os.Hostname() if err != nil { return "", errCannotReadContainerName diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 7d667c35f3b..045648ea035 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -45,8 +45,8 @@ func (detectorUtils *MockDetectorUtils) getContainerName() (string, error) { //succesfully return resource when process is running on Amazon ECS environment func TestDetect(t *testing.T) { os.Clearenv() - os.Setenv(metadataV3EnvVar, "3") - os.Setenv(metadataV4EnvVar, "4") + _ = os.Setenv(metadataV3EnvVar, "3") + _ = os.Setenv(metadataV4EnvVar, "4") detectorUtils := new(MockDetectorUtils) @@ -59,51 +59,51 @@ func TestDetect(t *testing.T) { } expectedResource := resource.NewWithAttributes(labels...) detector := ResourceDetector{detectorUtils} - resource, _ := detector.Detect(context.Background()) + res, _ := detector.Detect(context.Background()) - assert.Equal(t, resource, expectedResource, "Resource returned is incorrect") + assert.Equal(t, res, expectedResource, "Resource returned is incorrect") } //returns empty resource when detector cannot read container ID func TestDetectCannotReadContainerID(t *testing.T) { os.Clearenv() - os.Setenv(metadataV3EnvVar, "3") - os.Setenv(metadataV4EnvVar, "4") + _ = os.Setenv(metadataV3EnvVar, "3") + _ = os.Setenv(metadataV4EnvVar, "4") detectorUtils := new(MockDetectorUtils) detectorUtils.On("getContainerName").Return("container-Name", nil) detectorUtils.On("getContainerID").Return("", errCannotReadContainerID) detector := ResourceDetector{detectorUtils} - resource, err := detector.Detect(context.Background()) + res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerID, err) - assert.Equal(t, 0, len(resource.Attributes())) + assert.Equal(t, 0, len(res.Attributes())) } //returns empty resource when detector cannot read container Name func TestDetectCannotReadContainerName(t *testing.T) { os.Clearenv() - os.Setenv(metadataV3EnvVar, "3") - os.Setenv(metadataV4EnvVar, "4") + _ = os.Setenv(metadataV3EnvVar, "3") + _ = os.Setenv(metadataV4EnvVar, "4") detectorUtils := new(MockDetectorUtils) detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) detectorUtils.On("getContainerID").Return("0123456789A", nil) detector := ResourceDetector{detectorUtils} - resource, err := detector.Detect(context.Background()) + res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerName, err) - assert.Equal(t, 0, len(resource.Attributes())) + assert.Equal(t, 0, len(res.Attributes())) } //returns empty resource when process is not running ECS func TestReturnsIfNoEnvVars(t *testing.T) { os.Clearenv() detector := ResourceDetector{} - resource, err := detector.Detect(context.Background()) + res, err := detector.Detect(context.Background()) assert.Equal(t, errNotOnECS, err) - assert.Equal(t, 0, len(resource.Attributes())) + assert.Equal(t, 0, len(res.Attributes())) } From e12a4b185593413d0d459ffcd5507b20362c4819 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Sat, 6 Feb 2021 17:17:55 -0800 Subject: [PATCH 05/18] fix struct name as per golint suggestion --- detectors/aws/ecs/ecs.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 02c2718d78a..a319454e72b 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -43,17 +43,17 @@ var ( ) // Create interface for methods needing to be mocked -type detectorUtils interface { +type detectorUtilsResources interface { getContainerName() (string, error) getContainerID() (string, error) } // struct implements detectorUtils interface -type EcsDetectorUtils struct{} +type DetectorUtils struct{} // resource detector collects resource information from Elastic Container Service environment type ResourceDetector struct { - Utils detectorUtils + Utils detectorUtilsResources } // Detect finds associated resources when running on ECS environment. @@ -81,7 +81,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc } // returns docker container ID from default c group path -func (ecsUtils EcsDetectorUtils) getContainerID() (string, error) { +func (ecsUtils DetectorUtils) getContainerID() (string, error) { fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { return "", errCannotReadCGroupFile @@ -96,7 +96,7 @@ func (ecsUtils EcsDetectorUtils) getContainerID() (string, error) { } // returns host name reported by the kernel -func (ecsUtils EcsDetectorUtils) getContainerName() (string, error) { +func (ecsUtils DetectorUtils) getContainerName() (string, error) { hostName, err := os.Hostname() if err != nil { return "", errCannotReadContainerName From 86c04d1b71f1fcb56703afac36148503098553ec Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Sat, 6 Feb 2021 17:22:49 -0800 Subject: [PATCH 06/18] minor changes --- detectors/aws/ecs/ecs.go | 2 +- detectors/aws/ecs/ecs_test.go | 17 ----------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index a319454e72b..f6237a19f14 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -48,7 +48,7 @@ type detectorUtilsResources interface { getContainerID() (string, error) } -// struct implements detectorUtils interface +// struct implements detectorUtilsResources interface type DetectorUtils struct{} // resource detector collects resource information from Elastic Container Service environment diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 86239f61a81..045648ea035 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -98,23 +98,6 @@ func TestDetectCannotReadContainerName(t *testing.T) { assert.Equal(t, 0, len(res.Attributes())) } -//returns empty resource when detector cannot read container Name -func TestDetectCannotReadContainerName(t *testing.T) { - os.Clearenv() - os.Setenv(metadataV3EnvVar, "3") - os.Setenv(metadataV4EnvVar, "4") - detectorUtils := new(MockDetectorUtils) - - detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) - detectorUtils.On("getContainerID").Return("0123456789A", nil) - - detector := ResourceDetector{detectorUtils} - resource, err := detector.Detect(context.Background()) - - assert.Equal(t, errCannotReadContainerName, err) - assert.Equal(t, 0, len(resource.Attributes())) -} - //returns empty resource when process is not running ECS func TestReturnsIfNoEnvVars(t *testing.T) { os.Clearenv() From 047f5d0c663a283163d8d9154cff2c292bd76b0f Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Tue, 9 Feb 2021 09:58:45 -0800 Subject: [PATCH 07/18] added NewResourceDetector func and interface assertions --- detectors/aws/ecs/ecs.go | 21 ++++++++++++++++----- detectors/aws/ecs/ecs_test.go | 8 ++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index f6237a19f14..5978b1476f7 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -52,23 +52,34 @@ type detectorUtilsResources interface { type DetectorUtils struct{} // resource detector collects resource information from Elastic Container Service environment -type ResourceDetector struct { - Utils detectorUtilsResources +type resourceDetector struct { + utils detectorUtilsResources +} + +// compile time assertion that ecsDetectorUtils implements detectorUtilsResources interface +var _ detectorUtilsResources = (*DetectorUtils)(nil) + +// compile time assertion that resource detector implements the resource.Detector interface. +var _ resource.Detector = (*resourceDetector)(nil) + +// returns resource detector struct +func NewResourceDetector(detectorUtils detectorUtilsResources) resourceDetector{ + return resourceDetector{utils: detectorUtils} } // Detect finds associated resources when running on ECS environment. -func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { +func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { metadataURIV3 := os.Getenv(metadataV3EnvVar) metadataURIV4 := os.Getenv(metadataV4EnvVar) if len(metadataURIV3) == 0 && len(metadataURIV4) == 0 { return empty, errNotOnECS } - hostName, err := detector.Utils.getContainerName() + hostName, err := detector.utils.getContainerName() if err != nil { return empty, err } - containerID, err := detector.Utils.getContainerID() + containerID, err := detector.utils.getContainerID() if err != nil { return empty, err } diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 045648ea035..fda1d4ba210 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -58,7 +58,7 @@ func TestDetect(t *testing.T) { semconv.ContainerIDKey.String("0123456789A"), } expectedResource := resource.NewWithAttributes(labels...) - detector := ResourceDetector{detectorUtils} + detector := NewResourceDetector(detectorUtils) res, _ := detector.Detect(context.Background()) assert.Equal(t, res, expectedResource, "Resource returned is incorrect") @@ -74,7 +74,7 @@ func TestDetectCannotReadContainerID(t *testing.T) { detectorUtils.On("getContainerName").Return("container-Name", nil) detectorUtils.On("getContainerID").Return("", errCannotReadContainerID) - detector := ResourceDetector{detectorUtils} + detector := NewResourceDetector(detectorUtils) res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerID, err) @@ -91,7 +91,7 @@ func TestDetectCannotReadContainerName(t *testing.T) { detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) detectorUtils.On("getContainerID").Return("0123456789A", nil) - detector := ResourceDetector{detectorUtils} + detector := NewResourceDetector(detectorUtils) res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerName, err) @@ -101,7 +101,7 @@ func TestDetectCannotReadContainerName(t *testing.T) { //returns empty resource when process is not running ECS func TestReturnsIfNoEnvVars(t *testing.T) { os.Clearenv() - detector := ResourceDetector{} + detector := NewResourceDetector(nil) res, err := detector.Detect(context.Background()) assert.Equal(t, errNotOnECS, err) From 21db8fabc829a60110299e36658c1f9039b704b7 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Tue, 9 Feb 2021 14:42:29 -0800 Subject: [PATCH 08/18] fix golint failure --- detectors/aws/ecs/ecs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 5978b1476f7..57376e876e9 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -63,8 +63,8 @@ var _ detectorUtilsResources = (*DetectorUtils)(nil) var _ resource.Detector = (*resourceDetector)(nil) // returns resource detector struct -func NewResourceDetector(detectorUtils detectorUtilsResources) resourceDetector{ - return resourceDetector{utils: detectorUtils} +func NewResourceDetector(detectorUtils detectorUtilsResources) resource.Detector { + return &resourceDetector{utils: detectorUtils} } // Detect finds associated resources when running on ECS environment. From 5204d27fd5ab645060c5f07d746404b6b8ca54df Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Tue, 9 Feb 2021 17:24:15 -0800 Subject: [PATCH 09/18] minor changes to address review comments --- detectors/aws/ecs/ecs.go | 22 +++++++++++----------- detectors/aws/ecs/ecs_test.go | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 57376e876e9..2e21bb080b0 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -43,28 +43,28 @@ var ( ) // Create interface for methods needing to be mocked -type detectorUtilsResources interface { +type detectorUtils interface { getContainerName() (string, error) getContainerID() (string, error) } -// struct implements detectorUtilsResources interface -type DetectorUtils struct{} +// struct implements detectorUtils interface +type ecsDetectorUtils struct{} // resource detector collects resource information from Elastic Container Service environment type resourceDetector struct { - utils detectorUtilsResources + utils detectorUtils } -// compile time assertion that ecsDetectorUtils implements detectorUtilsResources interface -var _ detectorUtilsResources = (*DetectorUtils)(nil) +// compile time assertion that ecsDetectorUtils implements detectorUtils interface +var _ detectorUtils = (*ecsDetectorUtils)(nil) // compile time assertion that resource detector implements the resource.Detector interface. var _ resource.Detector = (*resourceDetector)(nil) -// returns resource detector struct -func NewResourceDetector(detectorUtils detectorUtilsResources) resource.Detector { - return &resourceDetector{utils: detectorUtils} +// NewResourceDetector returns a resource detector that will detect AWS ECS resources. +func NewResourceDetector() resource.Detector { + return &resourceDetector{utils: ecsDetectorUtils{}} } // Detect finds associated resources when running on ECS environment. @@ -92,7 +92,7 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc } // returns docker container ID from default c group path -func (ecsUtils DetectorUtils) getContainerID() (string, error) { +func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) { fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { return "", errCannotReadCGroupFile @@ -107,7 +107,7 @@ func (ecsUtils DetectorUtils) getContainerID() (string, error) { } // returns host name reported by the kernel -func (ecsUtils DetectorUtils) getContainerName() (string, error) { +func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) { hostName, err := os.Hostname() if err != nil { return "", errCannotReadContainerName diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index fda1d4ba210..c7719102818 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -58,7 +58,7 @@ func TestDetect(t *testing.T) { semconv.ContainerIDKey.String("0123456789A"), } expectedResource := resource.NewWithAttributes(labels...) - detector := NewResourceDetector(detectorUtils) + detector := &resourceDetector{utils: detectorUtils} res, _ := detector.Detect(context.Background()) assert.Equal(t, res, expectedResource, "Resource returned is incorrect") @@ -74,7 +74,7 @@ func TestDetectCannotReadContainerID(t *testing.T) { detectorUtils.On("getContainerName").Return("container-Name", nil) detectorUtils.On("getContainerID").Return("", errCannotReadContainerID) - detector := NewResourceDetector(detectorUtils) + detector := &resourceDetector{utils: detectorUtils} res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerID, err) @@ -91,7 +91,7 @@ func TestDetectCannotReadContainerName(t *testing.T) { detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) detectorUtils.On("getContainerID").Return("0123456789A", nil) - detector := NewResourceDetector(detectorUtils) + detector := &resourceDetector{utils: detectorUtils} res, err := detector.Detect(context.Background()) assert.Equal(t, errCannotReadContainerName, err) @@ -101,7 +101,7 @@ func TestDetectCannotReadContainerName(t *testing.T) { //returns empty resource when process is not running ECS func TestReturnsIfNoEnvVars(t *testing.T) { os.Clearenv() - detector := NewResourceDetector(nil) + detector := &resourceDetector{utils: nil} res, err := detector.Detect(context.Background()) assert.Equal(t, errNotOnECS, err) From 6b2e3c39de7ab30bac8aaac44340168fff7c0d59 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Thu, 3 Jun 2021 03:21:01 -0700 Subject: [PATCH 10/18] added benchmarks for aws id generator and propagator --- propagators/aws/xray/idgenerator_test.go | 12 ++ propagators/aws/xray/propagator_test.go | 33 ++++ .../aws/xray/sampled_unsampled_span_test.go | 170 ++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 propagators/aws/xray/sampled_unsampled_span_test.go diff --git a/propagators/aws/xray/idgenerator_test.go b/propagators/aws/xray/idgenerator_test.go index 4824e510bdf..3247ed166de 100644 --- a/propagators/aws/xray/idgenerator_test.go +++ b/propagators/aws/xray/idgenerator_test.go @@ -27,6 +27,12 @@ import ( "go.opentelemetry.io/otel/trace" ) +func generateNewTraceID() trace.TraceID { + idg := NewIDGenerator() + traceID, _ := idg.NewIDs(context.Background()) + return traceID +} + func TestTraceIDIsValidLength(t *testing.T) { idg := NewIDGenerator() traceID, _ := idg.NewIDs(context.Background()) @@ -102,3 +108,9 @@ func TestSpanIDIsNotNil(t *testing.T) { assert.False(t, bytes.Equal(spanID1[:], nilSpanID[:]), "SpanID cannot be empty.") assert.False(t, bytes.Equal(spanID2[:], nilSpanID[:]), "SpanID cannot be empty.") } + +func BenchmarkIDGenerator(b *testing.B) { + for i := 0; i < b.N; i++ { + generateNewTraceID() + } +} diff --git a/propagators/aws/xray/propagator_test.go b/propagators/aws/xray/propagator_test.go index 4072f9943f3..035735a9a1e 100644 --- a/propagators/aws/xray/propagator_test.go +++ b/propagators/aws/xray/propagator_test.go @@ -15,6 +15,10 @@ package xray import ( + "context" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" + "net/http" "strings" "testing" @@ -96,3 +100,32 @@ func TestAwsXrayExtract(t *testing.T) { assert.Equal(t, trace.NewSpanContext(test.expected), sc, info...) } } + +func BenchmarkPropagatorExtract(b *testing.B) { + propagator := Propagator{} + + ctx := context.Background() + req, _ := http.NewRequest("GET", "http://example.com", nil) + + req.Header.Set("Root", "1-8a3c60f7-d188f8fa79d48a391a778fa6") + req.Header.Set("Parent", "53995c3f42cd8ad8") + req.Header.Set("Sampled", "1") + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = propagator.Extract(ctx, propagation.HeaderCarrier(req.Header)) + } +} + +func BenchmarkPropagatorInject(b *testing.B) { + propagator := Propagator{} + tracer := otel.Tracer("test") + + req, _ := http.NewRequest("GET", "http://example.com", nil) + ctx, _ := tracer.Start(context.Background(), "Parent operation...") + + b.ResetTimer() + for i := 0; i < b.N; i++ { + propagator.Inject(ctx, propagation.HeaderCarrier(req.Header)) + } +} diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go new file mode 100644 index 00000000000..56b2c3956cb --- /dev/null +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -0,0 +1,170 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package xray + +import ( + "context" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/trace" + "testing" +) + +var tracer = otel.Tracer("sample-app") + +func startAndEndSampledSpan() { + var span trace.Span + _, span = tracer.Start( + context.Background(), + "Example Trace", + ) + + defer span.End() +} + +func startAndEndNestedSampledSpan() { + var span trace.Span + ctx, span := tracer.Start(context.Background(), "Parent operation...") + defer span.End() + + _, span = tracer.Start(ctx, "Sub operation...") + defer span.End() +} + +func getCurrentSampledSpan() trace.Span { + var span trace.Span + ctx, span := tracer.Start( + context.Background(), + "Example Trace", + ) + defer span.End() + + return trace.SpanFromContext(ctx) +} + +func addAttributesToSampledSpan() { + var span trace.Span + _, span = tracer.Start( + context.Background(), + "Example Trace", + ) + defer span.End() + + span.SetAttributes(attribute.Key("example attribute 1").String("value 1")) + span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) +} + +func startAndEndUnSampledSpan() { + var span trace.Span + _, span = tracer.Start( + context.Background(), + "Example Trace", + ) + + defer span.End() +} + +func startAndEndNestedUnSampledSpan() { + var span trace.Span + ctx, span := tracer.Start(context.Background(), "Parent operation...") + defer span.End() + + _, span = tracer.Start(ctx, "Sub operation...") + defer span.End() +} + +func getCurrentUnSampledSpan() trace.Span { + var span trace.Span + ctx, span := tracer.Start( + context.Background(), + "Example Trace", + ) + defer span.End() + + return trace.SpanFromContext(ctx) +} + +func addAttributesToUnSampledSpan() { + var span trace.Span + _, span = tracer.Start( + context.Background(), + "Example Trace", + ) + defer span.End() + + span.SetAttributes(attribute.Key("example attribute 1").String("value 1")) + span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) +} + +func init() { + idg := NewIDGenerator() + + tp := sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithIDGenerator(idg), + ) + + otel.SetTracerProvider(tp) + otel.SetTextMapPropagator(Propagator{}) +} + +func BenchmarkStartAndEndSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + startAndEndSampledSpan() + } +} + +func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + startAndEndNestedSampledSpan() + } +} + +func BenchmarkGetCurrentSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + getCurrentSampledSpan() + } +} + +func BenchmarkAddAttributesToSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + addAttributesToSampledSpan() + } +} + +func BenchmarkStartAndEndUnSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + startAndEndUnSampledSpan() + } +} + +func BenchmarkStartAndEndNestedUnSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + startAndEndNestedUnSampledSpan() + } +} + +func BenchmarkGetCurrentUnSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + getCurrentUnSampledSpan() + } +} + +func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + addAttributesToUnSampledSpan() + } +} \ No newline at end of file From 2622e4ecb9a1d67ba5d8ef3bb1f8a0990e268fb5 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Thu, 3 Jun 2021 03:44:00 -0700 Subject: [PATCH 11/18] ran gofmt --- propagators/aws/xray/sampled_unsampled_span_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 56b2c3956cb..880d8834e4e 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -167,4 +167,4 @@ func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { for i := 0; i < b.N; i++ { addAttributesToUnSampledSpan() } -} \ No newline at end of file +} From c50fed8401c3d7277e8c1a1dcb31d02f5897a9e9 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Thu, 3 Jun 2021 09:11:03 -0700 Subject: [PATCH 12/18] ran precommit --- propagators/aws/xray/propagator_test.go | 5 +++-- propagators/aws/xray/sampled_unsampled_span_test.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/propagators/aws/xray/propagator_test.go b/propagators/aws/xray/propagator_test.go index 035735a9a1e..ecc84063317 100644 --- a/propagators/aws/xray/propagator_test.go +++ b/propagators/aws/xray/propagator_test.go @@ -16,12 +16,13 @@ package xray import ( "context" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/propagation" "net/http" "strings" "testing" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/trace" diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 880d8834e4e..839546b40ff 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -16,11 +16,12 @@ package xray import ( "context" + "testing" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" sdktrace "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/trace" - "testing" ) var tracer = otel.Tracer("sample-app") From ac3b67a0e3660d5cd58587a085fc53c31e7beada Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Mon, 7 Jun 2021 11:48:28 -0700 Subject: [PATCH 13/18] remove idgenerator and added logic to restore global state --- propagators/aws/xray/idgenerator_test.go | 12 --------- .../aws/xray/sampled_unsampled_span_test.go | 26 ++++++++++++++++++- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/propagators/aws/xray/idgenerator_test.go b/propagators/aws/xray/idgenerator_test.go index 3247ed166de..4824e510bdf 100644 --- a/propagators/aws/xray/idgenerator_test.go +++ b/propagators/aws/xray/idgenerator_test.go @@ -27,12 +27,6 @@ import ( "go.opentelemetry.io/otel/trace" ) -func generateNewTraceID() trace.TraceID { - idg := NewIDGenerator() - traceID, _ := idg.NewIDs(context.Background()) - return traceID -} - func TestTraceIDIsValidLength(t *testing.T) { idg := NewIDGenerator() traceID, _ := idg.NewIDs(context.Background()) @@ -108,9 +102,3 @@ func TestSpanIDIsNotNil(t *testing.T) { assert.False(t, bytes.Equal(spanID1[:], nilSpanID[:]), "SpanID cannot be empty.") assert.False(t, bytes.Equal(spanID2[:], nilSpanID[:]), "SpanID cannot be empty.") } - -func BenchmarkIDGenerator(b *testing.B) { - for i := 0; i < b.N; i++ { - generateNewTraceID() - } -} diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 839546b40ff..b9a78508aca 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -110,7 +110,7 @@ func addAttributesToUnSampledSpan() { span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) } -func init() { +func initialization() { idg := NewIDGenerator() tp := sdktrace.NewTracerProvider( @@ -123,49 +123,73 @@ func init() { } func BenchmarkStartAndEndSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { startAndEndSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { startAndEndNestedSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkGetCurrentSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { getCurrentSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkAddAttributesToSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { addAttributesToSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkStartAndEndUnSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { startAndEndUnSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkStartAndEndNestedUnSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { startAndEndNestedUnSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkGetCurrentUnSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { getCurrentUnSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { + initialization() for i := 0; i < b.N; i++ { addAttributesToUnSampledSpan() } + otel.SetTracerProvider(otel.GetTracerProvider()) + otel.SetTextMapPropagator(otel.GetTextMapPropagator()) } From 8fe79d45ade331ee256052c48497d572ae596fa7 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Mon, 7 Jun 2021 12:05:08 -0700 Subject: [PATCH 14/18] added current state of propagator and trace provider --- .../aws/xray/sampled_unsampled_span_test.go | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index b9a78508aca..18bcfe28cd1 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -123,73 +123,98 @@ func initialization() { } func BenchmarkStartAndEndSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { startAndEndSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { startAndEndNestedSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkGetCurrentSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { getCurrentSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkAddAttributesToSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { addAttributesToSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndUnSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { startAndEndUnSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndNestedUnSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { startAndEndNestedUnSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkGetCurrentUnSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { getCurrentUnSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { + currentTraceProvider := otel.GetTracerProvider() + currentPropagator := otel.GetTextMapPropagator() + initialization() for i := 0; i < b.N; i++ { addAttributesToUnSampledSpan() } - otel.SetTracerProvider(otel.GetTracerProvider()) - otel.SetTextMapPropagator(otel.GetTextMapPropagator()) + + otel.SetTracerProvider(currentTraceProvider) + otel.SetTextMapPropagator(currentPropagator) } From 0d0588f768aa6349585fad193f79689d43aa8abe Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Mon, 7 Jun 2021 23:56:08 -0700 Subject: [PATCH 15/18] minor design change --- .../aws/xray/sampled_unsampled_span_test.go | 67 +++++++------------ 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 18bcfe28cd1..3d97a3fca98 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -110,7 +110,7 @@ func addAttributesToUnSampledSpan() { span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) } -func initialization() { +func initialization() func() { idg := NewIDGenerator() tp := sdktrace.NewTracerProvider( @@ -118,103 +118,86 @@ func initialization() { sdktrace.WithIDGenerator(idg), ) + curTp := otel.GetTracerProvider() + curProp := otel.GetTextMapPropagator() + otel.SetTracerProvider(tp) otel.SetTextMapPropagator(Propagator{}) + + return func() { + otel.SetTracerProvider(curTp) + otel.SetTextMapPropagator(curProp) + } } func BenchmarkStartAndEndSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { startAndEndSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { startAndEndNestedSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkGetCurrentSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { getCurrentSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkAddAttributesToSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { addAttributesToSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndUnSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { startAndEndUnSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkStartAndEndNestedUnSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { startAndEndNestedUnSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkGetCurrentUnSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { getCurrentUnSampledSpan() } - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { - currentTraceProvider := otel.GetTracerProvider() - currentPropagator := otel.GetTextMapPropagator() + restore := initialization() + defer restore() - initialization() for i := 0; i < b.N; i++ { addAttributesToUnSampledSpan() } - - otel.SetTracerProvider(currentTraceProvider) - otel.SetTextMapPropagator(currentPropagator) } From bd3d279df80120378cf9f303faafb32bbb4fddc8 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Fri, 18 Jun 2021 09:07:58 -0700 Subject: [PATCH 16/18] removed setting global propagators and minor changes --- .../aws/xray/sampled_unsampled_span_test.go | 100 +----------------- 1 file changed, 1 insertion(+), 99 deletions(-) diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 3d97a3fca98..91f1b51b98e 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -68,49 +68,7 @@ func addAttributesToSampledSpan() { span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) } -func startAndEndUnSampledSpan() { - var span trace.Span - _, span = tracer.Start( - context.Background(), - "Example Trace", - ) - - defer span.End() -} - -func startAndEndNestedUnSampledSpan() { - var span trace.Span - ctx, span := tracer.Start(context.Background(), "Parent operation...") - defer span.End() - - _, span = tracer.Start(ctx, "Sub operation...") - defer span.End() -} - -func getCurrentUnSampledSpan() trace.Span { - var span trace.Span - ctx, span := tracer.Start( - context.Background(), - "Example Trace", - ) - defer span.End() - - return trace.SpanFromContext(ctx) -} - -func addAttributesToUnSampledSpan() { - var span trace.Span - _, span = tracer.Start( - context.Background(), - "Example Trace", - ) - defer span.End() - - span.SetAttributes(attribute.Key("example attribute 1").String("value 1")) - span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) -} - -func initialization() func() { +func init() { idg := NewIDGenerator() tp := sdktrace.NewTracerProvider( @@ -118,86 +76,30 @@ func initialization() func() { sdktrace.WithIDGenerator(idg), ) - curTp := otel.GetTracerProvider() - curProp := otel.GetTextMapPropagator() - otel.SetTracerProvider(tp) - otel.SetTextMapPropagator(Propagator{}) - - return func() { - otel.SetTracerProvider(curTp) - otel.SetTextMapPropagator(curProp) - } } func BenchmarkStartAndEndSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - for i := 0; i < b.N; i++ { startAndEndSampledSpan() } } func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - for i := 0; i < b.N; i++ { startAndEndNestedSampledSpan() } } func BenchmarkGetCurrentSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - for i := 0; i < b.N; i++ { getCurrentSampledSpan() } } func BenchmarkAddAttributesToSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - for i := 0; i < b.N; i++ { addAttributesToSampledSpan() } } -func BenchmarkStartAndEndUnSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - - for i := 0; i < b.N; i++ { - startAndEndUnSampledSpan() - } -} - -func BenchmarkStartAndEndNestedUnSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - - for i := 0; i < b.N; i++ { - startAndEndNestedUnSampledSpan() - } -} - -func BenchmarkGetCurrentUnSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - - for i := 0; i < b.N; i++ { - getCurrentUnSampledSpan() - } -} - -func BenchmarkAddAttributesToUnSampledSpan(b *testing.B) { - restore := initialization() - defer restore() - - for i := 0; i < b.N; i++ { - addAttributesToUnSampledSpan() - } -} From 79e7602f7ae9df9e88cd51735ff787fa761b843c Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Fri, 18 Jun 2021 09:19:12 -0700 Subject: [PATCH 17/18] make precommit changes --- propagators/aws/xray/sampled_unsampled_span_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go index 91f1b51b98e..8aa8d517232 100644 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ b/propagators/aws/xray/sampled_unsampled_span_test.go @@ -102,4 +102,3 @@ func BenchmarkAddAttributesToSampledSpan(b *testing.B) { addAttributesToSampledSpan() } } - From c95f44a1307afd1e1898281872dc61abe24cc393 Mon Sep 17 00:00:00 2001 From: bhautikpip Date: Fri, 18 Jun 2021 10:39:45 -0700 Subject: [PATCH 18/18] removed unnecessary benchmark --- .../aws/xray/idgenerator_benchmark_test.go | 52 +++++++++ .../aws/xray/sampled_unsampled_span_test.go | 104 ------------------ 2 files changed, 52 insertions(+), 104 deletions(-) create mode 100644 propagators/aws/xray/idgenerator_benchmark_test.go delete mode 100644 propagators/aws/xray/sampled_unsampled_span_test.go diff --git a/propagators/aws/xray/idgenerator_benchmark_test.go b/propagators/aws/xray/idgenerator_benchmark_test.go new file mode 100644 index 00000000000..80a7ffec4b4 --- /dev/null +++ b/propagators/aws/xray/idgenerator_benchmark_test.go @@ -0,0 +1,52 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package xray + +import ( + "context" + "testing" + + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/trace" +) + +var tracer trace.Tracer + +func init() { + idg := NewIDGenerator() + + tracer = sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithIDGenerator(idg), + ).Tracer("sample-app") +} + +func BenchmarkStartAndEndSampledSpan(b *testing.B) { + for i := 0; i < b.N; i++ { + _, span := tracer.Start(context.Background(), "Example Trace") + span.End() + } +} + +func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { + ctx, parent := tracer.Start(context.Background(), "Parent operation...") + defer parent.End() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, span := tracer.Start(ctx, "Sub operation...") + span.End() + } +} diff --git a/propagators/aws/xray/sampled_unsampled_span_test.go b/propagators/aws/xray/sampled_unsampled_span_test.go deleted file mode 100644 index 8aa8d517232..00000000000 --- a/propagators/aws/xray/sampled_unsampled_span_test.go +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xray - -import ( - "context" - "testing" - - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - sdktrace "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/trace" -) - -var tracer = otel.Tracer("sample-app") - -func startAndEndSampledSpan() { - var span trace.Span - _, span = tracer.Start( - context.Background(), - "Example Trace", - ) - - defer span.End() -} - -func startAndEndNestedSampledSpan() { - var span trace.Span - ctx, span := tracer.Start(context.Background(), "Parent operation...") - defer span.End() - - _, span = tracer.Start(ctx, "Sub operation...") - defer span.End() -} - -func getCurrentSampledSpan() trace.Span { - var span trace.Span - ctx, span := tracer.Start( - context.Background(), - "Example Trace", - ) - defer span.End() - - return trace.SpanFromContext(ctx) -} - -func addAttributesToSampledSpan() { - var span trace.Span - _, span = tracer.Start( - context.Background(), - "Example Trace", - ) - defer span.End() - - span.SetAttributes(attribute.Key("example attribute 1").String("value 1")) - span.SetAttributes(attribute.Key("example attribute 2").String("value 2")) -} - -func init() { - idg := NewIDGenerator() - - tp := sdktrace.NewTracerProvider( - sdktrace.WithSampler(sdktrace.AlwaysSample()), - sdktrace.WithIDGenerator(idg), - ) - - otel.SetTracerProvider(tp) -} - -func BenchmarkStartAndEndSampledSpan(b *testing.B) { - for i := 0; i < b.N; i++ { - startAndEndSampledSpan() - } -} - -func BenchmarkStartAndEndNestedSampledSpan(b *testing.B) { - for i := 0; i < b.N; i++ { - startAndEndNestedSampledSpan() - } -} - -func BenchmarkGetCurrentSampledSpan(b *testing.B) { - for i := 0; i < b.N; i++ { - getCurrentSampledSpan() - } -} - -func BenchmarkAddAttributesToSampledSpan(b *testing.B) { - for i := 0; i < b.N; i++ { - addAttributesToSampledSpan() - } -}