Ejemplo n.º 1
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);
                }
            }
Ejemplo n.º 2
0
            public void WhenSetToTrueChangesTelemetryBufferCapacityToOneForImmediateTransmission()
            {
                var channel = new ServerTelemetryChannel();

                channel.DeveloperMode = true;
                Assert.Equal(1, channel.TelemetryBuffer.Capacity);
            }
Ejemplo n.º 3
0
            public void DeveloperModeCanBeModifiedByConfiguration()
            {
                var channel = new ServerTelemetryChannel();

                channel.DeveloperMode = true;
                Assert.True(channel.DeveloperMode.Value);
            }
Ejemplo n.º 4
0
            public void SetterChangesTelemetryBufferCapacity()
            {
                var channel = new ServerTelemetryChannel();

                channel.MaxTelemetryBufferCapacity = 42;
                Assert.Equal(42, channel.TelemetryBuffer.Capacity);
            }
Ejemplo n.º 5
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);
            }
Ejemplo n.º 6
0
            public void GetterReturnsTelemetryBufferCapacity()
            {
                var channel = new ServerTelemetryChannel();

                channel.TelemetryBuffer.Capacity = 42;
                Assert.AreEqual(42, channel.MaxTelemetryBufferCapacity);
            }
Ejemplo n.º 7
0
            public void DoesNotModifyComponentConfigurationWhenNewValueIsSameAsOldValue()
            {
                var channel = new ServerTelemetryChannel();
                int oldTelemetryBufferSize = channel.TelemetryBuffer.Capacity;

                channel.DeveloperMode = false;

                Assert.AreEqual(oldTelemetryBufferSize, channel.TelemetryBuffer.Capacity);
            }
            public void ReturnsMaxBufferCapacityOfTransmitter()
            {
                var channel = new ServerTelemetryChannel {
                    Transmitter = new StubTransmitter()
                };

                channel.Transmitter.MaxBufferCapacity = 42;
                Assert.Equal(42, channel.MaxTransmissionBufferCapacity);
            }
Ejemplo n.º 9
0
            public void ChangesMaxSenderCapacityOfTransmitter()
            {
                var channel = new ServerTelemetryChannel {
                    Transmitter = new StubTransmitter()
                };

                channel.MaxTransmissionSenderCapacity = 42;
                Assert.AreEqual(42, channel.Transmitter.MaxSenderCapacity);
            }
Ejemplo n.º 10
0
            public void SetterChangesStorageCapacityOfTransmitter()
            {
                var channel = new ServerTelemetryChannel {
                    Transmitter = new StubTransmitter()
                };

                channel.MaxTransmissionStorageCapacity = 42000;
                Assert.AreEqual(42000, channel.Transmitter.MaxStorageCapacity);
            }
Ejemplo n.º 11
0
            public void SetterChangesStorageFolderOfTransmitter()
            {
                var channel = new ServerTelemetryChannel {
                    Transmitter = new StubTransmitter()
                };

                channel.StorageFolder = "test";
                Assert.AreEqual("test", channel.Transmitter.StorageFolder);
            }
Ejemplo n.º 12
0
            public void DefaultBackoffEnabledReportingIntervalUpdatesBackoffLogicManager()
            {
                ServerTelemetryChannel channel = new ServerTelemetryChannel
                {
                    DefaultBackoffEnabledReportingInterval = TimeSpan.FromHours(42)
                };

                Assert.AreEqual(channel.Transmitter.BackoffLogicManager.DefaultBackoffEnabledReportingInterval, TimeSpan.FromHours(42));
            }
Ejemplo n.º 13
0
            public void WhenSetToFalseChangesTelemetryBufferCapacityToOriginalValueForBufferedTransmission()
            {
                var channel = new ServerTelemetryChannel();
                int originalTelemetryBufferSize = channel.TelemetryBuffer.Capacity;

                channel.DeveloperMode = true;
                channel.DeveloperMode = false;

                Assert.AreEqual(originalTelemetryBufferSize, channel.TelemetryBuffer.Capacity);
            }
Ejemplo n.º 14
0
            public void EndpointAddressCanBeModifiedByConfiguration()
            {
                var channel = new ServerTelemetryChannel();

                Uri expectedEndpoint = new Uri("http://abc.com");

                channel.EndpointAddress = expectedEndpoint.AbsoluteUri;

                Assert.AreEqual(expectedEndpoint, new Uri(channel.EndpointAddress));
            }
