Unverified Commit 12066739 authored by Lemon's avatar Lemon Committed by GitHub

Implement sampling (#55)

* Implement sampling

* Update sample projects
parent bca40262
......@@ -31,7 +31,9 @@ namespace SkyWalking.Config
public static string ApplicationCode { get; set; }
/// <summary>
/// Negative or zeor means off ,by default. Sample_N_Per_3_Secs means sampling N TraceSegment in 10 seconds tops.
/// The number of sampled traces per 3 seconds
/// Negative number means sample traces as many as possible, most likely 100% , by default
///
/// </summary>
public static int Sample_N_Per_3_Secs = -1;
......
......@@ -53,6 +53,7 @@ namespace SkyWalking.AspNetCore
LogManager.SetLoggerFactory(loggerFactory);
AgentConfig.ApplicationCode = options.Value.ApplicationCode;
CollectorConfig.DirectServers = options.Value.DirectServers;
AgentConfig.Sample_N_Per_3_Secs = options.Value.SamplePer3Secs;
_logger = LogManager.GetLogger<SkyWalkingHostedService>();
_diagnosticObserver = diagnosticObserver;
}
......
......@@ -47,5 +47,16 @@ namespace SkyWalking.AspNetCore
get;
set;
}
/// <summary>
/// The number of sampled traces per 3 seconds
/// Negative number means sample traces as many as possible, most likely 100% , by default
///
/// </summary>
public int SamplePer3Secs
{
get;
set;
} = -1;
}
}
\ No newline at end of file
}
......@@ -16,31 +16,49 @@
*
*/
using System;
using System.Threading;
using System.Threading.Tasks;
using SkyWalking.Boot;
using SkyWalking.Config;
using SkyWalking.Utils;
namespace SkyWalking.Sampling
{
public class SamplingService : ISampler
// ReSharper disable once ClassNeverInstantiated.Global
public class SamplingService :TimerService, ISampler
{
private readonly AtomicInteger _atomicInteger = new AtomicInteger();
private readonly int _sample_N_Per_3_Secs = AgentConfig.Sample_N_Per_3_Secs;
private readonly bool _sample_on = AgentConfig.Sample_N_Per_3_Secs > 0;
public bool TrySampling()
{
return true;
if (!_sample_on)
{
return true;
}
return _atomicInteger.Increment() < _sample_N_Per_3_Secs;
}
public void ForceSampled()
{
if (_sample_on)
{
_atomicInteger.Increment();
}
}
public void Dispose()
protected override TimeSpan Interval { get; } = TimeSpan.FromSeconds(3);
protected override Task Execute(CancellationToken token)
{
}
if (_sample_on)
{
_atomicInteger.Value = 0;
}
public int Order { get; } = 1;
public Task Initialize(CancellationToken token)
{
return Task.CompletedTask;
}
}
......
......@@ -24,7 +24,11 @@ namespace SkyWalking.Utils
{
private int _value;
public int Value => _value;
public int Value
{
get => _value;
set => Interlocked.Exchange(ref _value, value);
}
public AtomicInteger()
: this(0)
......
......@@ -60,5 +60,13 @@ namespace SkyWalking.Core.Tests.Utils
Assert.True(atomicInteger == 5);
Assert.False(atomicInteger != 5);
}
[Fact]
public void Set_Value()
{
AtomicInteger atomicInteger = 5;
atomicInteger.Value = 10;
Assert.Equal(10, atomicInteger.Value);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment