|
15 | 15 | package ochttp
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "fmt" |
19 |
| - |
20 | 18 | "go.opencensus.io/stats"
|
21 | 19 | "go.opencensus.io/stats/view"
|
22 | 20 | "go.opencensus.io/tag"
|
23 | 21 | )
|
24 | 22 |
|
| 23 | +// The following client HTTP measures are supported for use in custom views: |
25 | 24 | 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 | +) |
30 | 30 |
|
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 | +) |
33 | 45 |
|
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}) |
37 | 49 | )
|
38 | 50 |
|
| 51 | +// Package ochttp provides some convenience views. |
| 52 | +// You need to subscribe to the views for data to actually be collected. |
39 | 53 | 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) |
48 | 58 |
|
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", |
60 | 61 | "Client request count by HTTP method",
|
61 | 62 | []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", |
67 | 67 | "Client response count by status code",
|
68 | 68 | []tag.Key{StatusCode},
|
69 | 69 | 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{}) |
101 | 71 |
|
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, |
106 | 79 | }
|
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 | +) |
0 commit comments