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

Commit f6172e4

Browse files
semistrictrghetia
authored andcommitted
Replace tag.NewKey calls with tag.MustNewKey where appropriate (#1146)
1 parent a092815 commit f6172e4

19 files changed

Lines changed: 76 additions & 95 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Package `tag` allows adding or modifying tags in the current context.
7878

7979
[embedmd]:# (internal/readme/tags.go new)
8080
```go
81-
ctx, err = tag.New(ctx,
81+
ctx, err := tag.New(ctx,
8282
tag.Insert(osKey, "macOS-10.12.5"),
8383
tag.Upsert(userIDKey, "cde36753ed"),
8484
)

examples/helloworld/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ func main() {
5454

5555
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
5656

57-
frontendKey, err = tag.NewKey("example.com/keys/frontend")
58-
if err != nil {
59-
log.Fatal(err)
60-
}
57+
frontendKey = tag.MustNewKey("example.com/keys/frontend")
6158
videoSize = stats.Int64("example.com/measure/video_size", "size of processed videos", stats.UnitBytes)
6259
view.SetReportingPeriod(2 * time.Second)
6360

examples/quickstart/stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656

5757
// TagKeys for the stats quickstart.
5858
var (
59-
keyMethod, _ = tag.NewKey("method")
59+
keyMethod = tag.MustNewKey("method")
6060
)
6161

6262
// Views for the stats quickstart.

internal/readme/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Use the following commands to regenerate the README.
22

33
```bash
4-
$ go get github.com/rakyll/embedmd
4+
$ GO11MODULE=off go get github.com/rakyll/embedmd
55
$ embedmd -w ../../README.md
66
```

internal/readme/tags.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@ import (
2424
func tagsExamples() {
2525
ctx := context.Background()
2626

27-
osKey, err := tag.NewKey("example.com/keys/user-os")
28-
if err != nil {
29-
log.Fatal(err)
30-
}
31-
userIDKey, err := tag.NewKey("example.com/keys/user-id")
32-
if err != nil {
33-
log.Fatal(err)
34-
}
27+
osKey := tag.MustNewKey("example.com/keys/user-os")
28+
userIDKey := tag.MustNewKey("example.com/keys/user-id")
3529

3630
// START new
37-
ctx, err = tag.New(ctx,
31+
ctx, err := tag.New(ctx,
3832
tag.Insert(osKey, "macOS-10.12.5"),
3933
tag.Upsert(userIDKey, "cde36753ed"),
4034
)

plugin/ocgrpc/client_stats_handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import (
3636
)
3737

3838
func TestClientDefaultCollections(t *testing.T) {
39-
k1, _ := tag.NewKey("k1")
40-
k2, _ := tag.NewKey("k2")
39+
k1 := tag.MustNewKey("k1")
40+
k2 := tag.MustNewKey("k2")
4141

4242
type tagPair struct {
4343
k tag.Key
@@ -340,7 +340,7 @@ func TestClientDefaultCollections(t *testing.T) {
340340
}
341341

342342
func TestClientRecordExemplar(t *testing.T) {
343-
key, _ := tag.NewKey("test_key")
343+
key := tag.MustNewKey("test_key")
344344
tagInfo := &stats.RPCTagInfo{FullMethodName: "/package.service/method"}
345345
out := &stats.OutPayload{Length: 2000}
346346
end := &stats.End{Error: nil}

plugin/ocgrpc/end_to_end_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"go.opencensus.io/tag"
2828
)
2929

30-
var keyAccountId, _ = tag.NewKey("account_id")
30+
var keyAccountId = tag.MustNewKey("account_id")
3131

3232
func TestEndToEnd_Single(t *testing.T) {
3333
view.Register(ocgrpc.DefaultClientViews...)

plugin/ocgrpc/server_stats_handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
)
3333

3434
func TestServerDefaultCollections(t *testing.T) {
35-
k1, _ := tag.NewKey("k1")
36-
k2, _ := tag.NewKey("k2")
35+
k1 := tag.MustNewKey("k1")
36+
k2 := tag.MustNewKey("k2")
3737

3838
type tagPair struct {
3939
k tag.Key
@@ -338,7 +338,7 @@ func newDistributionData(countPerBucket []int64, count int64, min, max, mean, su
338338
}
339339

340340
func TestServerRecordExemplar(t *testing.T) {
341-
key, _ := tag.NewKey("test_key")
341+
key := tag.MustNewKey("test_key")
342342
tagInfo := &stats.RPCTagInfo{FullMethodName: "/package.service/method"}
343343
out := &stats.OutPayload{Length: 2000}
344344
end := &stats.End{Error: nil}

plugin/ocgrpc/stats_common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ var (
6161
// Server tags are applied to the context used to process each RPC, as well as
6262
// the measures at the end of each RPC.
6363
var (
64-
KeyServerMethod, _ = tag.NewKey("grpc_server_method")
65-
KeyServerStatus, _ = tag.NewKey("grpc_server_status")
64+
KeyServerMethod = tag.MustNewKey("grpc_server_method")
65+
KeyServerStatus = tag.MustNewKey("grpc_server_status")
6666
)
6767

6868
// Client tags are applied to measures at the end of each RPC.
6969
var (
70-
KeyClientMethod, _ = tag.NewKey("grpc_client_method")
71-
KeyClientStatus, _ = tag.NewKey("grpc_client_status")
70+
KeyClientMethod = tag.MustNewKey("grpc_client_method")
71+
KeyClientStatus = tag.MustNewKey("grpc_client_status")
7272
)
7373

7474
var (

plugin/ochttp/stats.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,38 @@ var (
9292
// The value of this tag can be controlled by the HTTP client, so you need
9393
// to watch out for potentially generating high-cardinality labels in your
9494
// metrics backend if you use this tag in views.
95-
Host, _ = tag.NewKey("http.host")
95+
Host = tag.MustNewKey("http.host")
9696

9797
// StatusCode is the numeric HTTP response status code,
9898
// or "error" if a transport error occurred and no status code was read.
99-
StatusCode, _ = tag.NewKey("http.status")
99+
StatusCode = tag.MustNewKey("http.status")
100100

101101
// Path is the URL path (not including query string) in the request.
102102
//
103103
// The value of this tag can be controlled by the HTTP client, so you need
104104
// to watch out for potentially generating high-cardinality labels in your
105105
// metrics backend if you use this tag in views.
106-
Path, _ = tag.NewKey("http.path")
106+
Path = tag.MustNewKey("http.path")
107107

108108
// Method is the HTTP method of the request, capitalized (GET, POST, etc.).
109-
Method, _ = tag.NewKey("http.method")
109+
Method = tag.MustNewKey("http.method")
110110

111111
// KeyServerRoute is a low cardinality string representing the logical
112112
// handler of the request. This is usually the pattern registered on the a
113113
// ServeMux (or similar string).
114-
KeyServerRoute, _ = tag.NewKey("http_server_route")
114+
KeyServerRoute = tag.MustNewKey("http_server_route")
115115
)
116116

117117
// Client tag keys.
118118
var (
119119
// KeyClientMethod is the HTTP method, capitalized (i.e. GET, POST, PUT, DELETE, etc.).
120-
KeyClientMethod, _ = tag.NewKey("http_client_method")
120+
KeyClientMethod = tag.MustNewKey("http_client_method")
121121
// KeyClientPath is the URL path (not including query string).
122-
KeyClientPath, _ = tag.NewKey("http_client_path")
122+
KeyClientPath = tag.MustNewKey("http_client_path")
123123
// KeyClientStatus is the HTTP status code as an integer (e.g. 200, 404, 500.), or "error" if no response status line was received.
124-
KeyClientStatus, _ = tag.NewKey("http_client_status")
124+
KeyClientStatus = tag.MustNewKey("http_client_status")
125125
// KeyClientHost is the value of the request Host header.
126-
KeyClientHost, _ = tag.NewKey("http_client_host")
126+
KeyClientHost = tag.MustNewKey("http_client_host")
127127
)
128128

129129
// Default distributions used by views in this package.

0 commit comments

Comments
 (0)