예제 #1
0
        public IBusSubscriber SubscribeCommand <TCommand>(string @namespace = null, string queueName = null,
                                                          Func <TCommand, DShopException, IRejectedEvent> onError = null)
            where TCommand : ICommand
        {
            _busClient.SubscribeAsync <TCommand, CorrelationContext>(async(command, correlationContext) =>
            {
                var context = SpanContext.ContextFromString(correlationContext.SpanContext);

                using (var scope = BuildScope())
                {
                    Console.WriteLine(scope.Span.Context.SpanId);

                    var commandHandler = _serviceProvider.GetService <ICommandHandler <TCommand> >();

                    return(await TryHandleAsync(command, correlationContext,
                                                () => commandHandler.HandleAsync(command, correlationContext), onError));
                }

                IScope BuildScope()
                => _tracer
                .BuildSpan("fetching-data-from-services")
                .WithTag("operation", "fetching")
                .AddReference(References.FollowsFrom, context)
                .StartActive(true);
            },
                                                                     ctx => ctx.UseSubscribeConfiguration(cfg =>
                                                                                                          cfg.FromDeclaredQueue(q => q.WithName(GetQueueName <TCommand>(@namespace, queueName)))));

            return(this);
        }
예제 #2
0
        public void TestContextFromString128bit()
        {
            SpanContext context = SpanContext.ContextFromString("66a9b9e94fbd82982dd4bf8929a43f92:dd:cc:4");

            Assert.Equal(0x66a9b9e94fbd8298L, context.TraceId.High);
            Assert.Equal(0x2dd4bf8929a43f92L, context.TraceId.Low);
            Assert.Equal(221L, context.SpanId);
            Assert.Equal(204L, context.ParentId);
            Assert.Equal(4, (byte)context.Flags);
        }
예제 #3
0
        public void TestContextFromString64bit()
        {
            SpanContext context = SpanContext.ContextFromString("ff:dd:cc:4");

            Assert.Equal(0L, context.TraceId.High);
            Assert.Equal(255L, context.TraceId.Low);
            Assert.Equal(221L, context.SpanId);
            Assert.Equal(204L, context.ParentId);
            Assert.Equal(4, (byte)context.Flags);
        }
예제 #4
0
        public void TestSpanToString()
        {
            Span        span            = (Span)tracer.BuildSpan("test-operation").Start();
            SpanContext expectedContext = span.Context;
            SpanContext actualContext   = SpanContext.ContextFromString(span.Context.ContextAsString());

            Assert.Equal(expectedContext.TraceId, actualContext.TraceId);
            Assert.Equal(expectedContext.SpanId, actualContext.SpanId);
            Assert.Equal(expectedContext.ParentId, actualContext.ParentId);
            Assert.Equal(expectedContext.Flags, actualContext.Flags);
        }
예제 #5
0
        private IScope BuildScope(string messageName, string serializedSpanContext)
        {
            var spanBuilder = _tracer
                              .BuildSpan($"processing-{messageName}")
                              .WithTag("message-type", messageName);

            if (string.IsNullOrEmpty(serializedSpanContext))
            {
                return(spanBuilder.StartActive(true));
            }

            var spanContext = SpanContext.ContextFromString(serializedSpanContext);

            return(spanBuilder
                   .AddReference(References.FollowsFrom, spanContext)
                   .StartActive(true));
        }
예제 #6
0
        protected override SpanContext Extract(ITextMap carrier)
        {
            SpanContext context = null;
            Dictionary <string, string> baggage = null;
            string debugId = null;

            foreach (var entry in carrier)
            {
                // TODO there should be no lower-case here
                string key = entry.Key.ToLowerInvariant();
                if (string.Equals(key, _contextKey, StringComparison.Ordinal))
                {
                    context = SpanContext.ContextFromString(DecodedValue(entry.Value));
                }
                else if (string.Equals(key, Constants.DebugIdHeaderKey, StringComparison.Ordinal))
                {
                    debugId = DecodedValue(entry.Value);
                }
                else if (key.StartsWith(_baggagePrefix, StringComparison.Ordinal))
                {
                    if (baggage == null)
                    {
                        baggage = new Dictionary <string, string>();
                    }
                    baggage[Keys.UnprefixedKey(key, _baggagePrefix)] = DecodedValue(entry.Value);
                }
            }
            if (context == null)
            {
                if (debugId != null)
                {
                    return(SpanContext.WithDebugId(debugId));
                }
                return(null);
            }
            if (baggage == null)
            {
                return(context);
            }
            return(context.WithBaggage(baggage));
        }
예제 #7
0
        public void TestToStringFormatsPositiveFields()
        {
            TraceId traceId  = new TraceId(-10L);
            SpanId  spanId   = new SpanId(-10L);
            SpanId  parentId = new SpanId(-10L);
            byte    flags    = (byte)129;

            // I use MIN_VALUE because the most significant bit, and thats when
            // we want to make sure the hex number is positive.
            SpanContext context = new SpanContext(traceId, spanId, parentId, (SpanContextFlags)flags);

            context.ContextAsString().Split(':');

            Assert.Equal("fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.ContextAsString());
            SpanContext contextFromStr = SpanContext.ContextFromString(context.ContextAsString());

            Assert.Equal(traceId, contextFromStr.TraceId);
            Assert.Equal(spanId, contextFromStr.SpanId);
            Assert.Equal(parentId, contextFromStr.ParentId);
            Assert.Equal(flags, (byte)contextFromStr.Flags);
        }
예제 #8
0
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            var header      = context.RequestHeaders.Where(p => p.Key == "jaeger").FirstOrDefault();
            var spanBuilder = GlobalTracer.Instance.BuildSpan(context.Method).WithTag("Request", request?.ToJson() ?? "");

            if (header != null)
            {
                var spanContext = SpanContext.ContextFromString(header.Value);
                spanBuilder = spanBuilder.AsChildOf(spanContext);
            }
            using (var scope = spanBuilder.StartActive(true))
            {
                try
                {
                    return(await continuation(request, context));
                }
                catch (Exception ex)
                {
                    scope.Span.SetTag("Error", ex.ToString());
                    throw;
                }
            }
        }
예제 #9
0
 public void TestContextFromStringMalformedException()
 {
     Assert.Throws <ArgumentException>(() => SpanContext.ContextFromString("ff:ff:ff"));
 }
예제 #10
0
 public void TestContextFromStringEmptyException()
 {
     Assert.Throws <ArgumentException>(() => SpanContext.ContextFromString(""));
 }