public async Task GrpcAiInputTests_StopsAndRestarts()
        {
            // ARRANGE
            int            batchesReceived = 0;
            TelemetryBatch receivedBatch   = null;

            int port  = Common.GetPort();
            var input = new GrpcAiInput("localhost", port);

            input.Start(telemetryBatch =>
            {
                batchesReceived++;
                receivedBatch = telemetryBatch;
            });

            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcAiInputTests.DefaultTimeout));

            var grpcWriter = new GrpcWriter(true, port);

            TelemetryBatch batch = new TelemetryBatch();

            batch.Items.Add(new Telemetry()
            {
                Event = new Event()
                {
                    Name = "Event1"
                }
            });

            await grpcWriter.Write(batch).ConfigureAwait(false);

            Common.AssertIsTrueEventually(
                () => input.GetStats().BatchesReceived == 1 && batchesReceived == 1 &&
                receivedBatch.Items.Single().Event.Name == "Event1", GrpcAiInputTests.DefaultTimeout);

            // ACT
            input.Stop();

            Common.AssertIsTrueEventually(
                () => !input.IsRunning && input.GetStats().BatchesReceived == 1 && batchesReceived == 1 &&
                receivedBatch.Items.Single().Event.Name == "Event1", GrpcAiInputTests.DefaultTimeout);

            input.Start(telemetryBatch =>
            {
                batchesReceived++;
                receivedBatch = telemetryBatch;
            });

            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcAiInputTests.DefaultTimeout));

            grpcWriter = new GrpcWriter(true, port);
            batch.Items.Single().Event.Name = "Event2";
            await grpcWriter.Write(batch).ConfigureAwait(false);

            // ASSERT
            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().BatchesReceived == 1 && batchesReceived == 2 &&
                receivedBatch.Items.Single().Event.Name == "Event2", GrpcAiInputTests.DefaultTimeout);
        }
        public async Task GrpcAiInputTests_CantStopWhileStopped()
        {
            // ARRANGE
            int port  = Common.GetPort();
            var input = new GrpcAiInput("localhost", port);

            // ACT
            input.Stop();

            // ASSERT
        }
        public async Task GrpcAiInputTests_CantStartWhileRunning()
        {
            // ARRANGE
            int port  = Common.GetPort();
            var input = new GrpcAiInput("localhost", port);

            input.Start(null);

            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcAiInputTests.DefaultTimeout));

            // ACT
            input.Start(null);

            // ASSERT
        }
Ejemplo n.º 4
0
        public async Task GrpcAiInputTests_ReceivesDataFromMultipleClients()
        {
            // ARRANGE
            int            batchesReceived = 0;
            TelemetryBatch receivedBatch   = null;

            int port  = Common.GetPort();
            var input = new GrpcAiInput("localhost", port);

            input.Start(
                (telemetryBatch, callContext) =>
            {
                Interlocked.Increment(ref batchesReceived);
                receivedBatch = telemetryBatch;
            },
                null);
            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcAiInputTests.DefaultTimeout));

            // ACT
            TelemetryBatch batch = new TelemetryBatch();

            batch.Items.Add(new Telemetry()
            {
                Event = new Event()
                {
                    Name = "Event1"
                }
            });

            Parallel.For(0, 1000, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 1000
            }, async i =>
            {
                var grpcWriter = new GrpcWriter(true, port);

                await grpcWriter.Write(batch).ConfigureAwait(false);
            });

            // ASSERT
            Common.AssertIsTrueEventually(
                () => input.GetStats().BatchesReceived == 1000 && batchesReceived == 1000, GrpcAiInputTests.DefaultTimeout);

            input.Stop();
            Assert.IsTrue(SpinWait.SpinUntil(() => !input.IsRunning, GrpcAiInputTests.DefaultTimeout));
        }
        public async Task GrpcAiInputTests_HandlesExceptionsInProcessingHandler()
        {
            // ARRANGE
            int port  = Common.GetPort();
            var input = new GrpcAiInput("localhost", port);

            input.Start(telemetryBatch => throw new InvalidOperationException());

            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcAiInputTests.DefaultTimeout));

            var grpcWriter = new GrpcWriter(true, port);

            TelemetryBatch batch = new TelemetryBatch();

            batch.Items.Add(new Telemetry()
            {
                Event = new Event()
                {
                    Name = "Event1"
                }
            });

            // ACT
            await grpcWriter.Write(batch).ConfigureAwait(false);

            // ASSERT

            // must have handled the exception by logging it
            // should still be able to process items
            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().BatchesReceived == 0 && input.GetStats().BatchesFailed == 1,
                GrpcAiInputTests.DefaultTimeout);

            await grpcWriter.Write(batch).ConfigureAwait(false);

            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().BatchesReceived == 0 && input.GetStats().BatchesFailed == 2,
                GrpcAiInputTests.DefaultTimeout);
        }
