@emartech/aggregate-metric-logger 2.1.1 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/gcp-metric-publisher.js +13 -2
package/package.json
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const logger = require('@emartech/json-logger')('aggregate-metric-logger');
|
|
4
4
|
const monitoring = require('@google-cloud/monitoring');
|
|
5
|
-
const client = new monitoring.MetricServiceClient();
|
|
6
5
|
|
|
7
6
|
exports.publishCountMetrics = async (metrics, startTime) => {
|
|
8
7
|
const timeSeries = Object.values(metrics).map(metric => ({
|
|
@@ -27,11 +26,23 @@ exports.publishCountMetrics = async (metrics, startTime) => {
|
|
|
27
26
|
valueType: 'int64Value'
|
|
28
27
|
}));
|
|
29
28
|
try {
|
|
29
|
+
const client = getMonitoringClient();
|
|
30
30
|
await client.createTimeSeries({
|
|
31
31
|
name: client.projectPath(process.env.GCP_PROJECT_ID),
|
|
32
32
|
timeSeries
|
|
33
33
|
});
|
|
34
34
|
} catch (error) {
|
|
35
|
-
logger.warnFromError('pushing-metrics-failed', error);
|
|
35
|
+
logger.warnFromError('pushing-metrics-failed', error, { do_not_alert: true });
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
+
|
|
39
|
+
let monitoringClientInstance = null;
|
|
40
|
+
const getMonitoringClient = () => {
|
|
41
|
+
if (!monitoringClientInstance) {
|
|
42
|
+
monitoringClientInstance = new monitoring.MetricServiceClient({
|
|
43
|
+
projectId: process.env.GCP_PROJECT_ID,
|
|
44
|
+
credentials: JSON.parse(process.env.GCP_CREDENTIALS || '{}')
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return monitoringClientInstance;
|
|
48
|
+
};
|