Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
feat: adding opentelemetry (#257)
Browse files Browse the repository at this point in the history
* feat: adding opentelemetry

* test: fix test

---------

Co-authored-by: Nicolas Burtey <[email protected]>
  • Loading branch information
nicolasburtey and Nicolas Burtey authored Oct 2, 2023
1 parent d4aa4a3 commit f01165f
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ With a default installation, Admin Panel can be accessed with `admin.domain.com`

## How to run this repo locally?

copy `.env` to `.env.local`. and edit environement variable accordingly.
copy `.env` to `.env.local`. and edit environment variable accordingly.

```
yarn install
Expand Down
9 changes: 9 additions & 0 deletions app/graphql-rsc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@apollo/experimental-nextjs-app-support/ssr"
import { cookies } from "next/headers"
import { env } from "./env"
import { propagation, context } from '@opentelemetry/api';

export const { getClient } = registerApolloClient(() => {
const cookieStore = cookies()
Expand All @@ -19,6 +20,14 @@ export const { getClient } = registerApolloClient(() => {
headers: {
cookie: cookieStore.toString(),
},
fetch: (uri, options) => {
const headersWithTrace = options?.headers || {}
propagation.inject(context.active(), headersWithTrace)
return fetch(uri, {
...options,
headers: headersWithTrace,
})
},
}),
})
})
35 changes: 35 additions & 0 deletions instrumentation.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NodeSDK } from "@opentelemetry/sdk-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
import { NetInstrumentation } from "@opentelemetry/instrumentation-net";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { GraphQLInstrumentation } from "@opentelemetry/instrumentation-graphql";
import { propagation } from "@opentelemetry/api"
import { W3CTraceContextPropagator } from "@opentelemetry/core"

const sdk = new NodeSDK({
textMapPropagator: new W3CTraceContextPropagator(),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: process.env.TRACING_SERVICE_NAME || "blink-fiat",
}),
spanProcessor: new SimpleSpanProcessor(new OTLPTraceExporter()),
instrumentations: [
new NetInstrumentation(),
new HttpInstrumentation(),
new GraphQLInstrumentation({
mergeItems: true,
allowValues: true,
}),
],
});
sdk.start();

process.on("SIGTERM", () => {
sdk
.shutdown()
.then(() => console.log("Tracing terminated"))
.catch((error) => console.log("Error terminating tracing", error))
.finally(() => process.exit(0));
});
7 changes: 7 additions & 0 deletions instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function register() {
console.log("register", process.env.NEXT_RUNTIME)

if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./instrumentation.node")
}
}
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
module.exports = {
experimental: {
serverActions: true,
instrumentationHook: true,
},
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
"dependencies": {
"@apollo/client": "^3.7.17",
"@apollo/experimental-nextjs-app-support": "^0.4.3",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.43.0",
"@opentelemetry/instrumentation-graphql": "^0.35.1",
"@opentelemetry/instrumentation-http": "^0.43.0",
"@opentelemetry/instrumentation-net": "^0.32.1",
"@opentelemetry/resources": "^1.17.0",
"@opentelemetry/sdk-node": "^0.43.0",
"@opentelemetry/sdk-trace-node": "^1.17.0",
"@opentelemetry/semantic-conventions": "^1.17.0",
"@svgr/webpack": "^8.0.1",
"@t3-oss/env-nextjs": "^0.6.1",
"graphql": "^16.7.1",
Expand Down
Loading

0 comments on commit f01165f

Please sign in to comment.