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

Commit ec07cc4

Browse files
committed
Add support for recording gRPC stats
1 parent 622cc75 commit ec07cc4

File tree

9 files changed

+299
-229
lines changed

9 files changed

+299
-229
lines changed

packages/opencensus-core/src/trace/instrumentation/base-plugin.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
import * as path from 'path';
1717
import * as semver from 'semver';
18+
1819
import {Logger} from '../../common/types';
20+
import {Stats} from '../../stats/types';
1921
import * as modelTypes from '../model/types';
22+
2023
import * as types from './types';
2124

2225
/**
@@ -49,6 +52,8 @@ export abstract class BasePlugin implements types.Plugin {
4952
protected basedir: string;
5053
/** plugin options */
5154
protected options: types.PluginConfig;
55+
/** A stats object. */
56+
protected stats: Stats;
5257

5358
/**
5459
* Constructs a new BasePlugin instance.
@@ -65,17 +70,19 @@ export abstract class BasePlugin implements types.Plugin {
6570
* @param version module version description
6671
* @param options plugin options
6772
* @param basedir module absolute path
73+
* @param stats a stats instance
6874
*/
6975
private setPluginContext(
7076
// tslint:disable-next-line:no-any
7177
moduleExports: any, tracer: modelTypes.Tracer, version: string,
72-
options: types.PluginConfig, basedir?: string) {
78+
options: types.PluginConfig, basedir?: string, stats?: Stats) {
7379
this.moduleExports = moduleExports;
7480
this.tracer = tracer;
7581
this.version = version;
7682
this.basedir = basedir;
7783
this.logger = tracer.logger;
7884
this.options = options;
85+
this.stats = stats;
7986
this.internalFilesExports = this.loadInternalFiles();
8087
}
8188

@@ -91,12 +98,14 @@ export abstract class BasePlugin implements types.Plugin {
9198
* @param version version of the current instaled module to patch
9299
* @param options plugin options
93100
* @param basedir module absolute path
101+
* @param stats a stats instance
94102
*/
95103
enable<T>(
96104
// tslint:disable-next-line:no-any
97105
moduleExports: T, tracer: modelTypes.Tracer, version: string,
98-
options: types.PluginConfig, basedir: string) {
99-
this.setPluginContext(moduleExports, tracer, version, options, basedir);
106+
options: types.PluginConfig, basedir: string, stats?: Stats) {
107+
this.setPluginContext(
108+
moduleExports, tracer, version, options, basedir, stats);
100109
return this.applyPatch();
101110
}
102111

packages/opencensus-instrumentation-grpc/package-lock.json

Lines changed: 30 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-instrumentation-grpc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@opencensus/core": "^0.0.9",
6666
"grpc": "~1.12.2",
6767
"lodash": "^4.17.10",
68+
"object-sizeof": "^1.3.0",
6869
"semver": "^5.5.0",
6970
"shimmer": "^1.2.0"
7071
}

packages/opencensus-instrumentation-grpc/src/grpc-stats/client-metrics.ts

Lines changed: 58 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,55 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AggregationType, globalStats, MeasureUnit, View} from '@opencensus/core';
18-
import {DEFAULT_BYTES_DISTRIBUTION, DEFAULT_MESSAGE_COUNT_DISTRIBUTION, DEFAULT_MILLI_SECONDS_DISTRIBUTION} from './stats-common';
17+
import {AggregationType, globalStats, Measure, MeasureUnit, View} from '@opencensus/core';
1918

20-
/**
21-
* {@link Measure} for number of messages sent in the RPC.
22-
*
23-
*/
24-
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC = globalStats.createMeasureInt64(
25-
'grpc.io/client/sent_messages_per_rpc', MeasureUnit.UNIT,
26-
'Number of messages sent in the RPC (always 1 for non-streaming RPCs).');
19+
import {DEFAULT_BYTES_DISTRIBUTION, DEFAULT_MESSAGE_COUNT_DISTRIBUTION, DEFAULT_MILLI_SECONDS_DISTRIBUTION} from './common-distributions';
20+
21+
/** {@link Measure} for number of messages sent in the RPC. */
22+
export const GRPC_CLIENT_SENT_MESSAGES_PER_RPC: Measure =
23+
globalStats.createMeasureInt64(
24+
'grpc.io/client/sent_messages_per_rpc', MeasureUnit.UNIT,
25+
'Number of messages sent in the RPC (always 1 for non-streaming RPCs).');
2726

