实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

时间:2022-06-24 10:18:00

对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时间及是否出错,下面是我对这两种请求的封装。

    /// <summary>
    /// cat中使用的HttpClient
    /// </summary>
    public class CatHttpClient
    {
        /// <summary>
        /// 返回当前Cat上下文
        /// </summary>
        /// <returns></returns>
        static CatContext GetCurrentContext(string message)
        {

            string currentUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
            var context = PureCat.CatClient.GetCatContextFromServer();
            if (context == null)
            {
                context = PureCat.CatClient.DoTransaction("xuexiba", currentUrl, () =>
                {
                    PureCat.CatClient.LogEvent("xuexiba", message, "0", currentUrl);
                });

            }
            else
            {

                context = PureCat.CatClient.DoTransaction("xuexiba", currentUrl, () =>
                {
                    PureCat.CatClient.LogRemoteCallServer(context);
                    PureCat.CatClient.LogEvent("xuexiba", message, "0", currentUrl);
                });
            }
            return context;
        }
        /// <summary>
        /// Post数据
        /// </summary>
        /// <param name="requestUri"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static HttpResponseMessage Post(string requestUri, HttpContent content)
        {
            var handler = new HttpClientHandler() { };
            using (var http = new HttpClient(handler))
            {
                PureCat.CatClient.SetCatContextToServer(http, GetCurrentContext("Post Request Sent..."));
                var response = http.PostAsync(requestUri, content).Result;
                return response;
            }
        }
        /// <summary>
        /// Get数据
        /// </summary>
        /// <param name="requestUri"></param>
        /// <returns></returns>
        public static HttpResponseMessage Get(string requestUri)
        {
            var handler = new HttpClientHandler() { };
            using (var http = new HttpClient(handler))
            {
                PureCat.CatClient.SetCatContextToServer(http, GetCurrentContext("Get Request Sent..."));//设置接口api的头,发送
                var response = http.GetAsync(requestUri).Result;
                return response;
            }
        }
    }

在程序中使用非常方便,如下代码,一看便知

[AllowAnonymous]
        public void Step1()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step2");
        }

        [AllowAnonymous]
        public void Step2()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step3");
        }

        [AllowAnonymous]
        public void Step3()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Step4");
        }
        [AllowAnonymous]
        public void Step4()
        {
            Lind.DDD.CatClientPur.CatHttpClient.Get("Http://localhost:4829/AdminCommon/Error");
        }

而它产生的消息树也是我们希望看到的,即从step1到step4的记录,如图

实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

下一讲我们将读一下,如何对一个请求从开始到结束进行cat的监控,敬请期待!