Ejemplo n.º 1
0
        public void TraceId_FromString()
        {
            var ex = Assert.Throws <Exception>(() => TraceId.FromString(""));

            Assert.Equal("Cannot parse Low TraceId from string: ", ex.Message);

            var tooLongTraceId = "0123456789abcdef0123456789abcdef012";

            ex = Assert.Throws <Exception>(() => TraceId.FromString(tooLongTraceId));
            Assert.Equal($"TraceId cannot be longer than 32 hex characters: {tooLongTraceId}", ex.Message);

            var traceId = TraceId.FromString("0123456789abcdeffedcba9876543210");

            Assert.Equal("0123456789abcdef", traceId.High.ToString("x016"));
            Assert.Equal("fedcba9876543210", traceId.Low.ToString("x016"));

            ex = Assert.Throws <Exception>(() => TraceId.FromString("01234_6789abcdeffedcba9876543210"));
            Assert.Equal("Cannot parse High TraceId from string: 01234_6789abcdef", ex.Message);

            var badLowTraceId = "0123456789abcdeffedcba_876543210";

            ex = Assert.Throws <Exception>(() => TraceId.FromString(badLowTraceId));
            Assert.Equal("Cannot parse Low TraceId from string: fedcba_876543210", ex.Message);

            traceId = TraceId.FromString("10000000000000001");
            Assert.Equal("1", traceId.High.ToString());
            Assert.Equal("1", traceId.Low.ToString());
        }
Ejemplo n.º 2
0
        public void FromString_128Bit_WithZeroes()
        {
            var traceId = TraceId.FromString("0123456789abcdeffedcba9876543210");

            Assert.Equal("0123456789abcdef", traceId.High.ToString("x016"));
            Assert.Equal("fedcba9876543210", traceId.Low.ToString("x016"));
        }
Ejemplo n.º 3
0
        public async Task Invoke()
        {
            if (_dbContext.PersistedEvents.Any(pe => !pe.Processed))
            {
                foreach (var persistedEvent in _dbContext.PersistedEvents.Where(pe => !pe.Processed).ToList())
                {
                    Console.WriteLine($"Sending event: {JsonSerializer.Serialize(persistedEvent)}");
                    var eventType             = GetEventType(persistedEvent.Type);
                    var @event                = JsonSerializer.Deserialize(persistedEvent.Body, eventType);
                    var persistedEventContext = _dbContext.EventContexts.Single(ec => ec.EventId == persistedEvent.Id);
                    var operationName         = $"Publishing Message: {eventType}";

                    var spanBuilder = GlobalTracer.Instance.BuildSpan(operationName);
                    var spanContext = new SpanContext(TraceId.FromString(persistedEventContext.TraceId),
                                                      SpanId.NewUniqueId(), SpanId.FromString(persistedEventContext.SpanId),
                                                      SpanContextFlags.Sampled);

                    using (var scope = spanBuilder.AsChildOf(spanContext).StartActive())
                    {
                        await _eventBus.Publish(@event);
                    }

                    persistedEvent.Processed = true;
                }
                await _dbContext.SaveChangesAsync();
            }
        }
Ejemplo n.º 4
0
        public void FromString_MoreThan128Bit()
        {
            var tooLongTraceId = "0123456789abcdef0123456789abcdef012";
            var ex             = Assert.Throws <Exception>(() => TraceId.FromString(tooLongTraceId));

            Assert.Equal($"TraceId cannot be longer than 32 hex characters: {tooLongTraceId}", ex.Message);
        }
Ejemplo n.º 5
0
        public void FromString_128Bit_WithoutZeroes()
        {
            var traceId = TraceId.FromString("10000000000000001");

            Assert.Equal("1", traceId.High.ToString());
            Assert.Equal("1", traceId.Low.ToString());
        }
Ejemplo n.º 6
0
        public void FromString_64Bit_WithZeroes()
        {
            var traceId = TraceId.FromString("0123456789abcdef");

            Assert.Equal(0, traceId.High);
            Assert.Equal(0x123456789abcdef, traceId.Low);
        }
Ejemplo n.º 7
0
        public void FromString_64Bit_LowMalformed()
        {
            var badLowTraceId = "fedcba_876543210";
            var ex            = Assert.Throws <Exception>(() => TraceId.FromString(badLowTraceId));

            Assert.Equal("Cannot parse Low TraceId from string: fedcba_876543210", ex.Message);
        }
Ejemplo n.º 8
0
        public void FromString_64Bit_WithoutZeroes()
        {
            var traceId = TraceId.FromString("1");

            Assert.Equal(0, traceId.High);
            Assert.Equal(1, traceId.Low);
        }
