コード例 #1
0
        public async Task ReportAsync(IReadOnlyCollection <SegmentRequest> segmentRequests,
                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_connectionManager.Ready)
            {
                return;
            }

            var connection = _connectionManager.GetConnection();

            try
            {
                var stopwatch = Stopwatch.StartNew();
                var client    = new TraceSegmentService.TraceSegmentServiceClient(connection);
                using (var asyncClientStreamingCall =
                           client.collect(_config.GetMeta(), _config.GetReportTimeout(), cancellationToken))
                {
                    foreach (var segment in segmentRequests)
                    {
                        await asyncClientStreamingCall.RequestStream.WriteAsync(SegmentV5Helpers.Map(segment));
                    }
                    await asyncClientStreamingCall.RequestStream.CompleteAsync();

                    await asyncClientStreamingCall.ResponseAsync;
                }

                stopwatch.Stop();
                _logger.LogInformation($"Report {segmentRequests.Count} trace segment. cost: {stopwatch.Elapsed}s");
            }
            catch (Exception ex)
            {
                _logger.LogError("Report trace segment fail.", ex);
                _connectionManager.Failure(ex);
            }
        }
コード例 #2
0
        public async Task CollectAsync(IEnumerable <TraceSegmentRequest> request, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_connectionManager.Ready)
            {
                return;
            }

            var connection = _connectionManager.GetConnection();

            var client = new TraceSegmentService.TraceSegmentServiceClient(connection);

            try
            {
                using (var asyncClientStreamingCall = client.collect(null, null, cancellationToken))
                {
                    foreach (var segment in request)
                    {
                        await asyncClientStreamingCall.RequestStream.WriteAsync(TraceSegmentHelpers.Map(segment));
                    }
                    await asyncClientStreamingCall.RequestStream.CompleteAsync();

                    await asyncClientStreamingCall.ResponseAsync;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Heartbeat error.", ex);
                _connectionManager.Failure(ex);
            }
        }
コード例 #3
0
        public async void AfterFinished(ITraceSegment traceSegment)
        {
            var segment             = traceSegment.Transform();
            var traceSegmentService =
                new TraceSegmentService.TraceSegmentServiceClient(GrpcChannelManager.Instance.Channel);

            using (var asyncClientStreamingCall = traceSegmentService.collect())
            {
                await asyncClientStreamingCall.RequestStream.WriteAsync(segment);

                await asyncClientStreamingCall.RequestStream.CompleteAsync();
            }
        }
コード例 #4
0
        private async Task BatchSendTraceSegments()
        {
            if (_traceSegments.Count == 0)
            {
                return;
            }

            var availableConnection = GrpcConnectionManager.Instance.GetAvailableConnection();

            if (availableConnection == null)
            {
                _logger.Warning(
                    $"Transform and send UpstreamSegment to collector fail. {GrpcConnectionManager.NotFoundErrorMessage}");
                return;
            }

            try
            {
                var traceSegmentService =
                    new TraceSegmentService.TraceSegmentServiceClient(availableConnection.GrpcChannel);
                using (var asyncClientStreamingCall = traceSegmentService.collect())
                {
                    while (_traceSegments.TryDequeue(out var segment))
                    {
                        await asyncClientStreamingCall.RequestStream.WriteAsync(segment.Transform());

                        _logger.Debug(
                            $"Transform and send UpstreamSegment to collector. [TraceSegmentId] = {segment.TraceSegmentId} [GlobalTraceId] = {segment.RelatedGlobalTraces.FirstOrDefault()}");
                    }
                    await asyncClientStreamingCall.RequestStream.CompleteAsync();

                    await asyncClientStreamingCall.ResponseAsync;
                }
            }
            catch (Exception e)
            {
                _logger.Warning($"Transform and send UpstreamSegment to collector fail. {e.Message}");
                availableConnection?.Failure();
                return;
            }
        }
コード例 #5
0
        public async void AfterFinished(ITraceSegment traceSegment)
        {
            if (traceSegment.IsIgnore)
            {
                return;
            }

            var availableConnection = GrpcConnectionManager.Instance.GetAvailableConnection();

            if (availableConnection == null)
            {
                _logger.Warning(
                    $"Transform and send UpstreamSegment to collector fail. {GrpcConnectionManager.NotFoundErrorMessage}");
                return;
            }

            try
            {
                var segment             = traceSegment.Transform();
                var traceSegmentService =
                    new TraceSegmentService.TraceSegmentServiceClient(availableConnection.GrpcChannel);
                using (var asyncClientStreamingCall = traceSegmentService.collect())
                {
                    await asyncClientStreamingCall.RequestStream.WriteAsync(segment);

                    await asyncClientStreamingCall.RequestStream.CompleteAsync();

                    await asyncClientStreamingCall.ResponseAsync;
                }

                _logger.Debug(
                    $"Transform and send UpstreamSegment to collector. [TraceSegmentId] = {traceSegment.TraceSegmentId} [GlobalTraceId] = {traceSegment.RelatedGlobalTraces.FirstOrDefault()}");
            }
            catch (Exception e)
            {
                _logger.Warning($"Transform and send UpstreamSegment to collector fail. {e.Message}");
                availableConnection?.Failure();
            }
        }