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); }
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); }
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); }
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)) { } }); } }