@mhubcloud/tms_core 3.7.2 → 3.8.0-beta.1
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/dist/index.js +64 -51
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -53,6 +53,7 @@ var import_service_bus = require("@azure/service-bus");
|
|
|
53
53
|
var utils_exports = {};
|
|
54
54
|
__export(utils_exports, {
|
|
55
55
|
EventPublisherBase: () => EventPublisherBase,
|
|
56
|
+
aiClient: () => aiClient,
|
|
56
57
|
encodeEmail: () => encodeEmail,
|
|
57
58
|
extractAdCode: () => extractAdCode,
|
|
58
59
|
extractClickId: () => extractClickId,
|
|
@@ -61,17 +62,37 @@ __export(utils_exports, {
|
|
|
61
62
|
flatten: () => flatten,
|
|
62
63
|
formatError: () => formatError,
|
|
63
64
|
handlePlatformRequest: () => handlePlatformRequest,
|
|
65
|
+
logAggregatedMetric: () => logAggregatedMetric,
|
|
64
66
|
logEvent: () => logEvent,
|
|
65
67
|
logException: () => logException,
|
|
66
68
|
logMessage: () => logMessage,
|
|
69
|
+
logMilestone: () => logMilestone,
|
|
67
70
|
prepareTestRequestHeaders: () => prepareTestRequestHeaders,
|
|
68
71
|
removeUndefined: () => removeUndefined
|
|
69
72
|
});
|
|
70
73
|
var crypto = __toESM(require("crypto"));
|
|
71
74
|
var appInsights = __toESM(require("applicationinsights"));
|
|
75
|
+
|
|
76
|
+
// src/utils/redisClient.ts
|
|
77
|
+
var import_redis = require("redis");
|
|
78
|
+
var client;
|
|
79
|
+
var port = process.env["RedisPort"] ?? "6380";
|
|
80
|
+
async function getRedisClient() {
|
|
81
|
+
if (!client) {
|
|
82
|
+
client = (0, import_redis.createClient)({
|
|
83
|
+
url: `rediss://${process.env["RedisUrl"]}:${port}`,
|
|
84
|
+
password: process.env["RedisPassword"],
|
|
85
|
+
socket: { tls: true }
|
|
86
|
+
});
|
|
87
|
+
await client.connect();
|
|
88
|
+
}
|
|
89
|
+
return client;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/utils/utils.ts
|
|
72
93
|
var aiClient = null;
|
|
73
94
|
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
|
|
74
|
-
appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, false).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false).setUseDiskRetryCaching(false).start();
|
|
95
|
+
appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, false).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false, false).setUseDiskRetryCaching(false).start();
|
|
75
96
|
aiClient = appInsights.defaultClient;
|
|
76
97
|
}
|
|
77
98
|
var EventPublisherBase = class {
|
|
@@ -95,6 +116,17 @@ var EventPublisherBase = class {
|
|
|
95
116
|
);
|
|
96
117
|
}
|
|
97
118
|
};
|
|
119
|
+
async function logRedisEvent(key) {
|
|
120
|
+
const redis = await getRedisClient();
|
|
121
|
+
if (!redis) return;
|
|
122
|
+
await redis.hIncrBy(key, "count", 1);
|
|
123
|
+
await redis.expire(key, 660);
|
|
124
|
+
}
|
|
125
|
+
function logAggregatedMetric(prefix, name, dimensions) {
|
|
126
|
+
const windowStart = Math.floor(Date.now() / 6e4) * 6e4;
|
|
127
|
+
const key = dimensions ? `${prefix}:${windowStart}:${dimensions}:${name}` : `${prefix}:${windowStart}:${name}`;
|
|
128
|
+
logRedisEvent(key);
|
|
129
|
+
}
|
|
98
130
|
function logMessage(level, message, properties) {
|
|
99
131
|
console.log(message);
|
|
100
132
|
aiClient?.trackTrace({
|
|
@@ -104,59 +136,40 @@ function logMessage(level, message, properties) {
|
|
|
104
136
|
});
|
|
105
137
|
}
|
|
106
138
|
function logEvent(publisher2, event, region, status, payload, id) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
aiClient?.trackMetric({
|
|
112
|
-
name: `DispatchSuccess`,
|
|
113
|
-
value: 1,
|
|
114
|
-
properties: {
|
|
115
|
-
publisher: publisher2,
|
|
116
|
-
event,
|
|
117
|
-
region
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
message = "Event dispatched to platform";
|
|
122
|
-
} else {
|
|
123
|
-
if (!process.env["DisableMetrics"]) {
|
|
124
|
-
aiClient?.trackMetric({
|
|
125
|
-
name: `DispatchFail`,
|
|
126
|
-
value: 1,
|
|
127
|
-
properties: {
|
|
128
|
-
publisher: publisher2,
|
|
129
|
-
event,
|
|
130
|
-
region
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
message = "Event dispatch failed";
|
|
135
|
-
}
|
|
136
|
-
} else {
|
|
137
|
-
message = "Event dispatched to platform";
|
|
139
|
+
if (!process.env["DisableMetrics"]) {
|
|
140
|
+
const metricRegion = process.env["DisableMetricRegionLogging"] ? "default" : region;
|
|
141
|
+
const name = status >= 200 && status < 400 ? "DispatchSuccess" : "DispatchFailure";
|
|
142
|
+
logAggregatedMetric("events", name, `${metricRegion}:${publisher2}:${event}`);
|
|
138
143
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
;
|
|
145
|
+
if (!process.env["DisableTraces"]) {
|
|
146
|
+
let properties = {
|
|
147
|
+
publisher: publisher2,
|
|
148
|
+
event,
|
|
149
|
+
region,
|
|
150
|
+
id,
|
|
151
|
+
status,
|
|
152
|
+
payload
|
|
153
|
+
};
|
|
154
|
+
const message = status >= 200 && status < 400 ? "Event dispatched to platform" : "Event dispatch failed";
|
|
155
|
+
properties = removeUndefined(properties);
|
|
156
|
+
logMessage(1, message, properties);
|
|
157
|
+
}
|
|
158
|
+
;
|
|
149
159
|
}
|
|
150
160
|
function logException(exception, publisher2) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
161
|
+
if (!process.env["DisableMetrics"]) {
|
|
162
|
+
const message = exception.message.split(".")[0]?.substring(0, 100).replace(/:/g, "");
|
|
163
|
+
logAggregatedMetric("errors", exception.name, `${publisher2 ?? "Other"}:${message}`);
|
|
164
|
+
}
|
|
165
|
+
;
|
|
166
|
+
if (!process.env["DisableTraces"]) {
|
|
167
|
+
aiClient?.trackException({ exception });
|
|
168
|
+
}
|
|
169
|
+
;
|
|
170
|
+
}
|
|
171
|
+
function logMilestone(name, value) {
|
|
172
|
+
logAggregatedMetric("milestones", String(value), name);
|
|
160
173
|
}
|
|
161
174
|
function removeUndefined(originalValue) {
|
|
162
175
|
if (originalValue instanceof Array) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mhubcloud/tms_core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0-beta.1",
|
|
4
4
|
"description": "Core function for mHub Cloud TMS publishers",
|
|
5
5
|
"homepage": "https://mhubcloud.com/",
|
|
6
6
|
"license": "Apache 2.0",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"@azure/data-tables": "^13.2.2",
|
|
26
26
|
"@azure/service-bus": "^7.9.5",
|
|
27
27
|
"ajv": "^8.12.0",
|
|
28
|
-
"applicationinsights": "^2.
|
|
28
|
+
"applicationinsights": "^2.9.8",
|
|
29
|
+
"redis": "^6.1.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@azure/functions": "^3.5.1",
|
|
@@ -44,4 +45,4 @@
|
|
|
44
45
|
"tsup": "^8.5.0",
|
|
45
46
|
"typescript": "^4.9.5"
|
|
46
47
|
}
|
|
47
|
-
}
|
|
48
|
+
}
|