@azure/monitor-opentelemetry-exporter 1.0.0-alpha.20260122.1 → 1.0.0-alpha.20260123.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.
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,CAmB/E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,IAAI,CAmBtE,CAAC"}
1
+ {"version":3,"file":"fileSystemHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,CAmB/E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,IAAI,CAmCtE,CAAC"}
@@ -40,6 +40,14 @@ const confirmDirExists = async (directory) => {
40
40
  if (!stats.isDirectory()) {
41
41
  throw new Error("Path existed but was not a directory");
42
42
  }
43
+ const ownerUid = stats.uid;
44
+ const currentUid = typeof process.getuid === "function" ? process.getuid() : undefined;
45
+ if (ownerUid !== undefined &&
46
+ currentUid !== undefined &&
47
+ ownerUid !== currentUid &&
48
+ ownerUid !== 0) {
49
+ throw new Error(`Directory ${directory} is owned by uid ${ownerUid}, not the current user (${currentUid}) or admin (uid 0).`);
50
+ }
43
51
  }
44
52
  catch (err) {
45
53
  if (err && err.code === "ENOENT") {
@@ -54,6 +62,10 @@ const confirmDirExists = async (directory) => {
54
62
  }
55
63
  }
56
64
  }
65
+ else {
66
+ // Propagate non-ENOENT errors (e.g., ownership mismatch, permission issues)
67
+ throw err;
68
+ }
57
69
  }
58
70
  };
59
71
  exports.confirmDirExists = confirmDirExists;
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemHelpers.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,4CAA0C;AAE1C,yCAAiC;AACjC,+CAA+D;AAE/D;;;GAGG;AACI,MAAM,uBAAuB,GAAG,KAAK,EAAE,SAAiB,EAAmB,EAAE;IAClF,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,CAAC;QACH,4BAA4B;QAC5B,MAAM,KAAK,GAAG,MAAM,IAAA,kBAAO,EAAC,SAAS,CAAC,CAAC;QAEvC,uBAAuB;QACvB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,SAAS,GAAG,MAAM,IAAA,eAAI,EAAC,IAAA,gBAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;YACpD,IAAI,SAAS,CAAC,MAAM,EAAE,EAAE,CAAC;gBACvB,SAAS,IAAI,SAAS,CAAC,IAAI,CAAC;YAC9B,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,UAAI,CAAC,KAAK,CAAC,iCAAiC,GAAG,EAAE,CAAC,CAAC;QACnD,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC,CAAC;AAnBW,QAAA,uBAAuB,2BAmBlC;AAEF;;;GAGG;AACI,MAAM,gBAAgB,GAAG,KAAK,EAAE,SAAiB,EAAiB,EAAE;IACzE,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,IAAA,gBAAK,EAAC,SAAS,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjC,IAAI,CAAC;gBACH,MAAM,OAAO,GAAyB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;gBAC1D,MAAM,IAAA,gBAAK,EAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAClC,CAAC;YAAC,OAAO,QAAa,EAAE,CAAC;gBACvB,IAAI,QAAQ,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBAC3C,2CAA2C;oBAC3C,MAAM,QAAQ,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAnBW,QAAA,gBAAgB,oBAmB3B","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { diag } from \"@opentelemetry/api\";\nimport type { MakeDirectoryOptions } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { lstat, mkdir, readdir, stat } from \"node:fs/promises\";\n\n/**\n * Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.\n * @internal\n */\nexport const getShallowDirectorySize = async (directory: string): Promise<number> => {\n let totalSize = 0;\n try {\n // Get the directory listing\n const files = await readdir(directory);\n\n // Query all file sizes\n for (const file of files) {\n const fileStats = await stat(join(directory, file));\n if (fileStats.isFile()) {\n totalSize += fileStats.size;\n }\n }\n\n return totalSize;\n } catch (err) {\n diag.error(`Error getting directory size: ${err}`);\n return 0;\n }\n};\n\n/**\n * Validate directory exists.\n * @internal\n */\nexport const confirmDirExists = async (directory: string): Promise<void> => {\n try {\n const stats = await lstat(directory);\n if (!stats.isDirectory()) {\n throw new Error(\"Path existed but was not a directory\");\n }\n } catch (err: any) {\n if (err && err.code === \"ENOENT\") {\n try {\n const options: MakeDirectoryOptions = { recursive: true };\n await mkdir(directory, options);\n } catch (mkdirErr: any) {\n if (mkdirErr && mkdirErr.code !== \"EEXIST\") {\n // Handle race condition by ignoring EEXIST\n throw mkdirErr;\n }\n }\n }\n }\n};\n"]}
1
+ {"version":3,"file":"fileSystemHelpers.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,4CAA0C;AAE1C,yCAAiC;AACjC,+CAA+D;AAE/D;;;GAGG;AACI,MAAM,uBAAuB,GAAG,KAAK,EAAE,SAAiB,EAAmB,EAAE;IAClF,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,CAAC;QACH,4BAA4B;QAC5B,MAAM,KAAK,GAAG,MAAM,IAAA,kBAAO,EAAC,SAAS,CAAC,CAAC;QAEvC,uBAAuB;QACvB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,SAAS,GAAG,MAAM,IAAA,eAAI,EAAC,IAAA,gBAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;YACpD,IAAI,SAAS,CAAC,MAAM,EAAE,EAAE,CAAC;gBACvB,SAAS,IAAI,SAAS,CAAC,IAAI,CAAC;YAC9B,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,UAAI,CAAC,KAAK,CAAC,iCAAiC,GAAG,EAAE,CAAC,CAAC;QACnD,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC,CAAC;AAnBW,QAAA,uBAAuB,2BAmBlC;AAEF;;;GAGG;AACI,MAAM,gBAAgB,GAAG,KAAK,EAAE,SAAiB,EAAiB,EAAE;IACzE,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,IAAA,gBAAK,EAAC,SAAS,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC1D,CAAC;QAED,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC;QAC3B,MAAM,UAAU,GAAG,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACvF,IACE,QAAQ,KAAK,SAAS;YACtB,UAAU,KAAK,SAAS;YACxB,QAAQ,KAAK,UAAU;YACvB,QAAQ,KAAK,CAAC,EACd,CAAC;YACD,MAAM,IAAI,KAAK,CACb,aAAa,SAAS,oBAAoB,QAAQ,2BAA2B,UAAU,qBAAqB,CAC7G,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjC,IAAI,CAAC;gBACH,MAAM,OAAO,GAAyB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;gBAC1D,MAAM,IAAA,gBAAK,EAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAClC,CAAC;YAAC,OAAO,QAAa,EAAE,CAAC;gBACvB,IAAI,QAAQ,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBAC3C,2CAA2C;oBAC3C,MAAM,QAAQ,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,4EAA4E;YAC5E,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAnCW,QAAA,gBAAgB,oBAmC3B","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { diag } from \"@opentelemetry/api\";\nimport type { MakeDirectoryOptions } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { lstat, mkdir, readdir, stat } from \"node:fs/promises\";\n\n/**\n * Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.\n * @internal\n */\nexport const getShallowDirectorySize = async (directory: string): Promise<number> => {\n let totalSize = 0;\n try {\n // Get the directory listing\n const files = await readdir(directory);\n\n // Query all file sizes\n for (const file of files) {\n const fileStats = await stat(join(directory, file));\n if (fileStats.isFile()) {\n totalSize += fileStats.size;\n }\n }\n\n return totalSize;\n } catch (err) {\n diag.error(`Error getting directory size: ${err}`);\n return 0;\n }\n};\n\n/**\n * Validate directory exists.\n * @internal\n */\nexport const confirmDirExists = async (directory: string): Promise<void> => {\n try {\n const stats = await lstat(directory);\n if (!stats.isDirectory()) {\n throw new Error(\"Path existed but was not a directory\");\n }\n\n const ownerUid = stats.uid;\n const currentUid = typeof process.getuid === \"function\" ? process.getuid() : undefined;\n if (\n ownerUid !== undefined &&\n currentUid !== undefined &&\n ownerUid !== currentUid &&\n ownerUid !== 0\n ) {\n throw new Error(\n `Directory ${directory} is owned by uid ${ownerUid}, not the current user (${currentUid}) or admin (uid 0).`,\n );\n }\n } catch (err: any) {\n if (err && err.code === \"ENOENT\") {\n try {\n const options: MakeDirectoryOptions = { recursive: true };\n await mkdir(directory, options);\n } catch (mkdirErr: any) {\n if (mkdirErr && mkdirErr.code !== \"EEXIST\") {\n // Handle race condition by ignoring EEXIST\n throw mkdirErr;\n }\n }\n } else {\n // Propagate non-ENOENT errors (e.g., ownership mismatch, permission issues)\n throw err;\n }\n }\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemPersist.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemPersist.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAG3D,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAEtE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAC;AAI7F;;;GAGG;AACH,qBAAa,iBAAkB,YAAW,iBAAiB;IAevD,OAAO,CAAC,QAAQ,CAAC;IACjB,OAAO,CAAC,wBAAwB,CAAC;IAfnC,MAAM,C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EpC,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,iBAAiB,CAA+B;IACxD,OAAO,CAAC,mBAAmB,CAAS;gBAGlC,kBAAkB,EAAE,MAAM,EAClB,QAAQ,CAAC,EAAE,2BAA2B,YAAA,EACtC,wBAAwB,CAAC,EAAE,uBAAuB,YAAA;IAuC5D,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBlC,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;IAkB/B;;;OAGG;YACW,mBAAmB;IA8BjC;;;;;OAKG;YACW,YAAY;YAiEZ,gBAAgB;CA+B/B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,kBAAkB,EAAE,MAAM,EAC1B,gBAAgB,EAAE,MAAM,GAAG,SAAS,GACnC,MAAM,CA+CR"}
@@ -10,6 +10,7 @@ const node_crypto_1 = require("node:crypto");
10
10
  const api_1 = require("@opentelemetry/api");
