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

Commit 8ba7367

Browse files
authored
Add Measure, TagKey and View constants for recording HTTP stats (#360)
* Add Measure, TagKeys and Views constants for recording HTTP stats * minor nit fix * Remove zero bounds
1 parent 8572750 commit 8ba7367

1 file changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License'};
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* gRPC://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {AggregationType, globalStats, MeasureUnit, Stats, View} from '@opencensus/core';
18+
19+
/**
20+
* {@link Measure} for the client-side total bytes sent in request body (not
21+
* including headers). This is uncompressed bytes.
22+
*/
23+
export const HTTP_CLIENT_SENT_BYTES = globalStats.createMeasureInt64(
24+
'opencensus.io/http/client/sent_bytes', MeasureUnit.BYTE,
25+
'Client-side total bytes sent in request body (uncompressed)');
26+
27+
/**
28+
* {@link Measure} for the client-side total bytes received in response
29+
* bodies (not including headers but including error responses with bodies).
30+
* Should be measured from actual bytes received and read, not the value of
31+
* the Content-Length header. This is uncompressed bytes. Responses with no
32+
* body should record 0 for this value.
33+
*/
34+
export const HTTP_CLIENT_RECEIVED_BYTES = globalStats.createMeasureInt64(
35+
'opencensus.io/http/client/received_bytes', MeasureUnit.BYTE,
36+
'Client-side total bytes received in response bodies (uncompressed)');
37+
38+
/**
39+
* {@link Measure} for the client-side time between first byte of request
40+
* headers sent to last byte of response received, or terminal error.
41+
*/
42+
export const HTTP_CLIENT_ROUNDTRIP_LATENCY = globalStats.createMeasureDouble(
43+
'opencensus.io/http/client/roundtrip_latency', MeasureUnit.MS,
44+
'Client-side time between first byte of request headers sent to last byte of response received, or terminal error');
45+
46+
/**
47+
* {@link Measure} for the server-side total bytes received in request body
48+
* (not including headers). This is uncompressed bytes.
49+
*/
50+
export const HTTP_SERVER_RECEIVED_BYTES = globalStats.createMeasureInt64(
51+
'opencensus.io/http/server/received_bytes', MeasureUnit.BYTE,
52+
'Server-side total bytes received in request body (uncompressed)');
53+
54+
/**
55+
* {@link Measure} for the server-side total bytes sent in response bodies
56+
* (not including headers but including error responses with bodies). Should
57+
* be measured from actual bytes written and sent, not the value of the
58+
* Content-Length header. This is uncompressed bytes. Responses with no
59+
* body should record 0 for this value.
60+
*/
61+
export const HTTP_SERVER_SENT_BYTES = globalStats.createMeasureInt64(
62+
'opencensus.io/http/server/sent_bytes', MeasureUnit.BYTE,
63+
'Server-side total bytes sent in response bodies (uncompressed)');
64+
65+
/**
66+
* {@link Measure} for the server-side time between first byte of request
67+
* headers received to last byte of response sent, or terminal error.
68+
*/
69+
export const HTTP_SERVER_LATENCY = globalStats.createMeasureDouble(
70+
'opencensus.io/http/server/server_latency', MeasureUnit.MS,
71+
'Server-side time between first byte of request headers received to last byte of response sent, or terminal error');
72+
73+
/** {@link TagKey} for the value of the client-side HTTP host header. */
74+
export const HTTP_CLIENT_HOST = {
75+
name: 'http_client_host'
76+
};
77+
78+
/** {@link TagKey} for the value of the server-side HTTP host header. */
79+
export const HTTP_SERVER_HOST = {
80+
name: 'http_server_host'
81+
};
82+
83+
/**
84+
* {@link TagKey} for the numeric client-side HTTP response status code (e.g.
85+
* 200, 404, 500). If a transport error occurred and no status code was read,
86+
* use "error" as the {@code TagValue}.
87+
*/
88+
export const HTTP_CLIENT_STATUS = {
89+
name: 'http_client_status'
90+
};
91+
92+
/**
93+
* {@link TagKey} for the numeric server-side HTTP response status code (e.g.
94+
* 200, 404, 500). If a transport error occurred and no status code was written
95+
* use "error" as the {@code TagValue}.
96+
*/
97+
export const HTTP_SERVER_STATUS = {
98+
name: 'http_server_status'
99+
};
100+
101+
/**
102+
* {@link TagKey} for the client-side URL path (not including query string) in
103+
* the request.
104+
*/
105+
export const HTTP_CLIENT_PATH = {
106+
name: 'http_client_path'
107+
};
108+
109+
/**
110+
* {@link TagKey} for the server-side URL path (not including query string) in
111+
* the request.
112+
*/
113+
export const HTTP_SERVER_PATH = {
114+
name: 'http_server_path'
115+
};
116+
117+
/**
118+
* {@link TagKey} for the client-side HTTP method of the request, capitalized
119+
* (GET, POST, etc.).
120+
*/
121+
export const HTTP_CLIENT_METHOD = {
122+
name: 'http_client_method'
123+
};
124+
125+
/**
126+
* {@link TagKey} for the server-side HTTP method of the request, capitalized
127+
* (GET, POST, etc.).
128+
*/
129+
export const HTTP_SERVER_METHOD = {
130+
name: 'http_server_method'
131+
};
132+
133+
/**
134+
* {@link TagKey} for the server-side logical route, a pattern that matched the
135+
* URL, of a handler that processed the request.
136+
*/
137+
export const HTTP_SERVER_ROUTE = {
138+
name: 'http_server_route'
139+
};
140+
141+
const SIZE_DISTRIBUTION: number[] = [
142+
1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864,
143+
268435456, 1073741824, 4294967296
144+
];
145+
146+
const LATENCY_DISTRIBUTION: number[] = [
147+
1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25,
148+
30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400,
149+
500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000
150+
];
151+
152+
/** {@link View} for count of client-side HTTP requests completed. */
153+
const HTTP_CLIENT_COMPLETED_COUNT_VIEW = globalStats.createView(
154+
'opencensus.io/http/client/completed_count', HTTP_CLIENT_ROUNDTRIP_LATENCY,
155+
AggregationType.COUNT, [HTTP_CLIENT_METHOD, HTTP_CLIENT_STATUS],
156+
'Count of client-side HTTP requests completed');
157+
158+
/** {@link View} for size distribution of client-side HTTP request body. */
159+
const HTTP_CLIENT_SENT_BYTES_VIEW = globalStats.createView(
160+
'opencensus.io/http/client/sent_bytes', HTTP_CLIENT_SENT_BYTES,
161+
AggregationType.DISTRIBUTION, [HTTP_CLIENT_METHOD, HTTP_CLIENT_STATUS],
162+
'Size distribution of client-side HTTP request body', SIZE_DISTRIBUTION);
163+
164+
/** {@link View} for size distribution of client-side HTTP response body. */
165+
const HTTP_CLIENT_RECEIVED_BYTES_VIEW = globalStats.createView(
166+
'opencensus.io/http/client/received_bytes', HTTP_CLIENT_RECEIVED_BYTES,
167+
AggregationType.DISTRIBUTION, [HTTP_CLIENT_METHOD, HTTP_CLIENT_STATUS],
168+
'Size distribution of client-side HTTP response body', SIZE_DISTRIBUTION);
169+
170+
/**
171+
* {@link View} for roundtrip latency distribution of client-side HTTP requests.
172+
*/
173+
const HTTP_CLIENT_ROUNDTRIP_LATENCY_VIEW = globalStats.createView(
174+
'opencensus.io/http/client/roundtrip_latency',
175+
HTTP_CLIENT_ROUNDTRIP_LATENCY, AggregationType.DISTRIBUTION,
176+
[HTTP_CLIENT_METHOD, HTTP_CLIENT_STATUS],
177+
'Roundtrip latency distribution of client-side HTTP requests',
178+
LATENCY_DISTRIBUTION);
179+
180+
/** {@link View} for count of server-side HTTP requests serving completed. */
181+
const HTTP_SERVER_COMPLETED_COUNT_VIEW = globalStats.createView(
182+
'opencensus.io/http/server/completed_count', HTTP_SERVER_LATENCY,
183+
AggregationType.COUNT,
184+
[HTTP_SERVER_METHOD, HTTP_SERVER_ROUTE, HTTP_SERVER_STATUS],
185+
'Count of HTTP server-side requests serving completed');
186+
187+
/** {@link View} for size distribution of server-side HTTP request body. */
188+
const HTTP_SERVER_RECEIVED_BYTES_VIEW = globalStats.createView(
189+
'opencensus.io/http/server/received_bytes', HTTP_SERVER_RECEIVED_BYTES,
190+
AggregationType.DISTRIBUTION,
191+
[HTTP_SERVER_METHOD, HTTP_SERVER_ROUTE, HTTP_SERVER_STATUS],
192+
'Size distribution of server-side HTTP request body', SIZE_DISTRIBUTION);
193+
194+
/** {@link View} for size distribution of server-side HTTP response body. */
195+
const HTTP_SERVER_SENT_BYTES_VIEW = globalStats.createView(
196+
'opencensus.io/http/server/sent_bytes', HTTP_SERVER_SENT_BYTES,
197+
AggregationType.DISTRIBUTION,
198+
[HTTP_SERVER_METHOD, HTTP_SERVER_ROUTE, HTTP_SERVER_STATUS],
199+
'Size distribution of server-side HTTP response body', SIZE_DISTRIBUTION);
200+
201+
/**
202+
* {@link View} for latency distribution of server-side HTTP requests serving.
203+
*/
204+
const HTTP_SERVER_LATENCY_VIEW = globalStats.createView(
205+
'opencensus.io/http/server/server_latency', HTTP_SERVER_LATENCY,
206+
AggregationType.DISTRIBUTION,
207+
[HTTP_SERVER_METHOD, HTTP_SERVER_ROUTE, HTTP_SERVER_STATUS],
208+
'Latency distribution of server-side HTTP requests serving',
209+
LATENCY_DISTRIBUTION);
210+
211+
const HTTP_BASIC_SERVER_VIEWS: View[] = [
212+
HTTP_SERVER_COMPLETED_COUNT_VIEW, HTTP_SERVER_RECEIVED_BYTES_VIEW,
213+
HTTP_SERVER_SENT_BYTES_VIEW, HTTP_SERVER_LATENCY_VIEW
214+
];
215+
216+
const HTTP_BASIC_CLIENT_VIEWS: View[] = [
217+
HTTP_CLIENT_COMPLETED_COUNT_VIEW, HTTP_CLIENT_SENT_BYTES_VIEW,
218+
HTTP_CLIENT_RECEIVED_BYTES_VIEW, HTTP_CLIENT_ROUNDTRIP_LATENCY_VIEW
219+
];
220+
221+
/** Register all default views. */
222+
export function registerAllViews(globalStats: Stats) {
223+
registerAllClientViews(globalStats);
224+
registerAllServerViews(globalStats);
225+
}
226+
227+
/** Register all default client views. */
228+
export function registerAllClientViews(globalStats: Stats) {
229+
for (const view of HTTP_BASIC_CLIENT_VIEWS) {
230+
globalStats.registerView(view);
231+
}
232+
}
233+
234+
/** Register all default server views. */
235+
export function registerAllServerViews(globalStats: Stats) {
236+
for (const view of HTTP_BASIC_SERVER_VIEWS) {
237+
globalStats.registerView(view);
238+
}
239+
}

0 commit comments

Comments
 (0)