public void CaptureEverythingConfigurationWorks()
        {
            var healthReporter = new Mock <IHealthReporter>();
            var observer       = new TestObserver();
            var ActivityNameA  = GetRandomName();
            var ActivityNameB  = GetRandomName();

            var sources = new List <ActivitySourceConfiguration>(new[]
            {
                // Empty configuration means capture all activities from all sources,
                // with all data, and both start and stop events
                new ActivitySourceConfiguration {
                }
            });

            using (var input = new ActivitySourceInput(new ActivitySourceInputConfiguration {
                Sources = sources
            }, healthReporter.Object))
            {
                input.Subscribe(observer);

                var activity = SourceOne.StartActivity(ActivityNameA);
                activity.Stop();

                activity = SourceTwo.StartActivity(ActivityNameB);
                activity.AddEvent(new ActivityEvent("ActivityB.Event1"));
                activity.Stop();
            }

            healthReporter.VerifyNoOtherCalls();

            Assert.True(observer.Completed);
            Assert.Null(observer.Error);

            var observed = observer.Data.ToArray();

            // We expect one stop evente for both activities, total 2.
            // There might be more because we are capturing, well, everyting in the system %-)
            Assert.Equal(2, observed.Count(e =>
                                           (OrdinalEquals(e.Payload["Name"], ActivityNameA) && OrdinalEquals(e.Payload["ActivitySourceName"], SourceOneName)) ||
                                           (OrdinalEquals(e.Payload["Name"], ActivityNameB) && OrdinalEquals(e.Payload["ActivitySourceName"], SourceTwoName))
                                           ));

            // Verify that all data was captured by checking for activity event associated with activity B
            var activityB_Stop = observed.First(e =>
                                                OrdinalEquals(e.Payload["Name"], ActivityNameB) &&
                                                OrdinalEquals(e.Payload["ActivitySourceName"], SourceTwoName) &&
                                                e.Payload.ContainsKey("EndTime")
                                                );

            Assert.Equal(1, ((IEnumerable <ActivityEvent>)activityB_Stop.Payload["Events"]).Count(ae => OrdinalEquals(ae.Name, "ActivityB.Event1")));
        }
        public void SourceCanBeCreatedAfterInput()
        {
            var healthReporter = new Mock <IHealthReporter>();
            var observer       = new TestObserver();
            var ActivityName   = GetRandomName();

            const string TestSourceName = "EventFlowTestActivitySource";
            var          sources        = new List <ActivitySourceConfiguration>(new[]
            {
                new ActivitySourceConfiguration
                {
                    ActivitySourceName = TestSourceName,
                    CapturedEvents     = CapturedActivityEvents.Stop
                }
            });

            using (var input = new ActivitySourceInput(new ActivitySourceInputConfiguration {
                Sources = sources
            }, healthReporter.Object))
            {
                input.Subscribe(observer);

                using (var activitySouce = new System.Diagnostics.ActivitySource("EventFlowTestActivitySource"))
                {
                    var activity = activitySouce.StartActivity(ActivityName);
                    activity.Stop();
                }
            }

            healthReporter.VerifyNoOtherCalls();

            Assert.True(observer.Completed);
            Assert.Null(observer.Error);

            var observed = observer.Data.ToArray();
            var e        = Assert.Single(observed);

            Assert.Equal(ActivityName, e.Payload["Name"]);
        }
        public void BasicActivityTracking()
        {
            var healthReporter = new Mock <IHealthReporter>();
            var observer       = new TestObserver();
            var ActivityName   = GetRandomName();

            var sources = new List <ActivitySourceConfiguration>(new[]
            {
                new ActivitySourceConfiguration
                {
                    ActivitySourceName = SourceOneName,
                    ActivityName       = ActivityName,
                    CapturedData       = ActivitySamplingResult.AllData,
                    CapturedEvents     = CapturedActivityEvents.Both
                }
            });

            using (var input = new ActivitySourceInput(new ActivitySourceInputConfiguration {
                Sources = sources
            }, healthReporter.Object))
            {
                input.Subscribe(observer);

                var ctx = new ActivityContext(
                    ActivityTraceId.CreateFromString(WellKnownTraceId),
                    ActivitySpanId.CreateFromString(SpanIdOne),
                    ActivityTraceFlags.None);
                var activity = SourceOne.StartActivity(ActivityName, ActivityKind.Internal, ctx);
                activity.Stop();
            }

            healthReporter.VerifyNoOtherCalls();

            Assert.Equal(2, observer.Data.Count);
            Assert.True(observer.Completed);
            Assert.Null(observer.Error);

            Assert.True(observer.Data.TryDequeue(out EventData e));
            VerifyActivityEvent(e, ActivityName, SourceOneName, CapturedActivityEvents.Start, WellKnownTraceId, SpanIdOne);
            Assert.True(observer.Data.TryDequeue(out e));
            VerifyActivityEvent(e, ActivityName, SourceOneName, CapturedActivityEvents.Stop, WellKnownTraceId, SpanIdOne);
        }