Пример #1
0
        private static void CreateAndVisitRecord(Span span, IAnnotation annotation)
        {
            var record  = new Record(span.SpanState, TimeUtils.UtcNow, annotation);
            var visitor = new ZipkinAnnotationVisitor(record, span);

            record.Annotation.Accept(visitor);
        }
        public void DefaultsValuesAreUsedIfNothingSpecified()
        {
            var spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var span      = new Span(spanState, TimeUtils.UtcNow);

            AddClientSendReceiveAnnotations(span);

            var thriftSpan = ThriftSpanSerializer.ConvertToThrift(span);

            AssertSpanHasRequiredFields(thriftSpan);

            const string defaultName        = SerializerUtils.DefaultRpcMethodName;
            const string defaultServiceName = SerializerUtils.DefaultServiceName;
            var          defaultIpv4        = SerializerUtils.IpToInt(SerializerUtils.DefaultEndPoint.Address);
            var          defaultPort        = SerializerUtils.DefaultEndPoint.Port;

            Assert.AreEqual(2, thriftSpan.Annotations.Count);
            thriftSpan.Annotations.ForEach(ann =>
            {
                Assert.AreEqual(defaultServiceName, ann.Host.Service_name);
                Assert.AreEqual(defaultIpv4, ann.Host.Ipv4);
                Assert.AreEqual(defaultPort, ann.Host.Port);
            });

            Assert.AreEqual(defaultName, thriftSpan.Name);
            Assert.IsNull(thriftSpan.Duration);
        }
        public void DefaultsValuesAreNotUsedIfValuesSpecified()
        {
            var spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var started   = TimeUtils.UtcNow;

            // Make sure we choose something different thant the default values
            const string serviceName = SerializerUtils.DefaultServiceName + "_notDefault";
            var          hostPort    = SerializerUtils.DefaultEndPoint.Port + 1;

            const string name = "myRPCmethod";

            var span = new Span(spanState, started)
            {
                Endpoint = new IPEndPoint(IPAddress.Loopback, hostPort), ServiceName = serviceName, Name = name
            };

            AddClientSendReceiveAnnotations(span);

            var thriftSpan = ThriftSpanSerializer.ConvertToThrift(span);

            AssertSpanHasRequiredFields(thriftSpan);

            Assert.NotNull(thriftSpan);
            Assert.AreEqual(2, thriftSpan.Annotations.Count);

            thriftSpan.Annotations.ForEach(annotation =>
            {
                Assert.AreEqual(serviceName, annotation.Host.Service_name);
                Assert.AreEqual(SerializerUtils.IpToInt(IPAddress.Loopback), annotation.Host.Ipv4);
                Assert.AreEqual(hostPort, annotation.Host.Port);
            });

            Assert.AreEqual(name, thriftSpan.Name);
        }
        public void RootSpanPropertyIsCorrect(long?parentSpanId)
        {
            var spanState = new SpanState(1, parentSpanId, 1, SpanFlags.None);
            var span      = new Span(spanState, TimeUtils.UtcNow);

            Assert.AreEqual(parentSpanId == null, span.IsRoot);
        }
        public void TimestampConvertedForLocalComponent()
        {
            var startTime = DateTime.Now;
            var spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var span      = new Span(spanState, TimeUtils.UtcNow);

            var recordStart  = new Record(spanState, startTime, Annotations.LocalOperationStart("Operation"));
            var visitorStart = new ZipkinAnnotationVisitor(recordStart, span);

            recordStart.Annotation.Accept(visitorStart);
            var recordStop  = new Record(spanState, startTime.AddHours(1), Annotations.LocalOperationStop());
            var visitorStop = new ZipkinAnnotationVisitor(recordStop, span);

            recordStop.Annotation.Accept(visitorStop);

            var thriftSpan = ThriftSpanSerializer.ConvertToThrift(span);

            Assert.AreEqual(startTime.ToUnixTimestamp(), thriftSpan.Timestamp);
            Assert.AreEqual(1, thriftSpan.Binary_annotations.Count);
            var endpoint = thriftSpan.Binary_annotations[0].Host;

            Assert.NotNull(endpoint);
            Assert.IsEmpty(endpoint.Service_name);
            Assert.IsNotNull(endpoint.Ipv4);
        }
