Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bai 849 use open telemetry to send instrumentation info #1158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/config/default.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,12 @@ module.exports = {
registry: 'registry',
},
},

instrumentation: {
enabled: false,
serviceName: 'backend',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably include authenticationToken. The config should include every possible field, even if it defaults to empty.

endpoint: '',
authenticationToken: '',
debug: false,
},
}
23,052 changes: 7,955 additions & 15,097 deletions backend/package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@aws-sdk/client-cognito-identity-provider": "^3.525.0",
"@aws-sdk/client-s3": "^3.421.0",
"@aws-sdk/lib-storage": "^3.462.0",
"@opentelemetry/auto-instrumentations-node": "^0.41.1",
"@opentelemetry/exporter-logs-otlp-proto": "^0.48.0",
"@smithy/node-http-handler": "^2.1.10",
"@types/express-session": "^1.18.0",
"app-root-path": "^3.1.0",
Expand Down
1 change: 1 addition & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './utils/signals.js'
import './instrumentation.js'

import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import shelljs from 'shelljs'
Expand Down
39 changes: 39 additions & 0 deletions backend/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api'
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'
import { Resource } from '@opentelemetry/resources'
import { logs, NodeSDK } from '@opentelemetry/sdk-node'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'

import config from './utils/config.js'

if (config.instrumentation.enabled) {
if (config.instrumentation.debug) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
}
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({
url: `${config.instrumentation.endpoint}/v1/traces`,
headers: { Authorization: `Bearer ${config.instrumentation.authenticationToken}` },
}),
logRecordProcessor: new logs.SimpleLogRecordProcessor(
new OTLPLogExporter({
url: `${config.instrumentation.endpoint}/v1/logs`,
headers: { Authorization: `Bearer ${config.instrumentation.authenticationToken}` },
}),
),
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-fs': {
enabled: false,
},
}),
],
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: `${config.instrumentation.serviceName}`,
}),
})

sdk.start()
}
7 changes: 7 additions & 0 deletions backend/src/services/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WritableStream } from 'node:stream/web'

import { OpenTelemetryBunyanStream } from '@opentelemetry/instrumentation-bunyan'
import bunyan from 'bunyan'
import chalk from 'chalk'
import { omit } from 'lodash-es'
Expand Down Expand Up @@ -149,6 +150,12 @@ if (process.env.NODE_ENV !== 'production') {
stream: process.stdout,
})
}
if (config.instrumentation.enabled) {
streams.push({
type: 'raw',
stream: new OpenTelemetryBunyanStream(),
})
}

const log = bunyan.createLogger({
name: 'bailo',
Expand Down
5 changes: 1 addition & 4 deletions backend/src/utils/__mocks__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ const config = {
internal: 'https://localhost:5000',
},
},
oauth: {
instrumentation: {
enabled: false,
},
experimental: {
v2: true,
},
}

export default config
11 changes: 8 additions & 3 deletions backend/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ export interface Config {
}

oauth: {
// Enabled only included for backward support with V1.
// After V1, authentication connector config should be used.
enabled: boolean
provider: string
grant: grant.GrantConfig | grant.GrantOptions
cognito: {
Expand All @@ -145,6 +142,14 @@ export interface Config {
modelCards: Array<DefaultSchema>
accessRequests: Array<DefaultSchema>
}

instrumentation: {
enabled: boolean
serviceName: string
endpoint: string
authenticationToken: string
debug: boolean
}
}

const config: Config = _config.util.toObject()
Expand Down
9 changes: 9 additions & 0 deletions frontend/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { registerOTel } from '@vercel/otel'

export async function register() {
if (process.env.OTEL_EXPORTER_OTLP_ENDPOINT && process.env.OTEL_EXPORTER_OTLP_HEADERS) {
registerOTel({
serviceName: 'frontend',
})
}
}
3 changes: 3 additions & 0 deletions frontend/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const backend = process.env.BACKEND_SERVICE ?? defaultBackend

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
instrumentationHook: true,
},
reactStrictMode: true,
output: 'standalone',
transpilePackages: ['nanoid', 'lodash-es'],
Expand Down
Loading
Loading