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)) { } }); } }
public string PostRequest(string host, ApiAttribute attr, IEnumerable <KeyValuePair <string, object> > paramList) { // 创建Promise Uri uri = new Uri(host); var restClient = new RestClient { BaseUrl = uri, UserAgent = UserAgentUtil.Instance.FinaleValue }; // 设置WebProxy restClient.Proxy = WebProxy; // 设置参数 var request = new RestRequest(attr.Path, Method.POST) { Timeout = attr.Timeout * 1000 }; // 设置请求头 foreach (var keyValuePair in _customHeaders) { request.AddHeader(keyValuePair.Key, keyValuePair.Value); } // 请求的追踪ID,同一个请求和响应的追踪ID是相同的 var traceId = TraceIdGenerator.NewTraceId(); var remark = $"HttpClient=RestSharp^TraceId={traceId}^Host={GalileoApiConfig.Instance.LoggerProcMaskHost?.Invoke(host) ?? host}^Method={attr.Method}^Path={attr.Path}"; switch (attr.Format) { // json格式,CashierApi/开放平台使用 case ApiConstants.Json: { var json = SensitiveKeywordManager.Instance.ReplaceSensitive(AddJsonBody(request, paramList)); GalileoLogger.Instance.Debug( $"Action=SendRequest^Format=JSON^{remark}^Format=Json^Body={json.Replace("\\\"", "'")}"); break; } // 默认Form格式,CashierGw使用 default: { var sb = new StringBuilder(); foreach (var param in paramList) { if (param.Value == null) { continue; } request.AddParameter(param.Key, param.Value); if (sb.Length != 0) { sb.Append("&"); } sb.Append($"{param.Key}:({param.Value})"); } GalileoLogger.Instance.Debug( $"Action=SendRequest^Format=FORM^{remark}^Format=Form^Body={{{sb}}})"); break; } } var startTick = Environment.TickCount; var response = restClient.Execute(request); var content = $"{response.Content}"; if (response.StatusCode != HttpStatusCode.OK) { content = string.IsNullOrEmpty(response.Content) ? "网络错误,请检查网络连接" : $"网络错误:{response.Content}"; } GalileoLogger.Instance.Debug( $"Action=GetResponse^{remark}^Duration={Environment.TickCount - startTick}^StatusCode={response.StatusCode}^Status={response.ResponseStatus}^Body={content}"); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpException(Convert.ToInt32(response.StatusCode), response.StatusDescription, (int)response.ResponseStatus, content, response.ErrorException); } return(response.Content); }