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

stats Flush not working #43

Open
razvanh opened this issue Sep 20, 2018 · 1 comment
Open

stats Flush not working #43

razvanh opened this issue Sep 20, 2018 · 1 comment

Comments

@razvanh
Copy link

razvanh commented Sep 20, 2018

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants