Skip to content

Commit

Permalink
feat: add axiom telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniciusCestarii committed Dec 4, 2024
1 parent 385b0e6 commit ac7eb55
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
DATABASE_URL=postgres://<username>:<password>@<host>:<port>/<database>
AXIOM_TOKEN=your_token
AXIOM_DATASET=modular-pets
PORT=3333
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"db:clean": "bun run scripts/clean-db.ts"
},
"dependencies": {
"@elysiajs/opentelemetry": "^1.1.7",
"@elysiajs/swagger": "^1.1.6",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.36.4",
Expand Down
2 changes: 2 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { z } from "zod";
const envSchema = z.object({
PORT: z.coerce.number().default(3333),
DATABASE_URL: z.string().url(),
AXIOM_DATASET: z.string().optional(),
AXIOM_TOKEN: z.string().optional(),
});

const _env = envSchema.safeParse(process.env);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import petsRoutes from "./modules/pet/pets/routes";
import speciesRoutes from "./modules/pet/species/routes";
import swagger from "@elysiajs/swagger";
import breedsRoutes from "./modules/pet/breeds/routes";
import { axiomTelemetry } from "./modules/shared/utilities/telemetry";

const app = new Elysia()
.use(axiomTelemetry())
.use(swagger())
.use(petsRoutes)
.use(speciesRoutes)
Expand Down
27 changes: 27 additions & 0 deletions src/modules/shared/utilities/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { env } from "@/env";
import { opentelemetry } from "@elysiajs/opentelemetry";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-node";
import Elysia from "elysia";

export const axiomTelemetry = () => (app: Elysia) => {
if (!env.AXIOM_DATASET || !env.AXIOM_TOKEN) {
return app;
}

console.info("Setting up telemetry");

return opentelemetry({
spanProcessors: [
new BatchSpanProcessor(
new OTLPTraceExporter({
url: "https://api.axiom.co/v1/traces",
headers: {
Authorization: `Bearer ${env.AXIOM_TOKEN}`,
"X-Axiom-Dataset": env.AXIOM_DATASET,
},
}),
),
],
});
};

0 comments on commit ac7eb55

Please sign in to comment.