Skip to content

Commit

Permalink
[Monitor OpenTelemetry Exporter] Add Exception Handling to File Name …
Browse files Browse the repository at this point in the history
…for Telemetry Caching (#28399)

### Packages impacted by this PR
@azure/monitor-opentelemetry-exporter

### Issues associated with this PR
microsoft/ApplicationInsights-node.js#1230

### Describe the problem that is addressed by this PR
Append the process ID to the file name created for holding disk cached
telemetry. This should resolve the issue with multiple Azure Functions
cores attempting to read/write/delete the same file when functions are
scaled to use multiple cores.

Extended this logic outside of Azure Functions so that in any case where
the SDK could be run concurrently we create distinct cache files.

### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [x] Added a changelog (if necessary)
  • Loading branch information
JacksonWeber committed Feb 2, 2024
1 parent 209aa43 commit 0886249
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
10 changes: 10 additions & 0 deletions 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
Expand Down
@@ -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";
Expand All @@ -15,20 +16,24 @@ const mkdirAsync = promisify(fs.mkdir);
* @internal
*/
export const getShallowDirectorySize = async (directory: string): Promise<number> => {
// 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;
}
};

/**
Expand Down
Expand Up @@ -46,7 +46,9 @@ const assertFirstFile = async (tempDir: string, expectation: unknown): Promise<v

// Read the first file in tempDir
const origFiles = await readdirAsync(tempDir);
const files = origFiles.filter((f) => 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
Expand Down

0 comments on commit 0886249

Please sign in to comment.