Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabling sdk feature #219

Merged
merged 41 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4edb4b5
DummySegment and DummySubSegment creation
bhautikpip Feb 13, 2020
0442d84
Revert "DummySegment and DummySubSegment creation"
bhautikpip Feb 13, 2020
a9d172a
Revert "Revert "DummySegment and DummySubSegment creation""
bhautikpip Feb 13, 2020
7b60842
ran gofmt command to remove golint warnings
bhautikpip Feb 13, 2020
ca86dc6
remove redundent declaration
bhautikpip Feb 13, 2020
aae8545
Merge branch 'master' into master
bhautikpip Feb 25, 2020
b22e192
Merge branch 'master' into master
bhautikpip Feb 28, 2020
a6facdd
Added Dummysegment creation logic
bhautikpip Feb 28, 2020
6b620c6
Added reviewdog suggestions
bhautikpip Feb 28, 2020
e6cacdf
modified code based on feedback
bhautikpip Mar 3, 2020
d45311a
Merge branch 'master' into master
bhautikpip Mar 3, 2020
4dfc445
Merge branch 'master' into master
bhautikpip Mar 30, 2020
c82844f
Merge remote-tracking branch 'upstream/master'
bhautikpip Mar 30, 2020
a51ef85
New Implementation of Dummy Segment and Subsegments
bhautikpip Apr 3, 2020
716928f
Merge remote-tracking branch 'upstream/master'
bhautikpip Apr 3, 2020
e4dbcd0
Merge branch 'master' of https://github.com/bhautikpip/aws-xray-sdk-go
bhautikpip Apr 3, 2020
6737986
Remove unnecessary code
bhautikpip Apr 3, 2020
459e09f
Review changes
bhautikpip Apr 6, 2020
9690d59
Merge branch 'master' into master
bhautikpip Apr 6, 2020
d8c4292
removed isDummy method
bhautikpip Apr 6, 2020
1aacd3e
Merge branch 'master' of https://github.com/bhautikpip/aws-xray-sdk-go
bhautikpip Apr 6, 2020
edae32d
Added return statement end of the block
bhautikpip Apr 6, 2020
c5d17eb
added SDK disabling feature
bhautikpip Apr 12, 2020
762b34a
Merge branch 'master' into Disable-SDK
bhautikpip Apr 12, 2020
7e8bf66
Added README instructions
bhautikpip Apr 12, 2020
37ef4e3
Merge branch 'Disable-SDK' of https://github.com/bhautikpip/aws-xray-…
bhautikpip Apr 12, 2020
63e208a
Added minor fix
bhautikpip Apr 12, 2020
15dd1b5
Added reviewdog fix
bhautikpip Apr 12, 2020
2e1102b
Added disablekey in DownStreamHeader API
bhautikpip Apr 24, 2020
318ebd3
Merge branch 'master' into Disable-SDK
bhautikpip Apr 24, 2020
d85d52e
Review changes
bhautikpip Apr 27, 2020
5e9b290
Merge branch 'Disable-SDK' of https://github.com/bhautikpip/aws-xray-…
bhautikpip Apr 27, 2020
48cc560
Fixed tests
bhautikpip Apr 27, 2020
19ab08a
Merge branch 'master' into Disable-SDK
bhautikpip Apr 28, 2020
2dc8c55
Merge branch 'master' into Disable-SDK
bhautikpip May 12, 2020
6286a80
added function for checking if sdk is disabled
bhautikpip May 30, 2020
94b3982
Merge branch 'Disable-SDK' of https://github.com/bhautikpip/aws-xray-…
bhautikpip May 30, 2020
70040fb
Modified BeginSubSegment disable logic
bhautikpip May 30, 2020
4ab77e1
Merge branch 'master' into Disable-SDK
bhautikpip May 30, 2020
9fe715c
Clean up code and minor chnages
bhautikpip May 31, 2020
5cb8e51
Merge branch 'Disable-SDK' of https://github.com/bhautikpip/aws-xray-…
bhautikpip May 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ func init() {
seg.Close(nil)
```

**Disabling XRay Tracing**

XRay tracing can be disabled by setting up environment variable `AWS_XRAY_SDK_DISABLED` . Disabling XRay can be useful for specific use case like if customer wants to stop tracing in their test environment they can do so just by setting up the environment variable.



```go
// Set environment variable TRUE to disable XRay
os.Setenv("AWS_XRAY_SDK_DISABLED", "TRUE")
```

**Capture**

```go
Expand Down
44 changes: 44 additions & 0 deletions xray/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"os"
"runtime"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -61,6 +62,12 @@ func BeginSegment(ctx context.Context, name string) (context.Context, *Segment)
}

