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

Commit

Permalink
Extract stats/view package.
Browse files Browse the repository at this point in the history
Extract the exporting and view-related parts of the stats
package into a separate subpackage.

This reinforces the philosophy of separating stats collection
(which is in the stats package) and stats reporting, viewing,
and exporting (what you do with the collected stats).
  • Loading branch information
Ramon Nogueira committed Feb 11, 2018
1 parent 7f68fb2 commit 904befa
Show file tree
Hide file tree
Showing 54 changed files with 1,530 additions and 1,699 deletions.
40 changes: 14 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Create and load measures with units:

[embedmd]:# (stats.go measure)
```go
videoSize, err := stats.NewMeasureInt64("my.org/video_size", "processed video size", "MB")
videoSize, err := stats.NewInt64("my.org/video_size", "processed video size", "MB")
if err != nil {
log.Fatal(err)
}
Expand All @@ -129,18 +129,6 @@ if m == nil {
}
```

Delete measure (this can be useful when replacing a measure by
another measure with the same name):

[embedmd]:# (stats.go deleteMeasure)
```go
if err := stats.DeleteMeasure(m); err != nil {
log.Fatal(err)
}
```
However, it is an error to delete a Measure that's used by at least one View. The
View using the Measure has to be unregistered first.

### Creating an aggregation

Currently 4 types of aggregations are supported. The CountAggregation is used to count
Expand All @@ -151,10 +139,10 @@ sample values.

[embedmd]:# (stats.go aggs)
```go
distAgg := stats.DistributionAggregation([]float64{0, 1 << 32, 2 << 32, 3 << 32})
countAgg := stats.CountAggregation{}
sumAgg := stats.SumAggregation{}
meanAgg := stats.MeanAggregation{}
distAgg := view.DistributionAggregation([]float64{0, 1 << 32, 2 << 32, 3 << 32})
countAgg := view.CountAggregation{}
sumAgg := view.SumAggregation{}
meanAgg := view.MeanAggregation{}
```

### Create an aggregation window
Expand All @@ -163,7 +151,7 @@ Use Cumulative to continuously aggregate the recorded data.

[embedmd]:# (stats.go windows)
```go
cum := stats.Cumulative{}
cum := view.Cumulative{}
```

### Creating, registering and unregistering a view
Expand All @@ -172,7 +160,7 @@ Create and register a view:

[embedmd]:# (stats.go view)
```go
view, err := stats.NewView(
v, err := view.New(
"my.org/video_size_distribution",
"distribution of processed video size over time",
nil,
Expand All @@ -183,7 +171,7 @@ view, err := stats.NewView(
if err != nil {
log.Fatalf("cannot create view: %v", err)
}
if err := stats.RegisterView(view); err != nil {
if err := view.Register(v); err != nil {
log.Fatal(err)
}
```
Expand All @@ -192,7 +180,7 @@ Find view by name:

