示例#1
0
文件: Trace.cs 项目: tvilkov/csharp
        public static ITraceContext CreateChildContext([NotNull] string contextName, [NotNull] string traceId, [NotNull] string parentContextId, bool?activate = null)
        {
            if (string.IsNullOrEmpty(contextName))
            {
                throw new InvalidOperationException("ContextName is empty");
            }
            if (string.IsNullOrEmpty(traceId))
            {
                throw new InvalidOperationException("TraceId is empty");
            }
            if (string.IsNullOrEmpty(parentContextId))
            {
                throw new InvalidOperationException("ParentContextId is empty");
            }

            InitializeIfNeeded();
            if (!configProvider.GetConfig().IsEnabled)
            {
                return(NoOpTraceContext.Instance);
            }

            var isSampled = activate ?? tracingEnvironment.TraceSampler.CanSampleTrace();

            if (!isSampled)
            {
                return(NoOpTraceContext.Instance);
            }

            var childContextId = TraceIdGenerator.CreateTraceContextId();
            var childContext   = new RealTraceContext(traceId, childContextId, contextName, parentContextId, tracingEnvironment, isRoot: false);

            SetRealTraceContext(childContext);
            return(childContext);
        }
示例#2
0
文件: Trace.cs 项目: tvilkov/csharp
        public static ITraceContext CreateChildContext([NotNull] string contextName, string contextId = null)
        {
            if (string.IsNullOrEmpty(contextName))
            {
                throw new InvalidOperationException("ContextName is empty");
            }

            var currentContext = TryGetRealTraceContext();

            if (currentContext == null)
            {
                return(NoOpTraceContext.Instance);
            }

            InitializeIfNeeded();
            if (!configProvider.GetConfig().IsEnabled)
            {
                return(NoOpTraceContext.Instance);
            }

            if (string.IsNullOrEmpty(contextId))
            {
                contextId = TraceIdGenerator.CreateTraceContextId();
            }
            var childContext = new RealTraceContext(currentContext.TraceId, contextId, contextName, currentContext.ContextId, tracingEnvironment, isRoot: false);

            SetRealTraceContext(childContext);
            return(childContext);
        }
示例#3
0
        public void CreateChildContext_should_set_context_id()
        {
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var rootContext = Trace.CreateRootContext("Test"))
                using (var childContext = Trace.CreateChildContext("Child", contextId))
                    childContext.ContextId.Should().BeSameAs(contextId);
        }
示例#4
0
        public void CreateChildContext_with_fixed_parent()
        {
            var traceId         = TraceIdGenerator.CreateTraceId();
            var parentContextId = TraceIdGenerator.CreateTraceContextId();

            using (var childContext = Trace.CreateChildContext("Child", traceId, parentContextId))
            {
                childContext.Should().NotBeSameAs(NoOpTraceContext.Instance);
                childContext.TraceId.Should().BeSameAs(traceId);
                childContext.IsActive.Should().BeTrue();
            }
        }
示例#5
0
        public void ContinueContext_should_set_null_traceid_and_contextid_when_not_active()
        {
            var traceId   = TraceIdGenerator.CreateTraceId();
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var traceContext = Trace.ContinueContext(traceId, contextId, isActive: false, isRoot: false))
            {
                traceContext.TraceId.Should().BeSameAs(string.Empty);
                traceContext.ContextId.Should().BeSameAs(string.Empty);
                traceContext.IsActive.Should().Be(false);
            }
        }
示例#6
0
        public void ContinueContext_should_use_trace_and_context_id_from_parameter_when_active()
        {
            var traceId   = TraceIdGenerator.CreateTraceId();
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var traceContext = Trace.ContinueContext(traceId, contextId, isActive: true, isRoot: false))
            {
                traceContext.TraceId.Should().BeSameAs(traceId);
                traceContext.ContextId.Should().BeSameAs(contextId);
                traceContext.IsActive.Should().Be(true);
            }
        }
        public void TryAdd_with_same_contextId_should_not_delete_previous()
        {
            var traceId   = TraceIdGenerator.CreateTraceId();
            var contextId = TraceIdGenerator.CreateTraceContextId();

            var info1 = GenerateContextInfo(traceId, contextId);
            var info2 = GenerateContextInfo(traceId, contextId);

            storage.TryAdd(info1);
            storage.TryAdd(info2);

            storage.GetAll().Should().Equal(info1, info2);
        }
示例#8
0
        public void ContinueContext_should_throw_exception_if_context_is_set_already(bool isActive)
        {
            var traceId   = TraceIdGenerator.CreateTraceId();
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var traceContext = Trace.CreateRootContext("Test"))
            {
                Assert.DoesNotThrow(() =>
                {
                    using (var continueContext = Trace.ContinueContext(traceId, contextId, isActive, isRoot: false))
                    {
                    }
                });
            }
        }