Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Created January 22, 2018 01:35
Show Gist options
  • Save DazWilkin/873f7e676a3f341895be59f8147b2b9c to your computer and use it in GitHub Desktop.
Save DazWilkin/873f7e676a3f341895be59f8147b2b9c to your computer and use it in GitHub Desktop.
OpenCensus Golang Prometheus sample
package main
import (
"context"
"fmt"
"log"
"math/rand"
"time"
"go.opencensus.io/exporter/stats/stackdriver"
"go.opencensus.io/stats"
)
func main() {
ctx := context.Background()
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: "[[YOUR-PROJECT]]",
})
if err != nil {
log.Fatal(err)
}
stats.RegisterExporter(exporter)
videoSize, err := stats.NewMeasureInt64("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 := stats.NewView(
"video_cum",
"processed video size over time",
nil,
videoSize,
stats.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
stats.Cumulative{},
)
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)
}
stats.SetReportingPeriod(1 * time.Second)
go func() {
for {
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