Skip to content

Commit

Permalink
Optionally add a AuthToken when exporting OTLP payloads (#440)
Browse files Browse the repository at this point in the history
* Optionally add a AuthToken when exporting OTLP payloads

* run: pnpm format

* Use shorthand
  • Loading branch information
hatchan authored Jan 17, 2025
1 parent b6a05c8 commit 1f7b2cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
16 changes: 8 additions & 8 deletions lilo/lilo-worker/add-allowed-user.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -237,4 +237,4 @@ function getLocalSQLiteDBPath() {

throw new Error("Error resolving local D1 DB", { cause: error });
}
}
}
49 changes: 26 additions & 23 deletions lilo/lilo-worker/src/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<AppType>();

Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions packages/client-library-otel/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
10 changes: 10 additions & 0 deletions packages/client-library-otel/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -166,6 +169,7 @@ export function instrument(app: HonoLikeApp, config?: FpxConfigOptions) {
const provider = setupTracerProvider({
serviceName,
endpoint,
authToken: authToken || undefined,
});

const promiseStore = new PromiseStore();
Expand Down Expand Up @@ -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();
Expand All @@ -303,8 +308,13 @@ function setupTracerProvider(options: {
}),
});

const headers: Record<string, string> = options.authToken
? { Authorization: `Bearer ${options.authToken}` }
: {};

const exporter = new OTLPTraceExporter({
url: options.endpoint,
headers,
});
provider.addSpanProcessor(
new SimpleSpanProcessor(exporter),
Expand Down

0 comments on commit 1f7b2cc

Please sign in to comment.