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

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

本文介绍了自定义OpenCensus指标未显示在Stackdriver上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)))
}

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

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

我想念什么?

推荐答案

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

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)

这篇关于自定义OpenCensus指标未显示在Stackdriver上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:24