@emartech/aggregate-metric-logger 2.1.0 → 2.1.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emartech/aggregate-metric-logger",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "aggregate metrics in memory for logging",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const logger = require('@emartech/json-logger')('aggregate-metric-logger');
3
4
  const monitoring = require('@google-cloud/monitoring');
4
- const client = new monitoring.MetricServiceClient();
5
5
 
6
6
  exports.publishCountMetrics = async (metrics, startTime) => {
7
7
  const timeSeries = Object.values(metrics).map(metric => ({
@@ -25,8 +25,24 @@ exports.publishCountMetrics = async (metrics, startTime) => {
25
25
  metricKind: 'CUMULATIVE',
26
26
  valueType: 'int64Value'
27
27
  }));
28
- await client.createTimeSeries({
29
- name: client.projectPath(process.env.GCP_PROJECT_ID),
30
- timeSeries
31
- });
28
+ try {
29
+ const client = getMonitoringClient();
30
+ await client.createTimeSeries({
31
+ name: client.projectPath(process.env.GCP_PROJECT_ID),
32
+ timeSeries
33
+ });
34
+ } catch (error) {
35
+ logger.warnFromError('pushing-metrics-failed', error);
36
+ }
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;
32
48
  };