11
11
  const fileAccessControl_js_1 = require("./fileAccessControl.js");
12
12
  const fileSystemHelpers_js_1 = require("./fileSystemHelpers.js");
13
+ const node_fs_1 = require("node:fs");
13
14
  const promises_1 = require("node:fs/promises");
14
15
  const types_js_1 = require("../../../export/statsbeat/types.js");
15
16
  /**
@@ -158,12 +159,18 @@ class FileSystemPersist {
158
159
  api_1.diag.warn(`Error while checking size of persistence directory: `, error && error.message);
159
160
  return false;
160
161
  }
161
- const fileName = `${new Date().getTime()}${FileSystemPersist.FILENAME_SUFFIX}`;
162
+ const fileName = `${new Date().getTime()}-${process.hrtime.bigint()}${FileSystemPersist.FILENAME_SUFFIX}`;
162
163
  const fileFullPath = (0, node_path_1.join)(this._tempDirectory, fileName);
163
164
  // Mode 600 is w/r for creator and no read access for others
164
165
  api_1.diag.info(`saving data to disk at: ${fileFullPath}`);
165
166
  try {
166
- await (0, promises_1.writeFile)(fileFullPath, payload, { mode: 0o600 });
167
+ const handle = await (0, promises_1.open)(fileFullPath, node_fs_1.constants.O_CREAT | node_fs_1.constants.O_EXCL | node_fs_1.constants.O_WRONLY, 0o600);
168
+ try {
169
+ await handle.writeFile(payload);
170
+ }
171
+ finally {
172
+ await handle.close();
173
+ }
167
174
  }
168
175
  catch (writeError) {
169
176
  // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user
@@ -183,7 +190,7 @@ class FileSystemPersist {
183
190
  return false;
184
191
  }
185
192
  else {
186
- files.forEach(async (file) => {
193
+ for (const file of files) {
187
194
  // Check expiration
188
195
  const fileCreationDate = new Date(parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]));
189
196
  const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;
@@ -191,7 +198,7 @@ class FileSystemPersist {
191
198
  const filePath = (0, node_path_1.join)(this._tempDirectory, file);
192
199
  await (0, promises_1.unlink)(filePath);
193
200
  }
194
- });
201
+ }
195
202
  return true;
196
203
  }
197
204
  }
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemPersist.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemPersist.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AA2PlC,kDAkDC;AA3SD,qCAA2C;AAC3C,yCAAoD;AACpD,6CAAyC;AACzC,4CAA0C;AAE1C,iEAA2D;AAC3D,iEAAmF;AAEnF,+CAA8E;AAE9E,iEAA6E;AAG7E;;;GAGG;AACH,MAAa,iBAAiB;IAelB;IACA;IAfV,MAAM,CAAC,cAAc,GAAG,uBAAuB,CAAC;IAChD,MAAM,CAAC,eAAe,GAAG,UAAU,CAAC;IAEpC,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;IACzD,cAAc,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;IAC1C,cAAc,GAAW,UAAU,CAAC,CAAC,QAAQ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Info } from \"node:os\";\nimport { basename, join, dirname } from \"node:path\";\nimport { createHash } from \"node:crypto\";\nimport { diag } from \"@opentelemetry/api\";\nimport type { PersistentStorage } from \"../../../types.js\";\nimport { FileAccessControl } from \"./fileAccessControl.js\";\nimport { confirmDirExists, getShallowDirectorySize } from \"./fileSystemHelpers.js\";\nimport type { AzureMonitorExporterOptions } from \"../../../config.js\";\nimport { readdir, readFile, stat, unlink, writeFile } from \"node:fs/promises\";\nimport type { CustomerSDKStatsMetrics } from \"../../../export/statsbeat/customerSDKStats.js\";\nimport { DropCode, ExceptionType } from \"../../../export/statsbeat/types.js\";\nimport type { TelemetryItem as Envelope } from \"../../../generated/index.js\";\n\n/**\n * File system persist class.\n * @internal\n */\nexport class FileSystemPersist implements PersistentStorage {\n static TEMPDIR_PREFIX = \"opentelemetry-nodejs-\";\n static FILENAME_SUFFIX = \".ai.json\";\n\n fileRetemptionPeriod = 2 * 24 * 60 * 60 * 1000; // 2 days\n cleanupTimeOut = 60 * 60 * 1000; // 1 hour\n maxBytesOnDisk: number = 50_000_000; // ~50MB\n\n private _enabled: boolean;\n private _tempDirectory: string = \"\";\n private _fileCleanupTimer: NodeJS.Timeout | null = null;\n private _instrumentationKey: string;\n\n constructor(\n instrumentationKey: string,\n private _options?: AzureMonitorExporterOptions,\n private _customerSDKStatsMetrics?: CustomerSDKStatsMetrics,\n ) {\n this._instrumentationKey = instrumentationKey;\n if (this._options?.disableOfflineStorage) {\n this._enabled = false;\n return;\n }\n this._enabled = true;\n FileAccessControl.checkFileProtection();\n\n if (!FileAccessControl.OS_PROVIDES_FILE_PROTECTION) {\n this._enabled = false;\n diag.error(\n \"Sufficient file protection capabilities were not detected. Files will not be persisted\",\n );\n }\n\n if (!this._instrumentationKey) {\n this._enabled = false;\n diag.error(\n `No instrumentation key was provided to FileSystemPersister. Files will not be persisted`,\n );\n }\n if (this._enabled) {\n this._tempDirectory = getStorageDirectory(\n this._instrumentationKey,\n this._options?.storageDirectory,\n );\n\n // Starts file cleanup task\n if (!this._fileCleanupTimer) {\n this._fileCleanupTimer = setTimeout(() => {\n this._fileCleanupTask();\n }, this.cleanupTimeOut);\n this._fileCleanupTimer.unref();\n }\n }\n }\n\n push(value: unknown[]): Promise<boolean> {\n if (this._enabled) {\n diag.debug(\"Pushing value to persistent storage\", value.toString());\n return this._storeToDisk(JSON.stringify(value), value as Envelope[]);\n }\n // Only return a false promise if the SDK isn't set to disable offline storage\n if (!this._options?.disableOfflineStorage) {\n return new Promise((resolve) => {\n resolve(false);\n });\n }\n return new Promise((resolve) => {\n resolve(true);\n });\n }\n\n async shift(): Promise<unknown> {\n if (this._enabled) {\n diag.debug(\"Searching for filesystem persisted files\");\n try {\n const buffer = await this._getFirstFileOnDisk();\n if (buffer) {\n return JSON.parse(buffer.toString(\"utf8\"));\n }\n } catch (e: any) {\n diag.debug(\"Failed to read persisted file\", e);\n }\n return null;\n }\n return new Promise((resolve) => {\n resolve(null);\n });\n }\n\n /**\n * Check for temp telemetry files\n * reads the first file if exist, deletes it and tries to send its load\n */\n private async _getFirstFileOnDisk(): Promise<Buffer | null> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return null;\n } else {\n const firstFile = files[0];\n const filePath = join(this._tempDirectory, firstFile);\n const payload = await readFile(filePath);\n // delete the file first to prevent double sending\n await unlink(filePath);\n return payload;\n }\n }\n return null;\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n // File does not exist -- return null instead of throwing\n return null;\n } else {\n throw e;\n }\n }\n }\n\n /**\n * Stores telemetry data to disk.\n * @param payload - The telemetry data to store.\n * @param envelopeLength -The length of the telemetry envelope.\n * @returns A promise that resolves to true if the data was stored successfully, false otherwise.\n */\n private async _storeToDisk(payload: string, envelopes: Envelope[]): Promise<boolean> {\n try {\n await confirmDirExists(this._tempDirectory);\n } catch (error: any) {\n // Check if error is due to permission/readonly issues\n if (error?.code === \"EACCES\" || error?.code === \"EPERM\") {\n this._customerSDKStatsMetrics?.countDroppedItems(envelopes, DropCode.CLIENT_READONLY);\n diag.warn(\n `Permission denied while checking/creating directory: ${this._tempDirectory}`,\n error?.message,\n );\n } else {\n diag.warn(`Error while checking/creating directory: `, error && error.message);\n }\n return false;\n }\n\n try {\n const size = await getShallowDirectorySize(this._tempDirectory);\n if (size > this.maxBytesOnDisk) {\n // If the directory size exceeds the max limit, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_PERSISTENCE_CAPACITY,\n );\n diag.warn(\n `Not saving data due to max size limit being met. Directory size in bytes is: ${size}`,\n );\n return false;\n }\n } catch (error: any) {\n diag.warn(`Error while checking size of persistence directory: `, error && error.message);\n return false;\n }\n\n const fileName = `${new Date().getTime()}${FileSystemPersist.FILENAME_SUFFIX}`;\n const fileFullPath = join(this._tempDirectory, fileName);\n\n // Mode 600 is w/r for creator and no read access for others\n diag.info(`saving data to disk at: ${fileFullPath}`);\n try {\n await writeFile(fileFullPath, payload, { mode: 0o600 });\n } catch (writeError: any) {\n // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_EXCEPTION,\n writeError?.message,\n ExceptionType.STORAGE_EXCEPTION,\n );\n diag.warn(`Error writing file to persistent file storage`, writeError);\n return false;\n }\n return true;\n }\n\n private async _fileCleanupTask(): Promise<boolean> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return false;\n } else {\n files.forEach(async (file) => {\n // Check expiration\n const fileCreationDate: Date = new Date(\n parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]),\n );\n const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;\n if (expired) {\n const filePath = join(this._tempDirectory, file);\n await unlink(filePath);\n }\n });\n return true;\n }\n }\n return false;\n } catch (error: any) {\n diag.info(`Failed cleanup of persistent file storage expired files`, error);\n return false;\n }\n }\n}\n\n/**\n * Return the deterministic local storage path for a given instrumentation key.\n *\n * On shared Linux hosts the first user to create `/tmp/Microsoft/AzureMonitor` can\n * block others because the directory inherits that user's `umask`. This is avoided by\n * inserting a hash of the instrumentation key, user name, process name, and\n * application directory, giving each user their own subdirectory, e.g.\n * `/tmp/Microsoft-AzureMonitor-1234...../opentelemetry-nodejs-<ikey>`.\n *\n * @param instrumentationKey - Application Insights instrumentation key.\n * @param storageDirectory - Optional custom storage directory path. If not provided, system temp directory is used.\n * @returns Absolute path to the storage directory.\n * @internal\n */\nexport function getStorageDirectory(\n instrumentationKey: string,\n storageDirectory: string | undefined,\n): string {\n let userSegment: string;\n let processName: string;\n let applicationDirectory: string;\n try {\n const user = userInfo();\n userSegment = user.username;\n } catch (error) {\n userSegment = \"\";\n }\n if (process.title.length > 0) {\n processName = process.title;\n } else {\n processName = \"\";\n }\n const applicationDir = dirname(process.cwd() || process.argv[1]);\n if (applicationDir) {\n applicationDirectory = applicationDir;\n } else {\n applicationDirectory = \"\";\n }\n const hash_input = `${instrumentationKey}|${userSegment}|${processName}|${applicationDirectory}`;\n\n let subDirectory: string;\n try {\n subDirectory = createHash(\"sha256\").update(hash_input).digest(\"hex\");\n } catch (error) {\n let hash = 5381;\n for (let i = 0; i < hash_input.length; i++) {\n const char = hash_input.charCodeAt(i);\n hash = (hash << 5) + hash + char;\n hash = hash & hash;\n }\n subDirectory = Math.abs(hash).toString(16);\n }\n\n let sharedRoot: string;\n if (storageDirectory) {\n sharedRoot = storageDirectory;\n } else {\n sharedRoot = tmpdir();\n }\n return join(\n sharedRoot,\n \"Microsoft-AzureMonitor-\" + subDirectory,\n FileSystemPersist.TEMPDIR_PREFIX + instrumentationKey,\n );\n}\n"]}
1
+ {"version":3,"file":"fileSystemPersist.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemPersist.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAqQlC,kDAkDC;AArTD,qCAA2C;AAC3C,yCAAoD;AACpD,6CAAyC;AACzC,4CAA0C;AAE1C,iEAA2D;AAC3D,iEAAmF;AAEnF,qCAAmD;AACnD,+CAAyE;AAEzE,iEAA6E;AAG7E;;;GAGG;AACH,MAAa,iBAAiB;IAelB;IACA;IAfV,MAAM,CAAC,cAAc,GAAG,uBAAuB,CAAC;IAChD,MAAM,CAAC,eAAe,GAAG,UAAU,CAAC;IAEpC,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;IACzD,cAAc,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;IAC1C,cAAc,GAAW,UAAU,CAAC,CAAC,QAAQ;IAErC,QAAQ,CAAU;IAClB,cAAc,GAAW,EAAE,CAAC;IAC5B,iBAAiB,GAA0B,IAAI,CAAC;IAChD,mBAAmB,CAAS;IAEpC,YACE,kBAA0B,EAClB,QAAsC,EACtC,wBAAkD;QADlD,aAAQ,GAAR,QAAQ,CAA8B;QACtC,6BAAwB,GAAxB,wBAAwB,CAA0B;QAE1D,IAAI,CAAC,mBAAmB,GAAG,kBAAkB,CAAC;QAC9C,IAAI,IAAI,CAAC,QAAQ,EAAE,qBAAqB,EAAE,CAAC;YACzC,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;YACtB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,wCAAiB,CAAC,mBAAmB,EAAE,CAAC;QAExC,IAAI,CAAC,wCAAiB,CAAC,2BAA2B,EAAE,CAAC;YACnD,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;YACtB,UAAI,CAAC,KAA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K,CAAC,CAAC;IACvE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3C,MAAM,IAAI,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACtC,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;YACjC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,IAAI,UAAkB,CAAC;IACvB,IAAI,gBAAgB,EAAE,CAAC;QACrB,UAAU,GAAG,gBAAgB,CAAC;IAChC,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,IAAA,gBAAM,GAAE,CAAC;IACxB,CAAC;IACD,OAAO,IAAA,gBAAI,EACT,UAAU,EACV,yBAAyB,GAAG,YAAY,EACxC,iBAAiB,CAAC,cAAc,GAAG,kBAAkB,CACtD,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { tmpdir, userInfo } from \"node:os\";\nimport { basename, join, dirname } from \"node:path\";\nimport { createHash } from \"node:crypto\";\nimport { diag } from \"@opentelemetry/api\";\nimport type { PersistentStorage } from \"../../../types.js\";\nimport { FileAccessControl } from \"./fileAccessControl.js\";\nimport { confirmDirExists, getShallowDirectorySize } from \"./fileSystemHelpers.js\";\nimport type { AzureMonitorExporterOptions } from \"../../../config.js\";\nimport { constants as fsConstants } from \"node:fs\";\nimport { open, readdir, readFile, stat, unlink } from \"node:fs/promises\";\nimport type { CustomerSDKStatsMetrics } from \"../../../export/statsbeat/customerSDKStats.js\";\nimport { DropCode, ExceptionType } from \"../../../export/statsbeat/types.js\";\nimport type { TelemetryItem as Envelope } from \"../../../generated/index.js\";\n\n/**\n * File system persist class.\n * @internal\n */\nexport class FileSystemPersist implements PersistentStorage {\n static TEMPDIR_PREFIX = \"opentelemetry-nodejs-\";\n static FILENAME_SUFFIX = \".ai.json\";\n\n fileRetemptionPeriod = 2 * 24 * 60 * 60 * 1000; // 2 days\n cleanupTimeOut = 60 * 60 * 1000; // 1 hour\n maxBytesOnDisk: number = 50_000_000; // ~50MB\n\n private _enabled: boolean;\n private _tempDirectory: string = \"\";\n private _fileCleanupTimer: NodeJS.Timeout | null = null;\n private _instrumentationKey: string;\n\n constructor(\n instrumentationKey: string,\n private _options?: AzureMonitorExporterOptions,\n private _customerSDKStatsMetrics?: CustomerSDKStatsMetrics,\n ) {\n this._instrumentationKey = instrumentationKey;\n if (this._options?.disableOfflineStorage) {\n this._enabled = false;\n return;\n }\n this._enabled = true;\n FileAccessControl.checkFileProtection();\n\n if (!FileAccessControl.OS_PROVIDES_FILE_PROTECTION) {\n this._enabled = false;\n diag.error(\n \"Sufficient file protection capabilities were not detected. Files will not be persisted\",\n );\n }\n\n if (!this._instrumentationKey) {\n this._enabled = false;\n diag.error(\n `No instrumentation key was provided to FileSystemPersister. Files will not be persisted`,\n );\n }\n if (this._enabled) {\n this._tempDirectory = getStorageDirectory(\n this._instrumentationKey,\n this._options?.storageDirectory,\n );\n\n // Starts file cleanup task\n if (!this._fileCleanupTimer) {\n this._fileCleanupTimer = setTimeout(() => {\n this._fileCleanupTask();\n }, this.cleanupTimeOut);\n this._fileCleanupTimer.unref();\n }\n }\n }\n\n push(value: unknown[]): Promise<boolean> {\n if (this._enabled) {\n diag.debug(\"Pushing value to persistent storage\", value.toString());\n return this._storeToDisk(JSON.stringify(value), value as Envelope[]);\n }\n // Only return a false promise if the SDK isn't set to disable offline storage\n if (!this._options?.disableOfflineStorage) {\n return new Promise((resolve) => {\n resolve(false);\n });\n }\n return new Promise((resolve) => {\n resolve(true);\n });\n }\n\n async shift(): Promise<unknown> {\n if (this._enabled) {\n diag.debug(\"Searching for filesystem persisted files\");\n try {\n const buffer = await this._getFirstFileOnDisk();\n if (buffer) {\n return JSON.parse(buffer.toString(\"utf8\"));\n }\n } catch (e: any) {\n diag.debug(\"Failed to read persisted file\", e);\n }\n return null;\n }\n return new Promise((resolve) => {\n resolve(null);\n });\n }\n\n /**\n * Check for temp telemetry files\n * reads the first file if exist, deletes it and tries to send its load\n */\n private async _getFirstFileOnDisk(): Promise<Buffer | null> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return null;\n } else {\n const firstFile = files[0];\n const filePath = join(this._tempDirectory, firstFile);\n const payload = await readFile(filePath);\n // delete the file first to prevent double sending\n await unlink(filePath);\n return payload;\n }\n }\n return null;\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n // File does not exist -- return null instead of throwing\n return null;\n } else {\n throw e;\n }\n }\n }\n\n /**\n * Stores telemetry data to disk.\n * @param payload - The telemetry data to store.\n * @param envelopeLength -The length of the telemetry envelope.\n * @returns A promise that resolves to true if the data was stored successfully, false otherwise.\n */\n private async _storeToDisk(payload: string, envelopes: Envelope[]): Promise<boolean> {\n try {\n await confirmDirExists(this._tempDirectory);\n } catch (error: any) {\n // Check if error is due to permission/readonly issues\n if (error?.code === \"EACCES\" || error?.code === \"EPERM\") {\n this._customerSDKStatsMetrics?.countDroppedItems(envelopes, DropCode.CLIENT_READONLY);\n diag.warn(\n `Permission denied while checking/creating directory: ${this._tempDirectory}`,\n error?.message,\n );\n } else {\n diag.warn(`Error while checking/creating directory: `, error && error.message);\n }\n return false;\n }\n\n try {\n const size = await getShallowDirectorySize(this._tempDirectory);\n if (size > this.maxBytesOnDisk) {\n // If the directory size exceeds the max limit, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_PERSISTENCE_CAPACITY,\n );\n diag.warn(\n `Not saving data due to max size limit being met. Directory size in bytes is: ${size}`,\n );\n return false;\n }\n } catch (error: any) {\n diag.warn(`Error while checking size of persistence directory: `, error && error.message);\n return false;\n }\n\n const fileName = `${new Date().getTime()}-${process.hrtime.bigint()}${FileSystemPersist.FILENAME_SUFFIX}`;\n const fileFullPath = join(this._tempDirectory, fileName);\n\n // Mode 600 is w/r for creator and no read access for others\n diag.info(`saving data to disk at: ${fileFullPath}`);\n try {\n const handle = await open(\n fileFullPath,\n fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY,\n 0o600,\n );\n try {\n await handle.writeFile(payload);\n } finally {\n await handle.close();\n }\n } catch (writeError: any) {\n // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_EXCEPTION,\n writeError?.message,\n ExceptionType.STORAGE_EXCEPTION,\n );\n diag.warn(`Error writing file to persistent file storage`, writeError);\n return false;\n }\n return true;\n }\n\n private async _fileCleanupTask(): Promise<boolean> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return false;\n } else {\n for (const file of files) {\n // Check expiration\n const fileCreationDate: Date = new Date(\n parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]),\n );\n const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;\n if (expired) {\n const filePath = join(this._tempDirectory, file);\n await unlink(filePath);\n }\n }\n return true;\n }\n }\n return false;\n } catch (error: any) {\n diag.info(`Failed cleanup of persistent file storage expired files`, error);\n return false;\n }\n }\n}\n\n/**\n * Return the deterministic local storage path for a given instrumentation key.\n *\n * On shared Linux hosts the first user to create `/tmp/Microsoft/AzureMonitor` can\n * block others because the directory inherits that user's `umask`. This is avoided by\n * inserting a hash of the instrumentation key, user name, process name, and\n * application directory, giving each user their own subdirectory, e.g.\n * `/tmp/Microsoft-AzureMonitor-1234...../opentelemetry-nodejs-<ikey>`.\n *\n * @param instrumentationKey - Application Insights instrumentation key.\n * @param storageDirectory - Optional custom storage directory path. If not provided, system temp directory is used.\n * @returns Absolute path to the storage directory.\n * @internal\n */\nexport function getStorageDirectory(\n instrumentationKey: string,\n storageDirectory: string | undefined,\n): string {\n let userSegment: string;\n let processName: string;\n let applicationDirectory: string;\n try {\n const user = userInfo();\n userSegment = user.username;\n } catch (error) {\n userSegment = \"\";\n }\n if (process.title.length > 0) {\n processName = process.title;\n } else {\n processName = \"\";\n }\n const applicationDir = dirname(process.cwd() || process.argv[1]);\n if (applicationDir) {\n applicationDirectory = applicationDir;\n } else {\n applicationDirectory = \"\";\n }\n const hash_input = `${instrumentationKey}|${userSegment}|${processName}|${applicationDirectory}`;\n\n let subDirectory: string;\n try {\n subDirectory = createHash(\"sha256\").update(hash_input).digest(\"hex\");\n } catch (error) {\n let hash = 5381;\n for (let i = 0; i < hash_input.length; i++) {\n const char = hash_input.charCodeAt(i);\n hash = (hash << 5) + hash + char;\n hash = hash & hash;\n }\n subDirectory = Math.abs(hash).toString(16);\n }\n\n let sharedRoot: string;\n if (storageDirectory) {\n sharedRoot = storageDirectory;\n } else {\n sharedRoot = tmpdir();\n }\n return join(\n sharedRoot,\n \"Microsoft-AzureMonitor-\" + subDirectory,\n FileSystemPersist.TEMPDIR_PREFIX + instrumentationKey,\n );\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,CAmB/E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,IAAI,CAmBtE,CAAC"}
1
+ {"version":3,"file":"fileSystemHelpers.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,eAAO,MAAM,uBAAuB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,CAmB/E,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,IAAI,CAmCtE,CAAC"}
@@ -36,6 +36,14 @@ export const confirmDirExists = async (directory) => {
36
36
  if (!stats.isDirectory()) {
37
37
  throw new Error("Path existed but was not a directory");
38
38
  }
39
+ const ownerUid = stats.uid;
40
+ const currentUid = typeof process.getuid === "function" ? process.getuid() : undefined;
41
+ if (ownerUid !== undefined &&
42
+ currentUid !== undefined &&
43
+ ownerUid !== currentUid &&
44
+ ownerUid !== 0) {
45
+ throw new Error(`Directory ${directory} is owned by uid ${ownerUid}, not the current user (${currentUid}) or admin (uid 0).`);
46
+ }
39
47
  }
