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

Replace tag.NewKey calls with tag.MustNewKey where appropriate #1146

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Package `tag` allows adding or modifying tags in the current context.

[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"),
)
Expand Down
5 changes: 1 addition & 4 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion examples/quickstart/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (

// TagKeys for the stats quickstart.
var (
keyMethod, _ = tag.NewKey("method")
keyMethod = tag.MustNewKey("method")
)

// Views for the stats quickstart.
Expand Down
2 changes: 1 addition & 1 deletion internal/readme/README.md
Original file line number Diff line number Diff line change
@@ -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
```
12 changes: 3 additions & 9 deletions internal/readme/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand Down
6 changes: 3 additions & 3 deletions plugin/ocgrpc/client_stats_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion plugin/ocgrpc/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
6 changes: 3 additions & 3 deletions plugin/ocgrpc/server_stats_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions plugin/ocgrpc/stats_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
18 changes: 9 additions & 9 deletions plugin/ochttp/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions stats/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
4 changes: 2 additions & 2 deletions stats/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
21 changes: 11 additions & 10 deletions stats/view/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ 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},
Expand Down
15 changes: 3 additions & 12 deletions stats/view/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
22 changes: 11 additions & 11 deletions stats/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions stats/view/view_to_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
4 changes: 2 additions & 2 deletions stats/view/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
15 changes: 7 additions & 8 deletions tag/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
5 changes: 2 additions & 3 deletions tag/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ 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
}
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 {
Expand Down