@@ -2,17 +2,17 @@ import * as api from '@opentelemetry/api';
22
33import { setupTracing } from './tracer' ;
44
5- setupTracing ( 'example-mongodb-server' )
5+ setupTracing ( 'example-mongodb-server' ) ;
66
77import { accessDB } from './utils' ;
88
99import * as http from 'http' ;
1010import { IncomingMessage , ServerResponse } from 'http' ;
1111import * as mongodb from 'mongodb' ;
12- import { Collection } from " mongodb" ;
12+ import { Collection } from ' mongodb' ;
1313
14- const DB_NAME = 'mydb'
15- const COLLECTION_NAME = 'users'
14+ const DB_NAME = 'mydb' ;
15+ const COLLECTION_NAME = 'users' ;
1616const URL = `mongodb://localhost:27017/${ DB_NAME } ` ;
1717
1818let db : mongodb . Db ;
@@ -28,7 +28,6 @@ function startServer(port: number) {
2828 throw err ;
2929 } ) ;
3030
31-
3231 // Creates a server
3332 const server = http . createServer ( handleRequest ) ;
3433 // Starts the server
@@ -40,16 +39,20 @@ function startServer(port: number) {
4039/** A function which handles requests and send response. */
4140function handleRequest ( request : IncomingMessage , response : ServerResponse ) {
4241 const currentSpan = api . trace . getSpan ( api . context . active ( ) ) ;
43- // display traceID in the terminal
44- const traceId = currentSpan ?. spanContext ( ) ;
45- console . log ( `traceid: ${ traceId } ` ) ;
46- console . log ( `Jaeger URL: http://localhost:16686/trace/${ traceId } ` ) ;
47- console . log ( `Zipkin URL: http://localhost:9411/zipkin/traces/${ traceId } ` ) ;
42+ if ( currentSpan ) {
43+ // display traceID in the terminal
44+ const { traceId } = currentSpan ?. spanContext ( ) ;
45+ console . log ( `traceid: ${ traceId } ` ) ;
46+ console . log ( `Jaeger URL: http://localhost:16686/trace/${ traceId } ` ) ;
47+ console . log ( `Zipkin URL: http://localhost:9411/zipkin/traces/${ traceId } ` ) ;
48+ } else {
49+ console . log ( 'No active span found' ) ;
50+ }
4851
4952 try {
5053 const body = [ ] ;
51- request . on ( 'error' , ( err ) => console . log ( err ) ) ;
52- request . on ( 'data' , ( chunk ) => body . push ( chunk ) ) ;
54+ request . on ( 'error' , err => console . log ( err ) ) ;
55+ request . on ( 'data' , chunk => body . push ( chunk ) ) ;
5356 request . on ( 'end' , async ( ) => {
5457 if ( request . url === '/collection/' ) {
5558 handleCreateCollection ( response ) ;
@@ -71,12 +74,13 @@ startServer(8080);
7174function handleInsertQuery ( response : ServerResponse ) {
7275 const obj = { name : 'John' , age : '20' } ;
7376 const usersCollection : Collection = db . collection ( COLLECTION_NAME ) ;
74- usersCollection . insertOne ( obj )
77+ usersCollection
78+ . insertOne ( obj )
7579 . then ( ( ) => {
7680 console . log ( '1 document inserted' ) ;
77- // find document to test context propagation using callback
78- usersCollection . findOne ( { } , function ( ) {
79- response . end ( ) ;
81+ // find document to test context
82+ usersCollection . findOne ( { } ) . then ( res => {
83+ console . log ( JSON . stringify ( res ) ) ;
8084 } ) ;
8185 } )
8286 . catch ( err => {
@@ -87,16 +91,16 @@ function handleInsertQuery(response: ServerResponse) {
8791
8892function handleGetQuery ( response : ServerResponse ) {
8993 const usersCollection : Collection = db . collection ( COLLECTION_NAME ) ;
90- usersCollection .
91- find ( { } )
94+ usersCollection
95+ . find ( { } )
9296 . toArray ( )
9397 . then ( ( ) => {
9498 console . log ( '1 document served' ) ;
9599 response . end ( ) ;
96100 } )
97101 . catch ( err => {
98102 throw err ;
99- } )
103+ } ) ;
100104}
101105
102106function handleCreateCollection ( response : ServerResponse ) {
@@ -108,7 +112,7 @@ function handleCreateCollection(response: ServerResponse) {
108112 . catch ( err => {
109113 console . log ( 'Error code:' , err . code ) ;
110114 response . end ( err . message ) ;
111- } ) ;
115+ } ) ;
112116}
113117
114118function handleNotFound ( response : ServerResponse ) {
0 commit comments