40
48
  catch (err) {
41
49
  if (err && err.code === "ENOENT") {
@@ -50,6 +58,10 @@ export const confirmDirExists = async (directory) => {
50
58
  }
51
59
  }
52
60
  }
61
+ else {
62
+ // Propagate non-ENOENT errors (e.g., ownership mismatch, permission issues)
63
+ throw err;
64
+ }
53
65
  }
54
66
  };
55
67
  //# sourceMappingURL=fileSystemHelpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemHelpers.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EA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C;YAAC,OAAO,QAAa,EAAE,CAAC;gBACvB,IAAI,QAAQ,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBAC3C,2CAA2C;oBAC3C,MAAM,QAAQ,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { diag } from \"@opentelemetry/api\";\nimport type { MakeDirectoryOptions } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { lstat, mkdir, readdir, stat } from \"node:fs/promises\";\n\n/**\n * Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.\n * @internal\n */\nexport const getShallowDirectorySize = async (directory: string): Promise<number> => {\n let totalSize = 0;\n try {\n // Get the directory listing\n const files = await readdir(directory);\n\n // Query all file sizes\n for (const file of files) {\n const fileStats = await stat(join(directory, file));\n if (fileStats.isFile()) {\n totalSize += fileStats.size;\n }\n }\n\n return totalSize;\n } catch (err) {\n diag.error(`Error getting directory size: ${err}`);\n return 0;\n }\n};\n\n/**\n * Validate directory exists.\n * @internal\n */\nexport const confirmDirExists = async (directory: string): Promise<void> => {\n try {\n const stats = await lstat(directory);\n if (!stats.isDirectory()) {\n throw new Error(\"Path existed but was not a directory\");\n }\n } catch (err: any) {\n if (err && err.code === \"ENOENT\") {\n try {\n const options: MakeDirectoryOptions = { recursive: true };\n await mkdir(directory, options);\n } catch (mkdirErr: any) {\n if (mkdirErr && mkdirErr.code !== \"EEXIST\") {\n // Handle race condition by ignoring EEXIST\n throw mkdirErr;\n }\n }\n }\n }\n};\n"]}
