簡體   English   中英

使用 OpenCensus 時無法獲取自定義指標數據

[英]Can't get custom metrics data when using OpenCensus

我正在遵循本指南monitoring_opencensus_metrics_quickstart-go 此外,我也嘗試了這個答案的方式。

代碼在這里:

package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "path"
    "time"

    "google.golang.org/api/option"

    "contrib.go.opencensus.io/exporter/stackdriver"
    "go.opencensus.io/stats"
    "go.opencensus.io/stats/view"
    "golang.org/x/exp/rand"
)

var (
    // The task latency in milliseconds.
    latencyMs = stats.Float64("task_latency", "The task latency in milliseconds", "ms")
)

func main() {
    ctx := context.Background()
    v := &view.View{
        Name:        "task_latency_distribution",
        Measure:     latencyMs,
        Description: "The distribution of the task latencies",
        Aggregation: view.Distribution(0, 100, 200, 400, 1000, 2000, 4000),
    }
    if err := view.Register(v); err != nil {
        log.Fatalf("Failed to register the view: %v", err)
    }

    exporter, err := stackdriver.NewExporter(stackdriver.Options{
        ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
        MonitoringClientOptions: []option.ClientOption{
            option.WithCredentialsFile(path.Join("./.gcp/stackdriver-monitor-admin.json")),
        },
    })
    if err != nil {
        log.Fatal(err)
    }
    view.RegisterExporter(exporter)
    view.SetReportingPeriod(60 * time.Second)
    // Flush must be called before main() exits to ensure metrics are recorded.
    defer exporter.Flush()

    if err := exporter.StartMetricsExporter(); err != nil {
        log.Fatalf("Error starting metric exporter: %v", err)
    }
    defer exporter.StopMetricsExporter()

    // Record 100 fake latency values between 0 and 5 seconds.
    for i := 0; i < 100; i++ {
        ms := float64(5*time.Second/time.Millisecond) * rand.Float64()
        fmt.Printf("Latency %d: %f\n", i, ms)
        stats.Record(ctx, latencyMs.M(ms))
        time.Sleep(1 * time.Second)
    }

    fmt.Println("Done recording metrics")
}

我在本地運行上述代碼,而不是在 GCE、GAE 和 GKE 環境中。

在 metrics explorer web UI 中,這里是 metric 查詢條件:

  • 資源類型: Consumed API
  • 指標: custom.googleapis.com/opencensus/task_latency_distribution

完整查詢:

fetch consumed_api
| metric 'custom.googleapis.com/opencensus/task_latency_distribution'
| align delta(1m)
| every 1m
| group_by [],
    [value_task_latency_distribution_aggregate:
       aggregate(value.task_latency_distribution)]

服務帳戶具有Monitoring Admin角色。

但是得到No data is available for the selected time frame

在此處輸入圖像描述

它對我有用,但我傾向於做一些稍微不同的事情:

  • export GOOGLE_APPLICATION_CREDENTIALS=path/to/creds而不是MonitoringClientOptions{}
  • 以前 (?;) 我在使用 0 桶進行分發時遇到問題; 嘗試刪除初始 (0) 桶並重試;
  • 從指標資源管理器中刪除resource.type 來自 APIs Explorer,如果有的話,這應該是global

Google APIs Explorer 是診斷 Stackdriver API 挑戰的絕佳方式。 您可以使用它來列出指標並列出時間序列(替換your-project-id並更新 2 個interval值):

https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/list?apix=true&apix_params=%7B%22name%22%3A%22projects%2Fyour-project-id%22%2C %22filter%22%3A%22metric.type%3D%5C%22custom.googleapis.com%2Fopencensus%2Ftask_latency_distribution%5C%22%22%7D

https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list?apix=true&apix_params=%7B%22name%22%3A%22projects%2Fyour-project-id%22%2C %22aggregation.alignmentPeriod%22%3A%22%2B300s%22%2C%22aggregation.crossSeriesReducer%22%3A%22REDUCE_MEAN%22%2C%22aggregation.perSeriesAligner%22%3A%22ALIGN_DELTA%22%2C%22filter%22%3A %22metric.type%3D%5C%22custom.googleapis.com%2Fopencensus%2Ftask_latency_distribution%5C%22%22%2C%22interval.endTime%22%3A%222020-09-08T23%3A59%3A59Z%22%2C%22interval .startTime%22%3A%222020-09-08T00%3A00%3A00Z%22%7D

使用 Chrome 的 Developer Console,您可以找到 Stackdriver 對 API 的調用之一,以便更輕松地重現它,例如

filter: metric.type="custom.googleapis.com/opencensus/task_latency_distribution"
aggregation.crossSeriesReducer: REDUCE_MEAN
aggregation.alignmentPeriod: +60s
aggregation.perSeriesAligner: ALIGN_DELTA
secondaryAggregation.crossSeriesReducer: REDUCE_NONE
interval.startTime: 2020-09-08T23:59:59Z
interval.endTime: 2020-09-08T00:00:00Z

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM