Skip to content

Commit

Permalink
support user-provided OTel API in lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Jul 19, 2024
1 parent c4bc125 commit 2e74a66
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"{{name}}": "{{version}}",
"@opentelemetry/api": "{{api-version}}",
"@opentelemetry/exporter-trace-otlp-grpc": "{{exporters-version}}",
"@opentelemetry/exporter-metrics-otlp-grpc": "{{exporters-version}}"
"@opentelemetry/exporter-metrics-otlp-grpc": "{{exporters-version}}",
"semver": "^7"
}
}
1 change: 0 additions & 1 deletion lambda/shim.cjs

This file was deleted.

59 changes: 58 additions & 1 deletion lambda/shim.mjs
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)
}
8 changes: 1 addition & 7 deletions lambda/wrapper
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/bin/bash

# Only use --require to load if SW_APM_CJS is set
if [ -z "${SW_APM_CJS}" ]; then
export NODE_OPTIONS="${NODE_OPTIONS} --import /opt/solarwinds-apm/shim.mjs"
else
export NODE_OPTIONS="${NODE_OPTIONS} --require /opt/solarwinds-apm/shim.cjs"
fi
export NODE_OPTIONS="${NODE_OPTIONS} --import /opt/solarwinds-apm/shim.mjs"

# The default for Node is http/proto but we only include the gRPC exporters
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc"
Expand Down

0 comments on commit 2e74a66

Please sign in to comment.