@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
149 lines (148 loc) • 6.52 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var CosmosDiagnosticsContext_exports = {};
__export(CosmosDiagnosticsContext_exports, {
CosmosDiagnosticContext: () => CosmosDiagnosticContext
});
module.exports = __toCommonJS(CosmosDiagnosticsContext_exports);
var import_common = require("../common/index.js");
var import_time = require("../utils/time.js");
class CosmosDiagnosticContext {
requestStartTimeUTCinMs;
failedAttempts = [];
metadataLookups = [];
gatewayStatistics = [];
locationEndpointsContacted = /* @__PURE__ */ new Set();
encryptionDiagnostics;
constructor() {
this.requestStartTimeUTCinMs = (0, import_time.getCurrentTimestampInMs)();
}
recordFailedAttempt(gatewayStatistics, retryAttemptNumber) {
const attempt = {
attemptNumber: retryAttemptNumber,
startTimeUTCInMs: gatewayStatistics.startTimeUTCInMs,
durationInMs: gatewayStatistics.durationInMs,
statusCode: gatewayStatistics.statusCode,
substatusCode: gatewayStatistics.subStatusCode,
requestPayloadLengthInBytes: gatewayStatistics.requestPayloadLengthInBytes,
responsePayloadLengthInBytes: gatewayStatistics.responsePayloadLengthInBytes,
activityId: gatewayStatistics.activityId,
operationType: gatewayStatistics.operationType,
resourceType: gatewayStatistics.resourceType
};
this.failedAttempts.push(attempt);
}
recordNetworkCall(gatewayStatistics) {
this.gatewayStatistics.push(gatewayStatistics);
}
recordEncryptionDiagnostics(encryptionDiagnostics) {
const { encryptContent, decryptContent } = encryptionDiagnostics;
const encryptionDuration = encryptContent[import_common.Constants.Encryption.DiagnosticsDuration] ?? 0;
const decryptionDuration = decryptContent[import_common.Constants.Encryption.DiagnosticsDuration] ?? 0;
encryptionDiagnostics.processingDurationInMs = encryptionDuration + decryptionDuration;
this.encryptionDiagnostics = encryptionDiagnostics;
}
/**
* Merge given DiagnosticContext to current node's DiagnosticContext, Treating GatewayRequests of
* given DiagnosticContext, as metadata requests.
*/
mergeDiagnostics(childDiagnostics, metadataType) {
childDiagnostics.locationEndpointsContacted.forEach(
(endpoint) => this.locationEndpointsContacted.add(endpoint)
);
childDiagnostics.gatewayStatistics.forEach(
(gateway) => this.metadataLookups.push({
activityId: gateway.activityId,
requestPayloadLengthInBytes: gateway.requestPayloadLengthInBytes,
responsePayloadLengthInBytes: gateway.responsePayloadLengthInBytes,
startTimeUTCInMs: gateway.startTimeUTCInMs,
operationType: gateway.operationType,
resourceType: gateway.resourceType,
durationInMs: gateway.durationInMs,
metaDataType: metadataType
})
);
}
/**
* Merge given DiagnosticContext to current node's DiagnosticContext for bulk
*/
mergeBulkDiagnostics(childDiagnostics) {
childDiagnostics.locationEndpointsContacted.forEach(
(endpoint) => this.locationEndpointsContacted.add(endpoint)
);
childDiagnostics.gatewayStatistics.forEach((gateway) => this.gatewayStatistics.push(gateway));
childDiagnostics.metadataLookups.forEach((lookup) => this.metadataLookups.push(lookup));
childDiagnostics.failedAttempts.forEach((lookup) => this.failedAttempts.push(lookup));
if (!this.encryptionDiagnostics) {
this.encryptionDiagnostics = childDiagnostics.encryptionDiagnostics;
} else if (childDiagnostics.encryptionDiagnostics) {
this.encryptionDiagnostics.decryptContent = childDiagnostics.encryptionDiagnostics.decryptContent;
this.encryptionDiagnostics.processingDurationInMs = (this.encryptionDiagnostics.processingDurationInMs || 0) + (childDiagnostics.encryptionDiagnostics.processingDurationInMs || 0);
}
}
getClientSideStats(endTimeUTCInMs = (0, import_time.getCurrentTimestampInMs)()) {
return {
requestStartTimeUTCInMs: this.requestStartTimeUTCinMs,
requestDurationInMs: endTimeUTCInMs - this.requestStartTimeUTCinMs,
totalRequestPayloadLengthInBytes: this.getTotalRequestPayloadLength(),
totalResponsePayloadLengthInBytes: this.getTotalResponsePayloadLength(),
locationEndpointsContacted: [...this.locationEndpointsContacted.values()],
metadataDiagnostics: {
metadataLookups: [...this.metadataLookups]
},
retryDiagnostics: {
failedAttempts: [...this.failedAttempts]
},
gatewayStatistics: this.gatewayStatistics,
encryptionDiagnostics: this.encryptionDiagnostics
};
}
getTotalRequestPayloadLength() {
let totalRequestPayloadLength = 0;
this.gatewayStatistics.forEach(
(req) => totalRequestPayloadLength += req.requestPayloadLengthInBytes
);
this.metadataLookups.forEach(
(req) => totalRequestPayloadLength += req.requestPayloadLengthInBytes
);
this.failedAttempts.forEach(
(req) => totalRequestPayloadLength += req.requestPayloadLengthInBytes
);
return totalRequestPayloadLength;
}
getTotalResponsePayloadLength() {
let totalResponsePayloadLength = 0;
this.gatewayStatistics.forEach(
(req) => totalResponsePayloadLength += req.responsePayloadLengthInBytes
);
this.metadataLookups.forEach(
(req) => totalResponsePayloadLength += req.responsePayloadLengthInBytes
);
this.failedAttempts.forEach(
(req) => totalResponsePayloadLength += req.responsePayloadLengthInBytes
);
return totalResponsePayloadLength;
}
recordEndpointResolution(location) {
this.locationEndpointsContacted.add(location);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CosmosDiagnosticContext
});