public static ISpanBuilder ObtainConsumerSpanBuilder(ITracer tracer, Headers headers, string spanName)
        {
            // See if we have a trace header
            IHeader traceHeader = null;

            // For whatever insane reason this linq query is not working ??
            //traceHeader = cr.Message.Headers.FirstOrDefault(p => p.Key == "tracing-id");
            foreach (var header in headers)
            {
                bool isKey = header.Key == "tracing-id";
                if (isKey)
                {
                    traceHeader = header;
                    break;
                }
            }

            ISpanBuilder spanBuilder;

            if (traceHeader != null)
            {
                var map = KafkaTracingContextHelper.Decode(traceHeader.GetValueBytes());
                var ctx = tracer.Extract(BuiltinFormats.TextMap, new TextMapExtractAdapter(map));
                spanBuilder = tracer.BuildSpan(spanName)
                              .WithTag(Tags.SpanKind.Key, Tags.SpanKindConsumer)
                              .AsChildOf(ctx);
            }
            else
            {
                spanBuilder = tracer.BuildSpan(spanName)
                              .WithTag(Tags.SpanKind.Key, Tags.SpanKindConsumer);
            }

            return(spanBuilder);
        }
예제 #2
0
        public Task ConsumeAsync(CancellationToken stoppingToken)
        {
            return(Task.Run(async() =>
            {
                var topics = string.Join(",", _consumer.Subscription);

                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        ConsumeResult <TKey, string> cr = _consumer.Consume(stoppingToken);
                        if (cr.IsPartitionEOF)
                        {
                            continue;
                        }

                        IHeader messageTypeHeader = cr.Message.Headers.First(h => h.Key == "type");
                        string eventType = Encoding.UTF8.GetString(messageTypeHeader.GetValueBytes());

                        IDomainEvent <TKey> @event = _eventDeserializer.Deserialize <TKey>(eventType, cr.Message.Value);
                        if (@event == null)
                        {
                            throw new SerializationException($"unable to deserialize event {eventType} : {cr.Message.Value}");
                        }

                        await OnEventReceived(@event);
                    }
                    catch (OperationCanceledException ex)
                    {
                        OnConsumerStopped(ex);
                        throw;
                    }
                    catch (Exception ex)
                    {
                        OnExceptionThrown(ex);
                    }
                }
            }, stoppingToken));
        }