Пример #6
0
        private static void AnnotationCorrectlyAdded(IAnnotation ann, string expectedValue, bool isBinaryAnnotation, bool spanCompleted)
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            var record  = new Record(SpanState, TimeUtils.UtcNow, ann);
            var visitor = new ZipkinAnnotationVisitor(record, span);

            Assert.AreEqual(0, span.Annotations.Count);
            Assert.AreEqual(0, span.BinaryAnnotations.Count);

            record.Annotation.Accept(visitor);

            if (isBinaryAnnotation)
            {
                Assert.AreEqual(0, span.Annotations.Count);
                Assert.AreEqual(1, span.BinaryAnnotations.Count);
                Assert.AreEqual(expectedValue, span.BinaryAnnotations.First(_ => true).Key);
            }
            else
            {
                Assert.AreEqual(1, span.Annotations.Count);
                Assert.AreEqual(0, span.BinaryAnnotations.Count);
                Assert.AreEqual(expectedValue, span.Annotations.First(_ => true).Value);
            }

            Assert.AreEqual(spanCompleted, span.Complete);
        }
Пример #7
0
        public void ServNameAnnotationChangesSpanServName()
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            CreateAndVisitRecord(span, Annotations.ServiceName("myService"));

            Assert.AreEqual("myService", span.ServiceName);
        }
        private static void AddClientSendReceiveAnnotations(Span span, DateTime startTime, TimeSpan timeOffset)
        {
            var endtime = startTime + timeOffset;

            span.AddAnnotation(new ZipkinAnnotation(startTime, zipkinCoreConstants.CLIENT_SEND));
            span.AddAnnotation(new ZipkinAnnotation(endtime, zipkinCoreConstants.CLIENT_RECV));
            span.SetAsComplete(endtime);
        }
Пример #9
0
        public void RpcNameAnnotationChangesSpanName()
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            CreateAndVisitRecord(span, Annotations.Rpc("myRPCmethod"));

            Assert.AreEqual("myRPCmethod", span.Name);
        }
Пример #10
0
        public void LastAnnotationValueIsKeptIfMultipleRecord()
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            CreateAndVisitRecord(span, Annotations.ServiceName("myService"));
            CreateAndVisitRecord(span, Annotations.ServiceName("someOtherName"));

            Assert.AreEqual("someOtherName", span.ServiceName);
        }
Пример #11
0
        public void UnsupportedTagAnnotationShouldThrow()
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            var record  = new Record(SpanState, TimeUtils.UtcNow, new TagAnnotation("magicKey", 1f));
            var visitor = new ZipkinAnnotationVisitor(record, span);

            Assert.Throws <ArgumentException>(() => record.Annotation.Accept(visitor));
        }
Пример #12
0
        public void LocalAddrAnnotationChangesSpanLocalAddr()
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            var ipEndpoint = new IPEndPoint(IPAddress.Loopback, 9987);

            CreateAndVisitRecord(span, Annotations.LocalAddr(ipEndpoint));

            Assert.AreEqual(ipEndpoint, span.Endpoint);
        }
Пример #13
0
        public void WhiteSpacesAreRemovedFromServiceName()
        {
            var spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var span      = new Span(spanState, TimeUtils.UtcNow)
            {
                ServiceName = "my Criteo Service"
            };

            AddClientSendReceiveAnnotations(span);

            var thriftSpan = ThriftSpanSerializer.ConvertToThrift(span);

            Assert.AreEqual("my_Criteo_Service", thriftSpan.Annotations[0].Host.Service_name);
        }