Ejemplo n.º 6
0
        public Library(string configuration)
        {
            this.config = new Configuration(configuration);

            this.ocToAiInstrumentationKey              = config.OpenCensusToApplicationInsights_InstrumentationKey;
            this.liveMetricsStreamInstrumentationKey   = config.ApplicationInsights_LiveMetricsStreamInstrumentationKey;
            this.liveMetricsStreamAuthenticationApiKey = config.ApplicationInsights_LiveMetricsStreamAuthenticationApiKey;
            this.opencensusPeers         = new OpenCensusClientCache <string, Node>();
            this.DefaultOpencensusConfig = new TraceConfig
            {
                ConstantSampler = new ConstantSampler
                {
                    Decision = true
                }
            };

            Diagnostics.LogInfo(
                FormattableString.Invariant($"Loaded configuration. {Environment.NewLine}{configuration}"));

            try
            {
                var activeConfiguration = TelemetryConfiguration.Active;
                activeConfiguration.InstrumentationKey = this.liveMetricsStreamInstrumentationKey;

                var channel = new ServerTelemetryChannel();
                channel.Initialize(activeConfiguration);
                activeConfiguration.TelemetryChannel = channel;

                var builder = activeConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;

                QuickPulseTelemetryProcessor processor = null;
                builder.Use((next) =>
                {
                    processor = new QuickPulseTelemetryProcessor(next);
                    return(processor);
                });

                if (config.ApplicationInsights_AdaptiveSampling_Enabled == true)
                {
                    builder.UseAdaptiveSampling(config.ApplicationInsights_AdaptiveSampling_MaxOtherItemsPerSecond ?? 5, excludedTypes: "Event");
                    builder.UseAdaptiveSampling(config.ApplicationInsights_AdaptiveSampling_MaxEventsPerSecond ?? 5, includedTypes: "Event");
                }

                builder.Build();

                var quickPulseModule = new QuickPulseTelemetryModule()
                {
                    AuthenticationApiKey = this.liveMetricsStreamAuthenticationApiKey
                };
                quickPulseModule.Initialize(activeConfiguration);
                quickPulseModule.RegisterTelemetryProcessor(processor);

                this.telemetryClient = new TelemetryClient(activeConfiguration);
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not initialize AI SDK. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not initialize AI SDK. {e.ToString()}"), e);
            }

            try
            {
                if (this.config.ApplicationInsightsInput_Enabled == true && this.config.ApplicationInsightsInput_Port.HasValue)
                {
                    this.gRpcAiInput = new GrpcAiInput(this.config.ApplicationInsightsInput_Host, this.config.ApplicationInsightsInput_Port.Value);

                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will listen for AI data on {this.config.ApplicationInsightsInput_Host}:{this.config.ApplicationInsightsInput_Port}"));
                }
                else
                {
                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will not listen for AI data"));
                }
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not create the gRPC AI channel. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not create the gRPC AI channel. {e.ToString()}"), e);
            }

            try
            {
                if (this.config.OpenCensusInput_Enabled == true && this.config.OpenCensusInput_Port.HasValue)
                {
                    this.gRpcOpenCensusInput = new GrpcOpenCensusInput(this.config.OpenCensusInput_Host, this.config.OpenCensusInput_Port.Value);

                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will listen for OpenCensus data on {this.config.OpenCensusInput_Host}:{this.config.OpenCensusInput_Port}"));
                }
                else
                {
                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will not listen for OpenCensus data"));
                }
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not create the gRPC OpenCensus channel. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not create the gRPC OpenCensus channel. {e.ToString()}"), e);
            }
        }
Ejemplo n.º 7
0
        public Library(string configuration)
        {
            this.config = new Configuration(configuration);

            this.ocToAiInstrumentationKey            = config.OpenCensusToApplicationInsights_InstrumentationKey;
            this.liveMetricsStreamInstrumentationKey = config.ApplicationInsights_LiveMetricsStreamInstrumentationKey;

            Diagnostics.LogInfo(
                FormattableString.Invariant($"Loaded configuration. {Environment.NewLine}{configuration}"));

            try
            {
                var activeConfiguration = TelemetryConfiguration.Active;
                activeConfiguration.InstrumentationKey = this.liveMetricsStreamInstrumentationKey;

                QuickPulseTelemetryProcessor processor = null;

                activeConfiguration.TelemetryProcessorChainBuilder
                .Use((next) =>
                {
                    processor = new QuickPulseTelemetryProcessor(next);
                    return(processor);
                })
                .Build();

                var QuickPulse = new QuickPulseTelemetryModule();
                QuickPulse.Initialize(activeConfiguration);
                QuickPulse.RegisterTelemetryProcessor(processor);

                this.telemetryClient = new TelemetryClient();
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not initialize AI SDK. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not initialize AI SDK. {e.ToString()}"), e);
            }

            try
            {
                if (this.config.ApplicationInsightsInput_Enabled)
                {
                    this.gRpcAiInput = new GrpcAiInput(this.config.ApplicationInsightsInput_Host, this.config.ApplicationInsightsInput_Port);

                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will listen for AI data on {this.config.ApplicationInsightsInput_Host}:{this.config.ApplicationInsightsInput_Port}"));
                }
                else
                {
                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will not listen for AI data"));
                }
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not create the gRPC AI channel. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not create the gRPC AI channel. {e.ToString()}"), e);
            }

            try
            {
                if (this.config.OpenCensusInput_Enabled)
                {
                    this.gRpcOpenCensusInput = new GrpcOpenCensusInput(this.config.OpenCensusInput_Host, this.config.OpenCensusInput_Port);

                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will listen for OpenCensus data on {this.config.OpenCensusInput_Host}:{this.config.OpenCensusInput_Port}"));
                }
                else
                {
                    Diagnostics.LogInfo(
                        FormattableString.Invariant($"We will not listen for OpenCensus data"));
                }
            }
            catch (Exception e)
            {
                Diagnostics.LogError(
                    FormattableString.Invariant($"Could not create the gRPC OpenCensus channel. {e.ToString()}"));

                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not create the gRPC OpenCensus channel. {e.ToString()}"), e);
            }
        }