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

Add an OnExport option. #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (se *statsExporter) ExportMetrics(ctx context.Context, metrics []*metricdat

func (se *statsExporter) handleMetricsUpload(metrics []*metricdata.Metric) {
err := se.uploadMetrics(metrics)
se.o.handleExportResult(err)
if err != nil {
se.o.handleError(err)
}
Expand Down
13 changes: 13 additions & 0 deletions stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ type Options struct {
// Optional.
OnError func(err error)

// OnExport is the hook to be called when an export happened. If the export
// is a success, the err will be nil.
// By default this is set to nil.
// Note OnError is used in places when no export happens, whereas OnExport
// is only called when export happens.
OnExport func(err error)

// MonitoringClientOptions are additional options to be passed
// to the underlying Stackdriver Monitoring API client.
// Optional.
Expand Down Expand Up @@ -469,6 +476,12 @@ func (e *Exporter) Flush() {
e.traceExporter.Flush()
}

func (o Options) handleExportResult(err error) {
if o.OnExport != nil {
o.OnExport(err)
}
}

func (o Options) handleError(err error) {
if o.OnError != nil {
o.OnError(err)
Expand Down
9 changes: 8 additions & 1 deletion stackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ func TestGRPC(t *testing.T) {
}

func TestUserAgent(t *testing.T) {
e, err := NewExporter(Options{UserAgent: "OpenCensus Service"})
projectID, ok := os.LookupEnv("STACKDRIVER_TEST_PROJECT_ID")
if !ok {
t.Skip("STACKDRIVER_TEST_PROJECT_ID not set")
}
e, err := NewExporter(Options{
UserAgent: "OpenCensus Service",
ProjectID: projectID,
})
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (e *statsExporter) ExportView(vd *view.Data) {
return
}
err := e.viewDataBundler.Add(vd, 1)
e.o.handleExportResult(err)
switch err {
case nil:
return
Expand All @@ -190,7 +191,9 @@ func getTaskValue() string {
// handleUpload handles uploading a slice
// of Data, as well as error handling.
func (e *statsExporter) handleUpload(vds ...*view.Data) {
if err := e.uploadStats(vds); err != nil {
err := e.uploadStats(vds)
e.o.handleExportResult(err)
if err != nil {
e.o.handleError(err)
}
}
Expand Down
2 changes: 2 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (e *traceExporter) ExportSpan(s *trace.SpanData) {
protoSpan := protoFromSpanData(s, e.projectID, e.o.Resource, e.o.UserAgent)
protoSize := proto.Size(protoSpan)
err := e.bundler.Add(protoSpan, protoSize)
e.o.handleExportResult(err)
switch err {
case nil:
return
Expand Down Expand Up @@ -174,6 +175,7 @@ func (e *traceExporter) uploadSpans(spans []*tracepb.Span) {
span.AddAttributes(trace.Int64Attribute("num_spans", int64(len(spans))))

err := e.client.BatchWriteSpans(ctx, &req)
e.o.handleExportResult(err)
if err != nil {
span.SetStatus(trace.Status{Code: 2, Message: err.Error()})
e.o.handleError(err)
Expand Down