Skip to content

Commit

Permalink
feat: open telemetry (#4664)
Browse files Browse the repository at this point in the history
* feat: OTEL tracing
* chore: document tracing functions
  • Loading branch information
TimBeyer authored Jun 28, 2023
1 parent 6d34525 commit 10aee8b
Show file tree
Hide file tree
Showing 26 changed files with 1,615 additions and 545 deletions.
4 changes: 4 additions & 0 deletions cli/bin/garden
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node


if (process.env.GARDEN_ENABLE_PROFILING === "1" || process.env.GARDEN_ENABLE_PROFILING === "true") {
// Patch require to profile module loading
const Mod = require("module")
Expand Down Expand Up @@ -34,6 +35,9 @@ if (process.env.GARDEN_ENABLE_PROFILING === "1" || process.env.GARDEN_ENABLE_PRO
}
}

const { initTracing } = require("@garden-io/core/build/src/util/tracing/tracing")
initTracing()

require("source-map-support").install()
const cli = require("../build/src/cli")

Expand Down
29 changes: 24 additions & 5 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { shutdown } from "@garden-io/core/build/src/util/util"
import { GardenCli, RunOutput } from "@garden-io/core/build/src/cli/cli"
import { GardenPluginReference } from "@garden-io/core/build/src/plugin/plugin"
import { GlobalConfigStore } from "@garden-io/core/build/src/config-store/global"
import { getOtelSDK } from "@garden-io/core/build/src/util/tracing/tracing"
import { withContextFromEnv } from "@garden-io/core/build/src/util/tracing/propagation"
import { wrapActiveSpan } from "@garden-io/core/build/src/util/tracing/spans"

// These plugins are always registered
export const getBundledPlugins = (): GardenPluginReference[] => [
Expand All @@ -35,11 +38,19 @@ export async function runCli({
}

try {
if (!cli) {
cli = new GardenCli({ plugins: getBundledPlugins(), initLogger })
}
// Note: We slice off the binary/script name from argv.
result = await cli.run({ args, exitOnError })
// initialize the tracing to capture the full cli execution
result = await withContextFromEnv(() =>
wrapActiveSpan("garden", async (span) => {
if (!cli) {
cli = new GardenCli({ plugins: getBundledPlugins(), initLogger })
}

// Note: We slice off the binary/script name from argv.
const results = await cli!.run({ args: args || [], exitOnError })

return results
})
)
code = result.code
} catch (err) {
// eslint-disable-next-line no-console
Expand All @@ -50,6 +61,14 @@ export async function runCli({
const globalConfigStore = new GlobalConfigStore()
await globalConfigStore.delete("activeProcesses", String(cli.processRecord.pid))
}

try {
await Promise.race([getOtelSDK().shutdown(), new Promise((resolve) => setTimeout(resolve, 3000))])
} catch (err) {
// eslint-disable-next-line no-console
console.log(`Debug: OTEL shutdown failed with error ${err.toString()}`)
}

await shutdown(code)
}

Expand Down
7 changes: 7 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"@codenamize/codenamize": "^1.1.1",
"@hapi/joi": "git+https://github.com/garden-io/joi.git#master",
"@kubernetes/client-node": "git+https://github.com/garden-io/javascript.git#master",
"@opentelemetry/api": "^1.4.1",
"@opentelemetry/sdk-trace-base": "^1.14.0",
"@opentelemetry/sdk-node": "^0.40.0",
"@opentelemetry/resources": "^1.14.0",
"@opentelemetry/semantic-conventions": "^1.14.0",
"@opentelemetry/instrumentation-http": "^0.40.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.40.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"analytics-node": "3.5.0",
Expand Down
Loading

0 comments on commit 10aee8b

Please sign in to comment.