public List <Span> Apply() { return((new int[this.traces]).SelectMany(i => { var start = DateTime.Now.AddHours(-(rnd.Next(8) + 1)); var trace = new GenTrace(); //DoRpc(trace, start, rnd.Next(this.maxDepth), GetRandRpcName(), withEndpoint(ep => ep)); withEndpoint(ep => { DoRpc(trace, start, rnd.Next(this.maxDepth), GetRandRpcName(), ep); return ep; }); return trace.Spans; }).ToList()); }
private DateTime DoRpc( GenTrace trace, DateTime time, int depth, string spanName, Endpoint endpoint, long spanId = long.MinValue, long?parentSpanId = null) { if (spanId == long.MinValue) { spanId = GetRandLong(); } var curTime = time.AddMilliseconds(1); var svrAnnos = new List <Annotation>(); svrAnnos.Add(new Annotation(Util.ToUnixTimMicroseconds(curTime), Constants.ServerRecv, endpoint)); var svrBinAnnos = new List <BinaryAnnotation>(); var randInt = rnd.Next(3); for (var i = 0; i < randInt; i++) { svrBinAnnos.Add(new BinaryAnnotation(GetRandSvcName(), Encoding.UTF8.GetBytes(GetRandSvcName()), AnnotationType.STRING, endpoint)); } // simulate some amount of work curTime = curTime.AddMilliseconds(rnd.Next(10)); randInt = rnd.Next(5) + 1; for (var i = 0; i < randInt; i++) { svrAnnos.Add(new Annotation(Util.ToUnixTimMicroseconds(curTime), GetRandSvcName(), endpoint)); curTime = curTime.AddMilliseconds(rnd.Next(10)); } if (depth > 0) { // parallel calls to downstream services var times = (new int[rnd.Next(depth) + 1]).Select(i => { return(withEndpoint(nextEp => { var thisSpanId = GetRandLong(); var thisParentId = spanId; var rpcName = GetRandRpcName(); var annos = new List <Annotation>(); var binAnnos = new List <BinaryAnnotation>(); var delay = rnd.Next(10) > 6 ? rnd.Next(10) : 0; annos.Add(new Annotation(Util.ToUnixTimMicroseconds(curTime) + delay, Constants.ClientSend, nextEp)); var thisTime = DoRpc(trace, curTime, rnd.Next(depth), rpcName, nextEp, thisSpanId, thisParentId).AddMilliseconds(1); annos.Add(new Annotation(Util.ToUnixTimMicroseconds(thisTime), Constants.ClientRecv, nextEp)); var thisTimestamp = annos[0].timestamp; var thisDuration = annos[1].timestamp - annos[0].timestamp; trace.AddSpan(rpcName, thisSpanId, thisParentId, thisTimestamp, thisDuration, annos, binAnnos); return thisTime; })); }); curTime = times.Max(); } svrAnnos.Add(new Annotation(Util.ToUnixTimMicroseconds(curTime), Constants.ServerSend, endpoint)); var timestamp = svrAnnos[0].timestamp; var duration = svrAnnos[svrAnnos.Count - 1].timestamp - svrAnnos[0].timestamp; trace.AddSpan(spanName, spanId, parentSpanId, timestamp, duration, svrAnnos, svrBinAnnos); return(curTime); }