【问题标题】:Unable to collect data from metric query language MQL - GCP无法从度量查询语言 MQL - GCP 收集数据
【发布时间】:2020-12-22 23:07:37
【问题描述】:

我想使用下面的库执行 MQL(指标查询语言)。

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-monitoring</artifactId>
    <version>v3-rev540-1.25.0</version>
</dependency>

这是我的代码 sn-p。这将创建监控客户端并尝试从 GCP 监控中收集数据。

public void queryTimeSeriesData() throws IOException {
        // create monitoring 
        Monitoring m = createAuthorizedMonitoringClient();
        QueryTimeSeriesRequest req  = new QueryTimeSeriesRequest();
        String query = "fetch consumed_api\n" + 
                "| metric 'serviceruntime.googleapis.com/api/request_count'\n" + 
                "| align rate(2m)\n" + 
                "| every 2m\n" + 
                "| group_by [metric.response_code],\n" + 
                "    [value_request_count_max: max(value.request_count)]";
        
        req.setQuery(query);
        HashMap<String, Object> queryTransformationSpec = new HashMap<String, Object>();
        HashMap<String, Object> timingState =  new HashMap<String, Object>();
        HashMap<String, Object> absoluteWindow = new HashMap<String, Object>();
        absoluteWindow.put("startTime", "2020-09-03T12:40:00.000Z");
        absoluteWindow.put("endTime", "2020-09-03T13:41:00.000Z");
        timingState.put("absoluteWindow", absoluteWindow);
        timingState.put("graphPeriod", "60s");
        timingState.put("queryPeriod", "60s");
        queryTransformationSpec.put("timingState", timingState);
        
        
        req.set("queryTransformationSpec", queryTransformationSpec);
        req.set("reportPeriodicStats", false);
        req.set("reportQueryPlan", false);
        
        QueryTimeSeriesResponse res = m.projects().timeSeries().query("projects/MY_PROJECT_NAME", req).execute();
        System.out.println(res);
    }

上面的代码工作正常,但它没有返回给定 startTime 和 endTime 的数据, 它总是返回可用的最新数据点。我的代码有问题吗?

【问题讨论】:

    标签: java google-cloud-platform google-api-java-client google-cloud-monitoring monitoring-query-language


    【解决方案1】:

    找到在给定时间范围内执行 MQL 查询的方法。这 新的工作代码如下:

    public void queryTimeSeriesData() throws IOException {
            // create monitoring 
            Monitoring m = createAuthorizedMonitoringClient();
            QueryTimeSeriesRequest req  = new QueryTimeSeriesRequest();
            String query = "fetch consumed_api\n" + 
                    "| metric 'serviceruntime.googleapis.com/api/request_count'\n" + 
                    "| align rate(5m)\n" + 
                    "| every 5m\n" + 
                    "| group_by [metric.response_code],\n" + 
                    "    [value_request_count_max: max(value.request_count)]" + 
                    "| within   d'2020/09/03-12:40:00', d'2020/09/03-12:50:00'\n";
            
            req.setQuery(query);
            QueryTimeSeriesResponse res = m.projects().timeSeries().query("projects/MY_PROJECT_NAME", req).execute();
            System.out.println(res);
        }
    

    使用within 运算符在查询本身中包含查询开始时间和结束时间。根据 MQL 查询的 google 文档:

    within - 指定查询输出的时间范围。

    【讨论】:

    • 嗨,Djai,你也可以接受你自己的答案 :)
    • :) 谢谢。我没有考虑分数和袋子并接受答案,似乎对其他读者有用。完成,将跟进。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多