问题描述
我正在遵循本指南monitoring_opencensus_metrics_quickstart-go。此外,我还尝试了使用此answer的方法。
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环境中运行。
在指标浏览器的Web UI中,这是指标查询条件:
- 资源类型:
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
;来自API Explorer,如果有的话,则应为global
Google APIs Explorer是诊断Stackdriver API挑战的绝佳方法。您可以使用它列出指标并列出时间序列(替换your-project-id
并更新2个interval
值):
使用Chrome的开发者控制台,您可以找到Stackdriver对该API的调用之一,以便更轻松地重现该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