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

Commit

Permalink
Add a public method to convert OpenCensus View to MetricDescriptor.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yegle committed Nov 10, 2020
1 parent 0fc2674 commit 9254941
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"go.opencensus.io/trace"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
metricpb "google.golang.org/genproto/googleapis/api/metric"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"

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

// ViewToMetricDescriptor converts an OpenCensus view to a MetricDescriptor.
//
// This is useful for cases when you want to use your Go code as source of
// truth of metric descriptors. You can extract or define views in a central
// place, then call this method to generate MetricDescriptors.
func (e *Exporter) ViewToMetricDescriptor(ctx context.Context, v *view.View) (*metricpb.MetricDescriptor, error) {
return e.statsExporter.viewToMetricDescriptor(ctx, v)
}

func (o Options) handleError(err error) {
if o.OnError != nil {
o.OnError(err)
Expand Down

0 comments on commit 9254941

Please sign in to comment.