Skip to content

Commit 2781561

Browse files
committed
Change metrics handler interface to support each metric
1 parent 2910408 commit 2781561

File tree

5 files changed

+170
-116
lines changed

5 files changed

+170
-116
lines changed

common/client-side-metrics-attributes.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export enum StreamingState {
3838
* Attributes associated with operation latency metrics for Bigtable client operations.
3939
* These attributes provide context about the Bigtable environment and the completed operation.
4040
*/
41-
interface OperationLatencyAttributes extends StandardAttributes {
41+
export interface OperationLatencyAttributes extends StandardAttributes {
4242
finalOperationStatus: FinalOperationStatus;
4343
streamingOperation: StreamingState;
4444
}
@@ -47,7 +47,7 @@ interface OperationLatencyAttributes extends StandardAttributes {
4747
* Attributes associated with attempt latency metrics for Bigtable client operations.
4848
* These attributes provide context about the Bigtable environment, the specific attempt, and whether the operation was streaming.
4949
*/
50-
interface AttemptLatencyAttributes extends StandardAttributes {
50+
export interface AttemptLatencyAttributes extends StandardAttributes {
5151
attemptStatus: AttemptStatus;
5252
streamingOperation: StreamingState;
5353
}
@@ -56,7 +56,7 @@ interface AttemptLatencyAttributes extends StandardAttributes {
5656
* Attributes associated with retry count metrics for Bigtable client operations. These attributes
5757
* provide context about the Bigtable environment and the final status of the operation.
5858
*/
59-
interface RetryCountAttributes extends StandardAttributes {
59+
export interface RetryCountAttributes extends StandardAttributes {
6060
finalOperationStatus: FinalOperationStatus;
6161
}
6262

@@ -70,15 +70,15 @@ type ApplicationBlockingLatenciesAttributes = StandardAttributes;
7070
* Attributes associated with first response latency metrics for Bigtable client operations.
7171
* These attributes provide context about the Bigtable environment and the final status of the operation.
7272
*/
73-
interface FirstResponseLatencyAttributes extends StandardAttributes {
73+
export interface FirstResponseLatencyAttributes extends StandardAttributes {
7474
finalOperationStatus: FinalOperationStatus;
7575
}
7676

7777
/**
7878
* Attributes associated with server latency metrics for Bigtable client operations.
7979
* These attributes provide context about the Bigtable environment, the specific attempt, and whether the operation was streaming.
8080
*/
81-
interface ServerLatenciesAttributes extends StandardAttributes {
81+
export interface ServerLatenciesAttributes extends StandardAttributes {
8282
attemptStatus: AttemptStatus;
8383
streamingOperation: StreamingState;
8484
}
@@ -87,7 +87,7 @@ interface ServerLatenciesAttributes extends StandardAttributes {
8787
* Attributes associated with connectivity error count metrics for Bigtable client operations.
8888
* These attributes provide context about the Bigtable environment and the status of the attempt.
8989
*/
90-
interface ConnectivityErrorCountAttributes extends StandardAttributes {
90+
export interface ConnectivityErrorCountAttributes extends StandardAttributes {
9191
attemptStatus: AttemptStatus;
9292
}
9393

@@ -118,10 +118,9 @@ export type AttemptStatus = grpc.status;
118118
* operation, and its final status. They are used for recording metrics such as
119119
* operation latency, first response latency, and retry count.
120120
*/
121-
export type OnOperationCompleteAttributes =
122-
| OperationLatencyAttributes
123-
| FirstResponseLatencyAttributes
124-
| RetryCountAttributes;
121+
export type OnOperationCompleteAttributes = OperationLatencyAttributes &
122+
FirstResponseLatencyAttributes &
123+
RetryCountAttributes;
125124

126125
/**
127126
* Attributes associated with the completion of a single attempt of a Bigtable
@@ -130,11 +129,10 @@ export type OnOperationCompleteAttributes =
130129
* are used for recording metrics such as attempt latency, server latency, and
131130
* connectivity errors.
132131
*/
133-
export type OnAttemptCompleteAttributes =
134-
| AttemptLatencyAttributes
135-
| ConnectivityErrorCountAttributes
136-
| ServerLatenciesAttributes
137-
| ClientBlockingLatenciesAttributes;
132+
export type OnAttemptCompleteAttributes = AttemptLatencyAttributes &
133+
ConnectivityErrorCountAttributes &
134+
ServerLatenciesAttributes &
135+
ClientBlockingLatenciesAttributes;
138136

139137
/**
140138
* Represents the names of Bigtable methods. These are used as attributes for

common/test-metrics-handler.ts

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,69 @@
1414

1515
import {WithLogger} from './logger';
1616
import {
17-
OnAttemptCompleteMetrics,
18-
OnOperationCompleteMetrics,
19-
} from '../src/client-side-metrics/metrics-handler';
20-
import {
21-
OnAttemptCompleteAttributes,
22-
OnOperationCompleteAttributes,
17+
AttemptLatencyAttributes,
18+
ConnectivityErrorCountAttributes,
19+
FirstResponseLatencyAttributes,
20+
OperationLatencyAttributes,
21+
RetryCountAttributes,
22+
ServerLatenciesAttributes,
2323
} from './client-side-metrics-attributes';
2424

2525
/**
2626
* A test implementation of the IMetricsHandler interface. Used for testing purposes.
2727
* It logs the metrics and attributes received by the onOperationComplete and onAttemptComplete methods.
2828
*/
2929
export class TestMetricsHandler extends WithLogger {
30-
/**
31-
* Logs the metrics and attributes received for an operation completion.
32-
* @param {OnOperationCompleteMetrics} metrics Metrics related to the completed operation.
33-
* @param {Attributes} attributes Attributes associated with the completed operation.
34-
*/
35-
onOperationComplete(
36-
metrics: OnOperationCompleteMetrics,
37-
attributes: OnOperationCompleteAttributes
30+
onRecordAttemptLatency(
31+
attemptLatency: number,
32+
attributes: AttemptLatencyAttributes
33+
) {
34+
this.logger.log(
35+
`Recording parameters for AttemptLatency: ${attemptLatency}:`
36+
);
37+
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
38+
}
39+
40+
onRecordConnectivityErrorCount(
41+
connectivityErrorCount: number,
42+
attributes: ConnectivityErrorCountAttributes
43+
) {
44+
this.logger.log(
45+
`Recording parameters for ConnectivityErrorCount: ${connectivityErrorCount}:`
46+
);
47+
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
48+
}
49+
50+
onRecordServerLatency(
51+
serverLatency: number,
52+
attributes: ServerLatenciesAttributes
53+
) {
54+
this.logger.log(`Recording parameters for ServerLatency: ${serverLatency}`);
55+
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
56+
}
57+
58+
onRecordOperationLatency(
59+
operationLatency: number,
60+
attributes: OperationLatencyAttributes
3861
) {
39-
attributes.clientName = 'nodejs-bigtable';
40-
this.logger.log('Recording parameters for onOperationComplete:');
41-
this.logger.log(`metrics: ${JSON.stringify(metrics)}`);
62+
this.logger.log(
63+
`Recording parameters for OperationLatency: ${operationLatency}`
64+
);
65+
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
66+
}
67+
68+
onRecordRetryCount(retryCount: number, attributes: RetryCountAttributes) {
69+
this.logger.log(`Recording parameters for RetryCount: ${retryCount}`);
4270
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
4371
}
4472

45-
/**
46-
* Logs the metrics and attributes received for an attempt completion.
47-
* @param {OnAttemptCompleteMetrics} metrics Metrics related to the completed attempt.
48-
* @param {Attributes} attributes Attributes associated with the completed attempt.
49-
*/
50-
onAttemptComplete(
51-
metrics: OnAttemptCompleteMetrics,
52-
attributes: OnAttemptCompleteAttributes
73+
onRecordFirstResponseLatency(
74+
firstResponseLatency: number,
75+
attributes: FirstResponseLatencyAttributes
5376
) {
54-
attributes.clientName = 'nodejs-bigtable';
55-
this.logger.log('Recording parameters for onAttemptComplete:');
56-
this.logger.log(`metrics: ${JSON.stringify(metrics)}`);
77+
this.logger.log(
78+
`Recording parameters for FirstResponseLatency: ${firstResponseLatency}`
79+
);
5780
this.logger.log(`attributes: ${JSON.stringify(attributes)}`);
5881
}
5982
}

src/client-side-metrics/gcp-metrics-handler.ts

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {
16-
IMetricsHandler,
17-
OnAttemptCompleteMetrics,
18-
OnOperationCompleteMetrics,
19-
} from './metrics-handler';
15+
import {IMetricsHandler} from './metrics-handler';
2016
import * as Resources from '@opentelemetry/resources';
2117
import * as ResourceUtil from '@google-cloud/opentelemetry-resource-util';
2218
import {MetricExporter} from '@google-cloud/opentelemetry-cloud-monitoring-exporter';
2319
import {
24-
OnAttemptCompleteAttributes,
25-
OnOperationCompleteAttributes,
20+
AttemptLatencyAttributes,
21+
ConnectivityErrorCountAttributes,
22+
FirstResponseLatencyAttributes,
23+
OperationLatencyAttributes,
24+
RetryCountAttributes,
25+
ServerLatenciesAttributes,
2626
} from '../../common/client-side-metrics-attributes';
2727
import {View} from '@opentelemetry/sdk-metrics';
2828
const {
@@ -161,48 +161,54 @@ export class GCPMetricsHandler implements IMetricsHandler {
161161
}
162162
}
163163

164-
/**
165-
* Records metrics for a completed Bigtable operation.
166-
* This method records the operation latency and retry count, associating them with provided attributes.
167-
* @param {OnOperationCompleteMetrics} metrics Metrics related to the completed operation.
168-
* @param {OnOperationCompleteAttributes} attributes Attributes associated with the completed operation.
169-
*/
170-
onOperationComplete(
171-
metrics: OnOperationCompleteMetrics,
172-
attributes: OnOperationCompleteAttributes
164+
onRecordAttemptLatency(
165+
attemptLatency: number,
166+
attributes: AttemptLatencyAttributes
173167
) {
174168
this.initialize();
175-
this.otelMetrics?.operationLatencies.record(
176-
metrics.operationLatency,
177-
attributes
178-
);
179-
this.otelMetrics?.retryCount.add(metrics.retryCount, attributes);
180-
this.otelMetrics?.firstResponseLatencies.record(
181-
metrics.firstResponseLatency,
182-
attributes
183-
);
169+
this.otelMetrics?.attemptLatencies.record(attemptLatency, attributes);
184170
}
185171

186-
/**
187-
* Records metrics for a completed attempt of a Bigtable operation.
188-
* This method records attempt latency, connectivity error count, server latency, and first response latency,
189-
* along with the provided attributes.
190-
* @param {OnAttemptCompleteMetrics} metrics Metrics related to the completed attempt.
191-
* @param {OnAttemptCompleteAttributes} attributes Attributes associated with the completed attempt.
192-
*/
193-
onAttemptComplete(
194-
metrics: OnAttemptCompleteMetrics,
195-
attributes: OnAttemptCompleteAttributes
172+
onRecordConnectivityErrorCount(
173+
connectivityErrorCount: number,
174+
attributes: ConnectivityErrorCountAttributes
196175
) {
197176
this.initialize();
198-
this.otelMetrics?.attemptLatencies.record(
199-
metrics.attemptLatency,
177+
this.otelMetrics?.connectivityErrorCount.record(
178+
connectivityErrorCount,
200179
attributes
201180
);
202-
this.otelMetrics?.connectivityErrorCount.record(
203-
metrics.connectivityErrorCount,
181+
}
182+
183+
onRecordServerLatency(
184+
serverLatency: number,
185+
attributes: ServerLatenciesAttributes
186+
) {
187+
this.initialize();
188+
this.otelMetrics?.serverLatencies.record(serverLatency, attributes);
189+
}
190+
191+
onRecordOperationLatency(
192+
operationLatency: number,
193+
attributes: OperationLatencyAttributes
194+
) {
195+
this.initialize();
196+
this.otelMetrics?.operationLatencies.record(operationLatency, attributes);
197+
}
198+
199+
onRecordRetryCount(retryCount: number, attributes: RetryCountAttributes) {
200+
this.initialize();
201+
this.otelMetrics?.retryCount.add(retryCount, attributes);
202+
}
203+
204+
onRecordFirstResponseLatency(
205+
firstResponseLatency: number,
206+
attributes: FirstResponseLatencyAttributes
207+
) {
208+
this.initialize();
209+
this.otelMetrics?.firstResponseLatencies.record(
210+
firstResponseLatency,
204211
attributes
205212
);
206-
this.otelMetrics?.serverLatencies.record(metrics.serverLatency, attributes);
207213
}
208214
}

src/client-side-metrics/metrics-handler.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
// limitations under the License.
1414

1515
import {
16-
OnAttemptCompleteAttributes,
17-
OnOperationCompleteAttributes,
16+
AttemptLatencyAttributes,
17+
ConnectivityErrorCountAttributes,
18+
FirstResponseLatencyAttributes,
19+
OperationLatencyAttributes,
20+
RetryCountAttributes,
21+
ServerLatenciesAttributes,
1822
} from '../../common/client-side-metrics-attributes';
1923

2024
/**
@@ -46,22 +50,33 @@ export interface OnAttemptCompleteMetrics {
4650
* Implementations of this interface can define how metrics are recorded and processed.
4751
*/
4852
export interface IMetricsHandler {
49-
/**
50-
* Called when an operation completes (successfully or unsuccessfully).
51-
* @param {OnOperationCompleteMetrics} metrics Metrics related to the completed operation.
52-
* @param {OnOperationCompleteAttributes} attributes Attributes associated with the completed operation.
53-
*/
54-
onOperationComplete?(
55-
metrics: OnOperationCompleteMetrics,
56-
attributes: OnOperationCompleteAttributes
53+
onRecordAttemptLatency?(
54+
attemptLatency: number,
55+
attributes: AttemptLatencyAttributes
5756
): void;
58-
/**
59-
* Called when an attempt (e.g., an RPC attempt) completes.
60-
* @param {OnAttemptCompleteMetrics} metrics Metrics related to the completed attempt.
61-
* @param {OnAttemptCompleteAttributes} attributes Attributes associated with the completed attempt.
62-
*/
63-
onAttemptComplete?(
64-
metrics: OnAttemptCompleteMetrics,
65-
attributes: OnAttemptCompleteAttributes
57+
58+
onRecordConnectivityErrorCount?(
59+
connectivityErrorCount: number,
60+
attributes: ConnectivityErrorCountAttributes
61+
): void;
62+
63+
onRecordServerLatency?(
64+
serverLatency: number,
65+
attributes: ServerLatenciesAttributes
66+
): void;
67+
68+
onRecordOperationLatency?(
69+
operationLatency: number,
70+
attributes: OperationLatencyAttributes
71+
): void;
72+
73+
onRecordRetryCount?(
74+
retryCount: number,
75+
attributes: RetryCountAttributes
76+
): void;
77+
78+
onRecordFirstResponseLatency?(
79+
firstResponseLatency: number,
80+
attributes: FirstResponseLatencyAttributes
6681
): void;
6782
}

0 commit comments

Comments
 (0)