Пример #1
0
        /// <summary>
        /// Records a key-value pair as a binary annotiation in the span.
        /// </summary>
        /// <typeparam name="T">The type of the value to be recorded. See remarks for the currently supported types.</typeparam>
        /// <param name="span">The span where the annotation will be recorded.</param>
        /// <param name="key">The key which is a reference to the recorded value.</param>
        /// <param name="value">The value of the annotation to be recorded.</param>
        /// <remarks>The RecordBinary will record a key-value pair which can be used to tag some additional information
        /// in the trace without any timestamps. The currently supported value types are <see cref="bool"/>,
        /// <see cref="byte[]"/>, <see cref="short"/>, <see cref="int"/>, <see cref="long"/>, <see cref="double"/> and
        /// <see cref="string"/>. Any other types will be passed as string annotation types.
        ///
        /// Please note, that although the values have types, they will be recorded and sent by calling their
        /// respective ToString() method.</remarks>
        public void RecordBinary <T>(Span span, string key, T value)
        {
            if (!IsTraceOn)
            {
                return;
            }

            try
            {
                spanTracer.RecordBinary <T>(span, key, value);
            }
            catch (Exception ex)
            {
            }
        }
        public void RecordBinary_WithSpanAndValue_AddsNewTypeCorrectBinaryAnnotation()
        {
            // Arrange
            var keyName    = "TestKey";
            var testValues = new dynamic[]
            {
                new { Value = true, ExpectedResult = true, Type = AnnotationType.Boolean },
                new { Value = short.MaxValue, ExpectedResult = short.MaxValue, Type = AnnotationType.Int16 },
                new { Value = int.MaxValue, ExpectedResult = int.MaxValue, Type = AnnotationType.Int32 },
                new { Value = long.MaxValue, ExpectedResult = long.MaxValue, Type = AnnotationType.Int64 },
                new { Value = double.MaxValue, ExpectedResult = double.MaxValue, Type = AnnotationType.Double },
                new { Value = "String", ExpectedResult = "String", Type = AnnotationType.String },
                new { Value = DateTime.MaxValue, ExpectedResult = DateTime.MaxValue, Type = AnnotationType.String }
            };

            var domain     = new Uri("http://server.com");
            var spanTracer = new SpanTracer(spanCollectorStub, zipkinEndpointStub, zipkinNotToBeDisplayedDomainList, domain);

            foreach (var testValue in testValues)
            {
                var expectedSpan = new Span();

                // Act
                spanTracer.RecordBinary(expectedSpan, keyName, testValue.Value);

                // Assert
                var actualAnnotation = expectedSpan
                                       .GetAnnotationsByType <BinaryAnnotation>()?
                                       .SingleOrDefault(a => a.Key == keyName);

                var result         = actualAnnotation?.Value;
                var annotationType = actualAnnotation?.AnnotationType;
                Assert.AreEqual(testValue.ExpectedResult, result, "The recorded value in the annotation is wrong.");
                Assert.AreEqual(testValue.Type, annotationType, "The Annotation Type is wrong.");
            }
        }