Пример #1
0
        public async Task Produce(TKey key, TValue value, IPipe <KafkaSendContext <TKey, TValue> > pipe, CancellationToken cancellationToken)
        {
            LogContext.SetCurrentIfNull(_context.LogContext);

            var context = new KafkaMessageSendContext <TKey, TValue>(key, value, cancellationToken);

            if (_consumeContext != null)
            {
                context.TransferConsumeContextHeaders(_consumeContext);
            }

            context.DestinationAddress = _topicAddress;

            await _context.Send(context).ConfigureAwait(false);

            if (pipe.IsNotEmpty())
            {
                await pipe.Send(context).ConfigureAwait(false);
            }

            context.SourceAddress ??= _context.HostAddress;
            context.ConversationId ??= NewId.NextGuid();

            StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(context,
                                                                                                             (nameof(context.Partition), context.Partition.ToString()));

            try
            {
                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.PreSend(context).ConfigureAwait(false);
                }

                var message = new Message <TKey, TValue>
                {
                    Key   = context.Key,
                    Value = context.Message
                };

                if (context.SentTime.HasValue)
                {
                    message.Timestamp = new Timestamp(context.SentTime.Value);
                }

                message.Headers = _context.HeadersSerializer.Serialize(context);

                var topic = new TopicPartition(_topicAddress.Topic, context.Partition);

                await _context.Produce(topic, message, context.CancellationToken).ConfigureAwait(false);

                context.LogSent();
                activity.AddSendContextHeadersPostSend(context);

                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.PostSend(context).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                context.LogFaulted(exception);

                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.SendFault(context, exception).ConfigureAwait(false);
                }

                throw;
            }
            finally
            {
                activity?.Stop();
            }
        }
Пример #2
0
            public async Task Send(ProducerContext <TKey, TValue> context)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                var sendContext = new KafkaMessageSendContext <TKey, TValue>(_key, _value, _cancellationToken)
                {
                    DestinationAddress = _context.TopicAddress
                };

                await _context.SendPipe.Send(sendContext).ConfigureAwait(false);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(sendContext).ConfigureAwait(false);
                }

                sendContext.SourceAddress ??= _context.HostAddress;
                sendContext.ConversationId ??= NewId.NextGuid();

                StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(sendContext,
                                                                                                                 (nameof(sendContext.Partition), sendContext.Partition.ToString()));

                try
                {
                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(sendContext).ConfigureAwait(false);
                    }

                    var message = new Message <TKey, TValue>
                    {
                        Key   = sendContext.Key,
                        Value = sendContext.Message
                    };

                    if (sendContext.SentTime.HasValue)
                    {
                        message.Timestamp = new Timestamp(sendContext.SentTime.Value);
                    }

                    message.Headers = context.HeadersSerializer.Serialize(sendContext);

                    var topic = new TopicPartition(_context.TopicAddress.Topic, sendContext.Partition);

                    await context.Produce(topic, message, context.CancellationToken).ConfigureAwait(false);

                    sendContext.LogSent();
                    activity.AddSendContextHeadersPostSend(sendContext);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(sendContext).ConfigureAwait(false);
                    }
                }
                catch (Exception exception)
                {
                    sendContext.LogFaulted(exception);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(sendContext, exception).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }