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

Add support for recording gRPC stats #357

Merged
merged 3 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
import * as path from 'path';
import * as semver from 'semver';

import {Logger} from '../../common/types';
import {Stats} from '../../stats/types';
import * as modelTypes from '../model/types';

import * as types from './types';

/**
Expand Down Expand Up @@ -49,6 +52,8 @@ export abstract class BasePlugin implements types.Plugin {
protected basedir: string;
/** plugin options */
protected options: types.PluginConfig;
/** A stats object. */
protected stats: Stats;

/**
* Constructs a new BasePlugin instance.
Expand All @@ -65,17 +70,19 @@ export abstract class BasePlugin implements types.Plugin {
* @param version module version description
* @param options plugin options
* @param basedir module absolute path
* @param stats a stats instance
*/
private setPluginContext(
// tslint:disable-next-line:no-any
moduleExports: any, tracer: modelTypes.Tracer, version: string,
options: types.PluginConfig, basedir?: string) {
options: types.PluginConfig, basedir?: string, stats?: Stats) {
this.moduleExports = moduleExports;
this.tracer = tracer;
this.version = version;
this.basedir = basedir;
this.logger = tracer.logger;
this.options = options;
this.stats = stats;
this.internalFilesExports = this.loadInternalFiles();
}

Expand All @@ -91,12 +98,14 @@ export abstract class BasePlugin implements types.Plugin {
* @param version version of the current instaled module to patch
* @param options plugin options
* @param basedir module absolute path
* @param stats a stats instance
*/
enable<T>(
// tslint:disable-next-line:no-any
moduleExports: T, tracer: modelTypes.Tracer, version: string,
options: types.PluginConfig, basedir: string) {
this.setPluginContext(moduleExports, tracer, version, options, basedir);
options: types.PluginConfig, basedir: string, stats?: Stats) {
this.setPluginContext(
moduleExports, tracer, version, options, basedir, stats);
return this.applyPatch();
}

Expand Down
38 changes: 30 additions & 8 deletions packages/opencensus-instrumentation-grpc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/opencensus-instrumentation-grpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@opencensus/core": "^0.0.9",
"grpc": "~1.12.2",
"lodash": "^4.17.10",
"object-sizeof": "^1.3.0",
"semver": "^5.5.0",
"shimmer": "^1.2.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,55 @@
* limitations under the License.
*/

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

/**
* {@link Measure} for number of messages sent in the RPC.
*
*/
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC = globalStats.createMeasureInt64(
'grpc.io/client/sent_messages_per_rpc', MeasureUnit.UNIT,
'Number of messages sent in the RPC (always 1 for non-streaming RPCs).');
import {DEFAULT_BYTES_DISTRIBUTION, DEFAULT_MESSAGE_COUNT_DISTRIBUTION, DEFAULT_MILLI_SECONDS_DISTRIBUTION} from './common-distributions';

/** {@link Measure} for number of messages sent in the RPC. */
export const GRPC_CLIENT_SENT_MESSAGES_PER_RPC: Measure =
globalStats.createMeasureInt64(
'grpc.io/client/sent_messages_per_rpc', MeasureUnit.UNIT,
'Number of messages sent in the RPC (always 1 for non-streaming RPCs).');

/**
* {@link Measure} for total bytes sent across all request messages per RPC.
*
*/
const GRPC_CLIENT_SENT_BYTES_PER_RPC = globalStats.createMeasureInt64(
'grpc.io/client/sent_bytes_per_rpc', MeasureUnit.BYTE,
'Total bytes sent across all request messages per RPC.');
export const GRPC_CLIENT_SENT_BYTES_PER_RPC: Measure =
globalStats.createMeasureInt64(
'grpc.io/client/sent_bytes_per_rpc', MeasureUnit.BYTE,
'Total bytes sent across all request messages per RPC.');

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

/**
* {@link Measure} for total bytes received across all response messages per RPC
*
*/
const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC = globalStats.createMeasureInt64(
'grpc.io/client/received_bytes_per_rpc', MeasureUnit.BYTE,
'Total bytes received across all response messages per RPC.');
export const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC: Measure =
globalStats.createMeasureInt64(
'grpc.io/client/received_bytes_per_rpc', MeasureUnit.BYTE,
'Total bytes received across all response messages per RPC.');

/**
* {@link Measure} for gRPC client roundtrip latency in milliseconds.
*
*/
const GRPC_CLIENT_ROUNDTRIP_LATENCY = globalStats.createMeasureDouble(
/** {@link Measure} for gRPC client roundtrip latency in milliseconds. */
export const GRPC_CLIENT_ROUNDTRIP_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/roundtrip_latency', MeasureUnit.MS,
'Time between first byte of request sent to last byte of response received, or terminal error.');

/**
* {@link Measure} for gRPC server latency in milliseconds.
*
*/
const GRPC_CLIENT_SERVER_LATENCY = globalStats.createMeasureDouble(
/** {@link Measure} for gRPC server latency in milliseconds. */
export const GRPC_CLIENT_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/server_latency', MeasureUnit.MS,
'Propagated from the server and should have the same value as "grpc.io/server/latency');

/**
* Tag key that represents a client gRPC method.
*
* <p>{@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
* {@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
* available in all the recorded metrics.
*
*/
const GRPC_CLIENT_METHOD = {
export const GRPC_CLIENT_METHOD = {
name: 'grpc_client_method'
};

Expand All @@ -82,35 +72,41 @@ const GRPC_CLIENT_METHOD = {
*
* <p>{@link #GRPC_CLIENT_STATUS} is set when an outgoing request finishes and
* is only available around metrics recorded at the end of the outgoing request.
*
*/
const GRPC_CLIENT_STATUS = {
export const GRPC_CLIENT_STATUS = {
name: 'grpc_client_status'
};

/**
* {@link View} for client sent bytes per RPC.
*
*/
const GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/sent_bytes_per_rpc', GRPC_CLIENT_SENT_BYTES_PER_RPC,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of bytes sent per RPC, by method.',
DEFAULT_BYTES_DISTRIBUTION);
/**
* {@link View} for client received bytes per RPC.
*
*/
/** {@link View} for client received messages per RPC. */
const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/received_messages_per_rpc',
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC, AggregationType.DISTRIBUTION,
[GRPC_CLIENT_METHOD],
'Distribution of received messages count per RPC, by method.',
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);

/** {@link View} for client received bytes per RPC. */
const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/received_bytes_per_rpc', GRPC_CLIENT_RECEIVED_BYTES_PER_RPC,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of bytes received per RPC, by method.',
DEFAULT_BYTES_DISTRIBUTION);

/**
* {@link View} for client roundtrip latency in milliseconds.
*
*/
/** {@link View} for client sent messages per RPC. */
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/sent_messages_per_rpc', GRPC_CLIENT_SENT_MESSAGES_PER_RPC,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of sent messages count per RPC, by method.',
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);

/** {@link View} for client sent bytes per RPC. */
const GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/sent_bytes_per_rpc', GRPC_CLIENT_SENT_BYTES_PER_RPC,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of bytes sent per RPC, by method.',
DEFAULT_BYTES_DISTRIBUTION);

