-
Notifications
You must be signed in to change notification settings - Fork 287
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
Made sampling based on UserId configurable #650
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor.AllowSamplingBasedOnUserId.get -> bool | ||
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor.AllowSamplingBasedOnUserId.set -> void | ||
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.SamplingTelemetryProcessor.AllowSamplingBasedOnUserId.get -> bool | ||
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.SamplingTelemetryProcessor.AllowSamplingBasedOnUserId.set -> void |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,8 @@ public void SamplingScoreGeneratedUsingUserIdIfPresent() | |
requestTelemetry.Context.User.Id = userId; | ||
requestTelemetry.Context.Operation.Id = GenerateRandomOperaitonId(); | ||
|
||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry); | ||
var requestTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(requestTelemetry); | ||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry, true); | ||
var requestTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(requestTelemetry, true); | ||
|
||
Assert.AreEqual(eventTelemetrySamplingScore, requestTelemetrySamplingScore, 12); | ||
} | ||
|
@@ -44,20 +44,46 @@ public void SamplingScoreGeneratedUsingOperationIdIfPresent() | |
var requestTelemetry = new RequestTelemetry(); | ||
requestTelemetry.Context.Operation.Id = operationId; | ||
|
||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry); | ||
var requestTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(requestTelemetry); | ||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry, true); | ||
var requestTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(requestTelemetry, true); | ||
|
||
Assert.AreEqual(eventTelemetrySamplingScore, requestTelemetrySamplingScore, 12); | ||
} | ||
|
||
[TestMethod] | ||
public void SamplingScoreGeneratedSkipsUserId() | ||
{ | ||
string operationId = GenerateRandomOperaitonId(); | ||
string userId = GenerateRandomUserId(); | ||
|
||
var eventTelemetry = new EventTelemetry(); | ||
eventTelemetry.Context.Operation.Id = operationId; | ||
eventTelemetry.Context.User.Id = userId; | ||
|
||
var requestTelemetry = new RequestTelemetry(); | ||
requestTelemetry.Context.Operation.Id = operationId; | ||
requestTelemetry.Context.User.Id = userId; | ||
|
||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ever test it with |
||
var requestTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(requestTelemetry, true); | ||
|
||
Assert.AreEqual(eventTelemetrySamplingScore, requestTelemetrySamplingScore, 12); | ||
|
||
var eventTelemetrySamplingScoreNoUserConfig = SamplingScoreGenerator.GetSamplingScore(eventTelemetry, true); | ||
var requestTelemetrySamplingScoreNoUserConfig = SamplingScoreGenerator.GetSamplingScore(requestTelemetry, true); | ||
|
||
Assert.AreNotEqual(eventTelemetrySamplingScore, eventTelemetrySamplingScoreNoUserConfig); | ||
Assert.AreNotEqual(requestTelemetrySamplingScore, requestTelemetrySamplingScoreNoUserConfig); | ||
} | ||
|
||
[TestMethod] | ||
public void SamplingScoreIsRandomIfUserIdAndOperationIdAreNotPresent() | ||
{ | ||
var eventTelemetry = new EventTelemetry(); | ||
var traceTelemetry = new TraceTelemetry(); | ||
|
||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry); | ||
var traceTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(traceTelemetry); | ||
var eventTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(eventTelemetry, true); | ||
var traceTelemetrySamplingScore = SamplingScoreGenerator.GetSamplingScore(traceTelemetry, true); | ||
|
||
Assert.AreNotEqual(eventTelemetrySamplingScore, traceTelemetrySamplingScore); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,13 @@ internal static class SamplingScoreGenerator | |
/// Generates telemetry sampling score between 0 and 100. | ||
/// </summary> | ||
/// <param name="telemetry">Telemetry item to score.</param> | ||
/// <param name="allowUserId">Sampling score can be determined by using the Context.User.Id information, default is false.</param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add remark that is can be used for backward compatibility |
||
/// <returns>Item sampling score.</returns> | ||
public static double GetSamplingScore(ITelemetry telemetry) | ||
public static double GetSamplingScore(ITelemetry telemetry, bool allowUserId = false) | ||
{ | ||
double samplingScore = 0; | ||
|
||
if (telemetry.Context.User.Id != null) | ||
if (allowUserId && telemetry.Context.User.Id != null) | ||
{ | ||
samplingScore = (double)telemetry.Context.User.Id.GetSamplingHashCode() / int.MaxValue; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ public SamplingTelemetryProcessor(ITelemetryProcessor next) | |
{ RequestTelemetryName, typeof(RequestTelemetry) }, | ||
{ TraceTelemetryName, typeof(TraceTelemetry) }, | ||
}; | ||
this.AllowSamplingBasedOnUserId = false; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -124,6 +125,17 @@ public string IncludedTypes | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether or not we should calculate sampling based on the Context.User.Id field. | ||
/// Default is false. | ||
/// | ||
/// <remarks> | ||
/// Calculation of the sampling score based on user_Id field is disabled by default to ensure successful correlation across many | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: why are you switching to Kusto names of the fields in remark? |
||
/// services on operation_Id field. | ||
/// </remarks> | ||
/// </summary> | ||
public bool AllowSamplingBasedOnUserId { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/// <summary> | ||
/// Gets or sets data sampling percentage (between 0 and 100) for all <see cref="ITelemetry"/> | ||
/// objects logged in this <see cref="TelemetryClient"/>. | ||
|
@@ -187,7 +199,7 @@ public void Process(ITelemetry item) | |
//// Ok, now we can actually sample: | ||
|
||
samplingSupportingTelemetry.SamplingPercentage = samplingPercentage; | ||
bool isSampledIn = SamplingScoreGenerator.GetSamplingScore(item) < samplingPercentage; | ||
bool isSampledIn = SamplingScoreGenerator.GetSamplingScore(item, this.AllowSamplingBasedOnUserId) < samplingPercentage; | ||
|
||
if (isSampledIn) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update the name of the test method.
IfPresentAndConfigured