Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/event-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
"@kubernetes/client-node": "^0.22.0",
"@octokit/auth-app": "catalog:",
"@octokit/rest": "catalog:",
"@opentelemetry/auto-instrumentations-node": "^0.52.1",
"@opentelemetry/exporter-logs-otlp-http": "^0.54.2",
"@opentelemetry/exporter-trace-otlp-http": "^0.54.2",
"@opentelemetry/resources": "^1.27.0",
"@opentelemetry/sdk-logs": "^0.54.2",
"@opentelemetry/sdk-node": "^0.54.2",
"@opentelemetry/sdk-trace-base": "^1.27.0",
"@opentelemetry/semantic-conventions": "^1.27.0",
"@smithy/types": "^3.7.1",
"@t3-oss/env-core": "catalog:",
"bullmq": "catalog:",
Expand Down
3 changes: 3 additions & 0 deletions apps/event-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { logger } from "@ctrlplane/logger";

import { register } from "./instrumentation.js";
import { createDispatchExecutionJobWorker } from "./job-dispatch/index.js";
import { redis } from "./redis.js";
import { createReleaseNewVersionWorker } from "./releases/new-version/index.js";
import { createReleaseVariableChangeWorker } from "./releases/variable-change/index.js";
import { createResourceScanWorker } from "./resource-scan/index.js";
import { workers } from "./workers/index.js";

await register();

const allWorkers = [
createResourceScanWorker(),
createDispatchExecutionJobWorker(),
Expand Down
57 changes: 57 additions & 0 deletions apps/event-worker/src/instrumentation-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { Resource } from "@opentelemetry/resources";
import { BatchLogRecordProcessor } from "@opentelemetry/sdk-logs";
import { NodeSDK } from "@opentelemetry/sdk-node";
import {
AlwaysOnSampler,
BatchSpanProcessor,
} from "@opentelemetry/sdk-trace-base";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

const sdk = new NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: "ctrlplane/event-worker",
}),
spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter())],
logRecordProcessors: [new BatchLogRecordProcessor(new OTLPLogExporter())],
instrumentations: [
getNodeAutoInstrumentations({
"@opentelemetry/instrumentation-fs": {
enabled: false,
},
"@opentelemetry/instrumentation-net": {
enabled: false,
},
"@opentelemetry/instrumentation-dns": {
enabled: false,
},
"@opentelemetry/instrumentation-http": {
enabled: true,
},
"@opentelemetry/instrumentation-pg": {
enabled: true,
enhancedDatabaseReporting: true,
addSqlCommenterCommentToQueries: true,
},
"@opentelemetry/instrumentation-ioredis": {
enabled: true,
},
"@opentelemetry/instrumentation-winston": {
enabled: true,
logHook: (span, record) => {
record["resource.service.name"] = "ctrlplane/event-worker";
},
},
}),
],
sampler: new AlwaysOnSampler(),
});

try {
sdk.start();
console.log("Tracing initialized");
} catch (error) {
console.error("Error initializing tracing", error);
}
Comment on lines +52 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider graceful shutdown of the OpenTelemetry SDK

The SDK is started correctly, but there's no mechanism to shut it down gracefully when the application terminates. Consider adding a shutdown function that can be called during the application's shutdown process.

try {
  sdk.start();
  console.log("Tracing initialized");
+  
+  // Register shutdown handler
+  process.on('beforeExit', async () => {
+    await sdk.shutdown();
+    console.log("Tracing terminated");
+  });
} catch (error) {
  console.error("Error initializing tracing", error);
}

Alternatively, you could export a shutdown function that can be called from the main shutdown handler in index.ts:

try {
  sdk.start();
  console.log("Tracing initialized");
} catch (error) {
  console.error("Error initializing tracing", error);
}

+export async function shutdownTracing() {
+  return sdk.shutdown();
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
sdk.start();
console.log("Tracing initialized");
} catch (error) {
console.error("Error initializing tracing", error);
}
try {
sdk.start();
console.log("Tracing initialized");
// Register shutdown handler
process.on('beforeExit', async () => {
await sdk.shutdown();
console.log("Tracing terminated");
});
} catch (error) {
console.error("Error initializing tracing", error);
}
Suggested change
try {
sdk.start();
console.log("Tracing initialized");
} catch (error) {
console.error("Error initializing tracing", error);
}
try {
sdk.start();
console.log("Tracing initialized");
} catch (error) {
console.error("Error initializing tracing", error);
}
export async function shutdownTracing() {
return sdk.shutdown();
}

4 changes: 4 additions & 0 deletions apps/event-worker/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs")
await import("./instrumentation-node.js");
}
6 changes: 6 additions & 0 deletions apps/webservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
"@octokit/webhooks-types": "^7.5.1",
"@openreplay/tracker": "^14.0.14",
"@opentelemetry/auto-instrumentations-node": "^0.52.1",
"@opentelemetry/exporter-logs-otlp-http": "^0.54.2",
"@opentelemetry/exporter-trace-otlp-http": "^0.54.2",
"@opentelemetry/resources": "^1.27.0",
"@opentelemetry/sdk-logs": "^0.54.2",
"@opentelemetry/sdk-node": "^0.54.2",
"@opentelemetry/sdk-trace-base": "^1.27.0",
"@opentelemetry/semantic-conventions": "^1.27.0",
"@t3-oss/env-nextjs": "catalog:",
"@tabler/icons-react": "catalog:",
"@tailwindcss/typography": "^0.5.16",
Expand Down
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading