Custom OpenCensus metrics not appearing on Stackdriver

扶醉桌前 提交于 2020-12-12 18:14:08

问题


I'm trying to send custom metrics to Stackdriver from my Go application using OpenCensus.

I've followed the guide, so the views and exporter are setup:

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
}

And then I send my metrics using:

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

But the custom metrics I'm sending aren't appearing in Stackdriver's Metrics Explorer.

What am I missing?


回答1:


The issue was in the user guide. You must in fact register the exporter and set a reporting interval:

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


来源:https://stackoverflow.com/questions/56937653/custom-opencensus-metrics-not-appearing-on-stackdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!