Skip to content

Commit

Permalink
feat: adding otel in consent (#3417)
Browse files Browse the repository at this point in the history
* feat: adding otel in consent

* chore: addressing comments

* fix: lint errors

---------

Co-authored-by: Siddharth <[email protected]>
  • Loading branch information
siddhart1o1 and Siddharth authored Oct 27, 2023
1 parent 991e792 commit adc1aff
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apps/consent/app/graphql/apollo-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloClient, HttpLink, InMemoryCache, ApolloLink } from "@apollo/client"
import { propagation, context } from "@opentelemetry/api"

import { env } from "../../env"

Expand All @@ -17,6 +18,14 @@ export const graphQlClient = (authToken?: string) => {
const httpLink = new HttpLink({
uri: env.GRAPHQL_ENDPOINT,
fetchOptions: { cache: "no-store" },
fetch: (uri, options) => {
const headersWithTrace = options?.headers || {}
propagation.inject(context.active(), headersWithTrace)
return fetch(uri, {
...options,
headers: headersWithTrace,
})
},
headers: {
...(authToken ? { authorization: `Bearer ${authToken}` } : undefined),
},
Expand Down
4 changes: 4 additions & 0 deletions apps/consent/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ export const env = createEnv({
},
shared: {
GRAPHQL_ENDPOINT: z.string().default("http://localhost:4455/graphql"),
OTEL_EXPORTER_OTLP_ENDPOINT: z.string().default("http://localhost:4318"),
TRACING_SERVICE_NAME: z.string().default("consent"),
},
runtimeEnv: {
CORE_AUTH_URL: process.env.CORE_AUTH_URL,
HYDRA_ADMIN_URL: process.env.HYDRA_ADMIN_URL,
GRAPHQL_ENDPOINT: process.env.GRAPHQL_ENDPOINT,
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
TRACING_SERVICE_NAME: process.env.TRACING_SERVICE_NAME,
},
})
36 changes: 36 additions & 0 deletions apps/consent/instrumentation.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 { W3CTraceContextPropagator } from "@opentelemetry/core"

import { env } from "./env"

const sdk = new NodeSDK({
textMapPropagator: new W3CTraceContextPropagator(),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: env.TRACING_SERVICE_NAME,
}),
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: Error) => console.log("Error terminating tracing", error))
.finally(() => process.exit(0))
})
5 changes: 5 additions & 0 deletions apps/consent/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./instrumentation.node")
}
}
10 changes: 10 additions & 0 deletions apps/consent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
},
"dependencies": {
"@apollo/client": "^3.8.0-rc.2",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/core": "^1.17.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.44.0",
"@opentelemetry/instrumentation-graphql": "^0.35.2",
"@opentelemetry/instrumentation-http": "^0.44.0",
"@opentelemetry/instrumentation-net": "^0.32.2",
"@opentelemetry/resources": "^1.17.1",
"@opentelemetry/sdk-node": "^0.44.0",
"@opentelemetry/sdk-trace-node": "^1.17.1",
"@opentelemetry/semantic-conventions": "^1.17.1",
"@ory/hydra-client": "^2.2.0-rc.3",
"@t3-oss/env-nextjs": "^0.6.1",
"axios": "^1.5.1",
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit adc1aff

Please sign in to comment.