自定义OpenCensus指标未显示在Stackdriver上

时间:2019-07-08 14:59:12

标签: go stackdriver google-cloud-stackdriver opencensus

我正在尝试使用OpenCensus从Go应用程序将自定义指标发送到Stackdriver。

我遵循了guide,因此设置了视图和导出器:

import (
    "context"
    "contrib.go.opencensus.io/exporter/stackdriver"
    "github.com/pkg/errors"
    "go.opencensus.io/stats"
    "go.opencensus.io/stats/view"
    "time"
)

var (
    apiRequestDurationMs = stats.Int64("api_request_duration", "API request duration in milliseconds", stats.UnitMilliseconds)
)

func NewMetricsExporter() (*stackdriver.Exporter, error) {
    v := &view.View{
        Name:        "api_request_durations",
        Measure:     apiRequestDurationMs,
        Description: "The distribution of request durations",
        Aggregation: view.Distribution(0, 100, 200, 400, 1000, 2000, 4000),
    }
    if registerError := view.Register(v); registerError != nil {
        return nil, errors.Wrapf(registerError, "failed to register request duration view")
    }

    exporter, exporterError := stackdriver.NewExporter(stackdriver.Options{ProjectID: "project-ID"})
    if exporterError != nil {
        return nil, errors.Wrapf(exporterError, "failed to create stackdriver exporter")
    }

    if startError := exporter.StartMetricsExporter(); startError != nil {
        return nil, errors.Wrapf(startError, "failed to create stackdriver exporter")

    }
    return exporter, nil
}

然后我使用以下方式发送指标:

func RequestDuration(d time.Duration) {
    stats.Record(context.Background(), apiRequestDurationMs.M(int64(d)))
}

但是我发送的自定义指标没有出现在Stackdriver的Metrics Explorer中。

我想念什么?

1 个答案:

答案 0 :(得分:0)

问题出在用户指南中。 实际上,您必须注册出口商并设置报告间隔:

view.RegisterExporter(exporter)
view.SetReportingPeriod(60 * time.Second)