Skip to content

Commit

Permalink
Integrate with Honeycomb (#6)
Browse files Browse the repository at this point in the history
This allows me to gain observability into the system when running in
production.
  • Loading branch information
dtinth authored Nov 27, 2022
1 parent c487c47 commit 984484f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "env NODE_OPTIONS='--require @bemuse/otel' next dev -p ${PORT:-8009}",
"dev": "env NODE_OPTIONS='--require @bemuse/otel/register' next dev -p ${PORT:-8009}",
"build": "next build && ./scripts/postbuild.sh",
"start": "next start -p ${PORT:-8009}",
"lint": "next lint"
Expand Down
49 changes: 36 additions & 13 deletions packages/otel/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,50 @@ const { miniTracer } = require('./api')
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http')
const { NetInstrumentation } = require('@opentelemetry/instrumentation-net')
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino')
const { HoneycombSDK } = require('@honeycombio/opentelemetry-node')
const {
MongoDBInstrumentation,
} = require('@opentelemetry/instrumentation-mongodb')

// For troubleshooting, set the log level to DiagLogLevel.DEBUG
opentelemetry.api.diag.setLogger(
new opentelemetry.api.DiagConsoleLogger(),
opentelemetry.api.DiagLogLevel.INFO,
)
function getSdk() {
if (process.env.HONEYCOMB_API_KEY) {
console.log(
'Note: Using Honeycomb SDK with dataset "%s" and service name "%s"',
process.env.HONEYCOMB_DATASET,
process.env.OTEL_SERVICE_NAME,
)
return new HoneycombSDK({
instrumentations: getInstrumentations(),
debug: true,
})
} else {
console.warn('Note: Honeycomb API key not found, just sending to OTLP')
// For troubleshooting, set the log level to DiagLogLevel.DEBUG
opentelemetry.api.diag.setLogger(
new opentelemetry.api.DiagConsoleLogger(),
opentelemetry.api.DiagLogLevel.INFO,
)
const exporter = new OTLPTraceExporter({
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
})
const baseSpanProcessor = new opentelemetry.tracing.BatchSpanProcessor(
exporter,
)
return new opentelemetry.NodeSDK({
spanProcessor: miniTracer.createSpanProcessor(baseSpanProcessor),
instrumentations: getInstrumentations(),
})
}
}

const exporter = new OTLPTraceExporter({ url: 'http://db:4318/v1/traces' })
const baseSpanProcessor = new opentelemetry.tracing.BatchSpanProcessor(exporter)

const sdk = new opentelemetry.NodeSDK({
spanProcessor: miniTracer.createSpanProcessor(baseSpanProcessor),
instrumentations: [
function getInstrumentations() {
return [
new HttpInstrumentation(),
new NetInstrumentation(),
new PinoInstrumentation(),
new MongoDBInstrumentation(),
],
})
]
}

const sdk = getSdk()
sdk.start()

0 comments on commit 984484f

Please sign in to comment.