/** {@link View} for client roundtrip latency in milliseconds. */
const GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = globalStats.createView(
'grpc.io/client/roundtrip_latency', GRPC_CLIENT_ROUNDTRIP_LATENCY,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
Expand All @@ -120,51 +116,19 @@ const GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW = globalStats.createView(
/**
* {@link View} for completed client RPCs.
*
* <p>This {@code View} uses measure {@code GRPC_CLIENT_ROUNDTRIP_LATENCY},
* This {@code View} uses measure {@code GRPC_CLIENT_ROUNDTRIP_LATENCY},
* since completed RPCs can be inferred over any measure recorded once per RPC
* (since it's just a count aggregation over the measure). It would be
* unnecessary to use a separate "count" measure.
*
*/
const GRPC_CLIENT_COMPLETED_RPC_VIEW = globalStats.createView(
'grpc.io/client/completed_rpcs', GRPC_CLIENT_ROUNDTRIP_LATENCY,
AggregationType.COUNT, [GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS],
'Count of RPCs by method and status.', DEFAULT_MILLI_SECONDS_DISTRIBUTION);

/**
* {@link View} for client sent messages per RPC.
*
*/
const GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/sent_messages_per_rpc', GRPC_CLIENT_SENT_MESSAGES_PER_RPC,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of sent messages count per RPC, by method.',
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);

/**
* {@link View} for client received messages per RPC.
*
*/
const GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW = globalStats.createView(
'grpc.io/client/received_messages_per_rpc',
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC, AggregationType.DISTRIBUTION,
[GRPC_CLIENT_METHOD],
'Distribution of received messages count per RPC, by method.',
DEFAULT_MESSAGE_COUNT_DISTRIBUTION);

/**
* {@link View} for client server latency in milliseconds.
*
*/
const GRPC_CLIENT_SERVER_LATENCY_VIEW = globalStats.createView(
'grpc.io/client/server_latency', GRPC_CLIENT_SERVER_LATENCY,
AggregationType.DISTRIBUTION, [GRPC_CLIENT_METHOD],
'Distribution of server latency as viewed by client, by method.',
DEFAULT_MILLI_SECONDS_DISTRIBUTION);
'Count of RPCs by method and status.', DEFAULT_MESSAGE_COUNT_DISTRIBUTION);

export const GRPC_BASIC_CLIENT_VIEWS: View[] = [
GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW, GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW,
GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, GRPC_CLIENT_COMPLETED_RPC_VIEW,
GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW,
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW, GRPC_CLIENT_SERVER_LATENCY_VIEW
GRPC_CLIENT_RECEIVED_MESSAGES_PER_RPC_VIEW,
GRPC_CLIENT_RECEIVED_BYTES_PER_RPC_VIEW,
GRPC_CLIENT_SENT_MESSAGES_PER_RPC_VIEW, GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW,
GRPC_CLIENT_ROUNDTRIP_LATENCY_VIEW, GRPC_CLIENT_COMPLETED_RPC_VIEW
];
Loading