Пример #14
0
        private static TimeSpan?GetSpanDuration(int offset, IAnnotation firstAnnotation, IAnnotation secondAnnotation = null)
        {
            var spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var span      = new Span(spanState, TimeUtils.UtcNow);

            var annotationTime = TimeUtils.UtcNow;
            var firstRecord    = new Record(spanState, annotationTime, firstAnnotation);

            firstRecord.Annotation.Accept(new ZipkinAnnotationVisitor(firstRecord, span));
            if (secondAnnotation != null)
            {
                var secondRecord = new Record(spanState, annotationTime.AddMilliseconds(offset), secondAnnotation);
                secondRecord.Annotation.Accept(new ZipkinAnnotationVisitor(secondRecord, span));
            }

            return(span.Duration);
        }
Пример #15
0
        public void TagAnnotationCorrectlyAdded(object value, byte[] expectedBytes, AnnotationType expectedType)
        {
            var span = new Span(SpanState, spanCreated: TimeUtils.UtcNow);

            var record  = new Record(SpanState, TimeUtils.UtcNow, new TagAnnotation("magicKey", value));
            var visitor = new ZipkinAnnotationVisitor(record, span);

            Assert.AreEqual(0, span.Annotations.Count);
            Assert.AreEqual(0, span.BinaryAnnotations.Count);

            record.Annotation.Accept(visitor);

            Assert.AreEqual(0, span.Annotations.Count);
            Assert.AreEqual(1, span.BinaryAnnotations.Count);

            var binAnn = span.BinaryAnnotations.First(_ => true);

            Assert.AreEqual("magicKey", binAnn.Key);
            Assert.AreEqual(expectedBytes, binAnn.Value);
            Assert.AreEqual(expectedType, binAnn.AnnotationType);
        }
Пример #16
0
        private static void VerifySpanDurationComputedWhenSetAsComplete(IAnnotation start, IAnnotation stop, bool isRootSpan, bool isSpanStartedAndDurationSet)
        {
            var startTime        = DateTime.Now;
            var endTime          = startTime.AddHours(1);
            var expectedDuration = endTime.Subtract(startTime);

            long?parentId = 0;

            if (isRootSpan)
            {
                parentId = null;
            }
            var spanState            = new SpanState(1, parentId, 2, SpanFlags.None);
            var spanCreatedTimestamp = TimeUtils.UtcNow;
            var span = new Span(spanState, spanCreatedTimestamp);

            var recordStart  = new Record(spanState, startTime, start);
            var visitorStart = new ZipkinAnnotationVisitor(recordStart, span);
            var recordStop   = new Record(spanState, endTime, stop);
            var visitorStop  = new ZipkinAnnotationVisitor(recordStop, span);

            Assert.AreEqual(spanCreatedTimestamp, span.SpanCreated);
            Assert.False(span.Duration.HasValue);
            Assert.False(span.Complete);
            recordStart.Annotation.Accept(visitorStart);
            Assert.False(span.Duration.HasValue);
            Assert.False(span.Complete);
            recordStop.Annotation.Accept(visitorStop);
            Assert.True(span.Complete);
            if (isSpanStartedAndDurationSet)
            {
                Assert.AreEqual(expectedDuration, span.Duration);
                Assert.AreEqual(startTime, span.SpanStarted);
            }
            else
            {
                Assert.False(span.Duration.HasValue);
                Assert.False(span.SpanStarted.HasValue);
            }
        }
Пример #17
0
        public void ClientDurationIsPreferredOverServer()
        {
            var       spanState = new SpanState(1, 0, 2, SpanFlags.None);
            var       span      = new Span(spanState, TimeUtils.UtcNow);
            const int offset    = 10;

            var annotationTime   = TimeUtils.UtcNow;
            var recordServerRecv = new Record(spanState, annotationTime, Annotations.ServerRecv());

            recordServerRecv.Annotation.Accept(new ZipkinAnnotationVisitor(recordServerRecv, span));
            var recordServerSend = new Record(spanState, annotationTime.AddMilliseconds(offset), Annotations.ServerSend());

            recordServerSend.Annotation.Accept(new ZipkinAnnotationVisitor(recordServerSend, span));
            var recordClientSend = new Record(spanState, annotationTime.AddMilliseconds(-offset), Annotations.ClientSend());

            recordClientSend.Annotation.Accept(new ZipkinAnnotationVisitor(recordClientSend, span));
            var recordClientRecv = new Record(spanState, annotationTime.AddMilliseconds(2 * offset), Annotations.ClientRecv());

            recordClientRecv.Annotation.Accept(new ZipkinAnnotationVisitor(recordClientRecv, span));

            Assert.True(span.Duration.HasValue);
            Assert.AreEqual(3 * offset, span.Duration.Value.TotalMilliseconds);
        }
