public static ITraceContext CreateRootContext([NotNull] string contextName, string traceId = null, bool?activate = null) { if (string.IsNullOrEmpty(contextName)) { throw new InvalidOperationException("ContextName is empty"); } InitializeIfNeeded(); if (!configProvider.GetConfig().IsEnabled) { return(NoOpTraceContext.Instance); } var isSampled = activate ?? tracingEnvironment.TraceSampler.CanSampleTrace(); if (!isSampled) { return(NoOpTraceContext.Instance); } if (string.IsNullOrEmpty(traceId)) { traceId = TraceIdGenerator.CreateTraceId(); } var rootContextId = TraceIdGenerator.CreateTraceContextId(); var rootContext = new RealTraceContext(traceId, rootContextId, contextName, null, tracingEnvironment, isRoot: true); SetRealTraceContext(rootContext); return(rootContext); }
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(); } }
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); } }
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); }
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)) { } }); } }