public void AdaptiveSampler_CheckNumItemsSampled() { SamplingParameters parameters = new SamplingParameters(this.parent, this.traceId, "test", this.activityKindServer, null, null); AdaptiveSampler sampler = new AdaptiveSampler(5, 0.5); int numSampled = 0; // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // sends 10 events in 1 second int events = 10; for (int j = 0; j < events; j++) { SamplingResult result = sampler.ShouldSample(parameters); if (result.IsSampled) { numSampled += 1; } Thread.Sleep(1000 / events); } } int expectedSampled = 10; Assert.Equal(expectedSampled, numSampled / 200); }
private static long SampleThreadTest(int maxItemsAllowed) { long counter = 0; for (int j = 0; j < 200; j++) { for (int i = 0; i < 10; i++) { // System.Console.WriteLine("on span {0}", counter); // Libraries would simply write the following lines of code to // emit activities, which are the .NET representation of OT Spans. var source = new ActivitySource("MyCompany.MyProduct.MyWebServer"); // The below commented out line shows more likely code in a real world webserver. // using (var parent = source.StartActivity("HttpIn", ActivityKind.Server, HttpContext.Request.Headers["traceparent"] )) using (var parent = source.StartActivity("HttpIn", ActivityKind.Server)) { // counter += 1; SamplingParameters parameters = new SamplingParameters(); SamplingResult result = Sampler.ShouldSample(parameters); if (result.IsSampled) { counter++; System.Console.WriteLine("num of items actually sampled: {0})", counter / 200); } // TagNames can follow the OT guidelines // from https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions parent?.AddTag("http.method", "GET"); parent?.AddTag("http.host", "MyHostName"); if (parent != null) { parent.DisplayName = "HttpIn DisplayName"; // IsAllDataRequested is equivalent of Span.IsRecording if (parent.IsAllDataRequested) { parent.AddTag("expensive data", "This data is expensive to obtain. Avoid it if activity is not being recorded"); } } try { // Actual code to achieve the purpose of the library. // For websebserver example, this would be calling // user middlware pipeline. // There can be child activities. // In this example HttpOut is a child of HttpIn. using (var child = source.StartActivity("HttpOut", ActivityKind.Client)) { child?.AddTag("http.url", "www.mydependencyapi.com"); try { // do actual work. child?.AddEvent(new ActivityEvent("sample activity event.")); child?.AddTag("http.status_code", "200"); } catch (Exception) { child?.AddTag("http.status_code", "500"); } } parent?.AddTag("http.status_code", "200"); } catch (Exception) { parent?.AddTag("http.status_code", "500"); } } Thread.Sleep(1000 / 10); // System.Console.WriteLine("num items sampled in: {0}", Sampler.GetItemsSampled()); } } // System.Console.WriteLine("Spans sampled: {0}", counter); System.Console.WriteLine("Press Enter key to exit."); return(Sampler.GetItemsSampled()); }