1
+ {"version":3,"file":"fileSystemHelpers.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemHelpers.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,WA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AQ,KAAK,SAAS;YACtB,UAAU,KAAK,SAAS;YACxB,QAAQ,KAAK,UAAU;YACvB,QAAQ,KAAK,CAAC,EACd,CAAC;YACD,MAAM,IAAI,KAAK,CACb,aAAa,SAAS,oBAAoB,QAAQ,2BAA2B,UAAU,qBAAqB,CAC7G,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjC,IAAI,CAAC;gBACH,MAAM,OAAO,GAAyB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;gBAC1D,MAAM,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAClC,CAAC;YAAC,OAAO,QAAa,EAAE,CAAC;gBACvB,IAAI,QAAQ,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBAC3C,2CAA2C;oBAC3C,MAAM,QAAQ,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,4EAA4E;YAC5E,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { diag } from \"@opentelemetry/api\";\nimport type { MakeDirectoryOptions } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { lstat, mkdir, readdir, stat } from \"node:fs/promises\";\n\n/**\n * Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.\n * @internal\n */\nexport const getShallowDirectorySize = async (directory: string): Promise<number> => {\n let totalSize = 0;\n try {\n // Get the directory listing\n const files = await readdir(directory);\n\n // Query all file sizes\n for (const file of files) {\n const fileStats = await stat(join(directory, file));\n if (fileStats.isFile()) {\n totalSize += fileStats.size;\n }\n }\n\n return totalSize;\n } catch (err) {\n diag.error(`Error getting directory size: ${err}`);\n return 0;\n }\n};\n\n/**\n * Validate directory exists.\n * @internal\n */\nexport const confirmDirExists = async (directory: string): Promise<void> => {\n try {\n const stats = await lstat(directory);\n if (!stats.isDirectory()) {\n throw new Error(\"Path existed but was not a directory\");\n }\n\n const ownerUid = stats.uid;\n const currentUid = typeof process.getuid === \"function\" ? process.getuid() : undefined;\n if (\n ownerUid !== undefined &&\n currentUid !== undefined &&\n ownerUid !== currentUid &&\n ownerUid !== 0\n ) {\n throw new Error(\n `Directory ${directory} is owned by uid ${ownerUid}, not the current user (${currentUid}) or admin (uid 0).`,\n );\n }\n } catch (err: any) {\n if (err && err.code === \"ENOENT\") {\n try {\n const options: MakeDirectoryOptions = { recursive: true };\n await mkdir(directory, options);\n } catch (mkdirErr: any) {\n if (mkdirErr && mkdirErr.code !== \"EEXIST\") {\n // Handle race condition by ignoring EEXIST\n throw mkdirErr;\n }\n }\n } else {\n // Propagate non-ENOENT errors (e.g., ownership mismatch, permission issues)\n throw err;\n }\n }\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemPersist.d.ts","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemPersist.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAG3D,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAEtE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAC;AAI7F;;;GAGG;AACH,qBAAa,iBAAkB,YAAW,iBAAiB;IAevD,OAAO,CAAC,QAAQ,CAAC;IACjB,OAAO,CAAC,wBAAwB,CAAC;IAfnC,MAAM,CAAC,cAAc,SAA2B;IAChD,MAAM,CAAC,eAAe,SAAc;IAEpC,oBAAoB,SAA2B;IAC/C,cAAc,SAAkB;IAChC,cAAc,EAAE,MAAM,CAAc;IAEpC,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,iBAAiB,CAA+B;IACxD,OAAO,CAAC,m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AuC5D,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBlC,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;IAkB/B;;;OAGG;YACW,mBAAmB;IA8BjC;;;;;OAKG;YACW,YAAY;YAiEZ,gBAAgB;CA+B/B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,kBAAkB,EAAE,MAAM,EAC1B,gBAAgB,EAAE,MAAM,GAAG,SAAS,GACnC,MAAM,CA+CR"}
@@ -6,7 +6,8 @@ import { createHash } from "node:crypto";
6
6
  import { diag } from "@opentelemetry/api";
