-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Subsegment streaming strategy. (#83)
* Added Subsegment streaming strategy. * Refactored ShouldStreamSubsegments() to ShouldStream() and StreamSubsegments() to Stream(). Added check for negative number in DefaultStreamingStrategy(). Added unit tests for the same. * added separate tests for custom and negative value of maxSubsegmentSize for DefaultStreamingStrategy.
- Loading branch information
Showing
9 changed files
with
230 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using Amazon.XRay.Recorder.Core.Internal.Entities; | ||
using Amazon.XRay.Recorder.Core.Internal.Emitters; | ||
using Amazon.XRay.Recorder.Core.Sampling; | ||
|
||
namespace Amazon.XRay.Recorder.Core.Strategies | ||
{ | ||
/// <summary> | ||
/// The default streaming strategy. It uses the total count of a segment's children subsegments as a threshold. If the threshold is breached, it uses subtree streaming to stream out. | ||
/// </summary> | ||
public class DefaultStreamingStrategy : IStreamingStrategy | ||
{ | ||
/// <summary> | ||
/// Default max subsegment size to stream for the strategy. | ||
/// </summary> | ||
private const long DefaultMaxSubsegmentSize = 100; | ||
|
||
/// <summary> | ||
/// Max subsegment size to stream fot the strategy. | ||
/// </summary> | ||
public long MaxSubsegmentSize { get; private set; } = DefaultMaxSubsegmentSize; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultStreamingStrategy"/> class. | ||
/// </summary> | ||
public DefaultStreamingStrategy() : this(DefaultMaxSubsegmentSize) | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultStreamingStrategy"/> class. | ||
/// </summary> | ||
/// <param name="maxSubsegmentSize"></param> | ||
public DefaultStreamingStrategy(long maxSubsegmentSize) | ||
{ | ||
if(maxSubsegmentSize < 0) | ||
{ | ||
throw new ArgumentException("maxSubsegmentSize cannot be a negative number."); | ||
} | ||
MaxSubsegmentSize = maxSubsegmentSize; | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether subsegments of the current instance of <see cref="Entity"/> should be streamed. | ||
/// </summary> | ||
/// <param name="entity">Instance of <see cref="Entity"/></param> | ||
/// <returns>True if the subsegments are streamable.</returns> | ||
public bool ShouldStream(Entity entity) | ||
{ | ||
return entity.Sampled == SampleDecision.Sampled && entity.RootSegment != null && entity.RootSegment.Size >= MaxSubsegmentSize; | ||
} | ||
|
||
/// <summary> | ||
/// Streams subsegments of instance of <see cref="Entity"/>. | ||
/// </summary> | ||
/// <param name="entity">Instance of <see cref="Entity"/>.</param> | ||
/// <param name="emitter">Instance of <see cref="ISegmentEmitter"/>.</param> | ||
public void Stream(Entity entity, ISegmentEmitter emitter) | ||
{ | ||
lock (entity.Subsegments) | ||
{ | ||
foreach (var next in entity.Subsegments) | ||
{ | ||
Stream(next, emitter); | ||
} | ||
|
||
entity.Subsegments.RemoveAll(x => x.HasStreamed); | ||
} | ||
|
||
if (entity is Segment || entity.IsInProgress || entity.Reference > 0 || entity.IsSubsegmentsAdded) | ||
{ | ||
return; | ||
} | ||
|
||
Subsegment subsegment = entity as Subsegment; | ||
subsegment.TraceId = entity.RootSegment.TraceId; | ||
subsegment.Type = "subsegment"; | ||
subsegment.ParentId = subsegment.Parent.Id; | ||
emitter.Send(subsegment); | ||
subsegment.RootSegment.DecrementSize(); | ||
subsegment.HasStreamed = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="IStreamingStrategy.cs" company="Amazon.com"> | ||
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
// </copyright> | ||
//----------------------------------------------------------------------------- | ||
|
||
using Amazon.XRay.Recorder.Core.Internal.Entities; | ||
using Amazon.XRay.Recorder.Core.Internal.Emitters; | ||
|
||
namespace Amazon.XRay.Recorder.Core.Strategies | ||
{ | ||
/// <summary> | ||
/// Interface of streaming strategy which is used to determine when and how the subsegments will be streamed. | ||
/// </summary> | ||
public interface IStreamingStrategy | ||
{ | ||
/// <summary> | ||
/// Determines whenther or not the provided segment/subsegment requires any subsegment streaming. | ||
/// </summary> | ||
/// <param name="input">An instance of <see cref="Entity"/>.</param> | ||
/// <returns>true if the segment/subsegment should be streamed.</returns> | ||
bool ShouldStream(Entity entity); | ||
|
||
/// <summary> | ||
/// Streams subsegments of instance of <see cref="Entity"/>. | ||
/// </summary> | ||
/// <param name="entity">Instance of <see cref="Entity"/>.</param> | ||
/// <param name="emitter">Instance if <see cref="ISegmentEmitter"/>.</param> | ||
void Stream(Entity entity, ISegmentEmitter emitter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.