Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Last active February 13, 2018 15:16
Show Gist options
  • Save DazWilkin/9947cf01dbe2df53cf373e3858c83fe5 to your computer and use it in GitHub Desktop.
Save DazWilkin/9947cf01dbe2df53cf373e3858c83fe5 to your computer and use it in GitHub Desktop.
Return to OpenCensus
package main
import (
"context"
"fmt"
"log"
"math/rand"
"time"
"go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)
func main() {
ctx := context.Background()
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: "[[YOUR-PROJECT]]"})
if err != nil {
log.Fatal(err)
}
view.RegisterExporter(exporter)
videoCount, err := stats.Int64("my.org/measures/video_count", "number of processed videos", "")
if err != nil {
log.Fatalf("Video count measure not created: %v", err)
}
viewCount, err := view.New(
"video_count",
"number of videos processed over time",
nil,
videoCount,
view.CountAggregation{},
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
}
if err := viewCount.Subscribe(); err != nil {
log.Fatalf("Cannot subscribe to view: %v", err)
}
videoSize, err := stats.Int64("my.org/measures/video_size_cum", "size of processed video", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}
viewSize, err := view.New(
"video_cum",
"processed video size over time",
nil,
videoSize,
view.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
}
if err := viewSize.Subscribe(); err != nil {
log.Fatalf("Cannot subscribe to the view: %v", err)
}
view.SetReportingPeriod(1 * time.Second)
go func() {
for {
stats.Record(ctx, videoCount.M(1))
stats.Record(ctx, videoSize.M(rand.Int63()))
<-time.After(time.Millisecond * time.Duration(1+rand.Intn(400)))
}
}()
fmt.Println("Run for 15 minutes")
time.Sleep(15 * time.Minute)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment