Skip to content

Commit

Permalink
examples(node-manual): add nodejs sample with manual instructions (#1842
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Adnan Rahić authored Jan 16, 2023
1 parent c0d9349 commit 3916865
Show file tree
Hide file tree
Showing 13 changed files with 7,182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/quick-start-nodejs-manual-instrumentation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
7 changes: 7 additions & 0 deletions examples/quick-start-nodejs-manual-instrumentation/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "with-grpc-tracer" ]
46 changes: 46 additions & 0 deletions examples/quick-start-nodejs-manual-instrumentation/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const opentelemetry = require('@opentelemetry/api')
const tracer = opentelemetry.trace.getTracer('quick-start-nodejs-manual-instrumentation-tracer')

const express = require('express')
const app = express()

app.get('/', (req, res) => {
const span = tracer.startSpan('Hello World')
span.end()
res.send('Hello World')
})

app.get('/books', booksListHandler)

function booksListHandler(req, res) {
const span = tracer.startSpan('Books List')
const books = getBooks()
span.setAttribute('books.list.count', books.length)
span.end()

res.json(books)
}

function getBooks() {
return [
{
id: 1,
title: 'Harry Potter',
isAvailable: 0,
},
{
id: 2,
title: 'Foundation',
isAvailable: 0,
},
{
id: 3,
title: 'Moby Dick',
isAvailable: 0,
},
]
}

app.listen(8080, () => {
console.log(`Listening for requests on http://localhost:8080`)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'
services:
app:
image: quick-start-nodejs
extra_hosts:
- "host.docker.internal:host-gateway"
build: .
ports:
- "8080:8080"
Loading

0 comments on commit 3916865

Please sign in to comment.