Exemple #1
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_NotEvenSpaceForTruncationEllipses()
        {
            byte[] buffer   = new byte[20];
            int    startPos = buffer.Length - Ellipses.Length + 1; // Not enough space for "...\n".
            int    endPos   = SelfDiagnosticsEventListener.EncodeInBuffer("abc", false, buffer, startPos);

            Assert.Equal(startPos, endPos);
        }
Exemple #2
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_Empty()
        {
            byte[] buffer   = new byte[20];
            int    startPos = 0;
            int    endPos   = SelfDiagnosticsEventListener.EncodeInBuffer(string.Empty, false, buffer, startPos);

            byte[] expected = Encoding.UTF8.GetBytes(string.Empty);
            AssertBufferOutput(expected, buffer, startPos, endPos);
        }
Exemple #3
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_IsParameter_NotEvenSpaceForTruncatedString()
        {
            byte[] buffer   = new byte[20];
            int    startPos = buffer.Length - EllipsesWithBrackets.Length; // Just enough space for "{...}\n".
            int    endPos   = SelfDiagnosticsEventListener.EncodeInBuffer("abc", true, buffer, startPos);

            byte[] expected = Encoding.UTF8.GetBytes("{...}\0");
            AssertBufferOutput(expected, buffer, startPos, endPos + 1);
        }
Exemple #4
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_IsParameter_EnoughSpace()
        {
            byte[] buffer   = new byte[20];
            int    startPos = buffer.Length - EllipsesWithBrackets.Length - 6; // Just enough space for "abc" even if "...\n" need to be added.
            int    endPos   = SelfDiagnosticsEventListener.EncodeInBuffer("abc", true, buffer, startPos);

            byte[] expected = Encoding.UTF8.GetBytes("{abc}\0");
            AssertBufferOutput(expected, buffer, startPos, endPos + 1);
        }
        public void SelfDiagnosticsEventListener_EventSourceSetup_LowerSeverity()
        {
            var configRefresherMock = new Mock <SelfDiagnosticsConfigRefresher>();
            var listener            = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object);

            // Emitting a Verbose event. Or any EventSource event with lower severity than Error.
            OpenTelemetrySdkEventSource.Log.ActivityStarted("Activity started", "1");
            configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny <int>(), out It.Ref <Stream> .IsAny, out It.Ref <int> .IsAny), Times.Never());
        }
Exemple #6
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_EnoughSpace()
        {
            byte[] buffer   = new byte[20];
            int    startPos = buffer.Length - Ellipses.Length - 6; // Just enough space for "abc" even if "...\n" needs to be added.
            int    endPos   = SelfDiagnosticsEventListener.EncodeInBuffer("abc", false, buffer, startPos);

            // '\n' will be appended to the original string "abc" after EncodeInBuffer is called.
            // The byte where '\n' will be placed should not be touched within EncodeInBuffer, so it stays as '\0'.
            byte[] expected = Encoding.UTF8.GetBytes("abc\0");
            AssertBufferOutput(expected, buffer, startPos, endPos + 1);
        }
Exemple #7
0
        public void SelfDiagnosticsEventListener_EncodeInBuffer_NotEnoughSpaceForFullString()
        {
            byte[] buffer   = new byte[20];
            int    startPos = buffer.Length - Ellipses.Length - 5; // Just not space for "abc" if "...\n" needs to be added.

            // It's a quick estimate by assumption that most Unicode characters takes up to 2 16-bit UTF-16 chars,
            // which can be up to 4 bytes when encoded in UTF-8.
            int endPos = SelfDiagnosticsEventListener.EncodeInBuffer("abc", false, buffer, startPos);

            byte[] expected = Encoding.UTF8.GetBytes("ab...\0");
            AssertBufferOutput(expected, buffer, startPos, endPos + 1);
        }
        public void SelfDiagnosticsEventListener_EventSourceSetup_HigherSeverity()
        {
            var configRefresherMock = new Mock <SelfDiagnosticsConfigRefresher>();

            configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(It.IsAny <int>(), out It.Ref <Stream> .IsAny, out It.Ref <int> .IsAny))
            .Returns(true);
            var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object);

            // Emitting an Error event. Or any EventSource event with higher than or equal to to Error severity.
            OpenTelemetrySdkEventSource.Log.TracerProviderException("TestEvent", "Exception Details");
            configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny <int>(), out It.Ref <Stream> .IsAny, out It.Ref <int> .IsAny));
        }
