Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 1996040

Browse files
authored
Add a public method to convert OpenCensus View to MetricDescriptor. (#277)
This is useful if you want to use your code as source of truth of MetricDescritors. Managing MetricDescriptors outside of the Go code (e.g. in Terraform configs) is painful, as the changes to MD requires delete/recreate, after deleting all alert policies associated with the metric. On the other hand, the externally managed MDs are bound to be out of sync with what's present in the code. In the end we decide to just extract/collect views from our go code as source of truth, and thus we need to have a public method to convert views to MD.
1 parent cb914a5 commit 1996040

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

stackdriver.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767
"go.opencensus.io/trace"
6868
"golang.org/x/oauth2/google"
6969
"google.golang.org/api/option"
70+
metricpb "google.golang.org/genproto/googleapis/api/metric"
7071
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
7172

7273
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
@@ -469,6 +470,15 @@ func (e *Exporter) Flush() {
469470
e.traceExporter.Flush()
470471
}
471472

473+
// ViewToMetricDescriptor converts an OpenCensus view to a MetricDescriptor.
474+
//
475+
// This is useful for cases when you want to use your Go code as source of
476+
// truth of metric descriptors. You can extract or define views in a central
477+
// place, then call this method to generate MetricDescriptors.
478+
func (e *Exporter) ViewToMetricDescriptor(ctx context.Context, v *view.View) (*metricpb.MetricDescriptor, error) {
479+
return e.statsExporter.viewToMetricDescriptor(ctx, v)
480+
}
481+
472482
func (o Options) handleError(err error) {
473483
if o.OnError != nil {
474484
o.OnError(err)

0 commit comments

Comments
 (0)