Пример #1
0
        /**
         * 查询数据,返回的数据为json格式,结构为: * "[ * " { * " metric: mysql.innodb.row_lock_time, * " tags: { * " host: web01, * " dc: beijing * " }, * " aggregateTags: [], * " dps: { * " 1435716527: 1234, * " 1435716529: 2345 * " } * " }, * " { * " metric: mysql.innodb.row_lock_time, * " tags: { * " host: web02, * " dc: beijing * " }, * " aggregateTags: [], * " dps: { * " 1435716627: 3456 * " } * " } * "]"; * * @param metric 要查询的指标 * @param aggregator 查询的聚合类型, 如: OpentsdbClient.AGGREGATOR_AVG, OpentsdbClient.AGGREGATOR_SUM * @param tagMap 查询的条件 * @param downsample 采样的时间粒度, 如: 1s,2m,1h,1d,2d * @param startTime 查询开始时间,时间格式为yyyy-MM-dd HH:mm:ss * @param endTime 查询结束时间,时间格式为yyyy-MM-dd HH:mm:ss
         */
        public string getData(string metric, Dictionary <string, string> tagMap, string aggregator, string downsample, string startTime, string endTime)
        {
            QueryBuilder queryBuilder = QueryBuilder.getInstance();
            Query        query        = queryBuilder.getQuery();
            long         starttemp    = (DateTimeUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss").ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000;
            long         endtemp      = (DateTimeUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss").ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000;

            query.setStart(starttemp);
            query.setEnd(endtemp);
            List <SubQueries> sqList = new List <SubQueries>();
            SubQueries        sq     = new SubQueries();

            sq.addMetric(metric);
            sq.addTag(tagMap);
            sq.addAggregator(aggregator);
            sq.setDownsample(downsample + "-" + aggregator);
            sqList.Add(sq);
            query.setQueries(sqList);
            try
            {
                log.Info("query request:" + queryBuilder.build()); //这行起到校验作用
                SimpleHttpResponse spHttpResponse = httpClient.pushQueries(queryBuilder, ExpectResponse.DETAIL);
                log.Debug("response.content:" + spHttpResponse.getContent());
                if (spHttpResponse.isSuccess())
                {
                    return(spHttpResponse.getContent());
                }
                return(null);
            }
            catch (Exception e)
            {
                log.Error(e);
                throw e;
            }
        }
Пример #2
0
        public SimpleHttpResponse doPost(string url, string data, string contentType = "application/json")
        {
            SimpleHttpResponse simpleResponse = new SimpleHttpResponse();
            string             r = "";
            List <KeyValuePair <string, string> > paramList = new List <KeyValuePair <string, string> >();

            paramList.Add(new KeyValuePair <string, string>("Content", data));
            #region ͨѶÂß¼­
            using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromSeconds(10000);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                //HttpContent content = new FormUrlEncodedContent(paramList);
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                System.Net.Http.HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
                simpleResponse.setStatusCode((int)response.StatusCode);
                try
                {
                    r = response.Content.ReadAsStringAsync().Result;
                    simpleResponse.setContent(r);
                }
                catch (Exception ex)
                {
                    simpleResponse.setContent("´íÎó:" + ex.Message);
                    return(simpleResponse);
                }
            }
            return(simpleResponse);

            #endregion
        }
Пример #3
0
        public override SimpleHttpResponse pushLastQueries(String content, ExpectResponse expectResponse)
        {
            if (content == null)
            {
                throw new Exception("content 不能为空");
            }

            SimpleHttpResponse response = httpClient.doPost(buildUrl(serviceUrl, QUERY_POST_LAST_API, expectResponse),
                                                            content);

            return(response);
        }
Пример #4
0
        public override SimpleHttpResponse pushQueries(QueryBuilder builder, ExpectResponse expectResponse)
        {
            if (builder == null)
            {
                throw new Exception("QueryBuilder 不能为空");
            }

            SimpleHttpResponse response = httpClient.doPost(buildUrl(serviceUrl, QUERY_POST_API, expectResponse),
                                                            builder.build());

            return(response);
        }
Пример #5
0
        public override Response pushMetrics(MetricBuilder builder, ExpectResponse expectResponse)
        {
            if (builder == null)
            {
                throw new Exception("QueryBuilder 不能为空");
            }

            SimpleHttpResponse response = httpClient.doPost(buildUrl(serviceUrl, PUT_POST_API, expectResponse),
                                                            builder.build());

            return(getResponse(response));
        }
Пример #6
0
        /// <summary>
        /// 获取某个metric的最新值,默认给出最近24*7小时(一周)的数据,限制回滚时间不超过5000小时,时间过长会导致时序数据库卡死
        /// </summary>
        /// <param name="metric"></param>
        /// <param name="tagMap"></param>
        /// <param name="backScan">获取最新数据回滚时间,单位小时</param>
        /// <returns>参数值</returns>
        public QueryLastResponse queryLastData(string metric, Dictionary <string, string> tagMap, int backScan = 7 *24)
        {
            if (backScan > 5000)
            {
                throw new Exception("不支持查询5000h前的最新数据");
            }
            QueryLast queryLast = new QueryLast();

            queryLast.backScan     = backScan;
            queryLast.resolveNames = true;
            SubQueryLast subQueryLst = new SubQueryLast(metric, tagMap);

            queryLast.addSubQuery(subQueryLst);
            try
            {
                SimpleHttpResponse spHttpResponse = httpClient.pushLastQueries(queryLast.build(), ExpectResponse.DETAIL);
                if (spHttpResponse.isSuccess())
                {
                    string res = spHttpResponse.getContent();
                    if (null == res || "" == res)
                    {
                        return(null);
                    }
                    else
                    {
                        List <QueryLastResponse> ob = Newtonsoft.Json.JsonConvert.DeserializeObject <List <QueryLastResponse> >(res);
                        if (ob.Count == 0)
                        {
                            return(null);
                        }
                        else
                        {
                            return(ob[0]);
                        }
                    }
                }
                else
                {
                    throw new Exception("get data from opentsdb error: " + spHttpResponse.getContent());
                }
            }
            catch (Exception e)
            {
                log.Error(e);
                throw e;
            }
        }
Пример #7
0
        private Response getResponse(SimpleHttpResponse httpResponse)
        {
            Response response = new Response(httpResponse.getStatusCode());
            string   content  = httpResponse.getContent();

            if (!string.IsNullOrEmpty(content))
            {
                if (response.isSuccess())
                {
                    ErrorDetail errorDetail = JsonConvert.DeserializeObject <ErrorDetail>(content);
                    response.setErrorDetail(errorDetail);
                }
                else
                {
                    //logger.error("request failed!" + httpResponse);
                }
            }
            return(response);
        }