diff --git a/sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md index 5f916b240c1d..74d6975b2e33 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md @@ -1,5 +1,15 @@ # Release History +## 1.0.0-beta.20 () + +### Features Added + +### Bugs Fixed + +- Added exception handling for reading files to avoid concurrency errors. + +### Other Changes + ## 1.0.0-beta.19 (2024-01-23) ### Features Added diff --git a/sdk/monitor/monitor-opentelemetry-exporter/src/platform/nodejs/persist/fileSystemHelpers.ts b/sdk/monitor/monitor-opentelemetry-exporter/src/platform/nodejs/persist/fileSystemHelpers.ts index 25420d347669..73ac7e6b0e23 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/src/platform/nodejs/persist/fileSystemHelpers.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/src/platform/nodejs/persist/fileSystemHelpers.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { diag } from "@opentelemetry/api"; import * as fs from "fs"; import * as path from "path"; import { promisify } from "util"; @@ -15,20 +16,24 @@ const mkdirAsync = promisify(fs.mkdir); * @internal */ export const getShallowDirectorySize = async (directory: string): Promise => { - // Get the directory listing - const files = await readdirAsync(directory); - let totalSize = 0; + try { + // Get the directory listing + const files = await readdirAsync(directory); - // Query all file sizes - for (const file of files) { - const fileStats = await statAsync(path.join(directory, file)); - if (fileStats.isFile()) { - totalSize += fileStats.size; + // Query all file sizes + for (const file of files) { + const fileStats = await statAsync(path.join(directory, file)); + if (fileStats.isFile()) { + totalSize += fileStats.size; + } } - } - return totalSize; + return totalSize; + } catch (err) { + diag.error(`Error getting directory size: ${err}`); + return 0; + } }; /** diff --git a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/fileSystemPersist.test.ts b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/fileSystemPersist.test.ts index 6f82d4183bee..f8c20a9e2564 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/fileSystemPersist.test.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/fileSystemPersist.test.ts @@ -46,7 +46,9 @@ const assertFirstFile = async (tempDir: string, expectation: unknown): Promise path.basename(f).includes(".ai.json")); + const files = origFiles.filter((f) => + path.basename(f).includes(FileSystemPersist.FILENAME_SUFFIX), + ); assert.ok(files.length > 0); // Assert file matches expectation