func BeginSegmentWithSampling(ctx context.Context, name string, r *http.Request, traceHeader *header.Header) (context.Context, *Segment) {
// If SDK is disabled then return with an empty segment
if SdkDisabled() {
seg := &Segment{}
bhautikpip marked this conversation as resolved.
Show resolved Hide resolved
return context.WithValue(ctx, ContextKey, seg), seg
}

seg := basicSegment(name, nil)
willarmiros marked this conversation as resolved.
Show resolved Hide resolved

cfg := GetRecorder(ctx)
Expand Down Expand Up @@ -204,6 +211,12 @@ func (seg *Segment) assignConfiguration(cfg *Config) {

// BeginSubsegment creates a subsegment for a given name and context.
func BeginSubsegment(ctx context.Context, name string) (context.Context, *Segment) {
// If SDK is disabled then return with an empty segment
if SdkDisabled() {
seg := &Segment{}
return context.WithValue(ctx, ContextKey, seg), seg
}

if len(name) > 200 {
name = name[:200]
}
Expand Down Expand Up @@ -271,8 +284,19 @@ func NewSegmentFromHeader(ctx context.Context, name string, r *http.Request, h *
return con, seg
}

// Check if SDK is disabled
func SdkDisabled() bool {
disableKey := os.Getenv("AWS_XRAY_SDK_DISABLED")
return strings.ToLower(disableKey) == "true"
}

// Close a segment.
func (seg *Segment) Close(err error) {
// If SDK is disabled then return
if SdkDisabled() {
return
}

seg.Lock()
if seg.parent != nil {
logger.Debugf("Closing subsegment named %s", seg.Name)
Expand All @@ -298,6 +322,11 @@ func (seg *Segment) Close(err error) {

// CloseAndStream closes a subsegment and sends it.
func (seg *Segment) CloseAndStream(err error) {
// If SDK is disabled then return
if SdkDisabled() {
return
}

seg.Lock()
defer seg.Unlock()

Expand Down Expand Up @@ -472,6 +501,11 @@ func (seg *Segment) beforeEmitSubsegment(s *Segment) {

// AddAnnotation allows adding an annotation to the segment.
func (seg *Segment) AddAnnotation(key string, value interface{}) error {
// If SDK is disabled then return
if SdkDisabled() {
return nil
}

seg.Lock()
defer seg.Unlock()

Expand All @@ -495,6 +529,11 @@ func (seg *Segment) AddAnnotation(key string, value interface{}) error {

// AddMetadata allows adding metadata to the segment.
func (seg *Segment) AddMetadata(key string, value interface{}) error {
// If SDK is disabled then return
if SdkDisabled() {
return nil
}

seg.Lock()
defer seg.Unlock()

Expand All @@ -515,6 +554,11 @@ func (seg *Segment) AddMetadata(key string, value interface{}) error {

// AddMetadataToNamespace allows adding a namespace into metadata for the segment.
func (seg *Segment) AddMetadataToNamespace(namespace string, key string, value interface{}) error {
// If SDK is disabled then return
if SdkDisabled() {
return nil
}

seg.Lock()
defer seg.Unlock()

Expand Down
6 changes: 6 additions & 0 deletions xray/segment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ type SQLData struct {
// DownstreamHeader returns a header for passing to downstream calls.
func (s *Segment) DownstreamHeader() *header.Header {
r := &header.Header{}

// If SDK is disabled then return with an empty header
if SdkDisabled() {
return r
}

if parent := s.ParentSegment.IncomingHeader; parent != nil {
*r = *parent // copy parent incoming header
}
Expand Down
57 changes: 57 additions & 0 deletions xray/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"net/http"
"net/url"
"os"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -157,6 +158,62 @@ func TestSegment_isDummy(t *testing.T) {
assert.False(t, subSeg2.Dummy)
}

func TestSDKDisable_inOrder(t *testing.T) {
os.Setenv("AWS_XRAY_SDK_DISABLED", "TRue")
ctx, td := NewTestDaemon()
defer td.Close()
ctx, root := BeginSegment(ctx, "Segment")
ctxSubSeg1, subSeg1 := BeginSubsegment(ctx, "Subegment1")
_, subSeg2 := BeginSubsegment(ctxSubSeg1, "Subsegment2")
subSeg2.Close(nil)
subSeg1.Close(nil)
root.Close(nil)

assert.Equal(t, root, &Segment{})
assert.Equal(t, subSeg1, &Segment{})
assert.Equal(t, subSeg2, &Segment{})

os.Setenv("AWS_XRAY_SDK_DISABLED", "FALSE")
}

func TestSDKDisable_outOrder(t *testing.T) {
os.Setenv("AWS_XRAY_SDK_DISABLED", "TRUE")
ctx, td := NewTestDaemon()
defer td.Close()
_, subSeg := BeginSubsegment(ctx, "Subegment1")
_, seg := BeginSegment(context.Background(), "Segment")

subSeg.Close(nil)
seg.Close(nil)

assert.Equal(t, subSeg, &Segment{})
assert.Equal(t, seg, &Segment{})
os.Setenv("AWS_XRAY_SDK_DISABLED", "FALSE")
}

func TestSDKDisable_otherMethods(t *testing.T) {
os.Setenv("AWS_XRAY_SDK_DISABLED", "true")
ctx, td := NewTestDaemon()
defer td.Close()
ctx, seg := BeginSegment(ctx, "Segment")
_, subSeg := BeginSubsegment(ctx, "Subegment1")

if err := seg.AddAnnotation("key", "value"); err != nil {
return
}
if err := seg.AddMetadata("key", "value"); err != nil {
return
}
seg.DownstreamHeader()

subSeg.Close(nil)
seg.Close(nil)

assert.Equal(t, seg, &Segment{})
assert.Equal(t, subSeg, &Segment{})
os.Setenv("AWS_XRAY_SDK_DISABLED", "FALSE")
}

// Benchmarks
func BenchmarkBeginSegment(b *testing.B) {
ctx, td := NewTestDaemon()
Expand Down