Example #1
0
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            //Console.WriteLine("Hello World!");
            var channel = GrpcChannel.ForAddress("http://localhost:6003", new GrpcChannelOptions()
            {
                //DisposeHttpClient = true,
                ThrowOperationCanceledOnCancellation = true,
            });



            var client   = new Jaeger.ApiV2.QueryService.QueryServiceClient(channel);
            var response = client.GetServices(new Jaeger.ApiV2.GetServicesRequest()
            {
            }, new Grpc.Core.CallOptions());

            Console.WriteLine(string.Join(",", response.Services));

            var response1 = client.GetOperations(new Jaeger.ApiV2.GetOperationsRequest()
            {
                Service = response.Services[0],
            }, new Grpc.Core.CallOptions());

            Console.WriteLine(string.Join(",", response1.OperationNames));

            var response2 = client.GetTrace(new Jaeger.ApiV2.GetTraceRequest()
            {
                TraceId = ByteString.CopyFrom(Jaeger.TraceId.FromString("4399add89a1b8a02").ToByteArray()),
            }, new Grpc.Core.CallOptions());

            while (await response2.ResponseStream.MoveNext(CancellationToken.None))
            {
                Console.WriteLine(string.Join("|", response2.ResponseStream.Current.Spans.Select(s => s.SpanId.ToString())));
            }
            var ps = new Jaeger.ApiV2.TraceQueryParameters()
            {
                ServiceName  = response.Services[0],
                StartTimeMin = Timestamp.FromDateTimeOffset(DateTimeOffset.Now.AddDays(-7)),
                //OperationName= response1.OperationNames[0],
                StartTimeMax  = Timestamp.FromDateTimeOffset(DateTimeOffset.Now),
                OperationName = "HTTP GET",
                //DurationMin=Duration.FromTimeSpan(TimeSpan.Zero)
            };
            //ps.Tags.Add("http.method", "GET");
            var response3 = client.FindTraces(new Jaeger.ApiV2.FindTracesRequest()
            {
                Query = ps
            }, new Grpc.Core.CallOptions());

            while (await response3.ResponseStream.MoveNext(CancellationToken.None))
            {
                Console.WriteLine(string.Join("|", response3.ResponseStream.Current.Spans.Select(s => s.SpanId.ToString())));
            }
        }
Example #2
0
        public async Task <QueryResponseServices <TracingDto> > Traces(
            [Required] string service,
            string operation,
            string tags,
            int?minDuration,
            int?maxDuration,
            [Required] long start,
            [Required] long end,
            string lookback,
            int limit = 10
            )
        {
            if (string.IsNullOrWhiteSpace(service))
            {
                return(new QueryResponseServices <TracingDto>());
            }
            var request = new Jaeger.ApiV2.FindTracesRequest()
            {
                Query = new Jaeger.ApiV2.TraceQueryParameters()
                {
                    ServiceName = service,
                }
            };

            if (!string.IsNullOrEmpty(operation))
            {
                request.Query.OperationName = operation;
            }
            if (!string.IsNullOrEmpty(tags))
            {
                try
                {
                    var request_tags = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(tags);
                    request.Query.Tags.Add(request_tags);
                }
                catch (Exception ex)
                {
                    //logger.LogError(ex, "Tags 格式有误!", tags);
                    return(new QueryResponseServices <TracingDto>()
                    {
                        Errors = "Tags 格式有误!"
                    });
                }
            }
            if (minDuration.HasValue)
            {
                request.Query.DurationMin = Google.Protobuf.WellKnownTypes.Duration.FromTimeSpan(TimeSpan.FromSeconds(minDuration.Value));
            }
            if (maxDuration.HasValue)
            {
                request.Query.DurationMax = Google.Protobuf.WellKnownTypes.Duration.FromTimeSpan(TimeSpan.FromSeconds(maxDuration.Value));
            }
            request.Query.StartTimeMin = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(DateTimeOffset.FromUnixTimeMilliseconds(start));
            request.Query.StartTimeMax = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(DateTimeOffset.FromUnixTimeMilliseconds(end));
            var            result = _QueryServiceClient.FindTraces(request);
            List <SpanDto> spans  = new List <SpanDto>();
            Dictionary <string, SpanProcess> dict_process = new Dictionary <string, SpanProcess>();

            while (await result.ResponseStream.MoveNext(CancellationToken.None))
            {
                foreach (var span in result.ResponseStream.Current.Spans)
                {
                    if (!dict_process.ContainsKey(span.ProcessId))
                    {
                        dict_process.Add(span.ProcessId, _Mapper.Map <SpanProcess>(span.Process));
                    }
                    spans.Add(_Mapper.Map <SpanDto>(span));
                }
            }
            List <TracingDto> tracingDtos = new List <TracingDto>();

            foreach (var item in spans.GroupBy(g => g.TraceId))
            {
                tracingDtos.Add(Convert(item, dict_process));
            }
            return(new QueryResponseServices <TracingDto>()
            {
                Data = tracingDtos
            });
        }