Replace tag.NewKey calls with tag.MustNewKey where appropriate (#1146)
diff --git a/README.md b/README.md
index fabab2e..a8cd09e 100644
--- a/README.md
+++ b/README.md
@@ -78,7 +78,7 @@
[embedmd]:# (internal/readme/tags.go new)
```go
-ctx, err = tag.New(ctx,
+ctx, err := tag.New(ctx,
tag.Insert(osKey, "macOS-10.12.5"),
tag.Upsert(userIDKey, "cde36753ed"),
)
diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go
index 5428d6e..ce647f6 100644
--- a/examples/helloworld/main.go
+++ b/examples/helloworld/main.go
@@ -54,10 +54,7 @@
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 7e97351..0493752 100644
--- a/examples/quickstart/stats.go
+++ b/examples/quickstart/stats.go
@@ -56,7 +56,7 @@
// TagKeys for the stats quickstart.
var (
- keyMethod, _ = tag.NewKey("method")
+ keyMethod = tag.MustNewKey("method")
)
// Views for the stats quickstart.
diff --git a/internal/readme/README.md b/internal/readme/README.md
index c744016..c98f2a1 100644
--- a/internal/readme/README.md
+++ b/internal/readme/README.md
@@ -1,6 +1,6 @@
Use the following commands to regenerate the README.
```bash
-$ go get github.com/rakyll/embedmd
+$ GO11MODULE=off go get github.com/rakyll/embedmd
$ embedmd -w ../../README.md
```
diff --git a/internal/readme/tags.go b/internal/readme/tags.go
index 09d9ac1..1219dd9 100644
--- a/internal/readme/tags.go
+++ b/internal/readme/tags.go
@@ -24,17 +24,11 @@
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 5bf7ef4..7a2c366 100644
--- a/plugin/ocgrpc/client_stats_handler_test.go
+++ b/plugin/ocgrpc/client_stats_handler_test.go
@@ -36,8 +36,8 @@
)
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 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 a305bbc..8715079 100644
--- a/plugin/ocgrpc/end_to_end_test.go
+++ b/plugin/ocgrpc/end_to_end_test.go
@@ -27,7 +27,7 @@
"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 bb3ca9a..4b4cad6 100644
--- a/plugin/ocgrpc/server_stats_handler_test.go
+++ b/plugin/ocgrpc/server_stats_handler_test.go
@@ -32,8 +32,8 @@
)
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 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 0ae5691..89cac9c 100644
--- a/plugin/ocgrpc/stats_common.go
+++ b/plugin/ocgrpc/stats_common.go
@@ -61,14 +61,14 @@
// 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 63bbcda..ee37290 100644
--- a/plugin/ochttp/stats.go
+++ b/plugin/ochttp/stats.go
@@ -92,38 +92,38 @@
// 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 3e0264f..1c467ec 100644
--- a/stats/benchmark_test.go
+++ b/stats/benchmark_test.go
@@ -65,14 +65,14 @@
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 ca46ed5..93a6522 100644
--- a/stats/record_test.go
+++ b/stats/record_test.go
@@ -42,8 +42,8 @@
)
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 0f195d4..5937b57 100644
--- a/stats/view/benchmark_test.go
+++ b/stats/view/benchmark_test.go
@@ -26,16 +26,17 @@
)
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 57720c1..2905a18 100644
--- a/stats/view/collector_test.go
+++ b/stats/view/collector_test.go
@@ -29,18 +29,9 @@
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 7d2bed9..2a2bde4 100644
--- a/stats/view/view_test.go
+++ b/stats/view/view_test.go
@@ -29,9 +29,9 @@
)
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_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 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 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 82ba969..da41354 100644
--- a/stats/view/view_to_metric_test.go
+++ b/stats/view/view_to_metric_test.go
@@ -111,9 +111,9 @@
}
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 8d4546e..6c15d37 100644
--- a/stats/view/worker_test.go
+++ b/stats/view/worker_test.go
@@ -124,8 +124,8 @@
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 fe0c5d9..9e3a597 100644
--- a/tag/example_test.go
+++ b/tag/example_test.go
@@ -37,15 +37,14 @@
_ = 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 4e63d08..71ec913 100644
--- a/tag/key.go
+++ b/tag/key.go
@@ -21,7 +21,7 @@
}
// 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 @@
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 {