2827
/**
2928
* {@link Measure} for total bytes sent across all request messages per RPC.
30-
*
3129
*/
32-
const GRPC_CLIENT_SENT_BYTES_PER_RPC = globalStats.createMeasureInt64(
33-
'grpc.io/client/sent_bytes_per_rpc', MeasureUnit.BYTE,
34-
'Total bytes sent across all request messages per RPC.');
30+
export const GRPC_CLIENT_SENT_BYTES_PER_RPC: Measure =
31+
globalStats.createMeasureInt64(
32+
'grpc.io/client/sent_bytes_per_rpc', MeasureUnit.BYTE,
33+
'Total bytes sent across all request messages per RPC.');
3534

36-
/**
37-
* {@link Measure} for number of response messages received per RPC.
38-
*
39-
*/
40-
const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC = globalStats.createMeasureInt64(
41-
'grpc.io/client/received_messages_per_rpc', MeasureUnit.UNIT,
42-
'Number of response messages received per RPC (always 1 for non-streaming RPCs).');
35+
/** {@link Measure} for number of response messages received per RPC. */
36+
export const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC: Measure =
37+
globalStats.createMeasureInt64(
38+
'grpc.io/client/received_messages_per_rpc', MeasureUnit.UNIT,
39+
'Number of response messages received per RPC (always 1 for non-streaming RPCs).');
4340

4441
/**
4542
* {@link Measure} for total bytes received across all response messages per RPC
46-
*
4743
*/
48-
const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC = globalStats.createMeasureInt64(
49-
'grpc.io/client/received_bytes_per_rpc', MeasureUnit.BYTE,
50-
'Total bytes received across all response messages per RPC.');
44+
export const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC: Measure =
45+
globalStats.createMeasureInt64(
46+
'grpc.io/client/received_bytes_per_rpc', MeasureUnit.BYTE,
47+
'Total bytes received across all response messages per RPC.');
5148

