Example #1
0
            public void SendWillBeMarkedAsInternalOperation()
            {
                bool wasCalled = false;
                var  channel   = new ServerTelemetryChannel();

                channel.TelemetrySerializer = new TelemetrySerializerStub(channel.Transmitter)
                {
                    WasCalled = (called) => { wasCalled = called; }
                };
#if NETCOREAPP
                channel.TelemetryBuffer = new TelemetryChannel.Implementation.TelemetryBuffer(channel.TelemetrySerializer, null);
#else
                channel.TelemetryBuffer = new TelemetryChannel.Implementation.TelemetryBuffer(channel.TelemetrySerializer, new WebApplicationLifecycle());
#endif
                channel.TelemetryProcessor         = channel.TelemetryBuffer;
                channel.MaxTelemetryBufferCapacity = 1;
                channel.Initialize(TelemetryConfiguration.CreateDefault());

                var telemetry = new StubTelemetry();
                telemetry.Context.InstrumentationKey = Guid.NewGuid().ToString();
                channel.Send(telemetry);
                Thread.Sleep(TimeSpan.FromSeconds(1));

                Assert.IsTrue(wasCalled);
            }
Example #2
0
            public void DropsTelemetryWithNoInstrumentationKey()
            {
                ITelemetry sentTelemetry = null;
                var        channel       = new ServerTelemetryChannel();

                channel.Initialize(TelemetryConfiguration.CreateDefault());
                channel.TelemetryProcessor = new StubTelemetryProcessor(null)
                {
                    OnProcess = (t) => sentTelemetry = t
                };

                var telemetry = new StubTelemetry();

                // No instrumentation key

                using (TestEventListener listener = new TestEventListener())
                {
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Verbose);

                    channel.Send(telemetry);

                    Assert.IsNull(sentTelemetry);
                    var expectedMessage = listener.Messages.First();
                    Assert.AreEqual(67, expectedMessage.EventId);
                }
            }
        public void ChannelLogsResponseBodyFromTransmissionWhenVerboseEnabled()
        {
            var expectedResponseContents = "this is the expected response";

            using (var localServer = new LocalInProcHttpServer(Localurl))
            {
                localServer.ServerLogic = async(ctx) =>
                {
                    await ctx.Response.WriteAsync(expectedResponseContents);
                };

                var channel = new ServerTelemetryChannel
                {
                    DeveloperMode   = true,
                    EndpointAddress = Localurl
                };
                var config = new TelemetryConfiguration("dummy")
                {
                    TelemetryChannel = channel
                };
                channel.Initialize(config);

                using (var listener = new TestEventListener())
                {
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.LogAlways,
                                          (EventKeywords)AllKeywords);

                    // Enable CoreEventSource as Transmission logic is in base sdk.
                    // and it'll parse response only on Verbose enabled.
                    using (var listenerCore = new TestEventListener())
                    {
                        listener.EnableEvents(CoreEventSource.Log, EventLevel.LogAlways,
                                              (EventKeywords)AllKeywords);


                        // ACT
                        var telemetry = new EventTelemetry("test event name");
                        telemetry.Context.InstrumentationKey = "dummy";
                        channel.Send(telemetry);
                        Thread.Sleep(SleepInMilliseconds);
                    }

                    // Assert:
                    var allTraces = listener.Messages.ToList();
                    var traces    = allTraces.Where(item => item.EventId == 70).ToList();
                    Assert.AreEqual(1, traces.Count);
                    Assert.IsTrue(traces[0].Payload[1].ToString().Contains(expectedResponseContents));
                }
            }
        }
            public void PassesTelemetryToTelemetryProcessor()
            {
                ITelemetry sentTelemetry = null;
                var        channel       = new ServerTelemetryChannel();

                channel.TelemetryProcessor = new StubTelemetryProcessor(null)
                {
                    OnProcess = (t) => sentTelemetry = t
                };

                var telemetry = new StubTelemetry();

                channel.Send(telemetry);

                Assert.Equal(telemetry, sentTelemetry);
            }
            public void PassesTelemetryToTelemetryProcessor()
            {
                ITelemetry sentTelemetry = null;
                var        channel       = new ServerTelemetryChannel();

                channel.Initialize(TelemetryConfiguration.CreateDefault());
                channel.TelemetryProcessor = new StubTelemetryProcessor(null)
                {
                    OnProcess = (t) => sentTelemetry = t
                };

                var telemetry = new StubTelemetry();

                channel.Send(telemetry);

                Assert.Equal(telemetry, sentTelemetry);
            }
        public void ChannelLogsFailedTransmissionDueToServerError()
        {
            using (var localServer = new LocalInProcHttpServer(Localurl))
            {
                localServer.ServerLogic = async(ctx) =>
                {
                    // Error from AI Backend.
                    ctx.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    await ctx.Response.WriteAsync("InternalError");
                };

                var channel = new ServerTelemetryChannel
                {
                    DeveloperMode   = true,
                    EndpointAddress = Localurl
                };
                var config = new TelemetryConfiguration("dummy")
                {
                    TelemetryChannel = channel
                };
                channel.Initialize(config);

                using (var listener = new TestEventListener())
                {
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.LogAlways,
                                          (EventKeywords)AllKeywords);

                    // ACT
                    var telemetry = new EventTelemetry("test event name");
                    telemetry.Context.InstrumentationKey = "dummy";
                    channel.Send(telemetry);
                    Thread.Sleep(SleepInMilliseconds);

                    // VERIFY
                    // We validate by checking SDK traces
                    var allTraces = listener.Messages.ToList();

                    // Event 54 is logged upon successful transmission.
                    var traces = allTraces.Where(item => item.EventId == 54).ToList();
                    Assert.AreEqual(1, traces.Count);
                    // 500 is the response code.
                    Assert.AreEqual("500", traces[0].Payload[1]);
                }
            }
        }