Ejemplo n.º 15
0
        public void FlushesTelemetryBuffer()
        {
            var mockTelemetryBuffer = new Mock <TelemetryChannel.Implementation.TelemetryBuffer>();
            var channel             = new ServerTelemetryChannel {
                TelemetryBuffer = mockTelemetryBuffer.Object
            };

            channel.Initialize(TelemetryConfiguration.CreateDefault());

            channel.FlushAsync(default);
Ejemplo n.º 16
0
            public void DataUploadIntervalInSecondsIsStoredByTelemetryBuffer()
            {
                var channel = new ServerTelemetryChannel();

                TimeSpan expectedUploadInterval = TimeSpan.FromSeconds(42);

                channel.MaxTelemetryBufferDelay = expectedUploadInterval;

                Assert.AreEqual(expectedUploadInterval, channel.MaxTelemetryBufferDelay);
            }
Ejemplo n.º 17
0
            public void EndpointAddressIsStoredBySerializer()
            {
                var channel = new ServerTelemetryChannel();

                Uri expectedEndpoint = new Uri("http://abc.com");

                channel.EndpointAddress = expectedEndpoint.AbsoluteUri;

                Assert.AreEqual(expectedEndpoint, channel.TelemetrySerializer.EndpointAddress);
            }
            public void FlushesTelemetryBuffer()
            {
                var mockTelemetryBuffer = new Mock <TelemetryBuffer>();
                var channel             = new ServerTelemetryChannel {
                    TelemetryBuffer = mockTelemetryBuffer.Object
                };

                channel.Flush();

                mockTelemetryBuffer.Verify(x => x.FlushAsync());
            }
Ejemplo n.º 19
0
 public static void AddSharedKernelLogging(this IServiceCollection services, IConfiguration configuration)
 {
     using var serverTelemetryChannel = new ServerTelemetryChannel
           {
               StorageFolder = configuration.GetSection(ApplicationInsightsOptions.Key)
                               .Get <ApplicationInsightsOptions>()
                               .ServerTelemetryChannelStoragePath
           };
     services.AddSingleton(typeof(ITelemetryChannel), serverTelemetryChannel);
     services.AddApplicationInsightsTelemetry();
 }
            public void VerifyEndpointConnectionString_SetFromInitialize_DefaultEndpoint()
            {
                var channel = new ServerTelemetryChannel();

                var configuration = new TelemetryConfiguration();

                Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);

                channel.Initialize(configuration);
                Assert.AreEqual("https://dc.services.visualstudio.com/v2/track", channel.EndpointAddress);
            }
Ejemplo n.º 21
0
            public void InitializesTransmitterWithNetworkAvailabilityPolicy()
            {
                var network = new StubNetwork {
                    OnIsAvailable = () => false
                };

                var channel = new ServerTelemetryChannel(network, new StubApplicationLifecycle());

                channel.Initialize(new TelemetryConfiguration());
                Thread.Sleep(50);

                Assert.AreEqual(0, channel.Transmitter.Sender.Capacity);
            }
        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));
                }
            }
        }
Ejemplo n.º 23
0
            public void VerifyEndpointConnectionString_DefaultScenario()
            {
                var channel = new ServerTelemetryChannel();

                Assert.AreEqual(null, channel.EndpointAddress);

                var configuration = new TelemetryConfiguration
                {
                    TelemetryChannel = channel,
                };

                Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);
                Assert.AreEqual("https://dc.services.visualstudio.com/v2/track", channel.EndpointAddress);
            }
Ejemplo n.º 24
0
            public void VerifyEndpointConnectionString_SetFromConfiguration_DefaultEndpoint()
            {
                var connectionstring = $"instrumentationkey=00000000-0000-0000-0000-000000000000";

                var channel = new ServerTelemetryChannel();

                var configuration = new TelemetryConfiguration
                {
                    TelemetryChannel = channel,
                    ConnectionString = connectionstring,
                };

                Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);
                Assert.AreEqual("https://dc.services.visualstudio.com/v2/track", channel.EndpointAddress);
            }
Ejemplo n.º 25
0
            public void VerifyCanChangeEndpointAfterInitialize()
            {
                var channel = new ServerTelemetryChannel();

                Assert.AreEqual(null, channel.EndpointAddress, "channel endpoint should be null before Initialize");

                var configuration = new TelemetryConfiguration();

                Assert.AreEqual("https://dc.services.visualstudio.com/", configuration.EndpointContainer.Ingestion.AbsoluteUri);

                channel.Initialize(configuration);
                Assert.AreEqual("https://dc.services.visualstudio.com/v2/track", channel.EndpointAddress, "channel endpoint should match config after Initialize");

                channel.EndpointAddress = "http://localhost:1234";
                Assert.AreEqual("http://localhost:1234/", channel.EndpointAddress, "channel endpoint was not set");
            }
Ejemplo n.º 26
0
            public void VerifyEndpointConnectionString_SetFromConfiguration_ExplicitEndpoint_WithoutTrailingSlash()
            {
                var explicitEndpoint = "https://127.0.0.1";
                var connectionString = $"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint={explicitEndpoint}";

                var channel = new ServerTelemetryChannel();

                var configuration = new TelemetryConfiguration
                {
                    TelemetryChannel = channel,
                    ConnectionString = connectionString,
                };

                Assert.AreEqual("https://127.0.0.1/", configuration.EndpointContainer.Ingestion.AbsoluteUri);
                Assert.AreEqual("https://127.0.0.1/v2/track", channel.EndpointAddress);
            }
            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 WaitsForAsynchronousFlushToCompleteAndAllowsItsExceptionsToBubbleUp()
            {
                var expectedException = new Exception();
                var tcs = new TaskCompletionSource <object>();

                tcs.SetException(expectedException);
                var mockTelemetryBuffer = new Mock <TelemetryBuffer>();

                mockTelemetryBuffer.Setup(x => x.FlushAsync()).Returns(tcs.Task);
                var channel = new ServerTelemetryChannel {
                    TelemetryBuffer = mockTelemetryBuffer.Object
                };

                var actualException = Assert.Throws <Exception>(() => channel.Flush());

                Assert.Same(expectedException, actualException);
            }
            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]);
                }
            }
        }