Skip to content

Commit e78abdd

Browse files
committed
logger: make cds data behavior dependent in cds version
1 parent c055782 commit e78abdd

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/shared/logger.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const VError = require("verror");
55
const { CfEnv } = require("./cf-env");
66
const { tryRequire } = require("./static");
77
const cds = tryRequire("@sap/cds");
8+
const cdsPackage = tryRequire("@sap/cds/package.json");
9+
10+
// NOTE: for sap/cds < 7.7.0 it was expected get the subdomain from express request
11+
const doReqSubdomain = cdsPackage.version.localeCompare("7.7.0", undefined, { numeric: true, sensitivity: "base" }) < 0;
12+
// NOTE: for 7.7.0 <= sap/cds < 9.3.0 it was expected to get the subdomain from cds request .user.tokenInfo
13+
const doTokenInfoSubdomain =
14+
!doReqSubdomain && cdsPackage.version.localeCompare("9.3.0", undefined, { numeric: true, sensitivity: "base" }) < 0;
815

916
const ENV = Object.freeze({
1017
LOG_LEVEL: "BTP_FEATURES_LOG_LEVEL",
@@ -171,17 +178,23 @@ class Logger {
171178
}
172179

173180
const cdsContext = cds?.context;
174-
const cdsData = cdsContext
175-
? {
176-
[FIELD.CORRELATION_ID]: cdsContext.id,
177-
[FIELD.REMOTE_USER]: cdsContext.user?.id,
178-
[FIELD.TENANT_ID]: cdsContext.tenant,
179-
[FIELD.TENANT_SUBDOMAIN]:
180-
cdsContext.user?.authInfo?.getSubdomain?.() ??
181-
cdsContext.user?.tokenInfo?.getSubdomain?.() ??
182-
cdsContext?.http?.req?.authInfo?.getSubdomain?.(),
183-
}
184-
: undefined;
181+
let cdsData;
182+
if (cdsContext) {
183+
cdsData = {
184+
[FIELD.CORRELATION_ID]: cdsContext.id,
185+
[FIELD.REMOTE_USER]: cdsContext.user?.id,
186+
[FIELD.TENANT_ID]: cdsContext.tenant,
187+
};
188+
189+
if (doReqSubdomain) {
190+
cdsData[FIELD.TENANT_SUBDOMAIN] = cdsContext?.http?.req?.authInfo?.getSubdomain?.();
191+
} else if (doTokenInfoSubdomain) {
192+
cdsData[FIELD.TENANT_SUBDOMAIN] = cdsContext.user?.tokenInfo?.getSubdomain?.();
193+
} else {
194+
cdsData[FIELD.TENANT_SUBDOMAIN] = cdsContext.user?.authInfo?.getSubdomain?.();
195+
}
196+
}
197+
185198
// NOTE: the start time of Date's milliseconds is the epoch and the start time for hrtime is an arbitrary time
186199
// close to the process startup, so it may look odd to add them here. however, we can use the sub-millisecond
187200
// offset of hrtime to keep logs with the same Date-millisecond in chronological order.

0 commit comments

Comments
 (0)