Пример #18
0
 private static void AddClientSendReceiveAnnotations(Span span)
 {
     AddClientSendReceiveAnnotations(span, TimeUtils.UtcNow, new TimeSpan(0));
 }
Пример #19
0
        public void SpanCorrectlyConvertedToThrift(long?parentSpanId)
        {
            var          hostIp      = IPAddress.Loopback;
            const int    hostPort    = 1234;
            const string serviceName = "myCriteoService";
            const string methodName  = "GET";

            var spanState = new SpanState(1, parentSpanId, 2, SpanFlags.None);
            var timestamp = TimeUtils.UtcNow;
            var span      = new Span(spanState, timestamp)
            {
                Endpoint = new IPEndPoint(hostIp, hostPort), ServiceName = serviceName, Name = methodName
            };

            var zipkinAnnDateTime = TimeUtils.UtcNow;
            var timeOffset        = TimeSpan.FromMilliseconds(500);

            AddClientSendReceiveAnnotations(span, zipkinAnnDateTime, timeOffset);
            span.AddAnnotation(new ZipkinAnnotation(zipkinAnnDateTime, SomeRandomAnnotation));

            const string         binAnnKey  = "http.uri";
            var                  binAnnVal  = new byte[] { 0x00 };
            const AnnotationType binAnnType = AnnotationType.STRING;

            span.AddBinaryAnnotation(new BinaryAnnotation(binAnnKey, binAnnVal, binAnnType, TimeUtils.UtcNow, null, null));

            var thriftSpan = ThriftSpanSerializer.ConvertToThrift(span);

            var expectedHost = new Endpoint()
            {
                Ipv4         = SerializerUtils.IpToInt(hostIp),
                Port         = hostPort,
                Service_name = serviceName
            };

            Assert.AreEqual(1, thriftSpan.Trace_id);
            Assert.AreEqual(2, thriftSpan.Id);
            Assert.True(thriftSpan.Timestamp.HasValue);

            if (span.IsRoot)
            {
                Assert.IsNull(thriftSpan.Parent_id); // root span has no parent
            }
            else
            {
                Assert.AreEqual(parentSpanId, thriftSpan.Parent_id);
            }

            Assert.AreEqual(false, thriftSpan.Debug);
            Assert.AreEqual(methodName, thriftSpan.Name);

            Assert.AreEqual(3, thriftSpan.Annotations.Count);

            thriftSpan.Annotations.ForEach(ann =>
            {
                Assert.AreEqual(expectedHost, ann.Host);
            });

            Assert.AreEqual(thriftSpan.Annotations.FirstOrDefault(a => a.Value.Equals(zipkinCoreConstants.CLIENT_SEND)).Timestamp, zipkinAnnDateTime.ToUnixTimestamp());
            Assert.AreEqual(thriftSpan.Annotations.FirstOrDefault(a => a.Value.Equals(zipkinCoreConstants.CLIENT_RECV)).Timestamp, (zipkinAnnDateTime + timeOffset).ToUnixTimestamp());
            Assert.AreEqual(thriftSpan.Annotations.FirstOrDefault(a => a.Value.Equals(SomeRandomAnnotation)).Timestamp, zipkinAnnDateTime.ToUnixTimestamp());

            Assert.AreEqual(1, thriftSpan.Binary_annotations.Count);

            thriftSpan.Binary_annotations.ForEach(ann =>
            {
                Assert.AreEqual(expectedHost, ann.Host);
                Assert.AreEqual(binAnnKey, ann.Key);
                Assert.AreEqual(binAnnVal, ann.Value);
                Assert.AreEqual(binAnnType, ann.Annotation_type);
            });

            Assert.AreEqual(thriftSpan.Duration, timeOffset.TotalMilliseconds * 1000);
        }