【问题标题】:TimeSeries Rest API https://monitoring.googleapis.com/v3/projects/../timeseries returning emptyTimeSeries Rest API https://monitoring.googleapis.com/v3/projects/../timeseries 返回空
【发布时间】:2020-01-17 06:54:23
【问题描述】:

我正在尝试使用 Google 监控 REST API(代码示例)获取性能数据。但它返回空数据“{}”。还观察到它正在重新调整响应的 ContentEncoding 是“gzip”类型。

private static void executeMetricUrl(Credential credential) throws Exception {
// Set up and execute a Google Cloud Storage request.
long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000);
Date currentDate = new Date(startMillis);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

//formatted value of current Date
System.out.println("Milliseconds to Date: " + df.format(currentDate));

//System.out.println(currentDate);
String URI = "https://monitoring.googleapis.com/v3/projects/" + projectId+"/timeSeries/?";
String interval =
    "interval.end_time=" + df.format(System.currentTimeMillis()) + "&interval.start_time="
        + df.format(currentDate);
String filter =
    "filter=metric.type=\"compute.googleapis.com/instance/cpu/utilization\""; //AND metric.labels.instance_name=sampleinstance-1";
String aggregation =
    "aggregation.perSeriesAligner=ALIGN_RATE&aggregation.alignmentPeriod=60s&aggregation.crossSeriesReducer=ALIGN_MEAN";

String finalUrl = URI+filter+"&"+interval;
System.out.println("Final URL ::: "+finalUrl);
TimeInterval interval1 = TimeInterval
    .newBuilder()
    .setStartTime(Timestamps.fromMillis(startMillis))
    .setEndTime(
            Timestamps.fromMillis(System
                    .currentTimeMillis())).build();

HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(finalUrl);
HttpRequest request = requestFactory.buildGetRequest(url);
//HttpHeaders headers = new HttpHeaders();    
//request.setHeaders(headers.setAcceptEncoding("gzip"));
HttpResponse response = request.execute();
String html = "";
BufferedReader  in = new BufferedReader(new InputStreamReader(response.getContent()));  
String inputLine;
while ((inputLine = in.readLine()) != null) {
  html += inputLine + "\n";
}
 System.out.println("content ::: "+html);

}

我正在使用 MetricServiceClient 获得具有相同过滤器和间隔的正确时间序列数据。我是否遗漏了 REST API 方法中的某些内容。

【问题讨论】:

  • 您可以尝试先使用Method: projects.timeSeries.list API 进行测试,看看是否得到相同的空结果。请注意,响应中仅包含包含指定时间间隔内的数据点的时间序列。
  • 是的,我也在这个页面上尝试过,得到相同的空 {} 结果。我正在尝试从当前时间获取 20 分钟的数据。

标签: java google-app-engine google-api


【解决方案1】:

我尝试使用Method: projects.timeSeries.list 重现您的案例

我使用了以下参数

name : project/your-project

filter : metric.type = "compute.googleapis.com/instance/cpu/utilization" AND  metric.labels.instance_name = "instance-name"

interval.startTime : 2020-01-10T15:01:23.045123456Z

interval.endTime : 2020-01-11T15:01:23.045123456Z

我收到以下回复:

  "timeSeries": [
    {
      "metric": {
        "labels": {
          "instance_name": "instance-name"
        },
        "type": "compute.googleapis.com/instance/cpu/utilization"
      },
      "resource": {
        "type": "gce_instance",
        "labels": {
          "instance_id": "XXXXXXXXXXX",
          "zone": "XXXXXXXXX",
          "project_id": "your-project"
        }
      },
      "metricKind": "GAUGE",
      "valueType": "DOUBLE",
      "points": [
        {
          "interval": {
            "startTime": "2020-01-11T15:01:00Z",
            "endTime": "2020-01-11T15:01:00Z"
          },
          "value": {
            "doubleValue": 0.00097723160608135617
          }
        },
        {
          "interval": {
            "startTime": "2020-01-11T15:00:00Z",
            "endTime": "2020-01-11T15:00:00Z"
          },
          "value": {
            "doubleValue": 0.00092651031620789577
          }
....................................................

我建议您注意如何设置 startTime 和 EndTime。

应该是 RFC3339 UTC "Zulu" 格式的时间戳TimeInterval

同时尝试在您的过滤器中包含实例名称。

【讨论】:

  • 谢谢玛丽安。我已经尝试过您的 TimeInterval ,现在返回数据。时间间隔好像有问题。
  • 如果我的回答有用,请将我的回答标记为已接受,以提高社区的知名度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多