2

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?

noamt
  • 7,397
  • 2
  • 37
  • 59
  • Have you enabled billing on the Google Cloud Platform project? – DazWilkin Jul 09 '19 at 00:38
  • APIs Explorer is an excellent tool for testing any Google REST API. See this for enumerating Stackdriver Monitoring time series: https://developers.google.com/apis-explorer/#search/monitoring.projects.timeseries.list/m/monitoring/v3/monitoring.projects.timeSeries.list – DazWilkin Jul 09 '19 at 00:43
  • The OpenCensus page also recommends `view.SetReportingPeriod(60 * time.Second)` for Stackdriver (https://opencensus.io/exporters/supported-exporters/go/stackdriver/#creating-the-exporter). You should `sd.Flush()` before exiting but I think that's not your issue. – DazWilkin Jul 09 '19 at 00:50
  • Thanks, @DazWilkin . Yes, billing is enabled. I'll check out the open census docs – noamt Jul 09 '19 at 07:02

1 Answers1

3

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)
noamt
  • 7,397
  • 2
  • 37
  • 59