public async Task OpenCensusClientCache_HandlesAddOverflow()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }


            await Task.Delay(2000);

            var newNode = CreateNode(42);

            cache.AddOrUpdate("new", newNode);

            foreach (var node in nodes)
            {
                Assert.IsFalse(cache.TryGet(node.Item1, out _));
            }

            Assert.IsTrue(cache.TryGet("new", out var actualNewNode));
            Assert.AreEqual(newNode, actualNewNode);
        }
        public void OpenCensusClientCache_GetNotExisting()
        {
            var cache = new OpenCensusClientCache <string, Node>();

            Assert.IsFalse(cache.TryGet("client0", out var node));
            Assert.IsNull(node);
        }
        public async Task OpenCensusClientCache_TryGetUpdatesTimestamp()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }


            await Task.Delay(2000);

            Assert.IsTrue(cache.TryGet(nodes[0].Item1, out _));

            cache.AddOrUpdate("new", CreateNode(42));

            for (int i = 1; i < nodes.Length; i++)
            {
                // spin a bit to avoid flakiness
                Assert.IsFalse(cache.TryGet(nodes[i].Item1, out _));
            }
            Assert.IsTrue(cache.TryGet("new", out _));
        }
        public void OpenCensusClientCache_AddsAndUpdates()
        {
            var cache = new OpenCensusClientCache <string, Node>();

            var node0 = CreateNode(0);
            var node1 = CreateNode(1);

            cache.AddOrUpdate("client0", node0);
            cache.AddOrUpdate("client1", node1);
            cache.AddOrUpdate("client0", node1);

            Assert.IsTrue(cache.TryGet("client0", out var actualNode0));
            Assert.AreEqual(node1, actualNode0);

            Assert.IsTrue(cache.TryGet("client1", out var actualNode1));
            Assert.AreEqual(node1, actualNode1);
        }
        public async Task OpenCensusClientCache_DoesNotRemoveItemsBeforeOverflow()
        {
            var cache = new OpenCensusClientCache <string, Node>(10, TimeSpan.FromSeconds(1));

            var nodes = Enumerable.Range(0, 10).Select(i => (i.ToString(), CreateNode((uint)i))).ToArray();

            foreach (var node in nodes)
            {
                cache.AddOrUpdate(node.Item1, node.Item2);
            }

            await Task.Delay(2000);

            foreach (var node in nodes)
            {
                Assert.IsTrue(cache.TryGet(node.Item1, out _));
            }
        }
Exemplo 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);
            }
        }