Example #1
0
        /// <summary>
        /// Get The log status(histogram info) from sls server which match input
        /// parameters. All the logs with logstore and topic in [from, to) which
        /// contain the keys in query are the matched data.
        /// </summary>
        /// <param name="request">The get histograms request</param>
        /// <exception>LogException</exception>
        /// <returns>The get histograms response</returns>
        public GetHistogramsResponse GetHistograms(GetHistogramsRequest request)
        {
            ServiceRequest sReq = new ServiceRequest();

            sReq.Method   = HttpMethod.Get;
            sReq.Endpoint = BuildReqEndpoint(request);

            //use empty string to replace Logstore if not set by user explicitly
            string logstore = request.IsSetLogstore() ? request.Logstore : String.Empty;

            sReq.ResourcePath = LogConsts.RESOURCE_LOGSTORES + LogConsts.RESOURCE_SEPARATOR + logstore;

            FillCommonHeaders(sReq);
            FillCommonParameters(sReq);

            sReq.Parameters.Add(LogConsts.PARAMETER_TYPE, LogConsts.VALUE_TYPE_STATUS);

            if (request.IsSetTopic())
            {
                sReq.Parameters.Add(LogConsts.PARAMETER_TOPIC, request.Topic);
            }

            if (request.IsSetFrom())
            {
                sReq.Parameters.Add(LogConsts.PARAMETER_FROM, request.From.ToString());
            }

            if (request.IsSetTo())
            {
                sReq.Parameters.Add(LogConsts.PARAMETER_TO, request.To.ToString());
            }

            if (request.IsSetQuery())
            {
                sReq.Parameters.Add(LogConsts.PARAMETER_QUERY, request.Query);
            }

            ExecutionContext context = new ExecutionContext();

            context.Signer      = new LogRequestSigner(sReq.ResourcePath, HttpMethod.Get);
            context.Credentials = new ServiceCredentials(this.AccessKeyId, this.AccessKey);

            using (ServiceResponse response = serviceClient.Send(sReq, context))
            {
                LogClientTools.ResponseErrorCheck(response, context.Credentials);
                JArray body = LogClientTools.ParserResponseToJArray(response.Content);
                GetHistogramsResponse res = new GetHistogramsResponse(response.Headers, body);
                return(res);
            }
        }
        public void TestGetHistograms()
        {
            LogClient client = new LogClient(ClientTestData.TEST_ENDPOINT, ClientTestData.TEST_ACCESSKEYID, ClientTestData.TEST_ACCESSKEY);

            client.SetWebSend(MockSend);
            GetHistogramsRequest request = new GetHistogramsRequest();

            request.Project  = ClientTestData.TEST_PROJECT;
            request.Logstore = "testlogstore";
            request.Query    = "error";
            request.Topic    = "mockTopic";
            request.From     = 1000;
            request.To       = 2000;
            GetHistogramsResponse response = client.GetHistograms(request);

            Assert.IsTrue(DicToString(Headers).CompareTo("[x-sls-apiversion:0.4.0][x-sls-bodyrawsize:0][x-sls-signaturemethod:hmac-sha1][User-Agent:aliyun-sdk-dotnet/1.0.0.0]") == 0);
            Assert.IsTrue(response != null && response.TotalCount == 2);
            Assert.IsTrue(Host.CompareTo("mock_project.mockhost.aliyuncs.com") == 0);
            Assert.IsTrue(RequestUri.CompareTo("http://mock_project.mockhost.aliyuncs.com/logstores/testlogstore?type=histogram&topic=mockTopic&from=1000&to=2000&query=error") == 0);
        }
        public void FT()
        {
            LogClient client = new LogClient("sls-failover.alibaba-inc.com", "", "");
            uint      topicFlag = DateUtils.TimeSpan();
            int       PUT_COUNT = 20, TOPIC_COUNT = 10, LOGITEM_COUNT = 20, CONTENT_COUNT = 10, SLEEP_INTERVAL = 2, SLEEP_TIME = 500;

            for (int j = 1; j <= PUT_COUNT; ++j)
            {
                PutLogsRequest putLogsReqError = new PutLogsRequest();
                putLogsReqError.Project  = "ali-winlogtail-project";
                putLogsReqError.Topic    = "dotnet_topic_" + topicFlag + "_" + (j % TOPIC_COUNT);
                putLogsReqError.Logstore = "sls-logstore-002";
                putLogsReqError.LogItems = new List <LogItem>();
                for (int i = 1; i <= LOGITEM_COUNT; ++i)
                {
                    LogItem logItem = new LogItem();
                    logItem.Time = (uint)(topicFlag + j);
                    for (int k = 0; k < CONTENT_COUNT; ++k)
                    {
                        logItem.PushBack("error_" + (j % TOPIC_COUNT) + "_" + k, "invalid operation: " + i * j);
                    }
                    putLogsReqError.LogItems.Add(logItem);
                }
                PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);
                if (j % SLEEP_INTERVAL == 0)
                {
                    Thread.Sleep(SLEEP_TIME);
                }
            }
            Thread.Sleep(50 * 1000);

            ListLogstoresRequest req = new ListLogstoresRequest();

            req.Project = "ali-winlogtail-project";
            ListLogstoresResponse res          = client.ListLogstores(req);
            HashSet <String>      logstoresSet = new HashSet <string>(res.Logstores);

            Assert.IsTrue(logstoresSet.Contains("sls-logstore-002"));


            ListTopicsRequest topicReq = new ListTopicsRequest();

            topicReq.Project  = "ali-winlogtail-project";
            topicReq.Logstore = "sls-logstore-002";
            topicReq.Lines    = TOPIC_COUNT;
            topicReq.Token    = "dotnet_topic_" + topicFlag + "_";
            ListTopicsResponse lstTopicsRequest = client.ListTopics(topicReq);

            Assert.IsTrue(lstTopicsRequest.Count >= TOPIC_COUNT);
            HashSet <String> topicSet = new HashSet <string>(lstTopicsRequest.Topics);

            for (int i = 0; i < TOPIC_COUNT; ++i)
            {
                Assert.IsTrue(topicSet.Contains("dotnet_topic_" + topicFlag + "_" + i));
            }
            Thread.Sleep(SLEEP_TIME);
            for (int i = 0; i < TOPIC_COUNT; ++i)
            {
                GetHistogramsRequest histReq = new GetHistogramsRequest();
                histReq.Project  = "ali-winlogtail-project";
                histReq.Logstore = "sls-logstore-002";
                histReq.Topic    = "dotnet_topic_" + topicFlag + "_" + i;
                histReq.To       = (uint)(topicFlag + PUT_COUNT + 1);
                histReq.From     = (uint)topicFlag;
                GetHistogramsResponse histResp = client.GetHistograms(histReq);
                Assert.IsTrue(histResp.TotalCount == (PUT_COUNT / TOPIC_COUNT) * LOGITEM_COUNT);
                if ((i + 1) % SLEEP_INTERVAL == 0)
                {
                    Thread.Sleep(SLEEP_TIME);
                }
            }
            Thread.Sleep(SLEEP_TIME);
            for (int i = 0; i < TOPIC_COUNT; ++i)
            {
                for (int k = 0; k < 2; ++k)
                {
                    GetHistogramsRequest histReq = new GetHistogramsRequest();
                    histReq.Project  = "ali-winlogtail-project";
                    histReq.Logstore = "sls-logstore-002";
                    histReq.Topic    = "dotnet_topic_" + topicFlag + "_" + i;
                    histReq.Query    = "error_" + i + "_" + k;
                    histReq.To       = (uint)(topicFlag + PUT_COUNT + 1);
                    histReq.From     = (uint)topicFlag;
                    GetHistogramsResponse histResp = client.GetHistograms(histReq);
                    Assert.IsTrue(histResp.TotalCount == (PUT_COUNT / TOPIC_COUNT) * LOGITEM_COUNT);
                    if ((k + 1) * (i + 1) % SLEEP_INTERVAL == 0)
                    {
                        Thread.Sleep(SLEEP_TIME);
                    }
                }
            }
            Thread.Sleep(SLEEP_TIME);
            for (int i = 0; i < TOPIC_COUNT; ++i)
            {
                GetLogsRequest getLogsReq = new GetLogsRequest();
                getLogsReq.Project  = "ali-winlogtail-project";
                getLogsReq.Logstore = "sls-logstore-002";
                getLogsReq.Topic    = "dotnet_topic_" + topicFlag + "_" + i;
                getLogsReq.Lines    = 120;
                getLogsReq.To       = (uint)(topicFlag + PUT_COUNT + 1);
                getLogsReq.From     = (uint)topicFlag;
                GetLogsResponse getLogsResp = client.GetLogs(getLogsReq);
                Assert.IsTrue(getLogsResp.Count == (PUT_COUNT / TOPIC_COUNT) * LOGITEM_COUNT);
                String logs = getLogsResp.Print();
                for (int m = 0; m < CONTENT_COUNT; ++m)
                {
                    String dstStr = "error_" + i + "_" + m;
                    Assert.IsTrue(ChildStringOccurTimes(logs, dstStr) == getLogsResp.Count);
                }
                if ((i + 1) % SLEEP_INTERVAL == 0)
                {
                    Thread.Sleep(SLEEP_TIME);
                }
            }
            Thread.Sleep(SLEEP_TIME);
            for (int i = 0; i < TOPIC_COUNT; ++i)
            {
                for (int k = 0; k < 2; ++k)
                {
                    GetLogsRequest getLogsReq = new GetLogsRequest();
                    getLogsReq.Project  = "ali-winlogtail-project";
                    getLogsReq.Logstore = "sls-logstore-002";
                    getLogsReq.Topic    = "dotnet_topic_" + topicFlag + "_" + i;
                    getLogsReq.Query    = "error_" + i + "_" + k;
                    getLogsReq.Lines    = 120;
                    getLogsReq.To       = (uint)(topicFlag + PUT_COUNT + 1);
                    getLogsReq.From     = (uint)topicFlag;
                    GetLogsResponse getLogsResp = client.GetLogs(getLogsReq);
                    Assert.IsTrue(getLogsResp.Count == (PUT_COUNT / TOPIC_COUNT) * LOGITEM_COUNT);
                    String logs = getLogsResp.Print();
                    for (int m = 0; m < CONTENT_COUNT; ++m)
                    {
                        String dstStr = "error_" + i + "_" + m;
                        Assert.IsTrue(ChildStringOccurTimes(logs, dstStr) == getLogsResp.Count);
                    }
                    if ((k + 1) * (i + 1) % SLEEP_INTERVAL == 0)
                    {
                        Thread.Sleep(SLEEP_TIME);
                    }
                }
            }
            Console.WriteLine();
        }