Example #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;
            }
        }
Example #2
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;
            }
        }