7
7
  import { FileAccessControl } from "./fileAccessControl.js";
8
8
  import { confirmDirExists, getShallowDirectorySize } from "./fileSystemHelpers.js";
9
- import { readdir, readFile, stat, unlink, writeFile } from "node:fs/promises";
9
+ import { constants as fsConstants } from "node:fs";
10
+ import { open, readdir, readFile, stat, unlink } from "node:fs/promises";
10
11
  import { DropCode, ExceptionType } from "../../../export/statsbeat/types.js";
11
12
  /**
12
13
  * File system persist class.
@@ -154,12 +155,18 @@ export class FileSystemPersist {
154
155
  diag.warn(`Error while checking size of persistence directory: `, error && error.message);
155
156
  return false;
156
157
  }
157
- const fileName = `${new Date().getTime()}${FileSystemPersist.FILENAME_SUFFIX}`;
158
+ const fileName = `${new Date().getTime()}-${process.hrtime.bigint()}${FileSystemPersist.FILENAME_SUFFIX}`;
158
159
  const fileFullPath = join(this._tempDirectory, fileName);
159
160
  // Mode 600 is w/r for creator and no read access for others
160
161
  diag.info(`saving data to disk at: ${fileFullPath}`);
161
162
  try {
162
- await writeFile(fileFullPath, payload, { mode: 0o600 });
163
+ const handle = await open(fileFullPath, fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY, 0o600);
164
+ try {
165
+ await handle.writeFile(payload);
166
+ }
167
+ finally {
168
+ await handle.close();
169
+ }
163
170
  }
164
171
  catch (writeError) {
165
172
  // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user
@@ -179,7 +186,7 @@ export class FileSystemPersist {
179
186
  return false;
180
187
  }
181
188
  else {
182
- files.forEach(async (file) => {
189
+ for (const file of files) {
183
190
  // Check expiration
184
191
  const fileCreationDate = new Date(parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]));
185
192
  const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;
@@ -187,7 +194,7 @@ export class FileSystemPersist {
187
194
  const filePath = join(this._tempDirectory, file);
188
195
  await unlink(filePath);
189
196
  }
190
- });
197
+ }
191
198
  return true;
192
199
  }
193
200
  }
@@ -1 +1 @@
1
- {"version":3,"file":"fileSystemPersist.js","sourceRoot":"","sources":["../../../../../src/platform/nodejs/persist/fileSystemPersist.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEnF,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAG7E;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAelB;IACA;IAfV,MAAM,CAAC,cAAc,GAAG,uBAAuB,CAAC;IAChD,MAAM,CAAC,eAAe,GAAG,UAAU,CAAC;IAEpC,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,G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from \"node:os\";\nimport { basename, join, dirname } from \"node:path\";\nimport { createHash } from \"node:crypto\";\nimport { diag } from \"@opentelemetry/api\";\nimport type { PersistentStorage } from \"../../../types.js\";\nimport { FileAccessControl } from \"./fileAccessControl.js\";\nimport { confirmDirExists, getShallowDirectorySize } from \"./fileSystemHelpers.js\";\nimport type { AzureMonitorExporterOptions } from \"../../../config.js\";\nimport { readdir, readFile, stat, unlink, writeFile } from \"node:fs/promises\";\nimport type { CustomerSDKStatsMetrics } from \"../../../export/statsbeat/customerSDKStats.js\";\nimport { DropCode, ExceptionType } from \"../../../export/statsbeat/types.js\";\nimport type { TelemetryItem as Envelope } from \"../../../generated/index.js\";\n\n/**\n * File system persist class.\n * @internal\n */\nexport class FileSystemPersist implements PersistentStorage {\n static TEMPDIR_PREFIX = \"opentelemetry-nodejs-\";\n static FILENAME_SUFFIX = \".ai.json\";\n\n fileRetemptionPeriod = 2 * 24 * 60 * 60 * 1000; // 2 days\n cleanupTimeOut = 60 * 60 * 1000; // 1 hour\n maxBytesOnDisk: number = 50_000_000; // ~50MB\n\n private _enabled: boolean;\n private _tempDirectory: string = \"\";\n private _fileCleanupTimer: NodeJS.Timeout | null = null;\n private _instrumentationKey: string;\n\n constructor(\n instrumentationKey: string,\n private _options?: AzureMonitorExporterOptions,\n private _customerSDKStatsMetrics?: CustomerSDKStatsMetrics,\n ) {\n this._instrumentationKey = instrumentationKey;\n if (this._options?.disableOfflineStorage) {\n this._enabled = false;\n return;\n }\n this._enabled = true;\n FileAccessControl.checkFileProtection();\n\n if (!FileAccessControl.OS_PROVIDES_FILE_PROTECTION) {\n this._enabled = false;\n diag.error(\n \"Sufficient file protection capabilities were not detected. Files will not be persisted\",\n );\n }\n\n if (!this._instrumentationKey) {\n this._enabled = false;\n diag.error(\n `No instrumentation key was provided to FileSystemPersister. Files will not be persisted`,\n );\n }\n if (this._enabled) {\n this._tempDirectory = getStorageDirectory(\n this._instrumentationKey,\n this._options?.storageDirectory,\n );\n\n // Starts file cleanup task\n if (!this._fileCleanupTimer) {\n this._fileCleanupTimer = setTimeout(() => {\n this._fileCleanupTask();\n }, this.cleanupTimeOut);\n this._fileCleanupTimer.unref();\n }\n }\n }\n\n push(value: unknown[]): Promise<boolean> {\n if (this._enabled) {\n diag.debug(\"Pushing value to persistent storage\", value.toString());\n return this._storeToDisk(JSON.stringify(value), value as Envelope[]);\n }\n // Only return a false promise if the SDK isn't set to disable offline storage\n if (!this._options?.disableOfflineStorage) {\n return new Promise((resolve) => {\n resolve(false);\n });\n }\n return new Promise((resolve) => {\n resolve(true);\n });\n }\n\n async shift(): Promise<unknown> {\n if (this._enabled) {\n diag.debug(\"Searching for filesystem persisted files\");\n try {\n const buffer = await this._getFirstFileOnDisk();\n if (buffer) {\n return JSON.parse(buffer.toString(\"utf8\"));\n }\n } catch (e: any) {\n diag.debug(\"Failed to read persisted file\", e);\n }\n return null;\n }\n return new Promise((resolve) => {\n resolve(null);\n });\n }\n\n /**\n * Check for temp telemetry files\n * reads the first file if exist, deletes it and tries to send its load\n */\n private async _getFirstFileOnDisk(): Promise<Buffer | null> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return null;\n } else {\n const firstFile = files[0];\n const filePath = join(this._tempDirectory, firstFile);\n const payload = await readFile(filePath);\n // delete the file first to prevent double sending\n await unlink(filePath);\n return payload;\n }\n }\n return null;\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n // File does not exist -- return null instead of throwing\n return null;\n } else {\n throw e;\n }\n }\n }\n\n /**\n * Stores telemetry data to disk.\n * @param payload - The telemetry data to store.\n * @param envelopeLength -The length of the telemetry envelope.\n * @returns A promise that resolves to true if the data was stored successfully, false otherwise.\n */\n private async _storeToDisk(payload: string, envelopes: Envelope[]): Promise<boolean> {\n try {\n await confirmDirExists(this._tempDirectory);\n } catch (error: any) {\n // Check if error is due to permission/readonly issues\n if (error?.code === \"EACCES\" || error?.code === \"EPERM\") {\n this._customerSDKStatsMetrics?.countDroppedItems(envelopes, DropCode.CLIENT_READONLY);\n diag.warn(\n `Permission denied while checking/creating directory: ${this._tempDirectory}`,\n error?.message,\n );\n } else {\n diag.warn(`Error while checking/creating directory: `, error && error.message);\n }\n return false;\n }\n\n try {\n const size = await getShallowDirectorySize(this._tempDirectory);\n if (size > this.maxBytesOnDisk) {\n // If the directory size exceeds the max limit, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_PERSISTENCE_CAPACITY,\n );\n diag.warn(\n `Not saving data due to max size limit being met. Directory size in bytes is: ${size}`,\n );\n return false;\n }\n } catch (error: any) {\n diag.warn(`Error while checking size of persistence directory: `, error && error.message);\n return false;\n }\n\n const fileName = `${new Date().getTime()}${FileSystemPersist.FILENAME_SUFFIX}`;\n const fileFullPath = join(this._tempDirectory, fileName);\n\n // Mode 600 is w/r for creator and no read access for others\n diag.info(`saving data to disk at: ${fileFullPath}`);\n try {\n await writeFile(fileFullPath, payload, { mode: 0o600 });\n } catch (writeError: any) {\n // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_EXCEPTION,\n writeError?.message,\n ExceptionType.STORAGE_EXCEPTION,\n );\n diag.warn(`Error writing file to persistent file storage`, writeError);\n return false;\n }\n return true;\n }\n\n private async _fileCleanupTask(): Promise<boolean> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return false;\n } else {\n files.forEach(async (file) => {\n // Check expiration\n const fileCreationDate: Date = new Date(\n parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]),\n );\n const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;\n if (expired) {\n const filePath = join(this._tempDirectory, file);\n await unlink(filePath);\n }\n });\n return true;\n }\n }\n return false;\n } catch (error: any) {\n diag.info(`Failed cleanup of persistent file storage expired files`, error);\n return false;\n }\n }\n}\n\n/**\n * Return the deterministic local storage path for a given instrumentation key.\n *\n * On shared Linux hosts the first user to create `/tmp/Microsoft/AzureMonitor` can\n * block others because the directory inherits that user's `umask`. This is avoided by\n * inserting a hash of the instrumentation key, user name, process name, and\n * application directory, giving each user their own subdirectory, e.g.\n * `/tmp/Microsoft-AzureMonitor-1234...../opentelemetry-nodejs-<ikey>`.\n *\n * @param instrumentationKey - Application Insights instrumentation key.\n * @param storageDirectory - Optional custom storage directory path. If not provided, system temp directory is used.\n * @returns Absolute path to the storage directory.\n * @internal\n */\nexport function getStorageDirectory(\n instrumentationKey: string,\n storageDirectory: string | undefined,\n): string {\n let userSegment: string;\n let processName: string;\n let applicationDirectory: string;\n try {\n const user = userInfo();\n userSegment = user.username;\n } catch (error) {\n userSegment = \"\";\n }\n if (process.title.length > 0) {\n processName = process.title;\n } else {\n processName = \"\";\n }\n const applicationDir = dirname(process.cwd() || process.argv[1]);\n if (applicationDir) {\n applicationDirectory = applicationDir;\n } else {\n applicationDirectory = \"\";\n }\n const hash_input = `${instrumentationKey}|${userSegment}|${processName}|${applicationDirectory}`;\n\n let subDirectory: string;\n try {\n subDirectory = createHash(\"sha256\").update(hash_input).digest(\"hex\");\n } catch (error) {\n let hash = 5381;\n for (let i = 0; i < hash_input.length; i++) {\n const char = hash_input.charCodeAt(i);\n hash = (hash << 5) + hash + char;\n hash = hash & hash;\n }\n subDirectory = Math.abs(hash).toString(16);\n }\n\n let sharedRoot: string;\n if (storageDirectory) {\n sharedRoot = storageDirectory;\n } else {\n sharedRoot = tmpdir();\n }\n return join(\n sharedRoot,\n \"Microsoft-AzureMonitor-\" + subDirectory,\n FileSystemPersist.TEMPDIR_PREFIX + instrumentationKey,\n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AAC;SAAM,CAAC;QACN,oBAAoB,GAAG,EAAE,CAAC;IAC5B,CAAC;IACD,MAAM,UAAU,GAAG,GAAG,kBAAkB,IAAI,WAAW,IAAI,WAAW,IAAI,oBAAoB,EAAE,CAAC;IAEjG,IAAI,YAAoB,CAAC;IACzB,IAAI,CAAC;QACH,YAAY,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3C,MAAM,IAAI,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YACtC,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;YACjC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,IAAI,UAAkB,CAAC;IACvB,IAAI,gBAAgB,EAAE,CAAC;QACrB,UAAU,GAAG,gBAAgB,CAAC;IAChC,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,MAAM,EAAE,CAAC;IACxB,CAAC;IACD,OAAO,IAAI,CACT,UAAU,EACV,yBAAyB,GAAG,YAAY,EACxC,iBAAiB,CAAC,cAAc,GAAG,kBAAkB,CACtD,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport { tmpdir, userInfo } from \"node:os\";\nimport { basename, join, dirname } from \"node:path\";\nimport { createHash } from \"node:crypto\";\nimport { diag } from \"@opentelemetry/api\";\nimport type { PersistentStorage } from \"../../../types.js\";\nimport { FileAccessControl } from \"./fileAccessControl.js\";\nimport { confirmDirExists, getShallowDirectorySize } from \"./fileSystemHelpers.js\";\nimport type { AzureMonitorExporterOptions } from \"../../../config.js\";\nimport { constants as fsConstants } from \"node:fs\";\nimport { open, readdir, readFile, stat, unlink } from \"node:fs/promises\";\nimport type { CustomerSDKStatsMetrics } from \"../../../export/statsbeat/customerSDKStats.js\";\nimport { DropCode, ExceptionType } from \"../../../export/statsbeat/types.js\";\nimport type { TelemetryItem as Envelope } from \"../../../generated/index.js\";\n\n/**\n * File system persist class.\n * @internal\n */\nexport class FileSystemPersist implements PersistentStorage {\n static TEMPDIR_PREFIX = \"opentelemetry-nodejs-\";\n static FILENAME_SUFFIX = \".ai.json\";\n\n fileRetemptionPeriod = 2 * 24 * 60 * 60 * 1000; // 2 days\n cleanupTimeOut = 60 * 60 * 1000; // 1 hour\n maxBytesOnDisk: number = 50_000_000; // ~50MB\n\n private _enabled: boolean;\n private _tempDirectory: string = \"\";\n private _fileCleanupTimer: NodeJS.Timeout | null = null;\n private _instrumentationKey: string;\n\n constructor(\n instrumentationKey: string,\n private _options?: AzureMonitorExporterOptions,\n private _customerSDKStatsMetrics?: CustomerSDKStatsMetrics,\n ) {\n this._instrumentationKey = instrumentationKey;\n if (this._options?.disableOfflineStorage) {\n this._enabled = false;\n return;\n }\n this._enabled = true;\n FileAccessControl.checkFileProtection();\n\n if (!FileAccessControl.OS_PROVIDES_FILE_PROTECTION) {\n this._enabled = false;\n diag.error(\n \"Sufficient file protection capabilities were not detected. Files will not be persisted\",\n );\n }\n\n if (!this._instrumentationKey) {\n this._enabled = false;\n diag.error(\n `No instrumentation key was provided to FileSystemPersister. Files will not be persisted`,\n );\n }\n if (this._enabled) {\n this._tempDirectory = getStorageDirectory(\n this._instrumentationKey,\n this._options?.storageDirectory,\n );\n\n // Starts file cleanup task\n if (!this._fileCleanupTimer) {\n this._fileCleanupTimer = setTimeout(() => {\n this._fileCleanupTask();\n }, this.cleanupTimeOut);\n this._fileCleanupTimer.unref();\n }\n }\n }\n\n push(value: unknown[]): Promise<boolean> {\n if (this._enabled) {\n diag.debug(\"Pushing value to persistent storage\", value.toString());\n return this._storeToDisk(JSON.stringify(value), value as Envelope[]);\n }\n // Only return a false promise if the SDK isn't set to disable offline storage\n if (!this._options?.disableOfflineStorage) {\n return new Promise((resolve) => {\n resolve(false);\n });\n }\n return new Promise((resolve) => {\n resolve(true);\n });\n }\n\n async shift(): Promise<unknown> {\n if (this._enabled) {\n diag.debug(\"Searching for filesystem persisted files\");\n try {\n const buffer = await this._getFirstFileOnDisk();\n if (buffer) {\n return JSON.parse(buffer.toString(\"utf8\"));\n }\n } catch (e: any) {\n diag.debug(\"Failed to read persisted file\", e);\n }\n return null;\n }\n return new Promise((resolve) => {\n resolve(null);\n });\n }\n\n /**\n * Check for temp telemetry files\n * reads the first file if exist, deletes it and tries to send its load\n */\n private async _getFirstFileOnDisk(): Promise<Buffer | null> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return null;\n } else {\n const firstFile = files[0];\n const filePath = join(this._tempDirectory, firstFile);\n const payload = await readFile(filePath);\n // delete the file first to prevent double sending\n await unlink(filePath);\n return payload;\n }\n }\n return null;\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n // File does not exist -- return null instead of throwing\n return null;\n } else {\n throw e;\n }\n }\n }\n\n /**\n * Stores telemetry data to disk.\n * @param payload - The telemetry data to store.\n * @param envelopeLength -The length of the telemetry envelope.\n * @returns A promise that resolves to true if the data was stored successfully, false otherwise.\n */\n private async _storeToDisk(payload: string, envelopes: Envelope[]): Promise<boolean> {\n try {\n await confirmDirExists(this._tempDirectory);\n } catch (error: any) {\n // Check if error is due to permission/readonly issues\n if (error?.code === \"EACCES\" || error?.code === \"EPERM\") {\n this._customerSDKStatsMetrics?.countDroppedItems(envelopes, DropCode.CLIENT_READONLY);\n diag.warn(\n `Permission denied while checking/creating directory: ${this._tempDirectory}`,\n error?.message,\n );\n } else {\n diag.warn(`Error while checking/creating directory: `, error && error.message);\n }\n return false;\n }\n\n try {\n const size = await getShallowDirectorySize(this._tempDirectory);\n if (size > this.maxBytesOnDisk) {\n // If the directory size exceeds the max limit, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_PERSISTENCE_CAPACITY,\n );\n diag.warn(\n `Not saving data due to max size limit being met. Directory size in bytes is: ${size}`,\n );\n return false;\n }\n } catch (error: any) {\n diag.warn(`Error while checking size of persistence directory: `, error && error.message);\n return false;\n }\n\n const fileName = `${new Date().getTime()}-${process.hrtime.bigint()}${FileSystemPersist.FILENAME_SUFFIX}`;\n const fileFullPath = join(this._tempDirectory, fileName);\n\n // Mode 600 is w/r for creator and no read access for others\n diag.info(`saving data to disk at: ${fileFullPath}`);\n try {\n const handle = await open(\n fileFullPath,\n fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY,\n 0o600,\n );\n try {\n await handle.writeFile(payload);\n } finally {\n await handle.close();\n }\n } catch (writeError: any) {\n // If the envelopes cannot be written to disk, we send customer SDK Stats and warn the user\n this._customerSDKStatsMetrics?.countDroppedItems(\n envelopes,\n DropCode.CLIENT_EXCEPTION,\n writeError?.message,\n ExceptionType.STORAGE_EXCEPTION,\n );\n diag.warn(`Error writing file to persistent file storage`, writeError);\n return false;\n }\n return true;\n }\n\n private async _fileCleanupTask(): Promise<boolean> {\n try {\n const stats = await stat(this._tempDirectory);\n if (stats.isDirectory()) {\n const origFiles = await readdir(this._tempDirectory);\n const files = origFiles.filter((f) =>\n basename(f).includes(FileSystemPersist.FILENAME_SUFFIX),\n );\n if (files.length === 0) {\n return false;\n } else {\n for (const file of files) {\n // Check expiration\n const fileCreationDate: Date = new Date(\n parseInt(file.split(FileSystemPersist.FILENAME_SUFFIX)[0]),\n );\n const expired = new Date(+new Date() - this.fileRetemptionPeriod) > fileCreationDate;\n if (expired) {\n const filePath = join(this._tempDirectory, file);\n await unlink(filePath);\n }\n }\n return true;\n }\n }\n return false;\n } catch (error: any) {\n diag.info(`Failed cleanup of persistent file storage expired files`, error);\n return false;\n }\n }\n}\n\n/**\n * Return the deterministic local storage path for a given instrumentation key.\n *\n * On shared Linux hosts the first user to create `/tmp/Microsoft/AzureMonitor` can\n * block others because the directory inherits that user's `umask`. This is avoided by\n * inserting a hash of the instrumentation key, user name, process name, and\n * application directory, giving each user their own subdirectory, e.g.\n * `/tmp/Microsoft-AzureMonitor-1234...../opentelemetry-nodejs-<ikey>`.\n *\n * @param instrumentationKey - Application Insights instrumentation key.\n * @param storageDirectory - Optional custom storage directory path. If not provided, system temp directory is used.\n * @returns Absolute path to the storage directory.\n * @internal\n */\nexport function getStorageDirectory(\n instrumentationKey: string,\n storageDirectory: string | undefined,\n): string {\n let userSegment: string;\n let processName: string;\n let applicationDirectory: string;\n try {\n const user = userInfo();\n userSegment = user.username;\n } catch (error) {\n userSegment = \"\";\n }\n if (process.title.length > 0) {\n processName = process.title;\n } else {\n processName = \"\";\n }\n const applicationDir = dirname(process.cwd() || process.argv[1]);\n if (applicationDir) {\n applicationDirectory = applicationDir;\n } else {\n applicationDirectory = \"\";\n }\n const hash_input = `${instrumentationKey}|${userSegment}|${processName}|${applicationDirectory}`;\n\n let subDirectory: string;\n try {\n subDirectory = createHash(\"sha256\").update(hash_input).digest(\"hex\");\n } catch (error) {\n let hash = 5381;\n for (let i = 0; i < hash_input.length; i++) {\n const char = hash_input.charCodeAt(i);\n hash = (hash << 5) + hash + char;\n hash = hash & hash;\n }\n subDirectory = Math.abs(hash).toString(16);\n }\n\n let sharedRoot: string;\n if (storageDirectory) {\n sharedRoot = storageDirectory;\n } else {\n sharedRoot = tmpdir();\n }\n return join(\n sharedRoot,\n \"Microsoft-AzureMonitor-\" + subDirectory,\n FileSystemPersist.TEMPDIR_PREFIX + instrumentationKey,\n );\n}\n"]}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@azure/monitor-opentelemetry-exporter",
3
3
  "author": "Microsoft Corporation",
4
4
  "sdk-type": "client",
5
- "version": "1.0.0-alpha.20260122.1",
5
+ "version": "1.0.0-alpha.20260123.1",
6
6
  "description": "Application Insights exporter for the OpenTelemetry JavaScript (Node.js) SDK",
7
7
  "main": "./dist/commonjs/index.js",
8
8
  "module": "./dist/esm/index.js",
@@ -66,9 +66,9 @@
66
66
  "typescript": "~5.9.3",
67
67
  "vitest": "^4.0.8",
68
68
  "@azure-tools/test-utils-vitest": "^2.0.1",
69
- "@azure/core-util": "^1.13.2-alpha.20260122.1",
70
- "@azure/dev-tool": "^1.0.0",
71
- "@azure/eslint-plugin-azure-sdk": "^3.0.0"
69
+ "@azure/core-util": "^1.13.2-alpha.20260123.1",
70
+ "@azure/eslint-plugin-azure-sdk": "^3.0.0",
71
+ "@azure/dev-tool": "^1.0.0"
72
72
  },
73
73
  "dependencies": {
74
74
  "@azure/core-auth": ">=1.10.2-alpha <1.10.2-alphb",