Skip to content
Advertisement

Unable to collect data from metric query language MQL – GCP

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

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

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 = "fetch consumed_apin" + 
                "| metric 'serviceruntime.googleapis.com/api/request_count'n" + 
                "| align rate(2m)n" + 
                "| every 2mn" + 
                "| 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);
    }

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 ?

Advertisement

Answer

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 = "fetch consumed_apin" + 
                "| metric 'serviceruntime.googleapis.com/api/request_count'n" + 
                "| align rate(5m)n" + 
                "| every 5mn" + 
                "| 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);
    }

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement