@payfit/audit-log 3.5.0-rc.22 → 3.5.0-rc.24
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 +19 -15
- package/dist/metrics.d.ts +2 -1
- package/dist/transport/implementation.d.ts +2 -2
- package/dist/transport/interface.d.ts +4 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";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 n=(s,e,t)=>(w(s,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("uuid"),a=require("winston"),v=require("hot-shots"),h=require("@aws-sdk/client-sqs"),E=require("zlib"),y="audit-logs",_="@payfit/audit-log",q=()=>{const s=process.env.LOG_LEVEL;return s&&s.trim()!==""?s.trim():"info"},L=(s,e)=>a.createLogger({level:q(),silent:process.env.NODE_ENV==="test",exitOnError:!1,defaultMeta:{namespace:y,sdk:_,service:s,environment:e},format:a.format.combine(a.format.timestamp(),a.format.errors({stack:!0}),a.format.json()),transports:[new a.transports.Console]}),M="payfit.audit_logs.",p=s=>Object.entries(s).filter(([,e])=>e!==void 0&&e!=="").map(([e,t])=>`${e}:${String(t)}`);class A{constructor(){n(this,"client");this.client=new v.StatsD({prefix:M,errorHandler:()=>{},cacheDns:!0})}increment(e,t){this.client.increment(e,1,p(t))}histogram(e,t,i){this.client.histogram(e,t,p(i))}async close(){await new Promise(e=>{this.client.close(()=>e())})}}const b=()=>new A;function C(s){return E.brotliCompressSync(Buffer.from(s,"utf8")).toString("base64")}function U(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"};class N{constructor(e,t){n(this,"environment");n(this,"logger");n(this,"client");n(this,"queueUrl");this.environment=e,this.logger=t;const i=D[e]??e,o=z[i];if(!o)throw new Error(`audit-logs SDK: no SQS queue URL configured for environment "${e}"`);this.queueUrl=o,this.client=new h.SQSClient({region:O})}async create(e){var i;const t=JSON.stringify(e);try{return await this.send(t),{compressed:!1}}catch(o){if(!U(o))throw o;return(i=this.logger)==null||i.info("audit-log payload exceeds the SQS size limit, retrying with Brotli compression",{id:e.id,namespace:e.namespace}),await this.send(C(t)),{compressed:!0}}}async send(e){await this.client.send(new h.SendMessageCommand({QueueUrl:this.queueUrl,MessageBody:e}))}}class Q{constructor(e,t={}){n(this,"params");n(this,"transport");n(this,"logger");n(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??b()}async log({scope:e,account:t,...i}){const o=S.v4(),{environment:l,service:m}=this.params,r={...i,...t,service:m,id:o,environment:l,...e&&{scope:e}},c={service:m,environment:l,namespace:r.namespace,event:r.event,target:r.target},u=Date.now();try{const{compressed:g}=await this.transport.create(r);this.metrics.increment("sent",{...c,compressed:String(g)}),this.metrics.histogram("send_duration_ms",Date.now()-u,c),this.logger.debug("audit-log sent",{id:r.id,event:r.event,target:r.target})}catch(g){this.metrics.increment("send_failed",c),this.metrics.histogram("send_duration_ms",Date.now()-u,c);const d=g.message;try{this.logger.error("audit-log write failed",{audit_log:r,error:d})}catch{this.metrics.increment("serialization_failed",c);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
|
@@ -21,7 +21,7 @@ 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
|
-
}), q = "payfit.
|
|
24
|
+
}), q = "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");
|
|
@@ -34,10 +34,10 @@ class b {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
increment(e, t) {
|
|
37
|
-
this.client.increment(e, 1,
|
|
37
|
+
this.client.increment(e, 1, h(t));
|
|
38
38
|
}
|
|
39
39
|
histogram(e, t, n) {
|
|
40
|
-
this.client.histogram(e, t,
|
|
40
|
+
this.client.histogram(e, t, h(n));
|
|
41
41
|
}
|
|
42
42
|
async close() {
|
|
43
43
|
await new Promise((e) => {
|
|
@@ -76,11 +76,11 @@ class k {
|
|
|
76
76
|
var n;
|
|
77
77
|
const t = JSON.stringify(e);
|
|
78
78
|
try {
|
|
79
|
-
await this.send(t);
|
|
79
|
+
return await this.send(t), { compressed: !1 };
|
|
80
80
|
} catch (o) {
|
|
81
81
|
if (!O(o))
|
|
82
82
|
throw o;
|
|
83
|
-
(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));
|
|
83
|
+
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
84
|
}
|
|
85
85
|
}
|
|
86
86
|
async send(e) {
|
|
@@ -99,33 +99,37 @@ class V {
|
|
|
99
99
|
this.params = e, this.logger = t.logger ?? U(e.service, e.environment), this.transport = t.transport ?? new k(e.environment, this.logger), this.metrics = t.metrics ?? D();
|
|
100
100
|
}
|
|
101
101
|
async log({ scope: e, account: t, ...n }) {
|
|
102
|
-
const o = w(), { environment:
|
|
102
|
+
const o = w(), { environment: m, service: l } = this.params, r = {
|
|
103
103
|
...n,
|
|
104
104
|
...t,
|
|
105
105
|
service: l,
|
|
106
106
|
id: o,
|
|
107
|
-
environment:
|
|
107
|
+
environment: m,
|
|
108
108
|
...e && { scope: e }
|
|
109
109
|
}, a = {
|
|
110
110
|
service: l,
|
|
111
|
-
environment:
|
|
111
|
+
environment: m,
|
|
112
112
|
namespace: r.namespace,
|
|
113
113
|
event: r.event,
|
|
114
114
|
target: r.target
|
|
115
|
-
},
|
|
115
|
+
}, d = Date.now();
|
|
116
116
|
try {
|
|
117
|
-
|
|
117
|
+
const { compressed: g } = await this.transport.create(r);
|
|
118
|
+
this.metrics.increment("sent", {
|
|
119
|
+
...a,
|
|
120
|
+
compressed: String(g)
|
|
121
|
+
}), this.metrics.histogram("send_duration_ms", Date.now() - d, a), this.logger.debug("audit-log sent", {
|
|
118
122
|
id: r.id,
|
|
119
123
|
event: r.event,
|
|
120
124
|
target: r.target
|
|
121
125
|
});
|
|
122
|
-
} catch (
|
|
123
|
-
this.metrics.increment("send_failed", a), this.metrics.histogram("send_duration_ms", Date.now() -
|
|
124
|
-
const
|
|
126
|
+
} catch (g) {
|
|
127
|
+
this.metrics.increment("send_failed", a), this.metrics.histogram("send_duration_ms", Date.now() - d, a);
|
|
128
|
+
const u = g.message;
|
|
125
129
|
try {
|
|
126
130
|
this.logger.error("audit-log write failed", {
|
|
127
131
|
audit_log: r,
|
|
128
|
-
error:
|
|
132
|
+
error: u
|
|
129
133
|
});
|
|
130
134
|
} catch {
|
|
131
135
|
this.metrics.increment("serialization_failed", a);
|
|
@@ -139,7 +143,7 @@ class V {
|
|
|
139
143
|
event: r.event,
|
|
140
144
|
target: r.target
|
|
141
145
|
},
|
|
142
|
-
error:
|
|
146
|
+
error: u
|
|
143
147
|
});
|
|
144
148
|
} catch {
|
|
145
149
|
}
|
package/dist/metrics.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const METRIC_PREFIX = "payfit.
|
|
1
|
+
export declare const METRIC_PREFIX = "payfit.audit_logs.";
|
|
2
2
|
export type MetricName = 'sent' | 'send_failed' | 'serialization_failed' | 'send_duration_ms';
|
|
3
3
|
export interface MetricTags {
|
|
4
4
|
service: string;
|
|
@@ -6,6 +6,7 @@ export interface MetricTags {
|
|
|
6
6
|
namespace?: string;
|
|
7
7
|
event?: string;
|
|
8
8
|
target?: string;
|
|
9
|
+
compressed?: string;
|
|
9
10
|
}
|
|
10
11
|
export interface IMetricsClient {
|
|
11
12
|
increment(name: MetricName, tags: MetricTags): void;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Logger } from 'winston';
|
|
2
|
-
import { IAuditLogCreationData, IAuditLogTransport } from './interface';
|
|
2
|
+
import { IAuditLogCreationData, IAuditLogCreationResult, IAuditLogTransport } from './interface';
|
|
3
3
|
export declare class Transport implements IAuditLogTransport {
|
|
4
4
|
protected environment: string;
|
|
5
5
|
private logger?;
|
|
6
6
|
private client;
|
|
7
7
|
private queueUrl;
|
|
8
8
|
constructor(environment: string, logger?: Logger | undefined);
|
|
9
|
-
create(auditLogData: IAuditLogCreationData): Promise<
|
|
9
|
+
create(auditLogData: IAuditLogCreationData): Promise<IAuditLogCreationResult>;
|
|
10
10
|
private send;
|
|
11
11
|
}
|
|
@@ -20,6 +20,9 @@ export interface IAuditLogCreationData {
|
|
|
20
20
|
specialRole?: string;
|
|
21
21
|
email?: string;
|
|
22
22
|
}
|
|
23
|
+
export interface IAuditLogCreationResult {
|
|
24
|
+
compressed: boolean;
|
|
25
|
+
}
|
|
23
26
|
export interface IAuditLogTransport {
|
|
24
|
-
create: (data: IAuditLogCreationData) => Promise<
|
|
27
|
+
create: (data: IAuditLogCreationData) => Promise<IAuditLogCreationResult>;
|
|
25
28
|
}
|