[embedmd]:# (stats.go findView)
```go
v := stats.FindView("my.org/video_size_distribution")
v = view.Find("my.org/video_size_distribution")
if v == nil {
log.Fatalln("view not found")
}
Expand All @@ -202,7 +190,7 @@ Unregister view:

[embedmd]:# (stats.go unregisterView)
```go
if err = stats.UnregisterView(v); err != nil {
if err = view.Unregister(v); err != nil {
log.Fatal(err)
}
```
Expand All @@ -214,7 +202,7 @@ a duration less than a certain minimum (maybe 1s) should have no effect.

[embedmd]:# (stats.go reportingPeriod)
```go
stats.SetReportingPeriod(5 * time.Second)
view.SetReportingPeriod(5 * time.Second)
```

### Recording measurements
Expand All @@ -234,7 +222,7 @@ Users need to subscribe to a view in order to retrieve collected data.

[embedmd]:# (stats.go subscribe)
```go
if err := view.Subscribe(); err != nil {
if err := v.Subscribe(); err != nil {
log.Fatal(err)
}
```
Expand All @@ -245,7 +233,7 @@ Subscribed views' data will be exported via the registered exporters.
```go
// Register an exporter to be able to retrieve
// the data from the subscribed views.
stats.RegisterExporter(&exporter{})
view.RegisterExporter(&exporter{})
```

An example logger exporter is below:
Expand All @@ -255,7 +243,7 @@ An example logger exporter is below:

type exporter struct{}

func (e *exporter) ExportView(vd *stats.ViewData) {
func (e *exporter) ExportView(vd *view.Data) {
log.Println(vd)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/grpc/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package exporter
import (
"log"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)

Expand All @@ -26,7 +26,7 @@ import (
type Exporter struct{}

// ExportView logs the view data.
func (e *Exporter) ExportView(vd *stats.ViewData) {
func (e *Exporter) ExportView(vd *view.Data) {
log.Println(vd)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/grpc/helloworld_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
pb "go.opencensus.io/examples/grpc/proto"
ocgrpc "go.opencensus.io/plugin/grpc"
"go.opencensus.io/plugin/grpc/grpcstats"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/zpages"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand All @@ -41,7 +41,7 @@ func main() {

// Register stats and trace exporters to export
// the collected data.
stats.RegisterExporter(&exporter.Exporter{})
view.RegisterExporter(&exporter.Exporter{})

// Subscribe to collect client request count.
if err := grpcstats.RPCClientRequestCountView.Subscribe(); err != nil {
Expand All @@ -62,7 +62,7 @@ func main() {
if len(os.Args) > 1 {
name = os.Args[1]
}
stats.SetReportingPeriod(time.Second)
view.SetReportingPeriod(time.Second)
for {
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions examples/grpc/helloworld_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
pb "go.opencensus.io/examples/grpc/proto"
ocgrpc "go.opencensus.io/plugin/grpc"
"go.opencensus.io/plugin/grpc/grpcstats"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/zpages"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand All @@ -47,7 +47,7 @@ func main() {
go func() { log.Fatal(http.ListenAndServe(":8081", nil)) }()
// Register stats and trace exporters to export
// the collected data.
stats.RegisterExporter(&exporter.Exporter{})
view.RegisterExporter(&exporter.Exporter{})

// Subscribe to collect server request count.
if err := grpcstats.RPCServerRequestCountView.Subscribe(); err != nil {
Expand Down
17 changes: 9 additions & 8 deletions examples/stats/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,43 @@ import (
"time"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)

func main() {
ctx := context.Background()

// Register an exporter to be able to retrieve
// the data from the subscribed views.
stats.RegisterExporter(&exporter{})
view.RegisterExporter(&exporter{})

// Create measures. The program will record measures for the size of
// processed videos and the nubmer of videos marked as spam.
videoSize, err := stats.NewMeasureInt64("my.org/measure/video_size", "size of processed videos", "MBy")
videoSize, err := stats.NewInt64("my.org/measure/video_size", "size of processed videos", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}

// Create view to see the processed video size
// distribution over 10 seconds.
view, err := stats.NewView(
v, err := view.New(
"my.org/views/video_size",
"processed video size over time",
nil,
videoSize,
stats.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
stats.Cumulative{},
view.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
view.Cumulative{},
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
}

// Set reporting period to report data at every second.
stats.SetReportingPeriod(1 * time.Second)
view.SetReportingPeriod(1 * time.Second)

// Subscribe will allow view data to be exported.
// Once no longer need, you can unsubscribe from the view.
if err := view.Subscribe(); err != nil {
if err := v.Subscribe(); err != nil {
log.Fatalf("Cannot subscribe to the view: %v", err)
}

Expand All @@ -73,6 +74,6 @@ func main() {

type exporter struct{}

func (e *exporter) ExportView(vd *stats.ViewData) {
func (e *exporter) ExportView(vd *view.Data) {
log.Println(vd)
}
21 changes: 11 additions & 10 deletions examples/stats/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)

func main() {
Expand All @@ -34,23 +35,23 @@ func main() {
if err != nil {
log.Fatal(err)
}
stats.RegisterExporter(exporter)
view.RegisterExporter(exporter)

// Create measures. The program will record measures for the size of
// processed videos and the number of videos marked as spam.
videoCount, err := stats.NewMeasureInt64("my.org/measures/video_count", "number of processed videos", "")
videoCount, err := stats.NewInt64("my.org/measures/video_count", "number of processed videos", "")
if err != nil {
log.Fatalf("Video count measure not created: %v", err)
}

// 1. Create view to see the number of processed videos cumulatively.
viewCount, err := stats.NewView(
viewCount, err := view.New(
"video_count",
"number of videos processed over time",
nil,
videoCount,
stats.CountAggregation{},
stats.Cumulative{},
view.CountAggregation{},
view.Cumulative{},
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
Expand All @@ -64,19 +65,19 @@ func main() {

// Create measures. The program will record measures for the size of
// processed videos and the number of videos marked as spam.
videoSize, err := stats.NewMeasureInt64("my.org/measures/video_size_cum", "size of processed video", "MBy")
videoSize, err := stats.NewInt64("my.org/measures/video_size_cum", "size of processed video", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}

// 2. Create view to see the amount of video processed
viewSize, err := stats.NewView(
viewSize, err := view.New(
"video_cum",
"processed video size over time",
nil,
videoSize,
stats.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
stats.Cumulative{},
view.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
view.Cumulative{},
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
Expand All @@ -89,7 +90,7 @@ func main() {
}

// Set reporting period to report data at every second.
stats.SetReportingPeriod(1 * time.Second)
view.SetReportingPeriod(1 * time.Second)

// Record some data points...
go func() {
Expand Down
15 changes: 8 additions & 7 deletions examples/stats/stackdriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
)

func main() {
Expand All @@ -46,34 +47,34 @@ func main() {
if err != nil {
log.Fatal(err)
}
stats.RegisterExporter(exporter)
view.RegisterExporter(exporter)

// Create measures. The program will record measures for the size of
// processed videos and the nubmer of videos marked as spam.
videoSize, err := stats.NewMeasureInt64("my.org/measure/video_size", "size of processed videos", "MBy")
videoSize, err := stats.NewInt64("my.org/measure/video_size", "size of processed videos", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}

// Create view to see the processed video size cumulatively.
view, err := stats.NewView(
v, err := view.New(
"my.org/views/video_size_cum",
"processed video size over time",
nil,
videoSize,
stats.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
stats.Cumulative{},
view.DistributionAggregation([]float64{0, 1 << 16, 1 << 32}),
view.Cumulative{},
)
if err != nil {
log.Fatalf("Cannot create view: %v", err)
}

// Set reporting period to report data at every second.
stats.SetReportingPeriod(1 * time.Second)
view.SetReportingPeriod(1 * time.Second)

// Subscribe will allow view data to be exported.
// Once no longer need, you can unsubscribe from the view.
if err := view.Subscribe(); err != nil {
if err := v.Subscribe(); err != nil {
log.Fatalf("Cannot subscribe to the view: %v", err)
}

Expand Down
Loading

0 comments on commit 904befa

Please sign in to comment.