Example #7
0
            public void PassesTelemetryToTelemetryProcessor()
            {
                ITelemetry sentTelemetry = null;
                var        channel       = new ServerTelemetryChannel();

                channel.Initialize(TelemetryConfiguration.CreateDefault());
                channel.TelemetryProcessor = new StubTelemetryProcessor(null)
                {
                    OnProcess = (t) => sentTelemetry = t
                };

                var telemetry = new StubTelemetry();

                telemetry.Context.InstrumentationKey = Guid.NewGuid().ToString();
                channel.Send(telemetry);

                Assert.AreEqual(telemetry, sentTelemetry);
            }
        public void ChannelHandlesFailedTransmissionDueToUnknownNetworkError()
        {
            using (var localServer = new LocalInProcHttpServer(Localurl))
            {
                localServer.ServerLogic = async(ctx) =>
                {
                    // This code does not matter as Channel is configured to
                    // with an incorrect endpoint.
                    await ctx.Response.WriteAsync("InternalError");
                };

                var channel = new ServerTelemetryChannel
                {
                    DeveloperMode   = true,
                    EndpointAddress = LocalurlNotRunning
                };
                var config = new TelemetryConfiguration("dummy")
                {
                    TelemetryChannel = channel
                };
                channel.Initialize(config);

                using (var listener = new TestEventListener())
                {
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.LogAlways,
                                          (EventKeywords)AllKeywords);

                    // ACT
                    var telemetry = new EventTelemetry("test event name");
                    telemetry.Context.InstrumentationKey = "dummy";
                    channel.Send(telemetry);
                    Thread.Sleep(SleepInMilliseconds);

                    // Assert:
                    var allTraces = listener.Messages.ToList();
                    var traces    = allTraces.Where(item => item.EventId == 54).ToList();
                    Assert.AreEqual(1, traces.Count);
                    Assert.IsTrue(traces[0].Payload[1].ToString().Contains("An error occurred while sending the request"));
                }
            }
        }
        public void ChannelSendsTransmission()
        {
            using (var localServer = new LocalInProcHttpServer(Localurl))
            {
                IList <ITelemetry> telemetryItems = new List <ITelemetry>();
                var telemetry = new EventTelemetry("test event name");
                telemetry.Context.InstrumentationKey = "dummy";
                telemetryItems.Add((telemetry));
                var serializedExpected = JsonSerializer.Serialize(telemetryItems);

                localServer.ServerLogic = async(ctx) =>
                {
                    byte[] buffer = new byte[2000];
                    await ctx.Request.Body.ReadAsync(buffer, 0, 2000);

                    Assert.AreEqual(serializedExpected, buffer);
                    await ctx.Response.WriteAsync("Ok");
                };

                var channel = new ServerTelemetryChannel
                {
                    DeveloperMode   = true,
                    EndpointAddress = Localurl
                };

                var config = new TelemetryConfiguration("dummy")
                {
                    TelemetryChannel = channel
                };
                channel.Initialize(config);

                // ACT
                // Data would be sent to the LocalServer which validates it.
                channel.Send(telemetry);
            }
        }