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 ThriftConversionLocalComponentWithHostAndEmptyServiceName() { var binAnn = new BinaryAnnotation(zipkinCoreConstants.LOCAL_COMPONENT, Encoding.ASCII.GetBytes("hello"), AnnotationType.STRING, TimeUtils.UtcNow, null, null); var thriftBinAnn = ThriftSpanSerializer.ConvertToThrift(binAnn, _someHost); AssertEndpointIsCorrect(thriftBinAnn.Host); }
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 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); }
public async Task CollectAsync(params Span[] spans) { var request = WebRequest.CreateHttp(_url); request.Method = "POST"; request.ContentType = "application/x-thrift"; using (var output = new MemoryStream()) { ThriftSpanSerializer.WriteSpans(spans, output); output.Position = 0; request.ContentLength = output.Length; using (var stream = await request.GetRequestStreamAsync()) { await output.CopyToAsync(stream); await stream.FlushAsync(); } } using (var reply = (HttpWebResponse)await request.GetResponseAsync()) { if (reply.StatusCode != HttpStatusCode.Accepted) { throw new ZipkinCollectorException($"Zipkin HTTP receiver responded with status code {reply.StatusCode}"); } } }
public void ThriftConversionZipkinAnnotationIsCorrect() { var now = TimeUtils.UtcNow; const string value = "anything"; var ann = new ZipkinAnnotation(now, value); var thriftAnn = ThriftSpanSerializer.ConvertToThrift(ann, _someHost); Assert.NotNull(thriftAnn); Assert.AreEqual(now.ToUnixTimestamp(), thriftAnn.Timestamp); Assert.AreEqual(value, thriftAnn.Value); AssertEndpointIsCorrect(thriftAnn.Host); }
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); }
private void AssertBinaryAnnotationConversion(string serviceName, IPEndPoint endpoint, Endpoint expectedEndpoint) { const string key = "myKey"; var data = Encoding.ASCII.GetBytes("hello"); const AnnotationType type = AnnotationType.STRING; var binAnn = new BinaryAnnotation(key, data, type, TimeUtils.UtcNow, serviceName, endpoint); var thriftBinAnn = ThriftSpanSerializer.ConvertToThrift(binAnn, _someHost); Assert.AreEqual(key, thriftBinAnn.Key); Assert.AreEqual(data, thriftBinAnn.Value); Assert.AreEqual(type, thriftBinAnn.Annotation_type); AssertEndpointIsEqual(expectedEndpoint, thriftBinAnn.Host); }
public async Task CollectAsync(params Span[] spans) { using (var stream = new MemoryStream()) { ThriftSpanSerializer.WriteSpans(spans, stream); stream.Position = 0; var message = new Message { Value = stream.ToArray() }; var result = await _producer.SendMessageAsync(_settings.ZipkinTopic, new[] { message }); var res = result.First(); if (res.Error != 0) { throw new ZipkinCollectorException($"An error (code: {res.Error}) occurred while sending trace data to zipkin-kafka"); } } }
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(2, 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(spanState.TraceIdHigh, thriftSpan.Trace_id_high); Assert.AreEqual(spanState.TraceId, thriftSpan.Trace_id); Assert.AreEqual(spanState.SpanId, 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); }