Ejemplo n.º 9
0
        protected override SpanContext Extract(ITextMap carrier)
        {
            TraceId?         traceId  = null;
            SpanId?          spanId   = null;
            SpanId           parentId = new SpanId(0); // Conventionally, parent id == 0 means the root span
            SpanContextFlags flags    = SpanContextFlags.None;

            foreach (var entry in carrier)
            {
                if (string.Equals(entry.Key, SampledName, StringComparison.OrdinalIgnoreCase))
                {
                    string value = entry.Value;
                    if (string.Equals(value, "1", StringComparison.Ordinal) || string.Equals(value, "true", StringComparison.OrdinalIgnoreCase))
                    {
                        flags |= SpanContextFlags.Sampled;
                    }
                }
                else if (string.Equals(entry.Key, TraceIdName, StringComparison.OrdinalIgnoreCase))
                {
                    traceId = TraceId.FromString(entry.Value);
                }
                else if (string.Equals(entry.Key, ParentSpanIdName, StringComparison.OrdinalIgnoreCase))
                {
                    parentId = SpanId.FromString(entry.Value);
                }
                else if (string.Equals(entry.Key, SpanIdName, StringComparison.OrdinalIgnoreCase))
                {
                    spanId = SpanId.FromString(entry.Value);
                }
                else if (string.Equals(entry.Key, FlagsName, StringComparison.OrdinalIgnoreCase))
                {
                    if (string.Equals(entry.Value, "1", StringComparison.OrdinalIgnoreCase))
                    {
                        flags |= SpanContextFlags.Debug;
                    }
                }
            }

            if (traceId != null && spanId != null)
            {
                return(new SpanContext(traceId.Value, spanId.Value, parentId, flags));
            }
            return(null);
        }
Ejemplo n.º 10
0
        public void TraceId_InitHighLow_ShouldReturnBytes()
        {
            var traceId = TraceId.FromString("0123456789abcdeffedcba9876543210");

            Assert.Collection(traceId.ToByteArray(),
                              b => Assert.Equal(b, (byte)0x01),
                              b => Assert.Equal(b, (byte)0x23),
                              b => Assert.Equal(b, (byte)0x45),
                              b => Assert.Equal(b, (byte)0x67),
                              b => Assert.Equal(b, (byte)0x89),
                              b => Assert.Equal(b, (byte)0xab),
                              b => Assert.Equal(b, (byte)0xcd),
                              b => Assert.Equal(b, (byte)0xef),
                              b => Assert.Equal(b, (byte)0xfe),
                              b => Assert.Equal(b, (byte)0xdc),
                              b => Assert.Equal(b, (byte)0xba),
                              b => Assert.Equal(b, (byte)0x98),
                              b => Assert.Equal(b, (byte)0x76),
                              b => Assert.Equal(b, (byte)0x54),
                              b => Assert.Equal(b, (byte)0x32),
                              b => Assert.Equal(b, (byte)0x10));
        }
Ejemplo n.º 11
0
        public void TraceId_InitLow_ShouldReturnBytes()
        {
            var traceId = TraceId.FromString("fedcba9876543210");

            Assert.Collection(traceId.ToByteArray(),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0x00),
                              b => Assert.Equal(b, (byte)0xfe),
                              b => Assert.Equal(b, (byte)0xdc),
                              b => Assert.Equal(b, (byte)0xba),
                              b => Assert.Equal(b, (byte)0x98),
                              b => Assert.Equal(b, (byte)0x76),
                              b => Assert.Equal(b, (byte)0x54),
                              b => Assert.Equal(b, (byte)0x32),
                              b => Assert.Equal(b, (byte)0x10));
        }
Ejemplo n.º 12
0
        public void FromString_Empty()
        {
            var ex = Assert.Throws <Exception>(() => TraceId.FromString(""));

            Assert.Equal("Cannot parse Low TraceId from string: ", ex.Message);
        }
Ejemplo n.º 13
0
        public void FromString_128Bit_HighMalformed()
        {
            var ex = Assert.Throws <Exception>(() => TraceId.FromString("01234_6789abcdeffedcba9876543210"));

            Assert.Equal("Cannot parse High TraceId from string: 01234_6789abcdef", ex.Message);
        }
Ejemplo n.º 14
0
        private Span ConvertToSpan(TempSpan tempSpan, Tracer tracer)
        {
            var mySpan = tempSpan.Span;
            var tags   = mySpan.Tags;

            var parentId = TryFindParentId(mySpan);
            var context  = new SpanContext(TraceId.FromString(mySpan.TraceId),
                                           SpanId.FromString(mySpan.SpanId),
                                           parentId,
                                           (SpanContextFlags)mySpan.Flags);

            if (SpanAssembly == null)
            {
                var filePath = AppDomain.CurrentDomain.Combine("Jaeger.dll");
                if (!File.Exists(filePath))
                {
                    throw new InvalidOperationException("SpanAssembly Not Found: " + filePath);
                }
                SpanAssembly = Assembly.LoadFile(filePath);
            }

            var references = new List <Reference>();

            foreach (var reference in mySpan.References)
            {
                references.Add(new Reference(context, reference.Type));
            }

            //hack it, attention for same types, or fail wit ex: System.MissingMethodException!
            //internal Span(
            //    Tracer tracer,
            //    string operationName,
            //    SpanContext context,
            //    DateTime startTimestampUtc,
            //    Dictionary<string, object> tags,
            //    IReadOnlyList<Reference> references)

            var span = CreateSpanWithReflection(SpanAssembly,
                                                tracer,
                                                mySpan.OpName,
                                                context,
                                                mySpan.StartTime.ToUniversalTime(),
                                                tags,
                                                references.AsReadOnly()
                                                );


            var logs = mySpan.Logs;

            foreach (var log in logs)
            {
                if (log.Message != null)
                {
                    //"fields": [
                    //{
                    //    "key": "event",
                    //    "type": "string",
                    //    "value": "myLog by FooDomain"
                    //}
                    span.Log(log.TimestampUtc, log.Message);
                }
                else
                {
                    span.Log(log.TimestampUtc, log.Fields);
                }
            }

            //span.Finish(mySpan.StopTime);
            return(span);
        }