private void SetLogContext(TraceId traceId, ulong spanId)
        {
            if (!_safeToAddToMdc)
            {
                return;
            }

            try
            {
                // TODO: Debug logs
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.ServiceKey, _defaultServiceName, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.VersionKey, _version, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.EnvKey, _env, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.TraceIdKey, traceId.ToString(), destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SpanIdKey, spanId.ToString(), destructure: false));
            }
            catch (Exception)
            {
                _safeToAddToMdc = false;
                RemoveAllCorrelationIdentifierContexts();
            }
        }
예제 #2
0
        public Property ShouldParse64BitTraceIds(long traceIdLow)
        {
            var traceId1 = new TraceId(traceIdLow);
            var parsed   = TraceId.TryParse(traceId1.ToString(), out var traceId2);

            return(parsed.Label($"Should have been able to parse {traceId1}")
                   .And(traceId2.Equals(traceId1)
                        .Label("Expected a trace parsed from an original trace to be equal to the original")));
        }
예제 #3
0
        public void ZipkinJSONTransport_ConvertSpan()
        {
            var span    = Substitute.For <ILetsTraceSpan>();
            var traceId = new TraceId {
                High = 4, Low = 197376
            };
            var parentId    = new SpanId(1);
            var spanId      = new SpanId(2);
            var spanContext = new SpanContext(traceId, spanId, parentId);
            var logs        = new List <LogRecord> {
                new LogRecord(DateTimeOffset.Now, new List <Field> {
                    new Field <string> {
                        Key = "field_log_key", Value = "message, yo"
                    }
                })
            };
            var tags = new Dictionary <string, Field> {
                { Tags.SpanKind, new Field <string> {
                      Value = Tags.SpanKindServer
                  } },
                { "randomkey", new Field <bool> {
                      Value = false
                  } }
            };
            var operationName = "testing";
            var startTime     = DateTimeOffset.Parse("2/12/2018 5:49:19 PM +00:00");
            var finishTime    = DateTimeOffset.Parse("2/12/2018 5:49:37 PM +00:00");
            var serviceName   = "testingService";
            var hostIpv4      = "192.168.1.1";
            var tracer        = Substitute.For <ILetsTraceTracer>();

            span.Context.Returns(spanContext);
            span.Tracer.Returns(tracer);
            tracer.ServiceName.Returns(serviceName);
            tracer.HostIPv4.Returns(hostIpv4);
            span.Logs.Returns(logs);
            span.Tags.Returns(tags);
            span.OperationName.Returns(operationName);
            span.StartTimestamp.Returns(startTime);
            span.FinishTimestamp.Returns(finishTime);


            var convertedSpan = ZipkinJSONTransport.ConvertSpan(span);

            Assert.Equal(traceId.ToString(), convertedSpan.TraceId);
            Assert.Equal(parentId.ToString(), convertedSpan.ParentId);
            Assert.Equal(spanId.ToString(), convertedSpan.Id);
            Assert.Equal(operationName, convertedSpan.Name);
            Assert.Equal(1518457759000000, convertedSpan.Timestamp);
            Assert.Equal(18000000, convertedSpan.Duration);
            Assert.Equal(serviceName, convertedSpan.LocalEndpoint.ServiceName);
            Assert.Equal(hostIpv4, convertedSpan.LocalEndpoint.Ipv4);
            Assert.Equal("field_log_key: message, yo", convertedSpan.Annotations[0].Value);
            Assert.Equal(false.ToString(), convertedSpan.Tags["randomkey"]);
            Assert.Equal(1, convertedSpan.Tags.Count);
            Assert.Equal("SERVER", convertedSpan.Kind.GetValueOrDefault().ToString());
        }
        private void SetSerilogCompatibleLogContext(TraceId traceId, ulong spanId)
        {
            if (!_safeToAddToMdc)
            {
                return;
            }

            try
            {
                if (_logProvider is CustomSerilogLogProvider customSerilogLogProvider)
                {
                    var currentEnricher = _currentEnricher.Get();

                    if (currentEnricher == null)
                    {
                        _currentEnricher.Set(customSerilogLogProvider.OpenContext(_serilogEnricher));
                    }

                    return;
                }

                // TODO: Debug logs
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SerilogServiceKey, _defaultServiceName, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SerilogVersionKey, _version, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SerilogEnvKey, _env, destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SerilogTraceIdKey, traceId.ToString(), destructure: false));
                _contextDisposalStack.Push(
                    LogProvider.OpenMappedContext(
                        CorrelationIdentifier.SerilogSpanIdKey, spanId.ToString(), destructure: false));
            }
            catch (Exception)
            {
                _safeToAddToMdc = false;
                RemoveAllCorrelationIdentifierContexts();
            }
        }
