-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support user-provided OTel API in lambda
- Loading branch information
1 parent
c4bc125
commit 2e74a66
Showing
4 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,58 @@ | ||
await import("{{name}}") | ||
import fs from "node:fs" | ||
import path from "node:path" | ||
import satisfies from "semver/functions/satisfies" | ||
|
||
try { | ||
const BUNDLED_API = "/opt/solarwinds-apm/node_modules/@opentelemetry/api" | ||
const dirs = [] | ||
|
||
// Add relative node_modules dirs to the search paths | ||
const TASK_ROOT = process.env.LAMBDA_TASK_ROOT | ||
if (TASK_ROOT) { | ||
let dir = TASK_ROOT | ||
while (true) { | ||
dirs.push(path.join(dir, "node_modules")) | ||
|
||
const parent = path.dirname(dir) | ||
if (dir !== parent) { | ||
dir = parent | ||
} else { | ||
break | ||
} | ||
} | ||
} | ||
|
||
// Add entries from NODE_PATH to the search paths | ||
const NODE_PATH = process.env.NODE_PATH?.split(":") ?? [] | ||
dirs.push(...NODE_PATH) | ||
|
||
// Search for a use-provided OTel API | ||
for (const dir of dirs) { | ||
const api = path.join(dir, "@opentelemetry", "api") | ||
let version | ||
try { | ||
// Try and read the package.json version in this search path | ||
version = JSON.parse( | ||
fs.readFileSync(path.join(api, "package.json"), { encoding: "utf-8" }), | ||
).version | ||
} catch { | ||
// No valid package.json found, search next path | ||
continue | ||
} | ||
|
||
try { | ||
// Try and override our bundled OTel API if the user provides it | ||
if (satisfies(version, "{{api-version}}")) { | ||
fs.rmSync(BUNDLED_API, { recursive: true, force: true }) | ||
fs.symlinkSync(api, BUNDLED_API) | ||
} | ||
} catch { | ||
// Something is wrong, bail | ||
break | ||
} | ||
} | ||
|
||
await import("{{name}}") | ||
} catch (error) { | ||
console.error(error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters