@payfit/audit-log 3.13.0 → 3.14.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 +1 -1
- package/dist/index.mjs +34 -29
- package/dist/transport/implementation.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var w=(s,e,t)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var
|
|
1
|
+
"use strict";var f=Object.defineProperty;var w=(s,e,t)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var i=(s,e,t)=>(w(s,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("uuid"),o=require("winston"),S=require("hot-shots"),h=require("@aws-sdk/client-sqs"),y=require("zlib"),E="audit-logs",_="@payfit/audit-log",q=()=>{const s=process.env.LOG_LEVEL;return s&&s.trim()!==""?s.trim():"info"},L=(s,e)=>o.createLogger({level:q(),silent:process.env.NODE_ENV==="test",exitOnError:!1,defaultMeta:{namespace:E,sdk:_,service:s,environment:e},format:o.format.combine(o.format.timestamp(),o.format.errors({stack:!0}),o.format.json()),transports:[new o.transports.Console]}),M="payfit.audit_logs.",p=s=>Object.entries(s).filter(([,e])=>e!==void 0&&e!=="").map(([e,t])=>`${e}:${String(t)}`);class b{constructor(){i(this,"client");this.client=new S.StatsD({prefix:M,errorHandler:()=>{},cacheDns:!0})}increment(e,t){this.client.increment(e,1,p(t))}histogram(e,t,n){this.client.histogram(e,t,p(n))}async close(){await new Promise(e=>{this.client.close(()=>e())})}}const A=()=>new b;function U(s){return y.brotliCompressSync(Buffer.from(s,"utf8")).toString("base64")}function C(s){return s instanceof Error&&s.message.includes("Message must be shorter than")}const O="eu-west-3",z={dev:"https://sqs.eu-west-3.amazonaws.com/533547282673/audit-logs-ingest",staging:"https://sqs.eu-west-3.amazonaws.com/589883667243/audit-logs-ingest",prod:"https://sqs.eu-west-3.amazonaws.com/614948436281/audit-logs-ingest"},D={working:"staging",production:"prod",development:"dev"};class N{constructor(e,t){i(this,"environment");i(this,"logger");i(this,"client");i(this,"queueUrl");this.environment=e,this.logger=t;const n=D[e]??e;if(this.queueUrl=z[n],!this.queueUrl){t==null||t.warn("audit-logs SDK: no SQS queue URL configured for this environment, audit logs are disabled",{environment:e});return}this.client=new h.SQSClient({region:O})}async create(e){var n;if(!this.client||!this.queueUrl)return{compressed:!1};const t=JSON.stringify(e);try{return await this.send(t),{compressed:!1}}catch(c){if(!C(c))throw c;return(n=this.logger)==null||n.info("audit-log payload exceeds the SQS size limit, retrying with Brotli compression",{id:e.id,namespace:e.namespace}),await this.send(U(t)),{compressed:!0}}}async send(e){await this.client.send(new h.SendMessageCommand({QueueUrl:this.queueUrl,MessageBody:e}))}}class Q{constructor(e,t={}){i(this,"params");i(this,"transport");i(this,"logger");i(this,"metrics");this.params=e,this.logger=t.logger??L(e.service,e.environment),this.transport=t.transport??new N(e.environment,this.logger),this.metrics=t.metrics??A()}async log({scope:e,account:t,...n}){const c=v.v4(),{environment:g,service:u}=this.params,r={...n,...t,service:u,id:c,environment:g,...e&&{scope:e}},a={service:u,environment:g,namespace:r.namespace,event:r.event,target:r.target},m=Date.now();try{const{compressed:l}=await this.transport.create(r);this.metrics.increment("sent",{...a,compressed:String(l)}),this.metrics.histogram("send_duration_ms",Date.now()-m,a),this.logger.debug("audit-log sent",{id:r.id,event:r.event,target:r.target})}catch(l){this.metrics.increment("send_failed",a),this.metrics.histogram("send_duration_ms",Date.now()-m,a);const d=l.message;try{this.logger.error("audit-log write failed",{audit_log:r,error:d})}catch{this.metrics.increment("serialization_failed",a);try{this.logger.error("audit-log write failed (serialization failed)",{audit_log_meta:{id:r.id,service:r.service,environment:r.environment,namespace:r.namespace,event:r.event,target:r.target},error:d})}catch{}}}}}exports.AuditLogger=Q;
|
package/dist/index.mjs
CHANGED
|
@@ -6,11 +6,11 @@ import { createLogger as v, format as c, transports as S } from "winston";
|
|
|
6
6
|
import { StatsD as E } from "hot-shots";
|
|
7
7
|
import { SQSClient as _, SendMessageCommand as y } from "@aws-sdk/client-sqs";
|
|
8
8
|
import { brotliCompressSync as L } from "zlib";
|
|
9
|
-
const M = "audit-logs", A = "@payfit/audit-log",
|
|
9
|
+
const M = "audit-logs", A = "@payfit/audit-log", U = () => {
|
|
10
10
|
const s = process.env.LOG_LEVEL;
|
|
11
11
|
return s && s.trim() !== "" ? s.trim() : "info";
|
|
12
|
-
},
|
|
13
|
-
level:
|
|
12
|
+
}, q = (s, e) => v({
|
|
13
|
+
level: U(),
|
|
14
14
|
silent: process.env.NODE_ENV === "test",
|
|
15
15
|
exitOnError: !1,
|
|
16
16
|
defaultMeta: {
|
|
@@ -21,12 +21,12 @@ const M = "audit-logs", A = "@payfit/audit-log", C = () => {
|
|
|
21
21
|
},
|
|
22
22
|
format: c.combine(c.timestamp(), c.errors({ stack: !0 }), c.json()),
|
|
23
23
|
transports: [new S.Console()]
|
|
24
|
-
}),
|
|
24
|
+
}), C = "payfit.audit_logs.", h = (s) => Object.entries(s).filter(([, e]) => e !== void 0 && e !== "").map(([e, t]) => `${e}:${String(t)}`);
|
|
25
25
|
class b {
|
|
26
26
|
constructor() {
|
|
27
27
|
i(this, "client");
|
|
28
28
|
this.client = new E({
|
|
29
|
-
prefix:
|
|
29
|
+
prefix: C,
|
|
30
30
|
// Loss of metrics never breaks the caller — DogStatsD UDP is best-effort by design.
|
|
31
31
|
errorHandler: () => {
|
|
32
32
|
},
|
|
@@ -58,7 +58,8 @@ const z = "eu-west-3", Q = {
|
|
|
58
58
|
prod: "https://sqs.eu-west-3.amazonaws.com/614948436281/audit-logs-ingest"
|
|
59
59
|
}, R = {
|
|
60
60
|
working: "staging",
|
|
61
|
-
production: "prod"
|
|
61
|
+
production: "prod",
|
|
62
|
+
development: "dev"
|
|
62
63
|
};
|
|
63
64
|
class k {
|
|
64
65
|
constructor(e, t) {
|
|
@@ -67,19 +68,23 @@ class k {
|
|
|
67
68
|
i(this, "client");
|
|
68
69
|
i(this, "queueUrl");
|
|
69
70
|
this.environment = e, this.logger = t;
|
|
70
|
-
const n = R[e] ?? e
|
|
71
|
-
if (!
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
const n = R[e] ?? e;
|
|
72
|
+
if (this.queueUrl = Q[n], !this.queueUrl) {
|
|
73
|
+
t == null || t.warn("audit-logs SDK: no SQS queue URL configured for this environment, audit logs are disabled", { environment: e });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
this.client = new _({ region: z });
|
|
74
77
|
}
|
|
75
78
|
async create(e) {
|
|
76
79
|
var n;
|
|
80
|
+
if (!this.client || !this.queueUrl)
|
|
81
|
+
return { compressed: !1 };
|
|
77
82
|
const t = JSON.stringify(e);
|
|
78
83
|
try {
|
|
79
84
|
return await this.send(t), { compressed: !1 };
|
|
80
|
-
} catch (
|
|
81
|
-
if (!O(
|
|
82
|
-
throw
|
|
85
|
+
} catch (a) {
|
|
86
|
+
if (!O(a))
|
|
87
|
+
throw a;
|
|
83
88
|
return (n = this.logger) == null || n.info("audit-log payload exceeds the SQS size limit, retrying with Brotli compression", { id: e.id, namespace: e.namespace }), await this.send(N(t)), { compressed: !0 };
|
|
84
89
|
}
|
|
85
90
|
}
|
|
@@ -96,43 +101,43 @@ class V {
|
|
|
96
101
|
i(this, "transport");
|
|
97
102
|
i(this, "logger");
|
|
98
103
|
i(this, "metrics");
|
|
99
|
-
this.params = e, this.logger = t.logger ??
|
|
104
|
+
this.params = e, this.logger = t.logger ?? q(e.service, e.environment), this.transport = t.transport ?? new k(e.environment, this.logger), this.metrics = t.metrics ?? D();
|
|
100
105
|
}
|
|
101
106
|
async log({ scope: e, account: t, ...n }) {
|
|
102
|
-
const
|
|
107
|
+
const a = w(), { environment: l, service: g } = this.params, r = {
|
|
103
108
|
...n,
|
|
104
109
|
...t,
|
|
105
|
-
service:
|
|
106
|
-
id:
|
|
107
|
-
environment:
|
|
110
|
+
service: g,
|
|
111
|
+
id: a,
|
|
112
|
+
environment: l,
|
|
108
113
|
...e && { scope: e }
|
|
109
|
-
},
|
|
110
|
-
service:
|
|
111
|
-
environment:
|
|
114
|
+
}, o = {
|
|
115
|
+
service: g,
|
|
116
|
+
environment: l,
|
|
112
117
|
namespace: r.namespace,
|
|
113
118
|
event: r.event,
|
|
114
119
|
target: r.target
|
|
115
120
|
}, d = Date.now();
|
|
116
121
|
try {
|
|
117
|
-
const { compressed:
|
|
122
|
+
const { compressed: m } = await this.transport.create(r);
|
|
118
123
|
this.metrics.increment("sent", {
|
|
119
|
-
...
|
|
120
|
-
compressed: String(
|
|
121
|
-
}), this.metrics.histogram("send_duration_ms", Date.now() - d,
|
|
124
|
+
...o,
|
|
125
|
+
compressed: String(m)
|
|
126
|
+
}), this.metrics.histogram("send_duration_ms", Date.now() - d, o), this.logger.debug("audit-log sent", {
|
|
122
127
|
id: r.id,
|
|
123
128
|
event: r.event,
|
|
124
129
|
target: r.target
|
|
125
130
|
});
|
|
126
|
-
} catch (
|
|
127
|
-
this.metrics.increment("send_failed",
|
|
128
|
-
const u =
|
|
131
|
+
} catch (m) {
|
|
132
|
+
this.metrics.increment("send_failed", o), this.metrics.histogram("send_duration_ms", Date.now() - d, o);
|
|
133
|
+
const u = m.message;
|
|
129
134
|
try {
|
|
130
135
|
this.logger.error("audit-log write failed", {
|
|
131
136
|
audit_log: r,
|
|
132
137
|
error: u
|
|
133
138
|
});
|
|
134
139
|
} catch {
|
|
135
|
-
this.metrics.increment("serialization_failed",
|
|
140
|
+
this.metrics.increment("serialization_failed", o);
|
|
136
141
|
try {
|
|
137
142
|
this.logger.error("audit-log write failed (serialization failed)", {
|
|
138
143
|
audit_log_meta: {
|
|
@@ -3,8 +3,8 @@ import { IAuditLogCreationData, IAuditLogCreationResult, IAuditLogTransport } fr
|
|
|
3
3
|
export declare class Transport implements IAuditLogTransport {
|
|
4
4
|
protected environment: string;
|
|
5
5
|
private logger?;
|
|
6
|
-
private client;
|
|
7
|
-
private queueUrl;
|
|
6
|
+
private readonly client;
|
|
7
|
+
private readonly queueUrl;
|
|
8
8
|
constructor(environment: string, logger?: Logger | undefined);
|
|
9
9
|
create(auditLogData: IAuditLogCreationData): Promise<IAuditLogCreationResult>;
|
|
10
10
|
private send;
|