예제 #5
0
        public override string ToString()
        {
            var sb = new StringBuilder("ObservedSpan(");

            if ((TraceId != null))
            {
                sb.Append(", TraceId: ");
                TraceId.ToString(sb);
            }
            sb.Append(", Sampled: ");
            Sampled.ToString(sb);
            if ((Baggage != null))
            {
                sb.Append(", Baggage: ");
                Baggage.ToString(sb);
            }
            sb.Append(')');
            return(sb.ToString());
        }
 public ErrorResponse(TraceId traceId)
 {
     this.TraceId = traceId.ToString();
 }
예제 #7
0
        public void TraceId_InitHighLow_ToString()
        {
            var traceId = new TraceId(16, 31);

            Assert.Equal("10000000000000001f", traceId.ToString());
        }
예제 #8
0
        public void TraceId_InitLow_ToString()
        {
            var traceId = new TraceId(10);

            Assert.Equal("a", traceId.ToString());
        }
예제 #9
0
 public override string ToString()
 {
     return(string.Join("-", new[] { Version, TraceId.ToString(), SpanId.ToString(), ((int)Flags).ToString("00") }));
 }
예제 #10
0
        public void valid(string id)
        {
            var tid = new TraceId(id);

            Assert.Equal(id, tid.ToString());
        }
예제 #11
0
        /// <summary>
        /// Запись события
        /// </summary>
        /// <param name="id">идентификатор трассировки</param>
        /// <param name="event"></param>
        public virtual void Write(TraceId id, TraceEvent @event)
        {
            if (Environment.UserInteractive == false)
            {
                return;
            }

            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (@event == null)
            {
                throw new ArgumentNullException("event");
            }

            lock (this)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(id.ToString());
                Console.ResetColor();

                foreach (var node in @event.Flatten())
                {
                    var e = node.Value;

                    string pref = new string(' ', node.Level * 2);
                    Console.Write(pref);
                    Console.ForegroundColor = ConsoleColor.Green;

                    Console.ResetColor();
                    Console.Write(e.Name);
                    if (e.PropertyCount > 0)
                    {
                        Console.Write("({0})"
                                      , string.Join(", "
                                                    , e.Properties().Select(kvp => kvp.Key + ": " + kvp.Value).ToArray()));
                    }

                    if (e.Duration.HasValue)
                    {
                        var dt = e.Duration.Value;
                        if (dt > TimeSpan.FromSeconds(2))
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                        }
                        Console.Write("[{0}]", dt.GetDisplayText());
                        Console.ResetColor();
                    }

                    if (e.ExcludedTime.HasValue)
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.Write("[{0}]", e.ExcludedTime.Value.GetDisplayText());
                        Console.ResetColor();
                    }

                    Console.WriteLine();
                }
            }
        }
예제 #12
0
 public void TraceId_ToString()
 {
     Assert.Contains("00000000000000000000000000000000", TraceId.Invalid.ToString());
     Assert.Contains("00000000000000000000000000000061", first.ToString());
     Assert.Contains("ff000000000000000000000000000041", second.ToString());
 }