| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Confluent.Kafka; |
| | | 6 | | using Google.Protobuf; |
| | | 7 | | using Google.Protobuf.WellKnownTypes; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using OpenTelemetry.Context.Propagation; |
| | | 10 | | |
| | | 11 | | namespace SeungYongShim.Kafka |
| | | 12 | | { |
| | | 13 | | public class KafkaProducer : IDisposable |
| | | 14 | | { |
| | | 15 | | private bool disposedValue; |
| | 0 | 16 | | private static readonly TextMapPropagator Propagator = Propagators.DefaultTextMapPropagator; |
| | | 17 | | |
| | 2 | 18 | | public KafkaProducer(KafkaConfig kafkaConfig, ILogger<KafkaConsumer> logger) |
| | 2 | 19 | | { |
| | 2 | 20 | | KafkaConfig = kafkaConfig; |
| | 2 | 21 | | _log = logger; |
| | | 22 | | |
| | 2 | 23 | | var config = new ProducerConfig |
| | 2 | 24 | | { |
| | 2 | 25 | | BootstrapServers = kafkaConfig.Brokers, |
| | 2 | 26 | | Acks = Acks.All, |
| | 2 | 27 | | }; |
| | | 28 | | |
| | 2 | 29 | | Producer = new ProducerBuilder<string, string>(config).Build(); |
| | 2 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task SendAsync(IMessage m, string topic, string key = "1") |
| | 2 | 33 | | { |
| | | 34 | | try |
| | 2 | 35 | | { |
| | 2 | 36 | | using var activity = ActivitySourceStatic.Instance.StartActivity("kafka", ActivityKind.Producer); |
| | | 37 | | |
| | 2 | 38 | | var message = JsonFormatter.ToDiagnosticString(Any.Pack(m)); |
| | 2 | 39 | | var headers = new Headers() |
| | 2 | 40 | | { |
| | 2 | 41 | | new Header("traceparent", Encoding.UTF8.GetBytes(activity?.Id ?? string.Empty)) |
| | 2 | 42 | | }; |
| | | 43 | | |
| | 2 | 44 | | var ret = await Producer.ProduceAsync(topic, new Message<string, string> |
| | 2 | 45 | | { |
| | 2 | 46 | | Key = key, |
| | 2 | 47 | | Headers = headers, |
| | 2 | 48 | | Value = message |
| | 4 | 49 | | }); ; |
| | | 50 | | |
| | 2 | 51 | | activity?.AddTag("topic", topic); |
| | 2 | 52 | | activity?.AddTag("message", message); |
| | 2 | 53 | | } |
| | 0 | 54 | | catch (Exception ex) |
| | 0 | 55 | | { |
| | 0 | 56 | | _log.LogWarning(ex, ""); |
| | 0 | 57 | | throw; |
| | | 58 | | } |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | private ILogger<KafkaConsumer> _log { get; } |
| | | 62 | | public KafkaConfig KafkaConfig { get; } |
| | | 63 | | private IProducer<string, string> Producer { get; } |
| | | 64 | | |
| | | 65 | | protected virtual void Dispose(bool disposing) |
| | 2 | 66 | | { |
| | 2 | 67 | | if (!disposedValue) |
| | 2 | 68 | | { |
| | 2 | 69 | | if (disposing) |
| | 2 | 70 | | { |
| | 2 | 71 | | Producer?.Dispose(); |
| | 2 | 72 | | } |
| | | 73 | | |
| | 2 | 74 | | disposedValue = true; |
| | 2 | 75 | | } |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | public void Dispose() |
| | 2 | 79 | | { |
| | 2 | 80 | | Dispose(disposing: true); |
| | 2 | 81 | | GC.SuppressFinalize(this); |
| | 2 | 82 | | } |
| | | 83 | | } |
| | | 84 | | } |