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

build: add dev environment instrumentation #679

Merged
merged 2 commits into from
Jan 26, 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
3 changes: 3 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ jobs:
firebase_project_id: ${{ secrets.FIREBASE_PROJECT_ID }}
firebase_private_key_base64: ${{ secrets.FIREBASE_PRIVATE_KEY_BASE64 }}
firebase_private_key_base64_gcp_secret_name: optic-firebase-private-key-dev
grafana_otlp_endpoint: ${{ secrets.HUB_GRAFANA_OTLP_ENDPOINT }}
grafana_instance_id: ${{ secrets.HUB_GRAFANA_INSTANCE_ID }}
grafana_api_key: ${{ secrets.HUB_GRAFANA_API_KEY }}
12 changes: 6 additions & 6 deletions .github/workflows/deploy-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ on:
firebase_private_key_base64_gcp_secret_name:
required: true
grafana_otlp_endpoint:
required: false
required: true
grafana_instance_id:
required: false
required: true
grafana_api_key:
required: false
required: true

jobs:
deploy:
Expand Down Expand Up @@ -72,9 +72,9 @@ jobs:
env_vars: |-
FIREBASE_CLIENT_EMAIL=${{ secrets.FIREBASE_CLIENT_EMAIL }},
FIREBASE_PROJECT_ID=${{ secrets.FIREBASE_PROJECT_ID }}
GRAFANA_OTLP_ENDPOINT=${{ secrets.grafana_otlp_endpoint || '' }},
GRAFANA_INSTANCE_ID=${{ secrets.grafana_instance_id || '' }},
GRAFANA_API_KEY=${{ secrets.grafana_api_key || ''}}
GRAFANA_OTLP_ENDPOINT=${{ secrets.grafana_otlp_endpoint }},
GRAFANA_INSTANCE_ID=${{ secrets.grafana_instance_id }},
GRAFANA_API_KEY=${{ secrets.grafana_api_key }}
secrets: |-
FIREBASE_PRIVATE_KEY_BASE64=${{ secrets.firebase_private_key_base64_gcp_secret_name }}:latest,
flags: --allow-unauthenticated --timeout=1800
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 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 --require ./server/instrumentation.js ./server/index.js",
"start": "cross-env NODE_ENV=production node --require ./server/instrumentation.js ./server/index.js",
"test": "node --test",
"husky:install": "npx husky",
"husky:uninstall": "git config --unset core.hooksPath"
Expand Down Expand Up @@ -42,6 +42,7 @@
"@commitlint/cli": "^19.6.1",
"@commitlint/config-conventional": "^19.6.0",
"c8": "^10.1.3",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-prettier-standard": "^4.0.1",
Expand Down
22 changes: 19 additions & 3 deletions server/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,31 @@ const {
processDetector,
hostDetector
} = require('@opentelemetry/resources')
const { diag, DiagLogLevel, DiagConsoleLogger } = require('@opentelemetry/api')

const pkg = require('../package.json')

require('dotenv').config()

const instrumentationEnabled =
process.env.GRAFANA_INSTRUMENTATION_ENABLED !== 'false' // default to true
const diagnosticsEnabled = process.env.GRAFANA_DIAGNOSTICS_ENABLED === 'true' // default to false
const endpoint = process.env.GRAFANA_OTLP_ENDPOINT
const instanceId = process.env.GRAFANA_INSTANCE_ID
const apiKey = process.env.GRAFANA_API_KEY
const environment = process.env.NODE_ENV ?? 'development'

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

if (!endpoint || !instanceId || !apiKey) {
throw new Error(
'Grafana instrumentation requires endpoint, instance id and api key.'
)
}

console.log(
`Starting Grafana instrumentation on '${endpoint}', instance '${instanceId}'...`
)
Expand All @@ -42,7 +53,8 @@ const base64Key = Buffer.from(`${instanceId}:${apiKey}`).toString('base64')
const sdk = new NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'optic',
[ATTR_SERVICE_VERSION]: pkg.version
[ATTR_SERVICE_VERSION]: pkg.version,
'deployment.environment': environment
}),
resourceDetectors: [envDetector, processDetector, hostDetector],
traceExporter: new OTLPTraceExporter({
Expand All @@ -64,6 +76,10 @@ const sdk = new NodeSDK({

sdk.start()

if (diagnosticsEnabled) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.VERBOSE)
}

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