-
Notifications
You must be signed in to change notification settings - Fork 63
/
monitoring.ts
297 lines (269 loc) · 9.17 KB
/
monitoring.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {
PushMetricExporter,
ResourceMetrics,
MetricData,
} from '@opentelemetry/sdk-metrics';
import {
ExportResult,
ExportResultCode,
VERSION as OT_VERSION,
} from '@opentelemetry/core';
import {ExporterOptions} from './external-types';
import {GoogleAuth, JWT} from 'google-auth-library';
// Import directly from this module instead of googleapis to improve bundler tree shaking
import {monitoring} from 'googleapis/build/src/apis/monitoring';
import type {monitoring_v3} from 'googleapis';
import {transformMetricDescriptor, createTimeSeries} from './transform';
import {MetricDescriptor, TimeSeries} from './types';
import {mountProjectIdPath, partitionList} from './utils';
import {diag} from '@opentelemetry/api';
import {mapOtelResourceToMonitoredResource} from '@google-cloud/opentelemetry-resource-util';
import {VERSION} from './version';
// Stackdriver Monitoring v3 only accepts up to 200 TimeSeries per
// CreateTimeSeries call.
const MAX_BATCH_EXPORT_SIZE = 200;
const OT_USER_AGENTS = [
{
product: 'opentelemetry-js',
version: OT_VERSION,
},
{
product: 'google-cloud-metric-exporter',
version: VERSION,
},
];
const OT_REQUEST_HEADER = {
'x-opentelemetry-outgoing-request': 0x1,
};
/**
* Format and sends metrics information to Google Cloud Monitoring.
*/
export class MetricExporter implements PushMetricExporter {
private _projectId: string | void | Promise<string | void>;
private readonly _metricPrefix: string;
private readonly _auth: GoogleAuth;
private readonly _disableCreateMetricDescriptors: boolean;
static readonly DEFAULT_METRIC_PREFIX: string = 'workload.googleapis.com';
/**
* Set of OTel metric names that have already had their metric descriptors successfully
* created
*/
private createdMetricDescriptors: Set<string> = new Set();
private _monitoring: monitoring_v3.Monitoring;
constructor(options: ExporterOptions = {}) {
this._metricPrefix = options.prefix ?? MetricExporter.DEFAULT_METRIC_PREFIX;
this._disableCreateMetricDescriptors =
!!options.disableCreateMetricDescriptors;
this._auth = new GoogleAuth({
credentials: options.credentials,
keyFile: options.keyFile,
keyFilename: options.keyFilename,
projectId: options.projectId,
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
this._monitoring = monitoring({
version: 'v3',
rootUrl:
'https://' + (options.apiEndpoint || 'monitoring.googleapis.com:443'),
headers: OT_REQUEST_HEADER,
userAgentDirectives: OT_USER_AGENTS.concat(
options.userAgent ? [options.userAgent] : []
),
});
// Start this async process as early as possible. It will be
// awaited on the first export because constructors are synchronous
this._projectId = this._auth.getProjectId().catch(err => {
diag.error(err);
});
}
/**
* Implementation for {@link PushMetricExporter.export}.
* Calls the async wrapper method {@link _exportAsync} and
* assures no rejected promises bubble up to the caller.
*
* @param metrics Metrics to be sent to the Google Cloud Monitoring backend
* @param resultCallback result callback to be called on finish
*/
export(
metrics: ResourceMetrics,
resultCallback: (result: ExportResult) => void
): void {
this._exportAsync(metrics).then(resultCallback, err => {
diag.error(err.message);
resultCallback({code: ExportResultCode.FAILED, error: err});
});
}
async shutdown(): Promise<void> {}
async forceFlush(): Promise<void> {}
/**
* Asnyc wrapper for the {@link export} implementation.
* Writes the current values of all exported {@link MetricRecord}s
* to the Google Cloud Monitoring backend.
*
* @param resourceMetrics Metrics to be sent to the Google Cloud Monitoring backend
*/
private async _exportAsync(
resourceMetrics: ResourceMetrics
): Promise<ExportResult> {
if (this._projectId instanceof Promise) {
this._projectId = await this._projectId;
}
if (!this._projectId) {
const error = new Error('expecting a non-blank ProjectID');
diag.error(error.message);
return {code: ExportResultCode.FAILED, error};
}
diag.debug('Google Cloud Monitoring export');
const resource = mapOtelResourceToMonitoredResource(
resourceMetrics.resource
);
const timeSeries: TimeSeries[] = [];
for (const scopeMetric of resourceMetrics.scopeMetrics) {
for (const metric of scopeMetric.metrics) {
const isRegistered =
this._disableCreateMetricDescriptors ||
(await this._registerMetricDescriptor(metric));
if (isRegistered) {
timeSeries.push(
...createTimeSeries(metric, resource, this._metricPrefix)
);
}
}
}
let failure: {sendFailed: false} | {sendFailed: true; error: Error} = {
sendFailed: false,
};
for (const batchedTimeSeries of partitionList(
timeSeries,
MAX_BATCH_EXPORT_SIZE
)) {
try {
await this._sendTimeSeries(batchedTimeSeries);
} catch (e) {
const err = asError(e);
err.message = `Send TimeSeries failed: ${err.message}`;
failure = {sendFailed: true, error: err};
diag.error(err.message);
}
}
if (failure.sendFailed) {
return {code: ExportResultCode.FAILED, error: failure.error};
}
return {code: ExportResultCode.SUCCESS};
}
/**
* Returns true if the given metricDescriptor is successfully registered to
* Google Cloud Monitoring, or the exact same metric has already been
* registered. Returns false otherwise and should be skipped.
*
* @param metric The OpenTelemetry MetricData.
*/
private async _registerMetricDescriptor(
metric: MetricData
): Promise<boolean> {
const isDescriptorCreated = this.createdMetricDescriptors.has(
metric.descriptor.name
);
if (isDescriptorCreated) {
return true;
}
const res = await this._createMetricDescriptorIfNeeded(metric);
if (res) {
this.createdMetricDescriptors.add(metric.descriptor.name);
return true;
}
return false;
}
/**
* Returns true if a descriptor already exists within the requested GCP project id;
* @param descriptor The metric descriptor to check
* @param projectIdPath The GCP project id path
* @param authClient The authenticated client which will be used to make the request
* @returns {boolean}
*/
private async _checkIfDescriptorExists(
descriptor: MetricDescriptor,
projectIdPath: string,
authClient: JWT
) {
try {
await this._monitoring.projects.metricDescriptors.get({
name: `${projectIdPath}/metricDescriptors/${descriptor.type}`,
auth: authClient,
});
return true;
} catch (error) {
return false;
}
}
/**
* Calls CreateMetricDescriptor in the GCM API for the given InstrumentDescriptor if needed
* @param metric The OpenTelemetry MetricData.
* @returns whether or not the descriptor was successfully created
*/
private async _createMetricDescriptorIfNeeded(
metric: MetricData
): Promise<boolean> {
const authClient = await this._authorize();
const descriptor = transformMetricDescriptor(metric, this._metricPrefix);
const projectIdPath = mountProjectIdPath(this._projectId as string);
try {
const descriptorExists = await this._checkIfDescriptorExists(
descriptor,
projectIdPath,
authClient
);
if (!descriptorExists) {
await this._monitoring.projects.metricDescriptors.create({
name: projectIdPath,
requestBody: descriptor,
auth: authClient,
});
diag.debug('sent metric descriptor', descriptor);
}
return true;
} catch (e) {
const err = asError(e);
diag.error('Failed to create metric descriptor: %s', err.message);
return false;
}
}
private async _sendTimeSeries(timeSeries: TimeSeries[]) {
if (timeSeries.length === 0) {
return Promise.resolve();
}
const authClient = await this._authorize();
await this._monitoring.projects.timeSeries.create({
name: mountProjectIdPath(this._projectId as string),
requestBody: {timeSeries},
auth: authClient,
});
diag.debug('sent time series', timeSeries);
}
/**
* Gets the Google Application Credentials from the environment variables
* and authenticates the client.
*/
private async _authorize(): Promise<JWT> {
return (await this._auth.getClient()) as JWT;
}
}
function asError(error: unknown): Error {
if (error instanceof Error) {
return error;
}
return new Error(String(error));
}