diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 5428d6e2a..ce647f6ad 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -54,10 +54,7 @@ func main() { trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) - frontendKey, err = tag.NewKey("example.com/keys/frontend") - if err != nil { - log.Fatal(err) - } + frontendKey = tag.MustNewKey("example.com/keys/frontend") videoSize = stats.Int64("example.com/measure/video_size", "size of processed videos", stats.UnitBytes) view.SetReportingPeriod(2 * time.Second) diff --git a/examples/quickstart/stats.go b/examples/quickstart/stats.go index 7e9735188..04937523d 100644 --- a/examples/quickstart/stats.go +++ b/examples/quickstart/stats.go @@ -56,7 +56,7 @@ var ( // TagKeys for the stats quickstart. var ( - keyMethod, _ = tag.NewKey("method") + keyMethod = tag.MustNewKey("method") ) // Views for the stats quickstart. diff --git a/internal/readme/tags.go b/internal/readme/tags.go index 09d9ac12f..1219dd965 100644 --- a/internal/readme/tags.go +++ b/internal/readme/tags.go @@ -24,17 +24,11 @@ import ( func tagsExamples() { ctx := context.Background() - osKey, err := tag.NewKey("example.com/keys/user-os") - if err != nil { - log.Fatal(err) - } - userIDKey, err := tag.NewKey("example.com/keys/user-id") - if err != nil { - log.Fatal(err) - } + osKey := tag.MustNewKey("example.com/keys/user-os") + userIDKey := tag.MustNewKey("example.com/keys/user-id") // START new - ctx, err = tag.New(ctx, + ctx, err := tag.New(ctx, tag.Insert(osKey, "macOS-10.12.5"), tag.Upsert(userIDKey, "cde36753ed"), ) diff --git a/plugin/ocgrpc/client_stats_handler_test.go b/plugin/ocgrpc/client_stats_handler_test.go index 5bf7ef4b0..7a2c366c0 100644 --- a/plugin/ocgrpc/client_stats_handler_test.go +++ b/plugin/ocgrpc/client_stats_handler_test.go @@ -36,8 +36,8 @@ import ( ) func TestClientDefaultCollections(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") type tagPair struct { k tag.Key @@ -340,7 +340,7 @@ func TestClientDefaultCollections(t *testing.T) { } func TestClientRecordExemplar(t *testing.T) { - key, _ := tag.NewKey("test_key") + key := tag.MustNewKey("test_key") tagInfo := &stats.RPCTagInfo{FullMethodName: "/package.service/method"} out := &stats.OutPayload{Length: 2000} end := &stats.End{Error: nil} diff --git a/plugin/ocgrpc/end_to_end_test.go b/plugin/ocgrpc/end_to_end_test.go index a305bbcff..8715079d7 100644 --- a/plugin/ocgrpc/end_to_end_test.go +++ b/plugin/ocgrpc/end_to_end_test.go @@ -27,7 +27,7 @@ import ( "go.opencensus.io/tag" ) -var keyAccountId, _ = tag.NewKey("account_id") +var keyAccountId = tag.MustNewKey("account_id") func TestEndToEnd_Single(t *testing.T) { view.Register(ocgrpc.DefaultClientViews...) diff --git a/plugin/ocgrpc/server_stats_handler_test.go b/plugin/ocgrpc/server_stats_handler_test.go index bb3ca9a5b..4b4cad69f 100644 --- a/plugin/ocgrpc/server_stats_handler_test.go +++ b/plugin/ocgrpc/server_stats_handler_test.go @@ -32,8 +32,8 @@ import ( ) func TestServerDefaultCollections(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") type tagPair struct { k tag.Key @@ -338,7 +338,7 @@ func newDistributionData(countPerBucket []int64, count int64, min, max, mean, su } func TestServerRecordExemplar(t *testing.T) { - key, _ := tag.NewKey("test_key") + key := tag.MustNewKey("test_key") tagInfo := &stats.RPCTagInfo{FullMethodName: "/package.service/method"} out := &stats.OutPayload{Length: 2000} end := &stats.End{Error: nil} diff --git a/plugin/ocgrpc/stats_common.go b/plugin/ocgrpc/stats_common.go index 0ae569182..89cac9c4e 100644 --- a/plugin/ocgrpc/stats_common.go +++ b/plugin/ocgrpc/stats_common.go @@ -61,14 +61,14 @@ var ( // Server tags are applied to the context used to process each RPC, as well as // the measures at the end of each RPC. var ( - KeyServerMethod, _ = tag.NewKey("grpc_server_method") - KeyServerStatus, _ = tag.NewKey("grpc_server_status") + KeyServerMethod = tag.MustNewKey("grpc_server_method") + KeyServerStatus = tag.MustNewKey("grpc_server_status") ) // Client tags are applied to measures at the end of each RPC. var ( - KeyClientMethod, _ = tag.NewKey("grpc_client_method") - KeyClientStatus, _ = tag.NewKey("grpc_client_status") + KeyClientMethod = tag.MustNewKey("grpc_client_method") + KeyClientStatus = tag.MustNewKey("grpc_client_status") ) var ( diff --git a/plugin/ochttp/stats.go b/plugin/ochttp/stats.go index 63bbcda5e..ee3729040 100644 --- a/plugin/ochttp/stats.go +++ b/plugin/ochttp/stats.go @@ -92,38 +92,38 @@ var ( // The value of this tag can be controlled by the HTTP client, so you need // to watch out for potentially generating high-cardinality labels in your // metrics backend if you use this tag in views. - Host, _ = tag.NewKey("http.host") + Host = tag.MustNewKey("http.host") // StatusCode is the numeric HTTP response status code, // or "error" if a transport error occurred and no status code was read. - StatusCode, _ = tag.NewKey("http.status") + StatusCode = tag.MustNewKey("http.status") // Path is the URL path (not including query string) in the request. // // The value of this tag can be controlled by the HTTP client, so you need // to watch out for potentially generating high-cardinality labels in your // metrics backend if you use this tag in views. - Path, _ = tag.NewKey("http.path") + Path = tag.MustNewKey("http.path") // Method is the HTTP method of the request, capitalized (GET, POST, etc.). - Method, _ = tag.NewKey("http.method") + Method = tag.MustNewKey("http.method") // KeyServerRoute is a low cardinality string representing the logical // handler of the request. This is usually the pattern registered on the a // ServeMux (or similar string). - KeyServerRoute, _ = tag.NewKey("http_server_route") + KeyServerRoute = tag.MustNewKey("http_server_route") ) // Client tag keys. var ( // KeyClientMethod is the HTTP method, capitalized (i.e. GET, POST, PUT, DELETE, etc.). - KeyClientMethod, _ = tag.NewKey("http_client_method") + KeyClientMethod = tag.MustNewKey("http_client_method") // KeyClientPath is the URL path (not including query string). - KeyClientPath, _ = tag.NewKey("http_client_path") + KeyClientPath = tag.MustNewKey("http_client_path") // KeyClientStatus is the HTTP status code as an integer (e.g. 200, 404, 500.), or "error" if no response status line was received. - KeyClientStatus, _ = tag.NewKey("http_client_status") + KeyClientStatus = tag.MustNewKey("http_client_status") // KeyClientHost is the value of the request Host header. - KeyClientHost, _ = tag.NewKey("http_client_host") + KeyClientHost = tag.MustNewKey("http_client_host") ) // Default distributions used by views in this package. diff --git a/stats/benchmark_test.go b/stats/benchmark_test.go index 3e0264fa5..1c467ec54 100644 --- a/stats/benchmark_test.go +++ b/stats/benchmark_test.go @@ -65,14 +65,14 @@ func BenchmarkRecord8_Parallel(b *testing.B) { func BenchmarkRecord8_8Tags(b *testing.B) { ctx := context.Background() - key1, _ := tag.NewKey("key1") - key2, _ := tag.NewKey("key2") - key3, _ := tag.NewKey("key3") - key4, _ := tag.NewKey("key4") - key5, _ := tag.NewKey("key5") - key6, _ := tag.NewKey("key6") - key7, _ := tag.NewKey("key7") - key8, _ := tag.NewKey("key8") + key1 := tag.MustNewKey("key1") + key2 := tag.MustNewKey("key2") + key3 := tag.MustNewKey("key3") + key4 := tag.MustNewKey("key4") + key5 := tag.MustNewKey("key5") + key6 := tag.MustNewKey("key6") + key7 := tag.MustNewKey("key7") + key8 := tag.MustNewKey("key8") tag.New(ctx, tag.Insert(key1, "value"), diff --git a/stats/record_test.go b/stats/record_test.go index ca46ed540..93a652200 100644 --- a/stats/record_test.go +++ b/stats/record_test.go @@ -42,8 +42,8 @@ var ( ) func TestRecordWithAttachments(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") distribution := view.Distribution(5, 10) m := stats.Int64("TestRecordWithAttachments/m1", "", stats.UnitDimensionless) v := &view.View{ diff --git a/stats/view/benchmark_test.go b/stats/view/benchmark_test.go index 0f195d43b..17ae36c76 100644 --- a/stats/view/benchmark_test.go +++ b/stats/view/benchmark_test.go @@ -26,16 +26,16 @@ import ( ) var ( - m = stats.Float64("m", "", "") - k1, _ = tag.NewKey("k1") - k2, _ = tag.NewKey("k2") - k3, _ = tag.NewKey("k3") - k4, _ = tag.NewKey("k4") - k5, _ = tag.NewKey("k5") - k6, _ = tag.NewKey("k6") - k7, _ = tag.NewKey("k7") - k8, _ = tag.NewKey("k8") - view = &View{ + m = stats.Float64("m", "", "") + k1 = tag.MustNewKey("k1") + k2 = tag.MustNewKey("k2") + k3 = tag.MustNewKey("k3") + k4 = tag.MustNewKey("k4") + k5 = tag.MustNewKey("k5") + k6 = tag.MustNewKey("k6") + k7 = tag.MustNewKey("k7") + k8 = tag.MustNewKey("k8") + view = &View{ Measure: m, Aggregation: Distribution(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TagKeys: []tag.Key{k1, k2}, diff --git a/stats/view/collector_test.go b/stats/view/collector_test.go index 57720c10a..2905a180d 100644 --- a/stats/view/collector_test.go +++ b/stats/view/collector_test.go @@ -29,18 +29,9 @@ func TestEncodeDecodeTags(t *testing.T) { want map[tag.Key][]byte } - k1, err := tag.NewKey("/encodedecodetest/k1") - if err != nil { - t.Fatal(err) - } - k2, err := tag.NewKey("/encodedecodetest/k2") - if err != nil { - t.Fatal(err) - } - k3, err := tag.NewKey("/encodedecodetest/k3") - if err != nil { - t.Fatal(err) - } + k1 = tag.MustNewKey("/encodedecodetest/k1") + k2 = tag.MustNewKey("/encodedecodetest/k2") + k3 = tag.MustNewKey("/encodedecodetest/k3") ctx1, _ := tag.New(ctx) ctx2, _ := tag.New(ctx, tag.Insert(k2, "v2")) diff --git a/stats/view/view_test.go b/stats/view/view_test.go index 7d2bed9c4..2a2bde494 100644 --- a/stats/view/view_test.go +++ b/stats/view/view_test.go @@ -29,9 +29,9 @@ import ( ) func Test_View_MeasureFloat64_AggregationDistribution(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") - k3, _ := tag.NewKey("k3") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") + k3 := tag.MustNewKey("k3") agg1 := Distribution(2) m := stats.Int64("Test_View_MeasureFloat64_AggregationDistribution/m1", "", stats.UnitDimensionless) view1 := &View{ @@ -199,9 +199,9 @@ func Test_View_MeasureFloat64_AggregationDistribution(t *testing.T) { } func Test_View_MeasureFloat64_AggregationSum(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") - k3, _ := tag.NewKey("k3") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") + k3 := tag.MustNewKey("k3") m := stats.Int64("Test_View_MeasureFloat64_AggregationSum/m1", "", stats.UnitDimensionless) view, err := newViewInternal(&View{TagKeys: []tag.Key{k1, k2}, Measure: m, Aggregation: Sum()}) if err != nil { @@ -315,8 +315,8 @@ func Test_View_MeasureFloat64_AggregationSum(t *testing.T) { } func TestCanonicalize(t *testing.T) { - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") m := stats.Int64("TestCanonicalize/m1", "desc desc", stats.UnitDimensionless) v := &View{TagKeys: []tag.Key{k2, k1}, Measure: m, Aggregation: Sum()} err := v.canonicalize() @@ -338,9 +338,9 @@ func TestCanonicalize(t *testing.T) { } func TestViewSortedKeys(t *testing.T) { - k1, _ := tag.NewKey("a") - k2, _ := tag.NewKey("b") - k3, _ := tag.NewKey("c") + k1 := tag.MustNewKey("a") + k2 := tag.MustNewKey("b") + k3 := tag.MustNewKey("c") ks := []tag.Key{k1, k3, k2} m := stats.Int64("TestViewSortedKeys/m1", "", stats.UnitDimensionless) diff --git a/stats/view/view_to_metric_test.go b/stats/view/view_to_metric_test.go index 82ba96943..da4135404 100644 --- a/stats/view/view_to_metric_test.go +++ b/stats/view/view_to_metric_test.go @@ -111,9 +111,9 @@ func init() { } func initTags() { - tk1, _ = tag.NewKey("k1") - tk2, _ = tag.NewKey("k2") - tk3, _ = tag.NewKey("k3") + tk1 = tag.MustNewKey("k1") + tk2 = tag.MustNewKey("k2") + tk3 = tag.MustNewKey("k3") tk1v1 = tag.Tag{Key: tk1, Value: v1} tk2v2 = tag.Tag{Key: tk2, Value: v2} diff --git a/stats/view/worker_test.go b/stats/view/worker_test.go index 8d4546ea4..6c15d37ef 100644 --- a/stats/view/worker_test.go +++ b/stats/view/worker_test.go @@ -124,8 +124,8 @@ func Test_Worker_RecordFloat64(t *testing.T) { someError := errors.New("some error") m := stats.Float64("Test_Worker_RecordFloat64/MF1", "desc MF1", "unit") - k1, _ := tag.NewKey("k1") - k2, _ := tag.NewKey("k2") + k1 := tag.MustNewKey("k1") + k2 := tag.MustNewKey("k2") ctx, err := tag.New(context.Background(), tag.Insert(k1, "v1"), tag.Insert(k2, "v2"), diff --git a/tag/example_test.go b/tag/example_test.go index fe0c5d9e9..9e3a59724 100644 --- a/tag/example_test.go +++ b/tag/example_test.go @@ -37,15 +37,14 @@ func ExampleNewKey() { _ = key // use key } +func ExampleMustNewKey() { + key := tag.MustNewKey("example.com/keys/user-os") + _ = key // use key +} + func ExampleNew() { - osKey, err := tag.NewKey("example.com/keys/user-os") - if err != nil { - log.Fatal(err) - } - userIDKey, err := tag.NewKey("example.com/keys/user-id") - if err != nil { - log.Fatal(err) - } + osKey := tag.MustNewKey("example.com/keys/user-os") + userIDKey := tag.MustNewKey("example.com/keys/user-id") ctx, err := tag.New(ctx, tag.Insert(osKey, "macOS-10.12.5"), diff --git a/tag/key.go b/tag/key.go index 4e63d08c9..71ec91365 100644 --- a/tag/key.go +++ b/tag/key.go @@ -21,7 +21,7 @@ type Key struct { } // NewKey creates or retrieves a string key identified by name. -// Calling NewKey consequently with the same name returns the same key. +// Calling NewKey more than once with the same name returns the same key. func NewKey(name string) (Key, error) { if !checkKeyName(name) { return Key{}, errInvalidKeyName @@ -29,8 +29,7 @@ func NewKey(name string) (Key, error) { return Key{name: name}, nil } -// MustNewKey creates or retrieves a string key identified by name. -// An invalid key name raises a panic. +// MustNewKey returns a key with the given name, and panics if name is an invalid key name. func MustNewKey(name string) Key { k, err := NewKey(name) if err != nil {