Exemple #9
0
        public void SelfDiagnosticsEventListener_WriteEvent()
        {
            var    configRefresherMock = new Mock <SelfDiagnosticsConfigRefresher>();
            var    memoryMappedFile    = MemoryMappedFile.CreateFromFile(LOGFILEPATH, FileMode.Create, null, 1024);
            Stream stream                = memoryMappedFile.CreateViewStream();
            string eventMessage          = "Event Message";
            int    timestampPrefixLength = "2020-08-14T20:33:24.4788109Z:".Length;

            byte[] bytes = Encoding.UTF8.GetBytes(eventMessage);
            int    availableByteCount = 100;

            configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(timestampPrefixLength + bytes.Length + 1, out stream, out availableByteCount))
            .Returns(true);
            var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object);

            listener.WriteEvent(eventMessage, null);
            configRefresherMock.Verify(refresher => refresher.TryGetLogStream(timestampPrefixLength + bytes.Length + 1, out stream, out availableByteCount));
            stream.Dispose();
            memoryMappedFile.Dispose();
            AssertFileOutput(LOGFILEPATH, eventMessage);
        }
        public void SelfDiagnosticsEventListener_EmitEvent_CaptureAsConfigured()
        {
            // Arrange
            var    configRefresherMock = new Mock <SelfDiagnosticsConfigRefresher>();
            var    memoryMappedFile    = MemoryMappedFile.CreateFromFile(LOGFILEPATH, FileMode.Create, null, 1024);
            Stream stream = memoryMappedFile.CreateViewStream();

            configRefresherMock.Setup(configRefresher => configRefresher.TryGetLogStream(It.IsAny <int>(), out stream, out It.Ref <int> .IsAny))
            .Returns(true);
            var listener = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object);

            // Act: emit an event with severity equal to configured
            OpenTelemetrySdkEventSource.Log.TracerProviderException("TestEvent", "Exception Details");

            // Assert
            configRefresherMock.Verify(refresher => refresher.TryGetLogStream(It.IsAny <int>(), out stream, out It.Ref <int> .IsAny));
            stream.Dispose();
            memoryMappedFile.Dispose();

            var expectedLog = "Unknown error in TracerProvider '{0}': '{1}'.{TestEvent}{Exception Details}";

            AssertFileOutput(LOGFILEPATH, expectedLog);
        }
        public void SelfDiagnosticsEventListener_DateTimeGetBytes()
        {
            var configRefresherMock = new Mock <SelfDiagnosticsConfigRefresher>();
            var listener            = new SelfDiagnosticsEventListener(EventLevel.Error, configRefresherMock.Object);

            // Check DateTimeKind of Utc, Local, and Unspecified
            DateTime[] datetimes = new DateTime[]
            {
                DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Utc),
                DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Local),
                DateTime.SpecifyKind(DateTime.Parse("1996-12-01T14:02:31.1234567-08:00"), DateTimeKind.Unspecified),
                DateTime.UtcNow,
                DateTime.Now,
            };

            // Expect to match output string from DateTime.ToString("O")
            string[] expected = new string[datetimes.Length];
            for (int i = 0; i < datetimes.Length; i++)
            {
                expected[i] = datetimes[i].ToString("O");
            }

            byte[] buffer = new byte[40 * datetimes.Length];
            int    pos    = 0;

            // Get string after DateTimeGetBytes() write into a buffer
            string[] results = new string[datetimes.Length];
            for (int i = 0; i < datetimes.Length; i++)
            {
                int len = listener.DateTimeGetBytes(datetimes[i], buffer, pos);
                results[i] = Encoding.Default.GetString(buffer, pos, len);
                pos       += len;
            }

            Assert.Equal(expected, results);
        }