public async Task ShutdownTwice()
        {
            using var activityProcessor = new BatchingActivityProcessor(new TestActivityExporter(null));
            await activityProcessor.ShutdownAsync(CancellationToken.None);

            // does not throw
            await activityProcessor.ShutdownAsync(CancellationToken.None);
        }
        public async Task ShutdownWithHugeScheduledDelay()
        {
            using var activityProcessor =
                      new BatchingActivityProcessor(new TestActivityExporter(null), 128, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(100), 32);
            var sw = Stopwatch.StartNew();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)))
            {
                cts.Token.ThrowIfCancellationRequested();
                await activityProcessor.ShutdownAsync(cts.Token).ConfigureAwait(false);
            }

            sw.Stop();
            Assert.InRange(sw.Elapsed, TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
        }
        public async Task ShutdownOnNotEmptyQueueNotFullFlush()
        {
            const int batchSize         = 25;
            int       exportCalledCount = 0;

            var activityExporter = new TestActivityExporter(_ => Interlocked.Increment(ref exportCalledCount), 30000);

            using var activityProcessor =
                      new BatchingActivityProcessor(activityExporter, 128, DefaultDelay, DefaultTimeout, batchSize);
            using (var openTelemetrySdk = Sdk.CreateTracerProviderBuilder()
                                          .AddSource(ActivitySourceName)
                                          .SetSampler(new AlwaysOnSampler())
                                          .AddProcessor(activityProcessor)
                                          .Build())
            {
                var activities = new List <Activity>();
                for (int i = 0; i < 100; i++)
                {
                    activities.Add(this.CreateActivity(i.ToString()));
                }

                Assert.True(activityExporter.ExportedActivities.Length < activities.Count);

                // we won't be able to export all before cancellation will fire
                using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)))
                {
                    bool canceled;
                    try
                    {
                        await activityProcessor.ShutdownAsync(cts.Token);

                        canceled = false;
                    }
                    catch (OperationCanceledException)
                    {
                        canceled = true;
                    }

                    Assert.True(canceled);
                }

                var exportedCount = activityExporter.ExportedActivities.Length;
                Assert.True(exportedCount < activities.Count);
            }
        }
Exemplo n.º 4
0
        public async Task ShutdownOnNotEmptyQueueFullFlush()
        {
            const int batchSize         = 2;
            int       exportCalledCount = 0;
            var       activityExporter  = new TestActivityExporter(_ => Interlocked.Increment(ref exportCalledCount));

            using var activityProcessor =
                      new BatchingActivityProcessor(activityExporter, 128, TimeSpan.FromMilliseconds(100), DefaultTimeout, batchSize);
            using var openTelemetrySdk = OpenTelemetrySdk.EnableOpenTelemetry(b => b
                                                                              .AddActivitySource(ActivitySourceName)
                                                                              .SetSampler(new AlwaysOnActivitySampler())
                                                                              .AddProcessorPipeline(pp => pp.AddProcessor(ap => activityProcessor)));

            using var inMemoryEventListener = new InMemoryEventListener();
            var activities = new List <Activity>();

            for (int i = 0; i < 100; i++)
            {
                activities.Add(this.CreateActivity(i.ToString()));
            }

            Assert.True(activityExporter.ExportedActivities.Length < activities.Count);
            using (var cts = new CancellationTokenSource(DefaultTimeout))
            {
                await activityProcessor.ShutdownAsync(cts.Token);
            }

            // Get the shutdown event.
            // 22 is the EventId for OpenTelemetrySdkEventSource.ForceFlushCompleted
            // TODO: Expose event ids as internal, so tests can access them more reliably.
            var shutdownEvent = inMemoryEventListener.Events.Where((e) => e.EventId == 22).First();

            int droppedCount = 0;

            if (shutdownEvent != null)
            {
                // There is a single payload which is the number of items left in buffer at shutdown.
                droppedCount = (int)shutdownEvent.Payload[0];
            }

            Assert.True(activityExporter.WasShutDown);
            Assert.Equal(activities.Count, droppedCount + activityExporter.ExportedActivities.Length);
            Assert.InRange(exportCalledCount, activities.Count / batchSize, activities.Count);
        }
Exemplo n.º 5
0
        public async Task ShutdownOnNotEmptyQueueNotFullFlush()
        {
            const int batchSize         = 2;
            int       exportCalledCount = 0;

            // we'll need about 1.5 sec to export all activities
            // we export 100 activities in batches of 2, each export takes 30ms, in one thread
            var activityExporter = new TestActivityExporter(_ =>
            {
                Interlocked.Increment(ref exportCalledCount);
                Thread.Sleep(30);
            });

            using var activityProcessor =
                      new BatchingActivityProcessor(activityExporter, 128, TimeSpan.FromMilliseconds(100), DefaultTimeout, batchSize);
            using var openTelemetrySdk = OpenTelemetrySdk.EnableOpenTelemetry(b => b
                                                                              .AddActivitySource(ActivitySourceName)
                                                                              .SetSampler(new AlwaysOnActivitySampler())
                                                                              .AddProcessorPipeline(pp => pp.AddProcessor(ap => activityProcessor)));
            var activities = new List <Activity>();

            for (int i = 0; i < 100; i++)
            {
                activities.Add(this.CreateActivity(i.ToString()));
            }

            Assert.True(activityExporter.ExportedActivities.Length < activities.Count);

            // we won't be able to export all before cancellation will fire
            using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)))
            {
                await activityProcessor.ShutdownAsync(cts.Token);
            }

            var exportedCount = activityExporter.ExportedActivities.Length;

            Assert.True(exportedCount < activities.Count);
        }