如何衡量Google云存储上每个用户的带宽使用情况?

时间:2019-07-02 12:58:21

标签: google-cloud-storage gcloud

我们要根据用户数据流量向用户收费。实际上,他们的数据正在消耗下行带宽。

我已经导出了Google云存储access_logs。从日志中,我可以计算出文件被访问的次数。 (文件大小*计数将是带宽使用量)

但是问题是这不适用于缓存的内容。我计算出的值比实际使用的要多得多。

我之所以使用这种方法,是因为我们的流量将是新的,并且不会使用缓存,这意味着差异无关紧要。但实际上,这似乎是一个现实问题。

这是一个常见的用例,我认为应该有一种更好的方法来解决Google云存储中的问题。

{
  "insertId": "-tohip8e1vmvw",
  "logName": "projects/bucket/logs/cloudaudit.googleapis.com%2Fdata_access",
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "authenticationInfo": {
      "principalEmail": "firebase-storage@system.gserviceaccount.com"
    },
    "authorizationInfo": [
      {
        "granted": true,
        "permission": "storage.objects.get",
        "resource": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
        "resourceAttributes": {}
      },
      {
        "granted": true,
        "permission": "storage.objects.getIamPolicy",
        "resource": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
        "resourceAttributes": {}
      }
    ],
    "methodName": "storage.objects.get",
    "requestMetadata": {
      "destinationAttributes": {},
      "requestAttributes": {
        "auth": {},
        "time": "2019-07-02T11:58:36.068Z"
      }
    },
    "resourceLocation": {
      "currentLocations": [
        "eu"
      ]
    },
    "resourceName": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
    "serviceName": "storage.googleapis.com",
    "status": {}
  },
  "receiveTimestamp": "2019-07-02T11:58:36.412798307Z",
  "resource": {
    "labels": {
      "bucket_name": "bucket.appspot.com",
      "location": "eu",
      "project_id": "project-id"
    },
    "type": "gcs_bucket"
  },
  "severity": "INFO",
  "timestamp": "2019-07-02T11:58:36.062Z"
}

日志条目。

我们现在正在使用单个存储桶。如果有帮助,也可以使用多个。

1 个答案:

答案 0 :(得分:0)

一种可能性是为每个用户使用单独的存储桶,并通过timeseries api获取存储桶的带宽使用情况。

用于此目的的端点是:

https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list

以下是实现发送一小时(我们可以指定60s以上的时间范围)的字节的参数,其总和将是从存储桶发送的总字节。

{
  "dataSets": [
    {
      "timeSeriesFilter": {
        "filter": "metric.type=\"storage.googleapis.com/network/sent_bytes_count\" resource.type=\"gcs_bucket\" resource.label.\"project_id\"=\"<<<< project id here >>>>\" resource.label.\"bucket_name\"=\"<<<< bucket name here >>>>\"",
        "perSeriesAligner": "ALIGN_SUM",
        "crossSeriesReducer": "REDUCE_SUM",
        "secondaryCrossSeriesReducer": "REDUCE_SUM",
        "minAlignmentPeriod": "3600s",
        "groupByFields": [
          "resource.label.\"bucket_name\""
        ],
        "unitOverride": "By"
      },
      "targetAxis": "Y1",
      "plotType": "LINE",
      "legendTemplate": "${resource.labels.bucket_name}"
    }
  ],
  "options": {
    "mode": "COLOR"
  },
  "constantLines": [],
  "timeshiftDuration": "0s",
  "y1Axis": {
    "label": "y1Axis",
    "scale": "LINEAR"
  }
}