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

fix: add production code instrumentation #677

Merged
merged 1 commit into from
Jan 24, 2025
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --write '**/*.js?(on)'",
"start": "node server/index.js",
"start": "node --require ./server/instrumentation.js ./server/index.js",
"test": "node --test",
"husky:install": "npx husky",
"husky:uninstall": "git config --unset core.hooksPath"
Expand Down
28 changes: 17 additions & 11 deletions server/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ const pkg = require('../package.json')

require('dotenv').config()

if (
!process.env.GRAFANA_OTLP_ENDPOINT ||
!process.env.GRAFANA_INSTANCE_ID ||
!process.env.GRAFANA_API_KEY
) {
console.error('Grafana instrumentation not fully configured, skipping.')
const endpoint = process.env.GRAFANA_OTLP_ENDPOINT
const instanceId = process.env.GRAFANA_INSTANCE_ID
const apiKey = process.env.GRAFANA_API_KEY

if (!endpoint || !instanceId || !apiKey) {
console.warn('Grafana instrumentation not fully configured, skipping.')
process.exit(0)
}

const base64Key = Buffer.from(
`${process.env.GRAFANA_INSTANCE_ID}:${process.env.GRAFANA_API_KEY}`
).toString('base64')
console.log(
`Starting Grafana instrumentation on '${endpoint}', instance '${instanceId}'...`
)

const base64Key = Buffer.from(`${instanceId}:${apiKey}`).toString('base64')

const sdk = new NodeSDK({
resource: new Resource({
Expand All @@ -44,14 +46,14 @@ const sdk = new NodeSDK({
}),
resourceDetectors: [envDetector, processDetector, hostDetector],
traceExporter: new OTLPTraceExporter({
url: `${process.env.GRAFANA_OTLP_ENDPOINT}/v1/traces`,
url: `${endpoint}/v1/traces`,
headers: {
Authorization: `Basic ${base64Key}`
}
}),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: `${process.env.GRAFANA_OTLP_ENDPOINT}/v1/metrics`,
url: `${endpoint}/v1/metrics`,
headers: {
Authorization: `Basic ${base64Key}`
}
Expand All @@ -61,3 +63,7 @@ const sdk = new NodeSDK({
})

sdk.start()

console.log(
`Grafana instrumentation started successfully on '${endpoint}', instance '${instanceId}'.`
)
Loading