无法从指标查询语言 MQL – GCP 收集数据。

huangapple go评论83阅读模式
英文:

Unable to collect data from metric query language MQL - GCP

问题

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

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

以下是我的代码片段。它将创建监控客户端,并尝试从 GCP 监控收集数据。

public void queryTimeSeriesData() throws IOException {
    // 创建监控
    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 的数据,它总是返回最新的可用数据点。我的代码有问题吗?

英文:

I want to execute MQL (metric query language) using below library.

&lt;dependency&gt;
	&lt;groupId&gt;com.google.apis&lt;/groupId&gt;
	&lt;artifactId&gt;google-api-services-monitoring&lt;/artifactId&gt;
	&lt;version&gt;v3-rev540-1.25.0&lt;/version&gt;
&lt;/dependency&gt;

Here is my code snippet. which will create monitoring client and will try to collect data from GCP monitoring.

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

Above code is working fine but its not returning data of given startTime and endTime ,
It always returns latest datapoint available. is there any problem with my code ?

答案1

得分: 3

发现了使用给定时间范围执行MQL查询的方法。新的工作代码如下:

public void queryTimeSeriesData() throws IOException {
        // 创建监控实例
        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运算符将查询的起始时间和结束时间包含在查询中。根据Google MQL查询的文档:

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

英文:

Found way to execute MQL query with given time range. The
new working code is the following:

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

Included query start time and end time in query itself by using within operator. As per google docs for MQL queries:

> within - Specifies the time range of the query output.

huangapple
  • 本文由 发表于 2020年9月4日 00:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63728245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定