Skip to content

Commit

Permalink
Dummy flag to reduce operations on non sampled traces (#194)
Browse files Browse the repository at this point in the history
* DummySegment and DummySubSegment creation

* Revert "DummySegment and DummySubSegment creation"

This reverts commit 4edb4b5.

* Revert "Revert "DummySegment and DummySubSegment creation""

This reverts commit 0442d84.

* ran gofmt command to remove golint warnings

* remove redundent declaration

* Added Dummysegment creation logic

* Added reviewdog suggestions

* modified code based on feedback

* New Implementation of Dummy Segment and Subsegments

* Remove unnecessary code

* Review changes

* removed isDummy method

* Added return statement end of the block
  • Loading branch information
bhautikpip committed Apr 6, 2020
1 parent 5667f5b commit 886d3aa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
43 changes: 40 additions & 3 deletions xray/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func BeginSegmentWithSampling(ctx context.Context, name string, r *http.Request,
}()
}

// check whether segment is dummy or not based on sampling decision
if !seg.ParentSegment.Sampled {
seg.Dummy = true
}

return context.WithValue(ctx, ContextKey, seg), seg
}

Expand All @@ -131,6 +136,7 @@ func basicSegment(name string, h *header.Header) *Segment {
seg.Name = name
seg.StartTime = float64(time.Now().UnixNano()) / float64(time.Second)
seg.InProgress = true
seg.Dummy = false

if h == nil {
seg.TraceID = NewTraceID()
Expand Down Expand Up @@ -228,6 +234,11 @@ func BeginSubsegment(ctx context.Context, name string) (context.Context, *Segmen

seg.ParentSegment = parent.ParentSegment

// check whether segment is dummy or not based on sampling decision
if !seg.ParentSegment.Sampled {
seg.Dummy = true
}

atomic.AddUint32(&seg.ParentSegment.totalSubSegments, 1)

parent.Lock()
Expand Down Expand Up @@ -274,6 +285,12 @@ func (seg *Segment) Close(err error) {
if err != nil {
seg.addError(err)
}

// If segment is dummy we return
if seg.Dummy {
return
}

seg.Unlock()
seg.send()
}
Expand All @@ -297,6 +314,11 @@ func (seg *Segment) CloseAndStream(err error) {
seg.addError(err)
}

// If segment is dummy we return
if seg.Dummy {
return
}

seg.beforeEmitSubsegment(seg.parent)
seg.emit()
}
Expand Down Expand Up @@ -449,15 +471,20 @@ func (seg *Segment) beforeEmitSubsegment(s *Segment) {

// AddAnnotation allows adding an annotation to the segment.
func (seg *Segment) AddAnnotation(key string, value interface{}) error {
seg.Lock()
defer seg.Unlock()

// If segment is dummy we return
if seg.Dummy {
return nil
}

switch value.(type) {
case bool, int, uint, float32, float64, string:
default:
return fmt.Errorf("failed to add annotation key: %q value: %q to subsegment %q. value must be of type string, number or boolean", key, value, seg.Name)
}

seg.Lock()
defer seg.Unlock()

if seg.Annotations == nil {
seg.Annotations = map[string]interface{}{}
}
Expand All @@ -470,6 +497,11 @@ func (seg *Segment) AddMetadata(key string, value interface{}) error {
seg.Lock()
defer seg.Unlock()

// If segment is dummy we return
if seg.Dummy {
return nil
}

if seg.Metadata == nil {
seg.Metadata = map[string]map[string]interface{}{}
}
Expand All @@ -485,6 +517,11 @@ func (seg *Segment) AddMetadataToNamespace(namespace string, key string, value i
seg.Lock()
defer seg.Unlock()

// If segment is dummy we return
if seg.Dummy {
return nil
}

if seg.Metadata == nil {
seg.Metadata = map[string]map[string]interface{}{}
}
Expand Down
3 changes: 3 additions & 0 deletions xray/segment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ type Segment struct {

// Lambda
Facade bool `json:"-"`

// Dummy Segment flag
Dummy bool
}

// CauseData provides the shape for unmarshalling data that records exception.
Expand Down
13 changes: 13 additions & 0 deletions xray/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ func TestParentSegmentTotalCount(t *testing.T) {
assert.Equal(t, 4*uint32(n), seg.ParentSegment.totalSubSegments, "totalSubSegments count should be correctly registered on the parent segment")
}

func TestSegment_isDummy(t *testing.T) {
ctx, root := BeginSegment(context.Background(), "Segment")
ctxSubSeg1, subSeg1 := BeginSubsegment(ctx, "Subegment1")
_, subSeg2 := BeginSubsegment(ctxSubSeg1, "Subsegment2")
subSeg2.Close(nil)
subSeg1.Close(nil)
root.Close(nil)

assert.False(t, root.Dummy)
assert.False(t, subSeg1.Dummy)
assert.False(t, subSeg2.Dummy)
}

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

0 comments on commit 886d3aa

Please sign in to comment.