opencensus-go-exporter-stackdriver
opencensus-go-exporter-stackdriver copied to clipboard
stats Flush not working
According to the documentation, Flush
waits for exported view data to be uploaded. This is useful if your program is ending and you do not want to lose recent spans.
This is supposed to be for the stats but the comment mentions spans. If this is supposed to work for stats, it is not working.
Here is my test case:
package main
import (
"context"
"fmt"
"log"
"time"
"go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var MerrorCount = stats.Int64("razvan.test/measures/error_count", "number of errors encounterd", "1")
var (
ErrorCountView = &view.View{
Name: "demo/razvan/rep_period_test",
Measure: MerrorCount,
Description: "Testing various reporting periods",
Aggregation: view.Count(),
}
)
func main() {
KeyMethod, _ := tag.NewKey("test")
ctx := context.Background()
ctx, _ = tag.New(ctx, tag.Insert(KeyMethod, "30 sec reporting"))
// Register the views
if err := view.Register(ErrorCountView); err != nil {
log.Fatalf("Failed to register views: %v", err)
}
// SD Exporter
sd, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: "opencensus-test",
// MetricPrefix helps uniquely identify your metrics.
MetricPrefix: "opencensus-test",
})
if err != nil {
log.Fatalf("Failed to create the Stackdriver exporter: %v", err)
}
// It is imperative to invoke flush before your main function exits
defer sd.Flush()
view.RegisterExporter(sd)
// Set reporting period to report data at every second.
view.SetReportingPeriod(60000 * time.Millisecond)
ticker := time.NewTicker(1000 * time.Millisecond)
i := 0
go func() {
for {
select {
case <-ticker.C:
stats.Record(ctx, MerrorCount.M(1))
i++
}
}
}()
runtime := 65000
time.Sleep(time.Duration(runtime) * time.Millisecond)
fmt.Printf("Incremented %d times\n", i)
}
I only get the value exported after 60 seconds.
Related to https://github.com/census-instrumentation/opencensus-go/issues/862 as you mention. We should provide a way to flush the in-memory buffer.
/cc @ramonza @songy23