Skip to content

Commit

Permalink
Initialize SDK using registerOTel()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Oct 30, 2024
1 parent 0f9ba8a commit 5888a1c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use server";

import { metricReader } from "@/instrumentation.node";
import { openai } from "@ai-sdk/openai";
import { streamObject } from "ai";
import { createStreamableValue } from "ai/rsc";
Expand Down Expand Up @@ -59,22 +58,24 @@ export async function generateArtifactStream(
});
const subscriptionId = await getUserSubscriptionId();
const isR06User = await isRoute06User();
const environment = process.env.NEXT_PUBLIC_VERCEL_ENV || "not-set";

console.log("Adding metric:", {
totalTokens: result.usage.totalTokens,
subscriptionId,
isR06User,
timestamp: new Date().toISOString(),
environment,
});

tokenCounter.add(result.usage.totalTokens, {
subscriptionId,
isR06User,
environment,
});
generation.end({
output: result,
});
waitUntil(metricReader.forceFlush());
await lf.shutdownAsync();
console.log("end of onFinish");
},
Expand Down
49 changes: 0 additions & 49 deletions instrumentation.node.ts

This file was deleted.

36 changes: 35 additions & 1 deletion instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import type { ExportResult } from "@opentelemetry/core";
import type { ResourceMetrics } from "@opentelemetry/sdk-metrics";
import {
ConsoleMetricExporter,
PeriodicExportingMetricReader,
} from "@opentelemetry/sdk-metrics";
import * as Sentry from "@sentry/nextjs";
import { registerOTel } from "@vercel/otel";

class DebugConsoleMetricExporter extends ConsoleMetricExporter {
override async export(
metrics: ResourceMetrics,
resultCallback: (result: ExportResult) => void,
) {
console.log("Export called at:", new Date().toISOString());
console.log("Metrics to export:", JSON.stringify(metrics, null, 2));

return super.export(metrics, (result) => {
console.log("Export result:", result);
resultCallback(result);
});
}
}
// Configure OTLP metrics exporter
const otlpMetricsExporter = new DebugConsoleMetricExporter();

export const metricReader = new PeriodicExportingMetricReader({
exporter: otlpMetricsExporter,
exportIntervalMillis: 1000,
exportTimeoutMillis: 500,
});

export async function register() {
registerOTel({
serviceName: "serverside",
metricReader,
});

if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./instrumentation.node");
await import("./sentry.server.config");
}

Expand Down

0 comments on commit 5888a1c

Please sign in to comment.