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

Commit c977418

Browse files
author
Ramon Nogueira
authored
Improve godoc for ochttp vars. (#444)
Also made tag keys agree with trace attributes. Fixes: #423
1 parent 2a66d06 commit c977418

File tree

5 files changed

+100
-93
lines changed

5 files changed

+100
-93
lines changed

plugin/ochttp/client_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func TestClient(t *testing.T) {
3434
}))
3535
defer server.Close()
3636

37+
for _, v := range ochttp.DefaultViews {
38+
v.Subscribe()
39+
}
40+
3741
views := []string{
3842
"opencensus.io/http/client/requests",
3943
"opencensus.io/http/client/latency",
@@ -46,7 +50,6 @@ func TestClient(t *testing.T) {
4650
t.Errorf("view not found %q", name)
4751
continue
4852
}
49-
v.Subscribe()
5053
}
5154

5255
var (

plugin/ochttp/stats.go

+44-91
Original file line numberDiff line numberDiff line change
@@ -15,113 +15,66 @@
1515
package ochttp
1616

1717
import (
18-
"fmt"
19-
2018
"go.opencensus.io/stats"
2119
"go.opencensus.io/stats/view"
2220
"go.opencensus.io/tag"
2321
)
2422

23+
// The following client HTTP measures are supported for use in custom views:
2524
var (
26-
// See: http://unitsofmeasure.org/ucum.html
27-
unitByte = "By"
28-
unitDimensionless = "1"
29-
unitMillisecond = "ms"
25+
ClientRequests, _ = stats.Int64("opencensus.io/http/client/requests", "Number of HTTP requests started", stats.UnitNone)
26+
ClientRequestBodySize, _ = stats.Int64("opencensus.io/http/client/request_size", "HTTP request body size if set as ContentLength (uncompressed)", stats.UnitBytes)
27+
ClientResponseBodySize, _ = stats.Int64("opencensus.io/http/client/response_size", "HTTP response body size (uncompressed)", stats.UnitBytes)
28+
ClientLatency, _ = stats.Float64("opencensus.io/http/client/latency", "End-to-end latency", stats.UnitMilliseconds)
29+
)
3030

31-
bytesBucketBoundaries = []float64{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296}
32-
millisBucketBoundaries = []float64{0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000}
31+
// The following tags are applied to stats recorded by this package. Host, Path
32+
// and Method are applied to all measures. StatusCode is not applied to
33+
// ClientRequests, since it is recorded before the status is known.
34+
var (
35+
// Host is the value of the HTTP Host header.
36+
Host, _ = tag.NewKey("http.host")
37+
// StatusCode is the numeric HTTP response status code,
38+
// or "error" if a transport error occurred and no status code was read.
39+
StatusCode, _ = tag.NewKey("http.status")
40+
// Path is the URL path (not including query string) in the request.
41+
Path, _ = tag.NewKey("http.path")
42+
// Method is the HTTP method of the request, capitalized (GET, POST, etc.).
43+
Method, _ = tag.NewKey("http.method")
44+
)
3345

34-
aggCount = view.CountAggregation{}
35-
aggDistBytes = view.DistributionAggregation(bytesBucketBoundaries)
36-
aggDistMillis = view.DistributionAggregation(millisBucketBoundaries)
46+
var (
47+
DefaultSizeDistribution = view.DistributionAggregation([]float64{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296})
48+
DefaultLatencyDistribution = view.DistributionAggregation([]float64{0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000})
3749
)
3850

51+
// Package ochttp provides some convenience views.
52+
// You need to subscribe to the views for data to actually be collected.
3953
var (
40-
// ClientRequest is the number of client requests started.
41-
ClientRequest = int64Measure("requests", "Number of HTTP requests started", unitDimensionless)
42-
// ClientRequestBodySize is the size of request body if set as ContentLength (uncompressed bytes).
43-
ClientRequestBodySize = int64Measure("request_size", "HTTP request body size (uncompressed)", unitByte)
44-
// ClientResponseBodySize is the size of response body (uncompressed bytes).
45-
ClientResponseBodySize = int64Measure("response_size", "HTTP response body size (uncompressed)", unitByte)
46-
// ClientLatency is the end-to-end client latency (in milliseconds).
47-
ClientLatency = floatMeasure("latency", "End-to-end latency", unitMillisecond)
54+
ClientRequestCount, _ = view.New("opencensus.io/http/client/requests", "Count of HTTP requests started", nil, ClientRequests, view.CountAggregation{})
55+
ClientRequestBodySizeDistribution, _ = view.New("opencensus.io/http/client/request_size", "Size distribution of HTTP request body", nil, ClientRequestBodySize, DefaultSizeDistribution)
56+
ClientResponseBodySizeDistribution, _ = view.New("opencensus.io/http/client/response_size", "Size distribution of HTTP response body", nil, ClientResponseBodySize, DefaultSizeDistribution)
57+
ClientLatencyDistribution, _ = view.New("opencensus.io/http/client/latency", "Latency distribution of HTTP requests", nil, ClientLatency, DefaultLatencyDistribution)
4858

49-
// ClientRequestCount is a count of all instrumented HTTP requests.
50-
ClientRequestCount = defaultView(ClientRequest, aggCount)
51-
// ClientRequestBodySizeDistribution is a view of the size distribution of all instrumented request bodies.
52-
ClientRequestBodySizeDistribution = defaultView(ClientRequestBodySize, aggDistBytes)
53-
// ClientResponseBodySizeDistribution is a view of the size distribution of instrumented response bodies.
54-
ClientResponseBodySizeDistribution = defaultView(ClientResponseBodySize, aggDistBytes)
55-
// ClientLatencyDistribution is a view of the latency distribution of all instrumented requests.
56-
ClientLatencyDistribution = defaultView(ClientLatency, aggDistMillis)
57-
// ClientRequestCountByMethod is a view of response counts by HTTP method.
58-
ClientRequestCountByMethod = mustView(view.New(
59-
qualify("request_count_by_method"),
59+
ClientRequestCountByMethod, _ = view.New(
60+
"opencensus.io/http/client/request_count_by_method",
6061
"Client request count by HTTP method",
6162
[]tag.Key{Method},
62-
ClientRequest,
63-
aggCount))
64-
// ClientResponseCountByStatusCode is a count of all instrumented HTTP responses HTTP status code.
65-
ClientResponseCountByStatusCode = mustView(view.New(
66-
qualify("response_count_by_status_code"),
63+
ClientRequests,
64+
view.CountAggregation{})
65+
ClientResponseCountByStatusCode, _ = view.New(
66+
"opencensus.io/http/client/response_count_by_status_code",
6767
"Client response count by status code",
6868
[]tag.Key{StatusCode},
6969
ClientLatency,
70-
aggCount))
71-
72-
// Host is the value of the HTTP Host header.
73-
Host = key("host")
74-
// StatusCode is the numeric HTTP response status code, or "error" if a transport error occurred and no status code
75-
// was read.
76-
StatusCode = key("status_code")
77-
// Path is the URL path (not including query string) in the request.
78-
Path = key("path")
79-
// Method is the HTTP method of the request, capitalized (GET, POST, etc.).
80-
Method = key("method")
81-
)
82-
83-
func defaultView(m stats.Measure, agg view.Aggregation) *view.View {
84-
v, err := view.New(m.Name(), m.Description(), nil, m, agg)
85-
if err != nil {
86-
panic(err)
87-
}
88-
if err := view.Register(v); err != nil {
89-
panic(err)
90-
}
91-
return v
92-
}
93-
94-
func key(name string) tag.Key {
95-
k, err := tag.NewKey(qualify(name))
96-
if err != nil {
97-
panic(err)
98-
}
99-
return k
100-
}
70+
view.CountAggregation{})
10171

102-
func int64Measure(name, desc, unit string) *stats.Int64Measure {
103-
m, err := stats.Int64(qualify(name), desc, unit)
104-
if err != nil {
105-
panic(err)
72+
DefaultViews = []*view.View{
73+
ClientRequestCount,
74+
ClientRequestBodySizeDistribution,
75+
ClientResponseBodySizeDistribution,
76+
ClientLatencyDistribution,
77+
ClientRequestCountByMethod,
78+
ClientResponseCountByStatusCode,
10679
}
107-
return m
108-
}
109-
110-
func floatMeasure(name, desc, unit string) *stats.Float64Measure {
111-
m, err := stats.Float64(qualify(name), desc, unit)
112-
if err != nil {
113-
panic(err)
114-
}
115-
return m
116-
}
117-
118-
func mustView(v *view.View, err error) *view.View {
119-
if err != nil {
120-
panic(err)
121-
}
122-
return v
123-
}
124-
125-
func qualify(suffix string) string {
126-
return fmt.Sprintf("opencensus.io/http/client/%s", suffix)
127-
}
80+
)

plugin/ochttp/stats_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ochttp
2+
3+
import (
4+
"testing"
5+
6+
"go.opencensus.io/stats"
7+
"go.opencensus.io/tag"
8+
)
9+
10+
func TestVarsInitialized(t *testing.T) {
11+
// Test that global initialization was successful
12+
for i, k := range []tag.Key{Host, StatusCode, Path, Method} {
13+
if k.Name() == "" {
14+
t.Errorf("key not initialized: %d", i)
15+
}
16+
}
17+
for i, m := range []stats.Measure{ClientRequests, ClientResponseBodySize, ClientRequestBodySize, ClientLatency} {
18+
if m == nil {
19+
t.Errorf("measure not initialized: %d", i)
20+
}
21+
}
22+
for i, v := range DefaultViews {
23+
if v == nil {
24+
t.Errorf("view not initialized: %d", i)
25+
}
26+
}
27+
}

plugin/ochttp/stats_transport.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (t statsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
4848
} else if req.ContentLength > 0 {
4949
track.reqSize = req.ContentLength
5050
}
51-
stats.Record(ctx, ClientRequest.M(1))
51+
stats.Record(ctx, ClientRequests.M(1))
5252

5353
// Perform request.
5454
resp, err := t.base.RoundTrip(req)

stats/units.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
package stats
17+
18+
// Units are encoded according to the case-sensitive abbreviations from the
19+
// Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html
20+
const (
21+
UnitNone = "1"
22+
UnitBytes = "By"
23+
UnitMilliseconds = "ms"
24+
)

0 commit comments

Comments
 (0)