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

Commit

Permalink
Remove error from stats.{Float,Int}63Measure signatures
Browse files Browse the repository at this point in the history
Fixes: #544
  • Loading branch information
Ramon Nogueira committed Mar 27, 2018
1 parent bae5a8b commit 7b734ca
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 169 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Here we create a view with the DistributionAggregation over our measure.

[embedmd]:# (stats.go view)
```go
if err = view.Subscribe(&view.View{
if err := view.Subscribe(&view.View{
Name: "my.org/video_size_distribution",
Description: "distribution of processed video size over time",
Measure: videoSize,
Expand Down
5 changes: 1 addition & 4 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
videoSize, err = stats.Int64("my.org/measure/video_size", "size of processed videos", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}
videoSize = stats.Int64("my.org/measure/video_size", "size of processed videos", stats.UnitBytes)

// Create view to see the processed video size
// distribution broken down by frontend.
Expand Down
18 changes: 7 additions & 11 deletions exporter/prometheus/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import (
"go.opencensus.io/stats/view"
)

// Create measures. The program will record measures for the size of
// processed videos and the number of videos marked as spam.
var (
videoCount = stats.Int64("my.org/measures/video_count", "number of processed videos", stats.UnitNone)
videoSize = stats.Int64("my.org/measures/video_size", "size of processed video", stats.UnitBytes)
)

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

Expand All @@ -37,17 +44,6 @@ func main() {
}
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.Int64("my.org/measures/video_count", "number of processed videos", "")
if err != nil {
log.Fatalf("Video count measure not created: %v", err)
}
videoSize, err := stats.Int64("my.org/measures/video_size", "size of processed video", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}

// Create view to see the number of processed videos cumulatively.
// Create view to see the amount of video processed
// Subscribe will allow view data to be exported.
Expand Down
49 changes: 19 additions & 30 deletions exporter/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -34,10 +33,7 @@ import (
)

func newView(measureName string, agg *view.Aggregation) *view.View {
m, err := stats.Int64(measureName, "bytes", stats.UnitBytes)
if err != nil {
log.Fatal(err)
}
m := stats.Int64(measureName, "bytes", stats.UnitBytes)
return &view.View{
Name: "foo",
Description: "bar",
Expand Down Expand Up @@ -175,28 +171,24 @@ func TestCollectNonRacy(t *testing.T) {
}()
}

type mCreator struct {
m *stats.Int64Measure
err error
}

type mSlice []*stats.Int64Measure

func (mc *mCreator) createAndAppend(measures *mSlice, name, desc, unit string) {
mc.m, mc.err = stats.Int64(name, desc, unit)
*measures = append(*measures, mc.m)
func (measures *mSlice) createAndAppend(name, desc, unit string) {
m := stats.Int64(name, desc, unit)
*measures = append(*measures, m)
}

type vCreator struct {
v *view.View
err error
}
type vCreator []*view.View

func (vc *vCreator) createAndSubscribe(name, description string, keys []tag.Key, measure stats.Measure, agg *view.Aggregation) {
vc.v, vc.err = view.New(name, description, keys, measure, agg)
if err := vc.v.Subscribe(); err != nil {
vc.err = err
func (vc *vCreator) createAndAppend(name, description string, keys []tag.Key, measure stats.Measure, agg *view.Aggregation) {
v := &view.View{
Name: name,
Description: description,
TagKeys: keys,
Measure: measure,
Aggregation: agg,
}
*vc = append(*vc, v)
}

func TestMetricsEndpointOutput(t *testing.T) {
Expand All @@ -208,20 +200,17 @@ func TestMetricsEndpointOutput(t *testing.T) {

names := []string{"foo", "bar", "baz"}

measures := make(mSlice, 0)
mc := &mCreator{}
var measures mSlice
for _, name := range names {
mc.createAndAppend(&measures, "tests/"+name, name, "")
}
if mc.err != nil {
t.Errorf("failed to create measures: %v", err)
measures.createAndAppend("tests/"+name, name, "")
}

vc := &vCreator{}
var vc vCreator
for _, m := range measures {
vc.createAndSubscribe(m.Name(), m.Description(), nil, m, view.Count())
vc.createAndAppend(m.Name(), m.Description(), nil, m, view.Count())
}
if vc.err != nil {

if err := view.Subscribe(vc...); err != nil {
t.Fatalf("failed to create views: %v", err)
}
view.SetReportingPeriod(time.Millisecond)
Expand Down
19 changes: 10 additions & 9 deletions exporter/stackdriver/examples/stats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"go.opencensus.io/stats/view"
)

// Create measures. The program will record measures for the size of
// processed videos and the nubmer of videos marked as spam.
var videoSize = stats.Int64("my.org/measure/video_size", "size of processed videos", stats.UnitBytes)

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

Expand All @@ -49,13 +53,6 @@ func main() {
}
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.Int64("my.org/measure/video_size", "size of processed videos", "MBy")
if err != nil {
log.Fatalf("Video size measure not created: %v", err)
}

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

Expand All @@ -71,11 +68,15 @@ func main() {
log.Fatalf("Cannot subscribe to the view: %v", err)
}

// Record data points.
stats.Record(ctx, videoSize.M(25648))
processVideo(ctx)

// Wait for a duration longer than reporting duration to ensure the stats
// library reports the collected data.
fmt.Println("Wait longer than the reporting duration...")
time.Sleep(1 * time.Minute)
}

func processVideo(ctx context.Context) {
// Do some processing and record stats.
stats.Record(ctx, videoSize.M(25648))
}
25 changes: 5 additions & 20 deletions exporter/stackdriver/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func TestNewExporterSingletonPerProcess(t *testing.T) {
}

func TestExporter_makeReq(t *testing.T) {
m, err := stats.Float64("test-measure", "measure desc", "unit")
if err != nil {
t.Fatal(err)
}
m := stats.Float64("test-measure", "measure desc", "unit")

key, err := tag.NewKey("test_key")
if err != nil {
Expand Down Expand Up @@ -275,10 +272,7 @@ func TestExporter_makeReq(t *testing.T) {
}

func TestExporter_makeReq_batching(t *testing.T) {
m, err := stats.Float64("test-measure/makeReq_batching", "measure desc", "unit")
if err != nil {
t.Fatal(err)
}
m := stats.Float64("test-measure/makeReq_batching", "measure desc", "unit")

key, err := tag.NewKey("test_key")
if err != nil {
Expand Down Expand Up @@ -446,10 +440,7 @@ func TestExporter_createMeasure(t *testing.T) {
}()

key, _ := tag.NewKey("test-key-one")
m, err := stats.Float64("test-measure/TestExporter_createMeasure", "measure desc", stats.UnitMilliseconds)
if err != nil {
t.Fatal(err)
}
m := stats.Float64("test-measure/TestExporter_createMeasure", "measure desc", stats.UnitMilliseconds)

v := &view.View{
Name: "test_view_sum",
Expand Down Expand Up @@ -525,10 +516,7 @@ func TestExporter_createMeasure_CountAggregation(t *testing.T) {
}()

key, _ := tag.NewKey("test-key-one")
m, err := stats.Float64("test-measure/TestExporter_createMeasure", "measure desc", stats.UnitMilliseconds)
if err != nil {
t.Fatal(err)
}
m := stats.Float64("test-measure/TestExporter_createMeasure", "measure desc", stats.UnitMilliseconds)

v := &view.View{
Name: "test_view_count",
Expand Down Expand Up @@ -585,10 +573,7 @@ func TestExporter_createMeasure_CountAggregation(t *testing.T) {
}

func TestExporter_makeReq_withCustomMonitoredResource(t *testing.T) {
m, err := stats.Float64("test-measure/TestExporter_makeReq_withCustomMonitoredResource", "measure desc", "unit")
if err != nil {
t.Fatal(err)
}
m := stats.Float64("test-measure/TestExporter_makeReq_withCustomMonitoredResource", "measure desc", "unit")

key, err := tag.NewKey("test_key")
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions internal/readme/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ import (
func statsExamples() {
ctx := context.Background()

videoSize, err := stats.Int64("my.org/video_size", "processed video size", "MB")
if err != nil {
log.Fatal(err)
}
videoSize := stats.Int64("my.org/video_size", "processed video size", "MB")

// START aggs
distAgg := view.Distribution(0, 1<<32, 2<<32, 3<<32)
Expand All @@ -43,7 +40,7 @@ func statsExamples() {
_, _, _ = distAgg, countAgg, sumAgg

// START view
if err = view.Subscribe(&view.View{
if err := view.Subscribe(&view.View{
Name: "my.org/video_size_distribution",
Description: "distribution of processed video size over time",
Measure: videoSize,
Expand Down
16 changes: 8 additions & 8 deletions plugin/ocgrpc/client_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (

// The following variables are measures are recorded by ClientHandler:
var (
ClientErrorCount, _ = stats.Int64("grpc.io/client/error_count", "RPC Errors", stats.UnitNone)
ClientRequestBytes, _ = stats.Int64("grpc.io/client/request_bytes", "Request bytes", stats.UnitBytes)
ClientResponseBytes, _ = stats.Int64("grpc.io/client/response_bytes", "Response bytes", stats.UnitBytes)
ClientStartedCount, _ = stats.Int64("grpc.io/client/started_count", "Number of client RPCs (streams) started", stats.UnitNone)
ClientFinishedCount, _ = stats.Int64("grpc.io/client/finished_count", "Number of client RPCs (streams) finished", stats.UnitNone)
ClientRequestCount, _ = stats.Int64("grpc.io/client/request_count", "Number of client RPC request messages", stats.UnitNone)
ClientResponseCount, _ = stats.Int64("grpc.io/client/response_count", "Number of client RPC response messages", stats.UnitNone)
ClientRoundTripLatency, _ = stats.Float64("grpc.io/client/roundtrip_latency", "RPC roundtrip latency in msecs", stats.UnitMilliseconds)
ClientErrorCount = stats.Int64("grpc.io/client/error_count", "RPC Errors", stats.UnitNone)
ClientRequestBytes = stats.Int64("grpc.io/client/request_bytes", "Request bytes", stats.UnitBytes)
ClientResponseBytes = stats.Int64("grpc.io/client/response_bytes", "Response bytes", stats.UnitBytes)
ClientStartedCount = stats.Int64("grpc.io/client/started_count", "Number of client RPCs (streams) started", stats.UnitNone)
ClientFinishedCount = stats.Int64("grpc.io/client/finished_count", "Number of client RPCs (streams) finished", stats.UnitNone)
ClientRequestCount = stats.Int64("grpc.io/client/request_count", "Number of client RPC request messages", stats.UnitNone)
ClientResponseCount = stats.Int64("grpc.io/client/response_count", "Number of client RPC response messages", stats.UnitNone)
ClientRoundTripLatency = stats.Float64("grpc.io/client/roundtrip_latency", "RPC roundtrip latency in msecs", stats.UnitMilliseconds)
)

// Predefined views may be subscribed to collect data for the above measures.
Expand Down
16 changes: 8 additions & 8 deletions plugin/ocgrpc/server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (

// The following variables are measures are recorded by ServerHandler:
var (
ServerErrorCount, _ = stats.Int64("grpc.io/server/error_count", "RPC Errors", stats.UnitNone)
ServerServerElapsedTime, _ = stats.Float64("grpc.io/server/server_elapsed_time", "Server elapsed time in msecs", stats.UnitMilliseconds)
ServerRequestBytes, _ = stats.Int64("grpc.io/server/request_bytes", "Request bytes", stats.UnitBytes)
ServerResponseBytes, _ = stats.Int64("grpc.io/server/response_bytes", "Response bytes", stats.UnitBytes)
ServerStartedCount, _ = stats.Int64("grpc.io/server/started_count", "Number of server RPCs (streams) started", stats.UnitNone)
ServerFinishedCount, _ = stats.Int64("grpc.io/server/finished_count", "Number of server RPCs (streams) finished", stats.UnitNone)
ServerRequestCount, _ = stats.Int64("grpc.io/server/request_count", "Number of server RPC request messages", stats.UnitNone)
ServerResponseCount, _ = stats.Int64("grpc.io/server/response_count", "Number of server RPC response messages", stats.UnitNone)
ServerErrorCount = stats.Int64("grpc.io/server/error_count", "RPC Errors", stats.UnitNone)
ServerServerElapsedTime = stats.Float64("grpc.io/server/server_elapsed_time", "Server elapsed time in msecs", stats.UnitMilliseconds)
ServerRequestBytes = stats.Int64("grpc.io/server/request_bytes", "Request bytes", stats.UnitBytes)
ServerResponseBytes = stats.Int64("grpc.io/server/response_bytes", "Response bytes", stats.UnitBytes)
ServerStartedCount = stats.Int64("grpc.io/server/started_count", "Number of server RPCs (streams) started", stats.UnitNone)
ServerFinishedCount = stats.Int64("grpc.io/server/finished_count", "Number of server RPCs (streams) finished", stats.UnitNone)
ServerRequestCount = stats.Int64("grpc.io/server/request_count", "Number of server RPC request messages", stats.UnitNone)
ServerResponseCount = stats.Int64("grpc.io/server/response_count", "Number of server RPC response messages", stats.UnitNone)
)

// TODO(acetechnologist): This is temporary and will need to be replaced by a
Expand Down
16 changes: 8 additions & 8 deletions plugin/ochttp/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (

// The following client HTTP measures are supported for use in custom views.
var (
ClientRequestCount, _ = stats.Int64("opencensus.io/http/client/request_count", "Number of HTTP requests started", stats.UnitNone)
ClientRequestBytes, _ = stats.Int64("opencensus.io/http/client/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
ClientResponseBytes, _ = stats.Int64("opencensus.io/http/client/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
ClientLatency, _ = stats.Float64("opencensus.io/http/client/latency", "End-to-end latency", stats.UnitMilliseconds)
ClientRequestCount = stats.Int64("opencensus.io/http/client/request_count", "Number of HTTP requests started", stats.UnitNone)
ClientRequestBytes = stats.Int64("opencensus.io/http/client/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
ClientResponseBytes = stats.Int64("opencensus.io/http/client/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
ClientLatency = stats.Float64("opencensus.io/http/client/latency", "End-to-end latency", stats.UnitMilliseconds)
)

// The following server HTTP measures are supported for use in custom views:
var (
ServerRequestCount, _ = stats.Int64("opencensus.io/http/server/request_count", "Number of HTTP requests started", stats.UnitNone)
ServerRequestBytes, _ = stats.Int64("opencensus.io/http/server/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
ServerResponseBytes, _ = stats.Int64("opencensus.io/http/server/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
ServerLatency, _ = stats.Float64("opencensus.io/http/server/latency", "End-to-end latency", stats.UnitMilliseconds)
ServerRequestCount = stats.Int64("opencensus.io/http/server/request_count", "Number of HTTP requests started", stats.UnitNone)
ServerRequestBytes = stats.Int64("opencensus.io/http/server/request_bytes", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
ServerResponseBytes = stats.Int64("opencensus.io/http/server/response_bytes", "HTTP response body size (uncompressed)", stats.UnitBytes)
ServerLatency = stats.Float64("opencensus.io/http/server/latency", "End-to-end latency", stats.UnitMilliseconds)
)

// The following tags are applied to stats recorded by this package. Host, Path
Expand Down
7 changes: 1 addition & 6 deletions stats/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package stats_test

import (
"context"
"log"
"testing"

"go.opencensus.io/stats"
Expand Down Expand Up @@ -93,9 +92,5 @@ func BenchmarkRecord8_8Tags(b *testing.B) {
}

func makeMeasure() *stats.Int64Measure {
m, err := stats.Int64("m", "test measure", "")
if err != nil {
log.Fatal(err)
}
return m
return stats.Int64("m", "test measure", "")
}
10 changes: 5 additions & 5 deletions stats/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ package stats_test

import (
"context"
"log"

"go.opencensus.io/stats"
)

func ExampleRecord() {
ctx := context.Background()
openConns, err := stats.Int64("my.org/measure/openconns", "open connections", stats.UnitNone)
if err != nil {
log.Fatal(err)
}

// Measures are usually declared as package-private global variables.
openConns := stats.Int64("my.org/measure/openconns", "open connections", stats.UnitNone)

// Instrumented packages call stats.Record() to record measuremens.
stats.Record(ctx, openConns.M(124)) // Record 124 open connections.
}
4 changes: 2 additions & 2 deletions stats/measure_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m *Float64Measure) M(v float64) Measurement {

// Float64 creates a new measure of type Float64Measure.
// It never returns an error.
func Float64(name, description, unit string) (*Float64Measure, error) {
func Float64(name, description, unit string) *Float64Measure {
mi := registerMeasureHandle(name, description, unit)
return &Float64Measure{mi}, nil
return &Float64Measure{mi}
}
4 changes: 2 additions & 2 deletions stats/measure_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m *Int64Measure) M(v int64) Measurement {

// Int64 creates a new measure of type Int64Measure.
// It never returns an error.
func Int64(name, description, unit string) (*Int64Measure, error) {
func Int64(name, description, unit string) *Int64Measure {
mi := registerMeasureHandle(name, description, unit)
return &Int64Measure{mi}, nil
return &Int64Measure{mi}
}
Loading

0 comments on commit 7b734ca

Please sign in to comment.