From 1f7b2cc5320e865eadf461c60aa94ad0d4dbf2fc Mon Sep 17 00:00:00 2001 From: Benno van den Berg Date: Fri, 17 Jan 2025 12:46:41 +0100 Subject: [PATCH] Optionally add a AuthToken when exporting OTLP payloads (#440) * Optionally add a AuthToken when exporting OTLP payloads * run: pnpm format * Use shorthand --- lilo/lilo-worker/add-allowed-user.ts | 16 +++--- lilo/lilo-worker/src/index.tsx | 49 ++++++++++--------- packages/client-library-otel/src/constants.ts | 1 + .../src/instrumentation.ts | 10 ++++ 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lilo/lilo-worker/add-allowed-user.ts b/lilo/lilo-worker/add-allowed-user.ts index 8a761409c..b417c1506 100644 --- a/lilo/lilo-worker/add-allowed-user.ts +++ b/lilo/lilo-worker/add-allowed-user.ts @@ -1,18 +1,18 @@ import fs from "node:fs"; import path from "node:path"; +import { createClient } from "@libsql/client"; import { config } from "dotenv"; import { - drizzle as drizzleLibsql, type LibSQLDatabase, + drizzle as drizzleLibsql, } from "drizzle-orm/libsql"; // import type { SQLiteTable } from "drizzle-orm/sqlite-core"; import { type AsyncBatchRemoteCallback, type AsyncRemoteCallback, - drizzle as drizzleSQLiteProxy, type SqliteRemoteDatabase, + drizzle as drizzleSQLiteProxy, } from "drizzle-orm/sqlite-proxy"; -import { createClient } from "@libsql/client"; import * as schema from "./src/db/schema"; @@ -22,9 +22,10 @@ type Any = any; addAllowedUser(); async function addAllowedUser() { - const db = process.env.ENVIRONMENT === "production" - ? await getProductionDatabase() - : await getLocalDatabase(); + const db = + process.env.ENVIRONMENT === "production" + ? await getProductionDatabase() + : await getLocalDatabase(); const args = process.argv.slice(2); if (args.length === 0) { @@ -64,7 +65,6 @@ async function getLocalDatabase(): Promise< return drizzleLibsql(client); } - /** * Creates a connection to the production Cloudflare D1 database and returns a Drizzle ORM instance. * Loads production environment variables from .prod.vars file. @@ -237,4 +237,4 @@ function getLocalSQLiteDBPath() { throw new Error("Error resolving local D1 DB", { cause: error }); } -} \ No newline at end of file +} diff --git a/lilo/lilo-worker/src/index.tsx b/lilo/lilo-worker/src/index.tsx index 3d10c4300..2c4280c3f 100644 --- a/lilo/lilo-worker/src/index.tsx +++ b/lilo/lilo-worker/src/index.tsx @@ -1,3 +1,4 @@ +import { createMiddleware } from "@fiberplane/embedded"; import { instrument } from "@fiberplane/hono-otel"; import { OpenAPIHono } from "@hono/zod-openapi"; import { logger } from "hono/logger"; @@ -7,7 +8,6 @@ import { internalApiRouter } from "./routes/internal/api"; import { dashboardAuthRouter } from "./routes/internal/auth"; import { apiReferenceRouter } from "./routes/reference"; import type { AppType } from "./types"; -import { createMiddleware } from "@fiberplane/embedded"; const app = new OpenAPIHono(); @@ -43,30 +43,33 @@ app.doc("/doc", (c) => ({ })); // Mount the Fiberplane playground to play with the API -app.use("/fp/*", createMiddleware({ - cdn: "https://cdn.jsdelivr.net/npm/@fiberplane/embedded/dist/playground/", - // @ts-expect-error - The imported spec does not match our expected OpenAPIv3 type - spec: app.getOpenAPIDocument({ - openapi: "3.0.0", - info: { - title: "Lilo API", - version: "0.0.1", - description: "API documentation for Lilo", - }, - servers: [ - { - // url: new URL(c.req.url).origin, - url: "http://localhost:6246", - description: "Local", +app.use( + "/fp/*", + createMiddleware({ + cdn: "https://cdn.jsdelivr.net/npm/@fiberplane/embedded/dist/playground/", + // @ts-expect-error - The imported spec does not match our expected OpenAPIv3 type + spec: app.getOpenAPIDocument({ + openapi: "3.0.0", + info: { + title: "Lilo API", + version: "0.0.1", + description: "API documentation for Lilo", }, - { - // url: new URL(c.req.url).origin, - url: "https://lilo.fp.dev", - description: "Production", - }, - ], + servers: [ + { + // url: new URL(c.req.url).origin, + url: "http://localhost:6246", + description: "Local", + }, + { + // url: new URL(c.req.url).origin, + url: "https://lilo.fp.dev", + description: "Production", + }, + ], + }), }), -})); +); app.route("/reference", apiReferenceRouter); diff --git a/packages/client-library-otel/src/constants.ts b/packages/client-library-otel/src/constants.ts index a5351e89e..f1e5d3d73 100644 --- a/packages/client-library-otel/src/constants.ts +++ b/packages/client-library-otel/src/constants.ts @@ -1,6 +1,7 @@ /** * Constants for the environment variables we use to configure the library. */ +export const ENV_FPX_AUTH_TOKEN = "FPX_AUTH_TOKEN"; export const ENV_FPX_ENDPOINT = "FPX_ENDPOINT"; export const ENV_FPX_LOG_LEVEL = "FPX_LOG_LEVEL"; export const ENV_FPX_SERVICE_NAME = "FPX_SERVICE_NAME"; diff --git a/packages/client-library-otel/src/instrumentation.ts b/packages/client-library-otel/src/instrumentation.ts index a634e0a91..f7bd05ac8 100644 --- a/packages/client-library-otel/src/instrumentation.ts +++ b/packages/client-library-otel/src/instrumentation.ts @@ -11,6 +11,7 @@ import type { ExecutionContext } from "hono"; // TODO figure out we can use something else import { AsyncLocalStorageContextManager } from "./async-hooks"; import { + ENV_FPX_AUTH_TOKEN, ENV_FPX_ENDPOINT, ENV_FPX_LOG_LEVEL, ENV_FPX_SERVICE_NAME, @@ -115,6 +116,8 @@ export function instrument(app: HonoLikeApp, config?: FpxConfigOptions) { const endpoint = getFromEnv(env, ENV_FPX_ENDPOINT); const isEnabled = !!endpoint && typeof endpoint === "string"; + const authToken = getFromEnv(env, ENV_FPX_AUTH_TOKEN); + const FPX_LOG_LEVEL = libraryDebugMode ? "debug" : getFromEnv(env, ENV_FPX_LOG_LEVEL); @@ -166,6 +169,7 @@ export function instrument(app: HonoLikeApp, config?: FpxConfigOptions) { const provider = setupTracerProvider({ serviceName, endpoint, + authToken: authToken || undefined, }); const promiseStore = new PromiseStore(); @@ -292,6 +296,7 @@ export function instrument(app: HonoLikeApp, config?: FpxConfigOptions) { function setupTracerProvider(options: { serviceName: string; endpoint: string; + authToken?: string; }) { // We need to use async hooks to be able to propagate context const asyncHooksContextManager = new AsyncLocalStorageContextManager(); @@ -303,8 +308,13 @@ function setupTracerProvider(options: { }), }); + const headers: Record = options.authToken + ? { Authorization: `Bearer ${options.authToken}` } + : {}; + const exporter = new OTLPTraceExporter({ url: options.endpoint, + headers, }); provider.addSpanProcessor( new SimpleSpanProcessor(exporter),