Exemple #1
0
        public override async Task FindTraces(FindTracesRequest request, IServerStreamWriter <SpansResponseChunk> responseStream, ServerCallContext context)
        {
            var query  = _Mapper.Map <Model.Dto.TracingQueryParameter>(request.Query);
            var result = await _TracingQuery.GetTracing(query, context.CancellationToken);

            SpansResponseChunk       spansResponseChunk = new SpansResponseChunk();
            List <Jaeger.ApiV2.Span> spans = new List <Span>();

            foreach (var item in result)
            {
                var process = item.Processes;
                foreach (var spanModel in item.Spans)
                {
                    var span = _Mapper.Map <Span>(spanModel);
                    if (!string.IsNullOrEmpty(spanModel.ProcessId) &&
                        process.TryGetValue(spanModel.ProcessId, out var spanProcess) && spanProcess != null)
                    {
                        span.Process = _Mapper.Map <Jaeger.ApiV2.Process>(spanProcess);
                    }
                    spans.Add(span);
                }
            }
            var repeatedField_span = new RepeatedField <Span>();

            repeatedField_span.AddRange(spans);
            spansResponseChunk.Spans.Add(_Mapper.Map <RepeatedField <Span> >(repeatedField_span));
            await responseStream.WriteAsync(spansResponseChunk);
        }
 public override Task FindTraces(FindTracesRequest request, IServerStreamWriter <SpansResponseChunk> responseStream, ServerCallContext context)
 {
     return(base.FindTraces(request, responseStream, context));
 }
        public async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var runInfo = new RunInfo {
                LastRunTime = (DateTimeOffset.UtcNow - TimeSpan.FromHours(1)).UtcTicks
            };

            if (File.Exists("runInfo.json"))
            {
                using var r = new StreamReader("runInfo.json");
                var json = await r.ReadToEndAsync();

                runInfo = JsonConvert.DeserializeObject <RunInfo>(json);
            }

            var jaegerUrl = $"http://{_configuration["JaegerUrl"]}";

            var channel = GrpcChannel.ForAddress(jaegerUrl);

            var client = new QueryService.QueryServiceClient(channel);

            var findTracesRequest = new FindTracesRequest
            {
                Query = new TraceQueryParameters
                {
                    OperationName = "ProcessDataEvent",
                    ServiceName   = "Orchestrator",
                    StartTimeMin  = Timestamp.FromDateTimeOffset(new DateTimeOffset(runInfo.LastRunTime, TimeSpan.Zero)),
                    StartTimeMax  = Timestamp.FromDateTimeOffset(DateTimeOffset.UtcNow),
                    SearchDepth   = 1000
                }
            };
            // I could store the time of the last run and look for traces since then...
            // I could also keep track of all the traces ever looked up, and ignore those.
            var streamResult = client.FindTraces(findTracesRequest, cancellationToken: stoppingToken).ResponseStream;

            List <ByteString> traceIds       = new List <ByteString>();
            var          currentTraceId      = "";
            TraceDetails currentTraceDetails = null;

            using var fileName   = File.OpenWrite(_configuration["outputPath"]);
            using var textWriter = new StreamWriter(fileName);

            var headers = string.Join(',', "tId"
                                      , "dataPullType"
                                      , "FromZone"
                                      , "ToZone"
                                      , "DataSize"
                                      , "TotalDuration"
                                      , "TriggerStepClientDuration"
                                      , "TriggerStepServerDuration"
                                      , "DataPullDuration"
                                      , "DataPushDuration"
                                      , "ComputeDuration"
                                      , "DataMasterPullCall"
                                      , "DataPeerPullCall"
                                      , "DataMasterPushCall");

            await textWriter.WriteLineAsync(headers);

            while (await streamResult.MoveNext(CancellationToken.None))
            {
                var currentChunk = streamResult.Current;

                // I need to identify the numbers on my figure.
                // And then, for each trace, calculate the aggregates
                foreach (var span in currentChunk.Spans)
                {
                    if (runInfo.UsedTraceIds.Contains(span.TraceId.ToBase64()))
                    {
                        Console.WriteLine("Found an old trace, ignoring");
                        continue;
                    }

                    if (span.TraceId.ToBase64() != currentTraceId)
                    {
                        await textWriter.FlushAsync();

                        // new trace is starting.
                        if (currentTraceDetails != null)
                        {
                            await textWriter.WriteLineAsync(currentTraceDetails.ToString());
                        }
                        currentTraceDetails         = new TraceDetails();
                        currentTraceId              = span.TraceId.ToBase64();
                        currentTraceDetails.TraceId = currentTraceId;
                        traceIds.Add(span.TraceId);
                    }

                    AddInfoToCurrentTraceDetails(span, currentTraceDetails);
                }
            }

            if (currentTraceDetails != null)
            {
                await textWriter.WriteLineAsync(currentTraceDetails.ToString());
            }


            foreach (var traceId in traceIds)
            {
                runInfo.UsedTraceIds.Add(traceId.ToBase64());
            }

            runInfo.LastRunTime = DateTimeOffset.UtcNow.Ticks;

            await File.WriteAllTextAsync("runInfo.json", JsonConvert.SerializeObject(runInfo));
        }