public async Task GrpcOpenCensusInputTests_StopsAndRestarts()
        {
            // ARRANGE
            int batchesReceived             = 0;
            ExportSpanRequest receivedBatch = null;

            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

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

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

            var grpcWriter = new GrpcWriter(false, port);

            ExportSpanRequest batch = new ExportSpanRequest();

            batch.Spans.Add(new Span()
            {
                Name = new TruncatableString()
                {
                    Value = "Event1"
                }
            });

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

            Common.AssertIsTrueEventually(
                () => input.GetStats().BatchesReceived == 1 && batchesReceived == 1 &&
                receivedBatch.Spans.Single().Name.Value == "Event1", GrpcOpenCensusInputTests.DefaultTimeout);

            // ACT
            input.Stop();

            Common.AssertIsTrueEventually(
                () => !input.IsRunning && input.GetStats().BatchesReceived == 1 && batchesReceived == 1 &&
                receivedBatch.Spans.Single().Name.Value == "Event1", GrpcOpenCensusInputTests.DefaultTimeout);

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

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

            grpcWriter = new GrpcWriter(false, port);
            batch.Spans.Single().Name.Value = "Event2";
            await grpcWriter.Write(batch).ConfigureAwait(false);

            // ASSERT
            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().BatchesReceived == 1 && batchesReceived == 2 &&
                receivedBatch.Spans.Single().Name.Value == "Event2", GrpcOpenCensusInputTests.DefaultTimeout);
        }
        public async Task GrpcOpenCensusInputTests_CantStopWhileStopped()
        {
            // ARRANGE
            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            // ACT
            input.Stop();

            // ASSERT
        }
        public async Task GrpcOpenCensusInputTests_ReceivesConfigWithNode()
        {
            // ARRANGE
            int configsReceived = 0;
            CurrentLibraryConfig receivedConfigRequest = null;

            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            input.Start(
                (telemetryBatch, callContext) => { },
                (configRequest, callContext) =>
            {
                configsReceived++;
                receivedConfigRequest = configRequest;
                return(ConfigResponse);
            });
            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcOpenCensusInputTests.DefaultTimeout));

            var grpcWriter = new GrpcWriter(false, port);

            // ACT
            CurrentLibraryConfig request =
                new CurrentLibraryConfig
            {
                Config = new TraceConfig {
                    RateLimitingSampler = new RateLimitingSampler {
                        Qps = 1
                    }
                },
                Node = new Node {
                    Identifier = new ProcessIdentifier {
                        Pid = 2
                    }
                }
            };

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

            // ASSERT
            Common.AssertIsTrueEventually(
                () => input.GetStats().ConfigsReceived == 1 && configsReceived == 1 &&
                receivedConfigRequest.Node.Identifier.Pid == 2 &&
                receivedConfigRequest.Config.RateLimitingSampler.Qps == 1,
                GrpcOpenCensusInputTests.DefaultTimeout);

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

            input.Start(null);

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

            // ACT
            input.Start(null);

            // ASSERT
        }
        public void GrpcOpenCensusInputTests_StartsAndStops()
        {
            // ARRANGE
            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            // ACT
            input.Start(null, null);

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

            input.Stop();

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

            // ASSERT
        }
        public void GrpcOpenCensusInputTests_ReceivesDataFromMultipleClients()
        {
            // ARRANGE
            int batchesReceived = 0;
            ExportTraceServiceRequest receivedBatch = null;

            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            input.Start(
                (exportSpanRequest, callContext) =>
            {
                Interlocked.Increment(ref batchesReceived);
                receivedBatch = exportSpanRequest;
            },
                (configRequest, callContext) => ConfigResponse);
            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcOpenCensusInputTests.DefaultTimeout));

            // ACT
            ExportTraceServiceRequest batch = new ExportTraceServiceRequest();

            batch.Spans.Add(new Span()
            {
                Name = new TruncatableString()
                {
                    Value = "Event1"
                }
            });

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

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

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

            input.Stop();
            Assert.IsTrue(SpinWait.SpinUntil(() => !input.IsRunning, GrpcOpenCensusInputTests.DefaultTimeout));
        }
        public async Task GrpcOpenCensusInputTests_ReceivesSpansWithNode()
        {
            // ARRANGE
            int batchesReceived = 0;
            ExportTraceServiceRequest receivedBatch = null;

            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            input.Start(
                (telemetryBatch, callContext) =>
            {
                batchesReceived++;
                receivedBatch = telemetryBatch;
            },
                null);
            Assert.IsTrue(SpinWait.SpinUntil(() => input.IsRunning, GrpcOpenCensusInputTests.DefaultTimeout));

            var grpcWriter = new GrpcWriter(false, port);

            // ACT
            ExportTraceServiceRequest batch = new ExportTraceServiceRequest();

            batch.Spans.Add(new Span {
                Name = new TruncatableString {
                    Value = "Event1"
                }
            });
            batch.Node = new Node {
                Identifier = new ProcessIdentifier {
                    Pid = 2
                }
            };

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

            // ASSERT
            Common.AssertIsTrueEventually(
                () => input.GetStats().BatchesReceived == 1 && batchesReceived == 1 &&
                receivedBatch.Node.Identifier.Pid == 2 &&
                receivedBatch.Spans.Single().Name.Value == "Event1",
                GrpcOpenCensusInputTests.DefaultTimeout);

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

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

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

            var grpcWriter = new GrpcWriter(false, port);

            ExportSpanRequest batch = new ExportSpanRequest();

            batch.Spans.Add(new Span()
            {
                Name = new TruncatableString()
                {
                    Value = "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,
                GrpcOpenCensusInputTests.DefaultTimeout);

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

            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().BatchesReceived == 0 && input.GetStats().BatchesFailed == 2,
                GrpcOpenCensusInputTests.DefaultTimeout);
        }
        public async Task GrpcOpenCensusInputTests_HandlesExceptionsInConfigRequestsProcessingHandler()
        {
            // ARRANGE
            int port  = GetPort();
            var input = new GrpcOpenCensusInput("localhost", port);

            input.Start((exportSpanRequest, callContext) => { }, (cr, cc) => throw new InvalidOperationException());

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

            var grpcWriter = new GrpcWriter(false, port);

            CurrentLibraryConfig configRequest = new CurrentLibraryConfig
            {
                Config = new TraceConfig {
                    RateLimitingSampler = new RateLimitingSampler {
                        Qps = 1
                    }
                }
            };

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

            // ASSERT

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

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

            Common.AssertIsTrueEventually(
                () => input.IsRunning && input.GetStats().ConfigsReceived == 0 && input.GetStats().ConfigsFailed == 2,
                GrpcOpenCensusInputTests.DefaultTimeout);
        }
示例#10
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);
            }
        }
示例#11
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);
            }
        }