52-
/**
53-
* {@link Measure} for gRPC client roundtrip latency in milliseconds.
54-
*
55-
*/
56-
const GRPC_CLIENT_ROUNDTRIP_LATENCY = globalStats.createMeasureDouble(
49+
/** {@link Measure} for gRPC client roundtrip latency in milliseconds. */
50+
export const GRPC_CLIENT_ROUNDTRIP_LATENCY: Measure = globalStats.createMeasureDouble(
5751
'grpc.io/client/roundtrip_latency', MeasureUnit.MS,
5852
'Time between first byte of request sent to last byte of response received, or terminal error.');
5953

60-
/**
61-
* {@link Measure} for gRPC server latency in milliseconds.
62-
*
63-
*/
64-
const GRPC_CLIENT_SERVER_LATENCY = globalStats.createMeasureDouble(
54+
/** {@link Measure} for gRPC server latency in milliseconds. */
55+
export const GRPC_CLIENT_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
6556
'grpc.io/client/server_latency', MeasureUnit.MS,
6657
'Propagated from the server and should have the same value as "grpc.io/server/latency');
6758

6859
/**
6960
* Tag key that represents a client gRPC method.
7061
*
71-
* <p>{@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
62+
* {@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
7263
* available in all the recorded metrics.
73-
*
7464
*/
75-
const GRPC_CLIENT_METHOD = {
65+
export const GRPC_CLIENT_METHOD = {
7666
name: 'grpc_client_method'
7767
};
7868

@@ -82,35 +72,41 @@ const GRPC_CLIENT_METHOD = {
8272
*
8373
* <p>{@link #GRPC_CLIENT_STATUS} is set when an outgoing request finishes and
8474
* is only available around metrics recorded at the end of the outgoing request.
85-
*
8675
*/
87-
const GRPC_CLIENT_STATUS = {
76+
export const GRPC_CLIENT_STATUS = {
8877
name: 'grpc_client_status'
8978
};
9079

91-
/**
92-
* {@link View} for client sent bytes per RPC.
93-
*
94-
*/
95-
const GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW = globalStats.createView(
96-
'grpc.io/client/sent_bytes_per_rpc', GRPC_CLIENT_SENT_BYTES_PER_RPC,
97-
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
98-
'Distribution of bytes sent per RPC, by method.',
99-
DEFAULT_BYTES_DISTRIBUTION);
100-
/**
101-
* {@link View} for client received bytes per RPC.
102-
*
103-
*/
80+
/** {@link View} for client received messages per RPC. */
81+
const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW = globalStats.createView(
82+
'grpc.io/client/received_messages_per_rpc',
83+
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC, AggregationType.DISTRIBUTION,
84+
[GRPC_CLIENT_METHOD],
85+
'Distribution of received messages count per RPC, by method.',
86+
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);
87+
88+
/** {@link View} for client received bytes per RPC. */
10489
const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW = globalStats.createView(
10590
'grpc.io/client/received_bytes_per_rpc', GRPC_CLIENT_RECEIVED_BYTES_PER_RPC,
10691
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
10792
'Distribution of bytes received per RPC, by method.',
10893
DEFAULT_BYTES_DISTRIBUTION);
10994

110-
/**
111-
* {@link View} for client roundtrip latency in milliseconds.
112-
*
113-
*/
95+
/** {@link View} for client sent messages per RPC. */
96+
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW = globalStats.createView(
97+
'grpc.io/client/sent_messages_per_rpc', GRPC_CLIENT_SENT_MESSAGES_PER_RPC,
98+
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
99+
'Distribution of sent messages count per RPC, by method.',
100+
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);
101+
102+
/** {@link View} for client sent bytes per RPC. */
103+
const GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW = globalStats.createView(
104+
'grpc.io/client/sent_bytes_per_rpc', GRPC_CLIENT_SENT_BYTES_PER_RPC,
105+
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
106+
'Distribution of bytes sent per RPC, by method.',
107+
DEFAULT_BYTES_DISTRIBUTION);
108+
109+
/** {@link View} for client roundtrip latency in milliseconds. */
114110
const GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = globalStats.createView(
115111
'grpc.io/client/roundtrip_latency', GRPC_CLIENT_ROUNDTRIP_LATENCY,
116112
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
@@ -120,51 +116,19 @@ const GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = globalStats.createView(
120116
/**
121117
* {@link View} for completed client RPCs.
122118
*
123-
* <p>This {@code View} uses measure {@code GRPC_CLIENT_ROUNDTRIP_LATENCY},
119+
* This {@code View} uses measure {@code GRPC_CLIENT_ROUNDTRIP_LATENCY},
124120
* since completed RPCs can be inferred over any measure recorded once per RPC
125121
* (since it's just a count aggregation over the measure). It would be
126122
* unnecessary to use a separate "count" measure.
127-
*
128123
*/
129124
const GRPC_CLIENT_COMPLETED_RPC_VIEW = globalStats.createView(
130125
'grpc.io/client/completed_rpcs', GRPC_CLIENT_ROUNDTRIP_LATENCY,
131126
AggregationType.COUNT, [GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS],
132-
'Count of RPCs by method and status.', DEFAULT_MILLI_SECONDS_DISTRIBUTION);
133-
134-
/**
135-
* {@link View} for client sent messages per RPC.
136-
*
137-
*/
138-
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW = globalStats.createView(
139-
'grpc.io/client/sent_messages_per_rpc', GRPC_CLIENT_SENT_MESSAGES_PER_RPC,
140-
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
141-
'Distribution of sent messages count per RPC, by method.',
142-
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);
143-
144-
/**
145-
* {@link View} for client received messages per RPC.
146-
*
147-
*/
148-
const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW = globalStats.createView(
149-
'grpc.io/client/received_messages_per_rpc',
150-
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC, AggregationType.DISTRIBUTION,
151-
[GRPC_CLIENT_METHOD],
152-
'Distribution of received messages count per RPC, by method.',
153-
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);
154-
155-
/**
156-
* {@link View} for client server latency in milliseconds.
157-
*
158-
*/
159-
const GRPC_CLIENT_SERVER_LATENCY_VIEW = globalStats.createView(
160-
'grpc.io/client/server_latency', GRPC_CLIENT_SERVER_LATENCY,
161-
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
162-
'Distribution of server latency as viewed by client, by method.',
163-
DEFAULT_MILLI_SECONDS_DISTRIBUTION);
127+
'Count of RPCs by method and status.', DEFAULT_MESSAGE_COUNT_DISTRIBUTION);
164128

165129
export const GRPC_BASIC_CLIENT_VIEWS: View[] = [
166-
GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW, GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW,
167-
GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, GRPC_CLIENT_COMPLETED_RPC_VIEW,
168-
GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW,
169-
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW, GRPC_CLIENT_SERVER_LATENCY_VIEW
130+
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW,
131+
GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW,
132+
GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW, GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW,
133+
GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, GRPC_CLIENT_COMPLETED_RPC_VIEW
170134
];

0 commit comments

Comments
 (0)