@@ -2,17 +2,17 @@ import * as api from '@opentelemetry/api';
2
2
3
3
import { setupTracing } from './tracer' ;
4
4
5
- setupTracing ( 'example-mongodb-server' )
5
+ setupTracing ( 'example-mongodb-server' ) ;
6
6
7
7
import { accessDB } from './utils' ;
8
8
9
9
import * as http from 'http' ;
10
10
import { IncomingMessage , ServerResponse } from 'http' ;
11
11
import * as mongodb from 'mongodb' ;
12
- import { Collection } from " mongodb" ;
12
+ import { Collection } from ' mongodb' ;
13
13
14
- const DB_NAME = 'mydb'
15
- const COLLECTION_NAME = 'users'
14
+ const DB_NAME = 'mydb' ;
15
+ const COLLECTION_NAME = 'users' ;
16
16
const URL = `mongodb://localhost:27017/${ DB_NAME } ` ;
17
17
18
18
let db : mongodb . Db ;
@@ -28,7 +28,6 @@ function startServer(port: number) {
28
28
throw err ;
29
29
} ) ;
30
30
31
-
32
31
// Creates a server
33
32
const server = http . createServer ( handleRequest ) ;
34
33
// Starts the server
@@ -40,16 +39,20 @@ function startServer(port: number) {
40
39
/** A function which handles requests and send response. */
41
40
function handleRequest ( request : IncomingMessage , response : ServerResponse ) {
42
41
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
+ }
48
51
49
52
try {
50
53
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 ) ) ;
53
56
request . on ( 'end' , async ( ) => {
54
57
if ( request . url === '/collection/' ) {
55
58
handleCreateCollection ( response ) ;
@@ -71,12 +74,13 @@ startServer(8080);
71
74
function handleInsertQuery ( response : ServerResponse ) {
72
75
const obj = { name : 'John' , age : '20' } ;
73
76
const usersCollection : Collection = db . collection ( COLLECTION_NAME ) ;
74
- usersCollection . insertOne ( obj )
77
+ usersCollection
78
+ . insertOne ( obj )
75
79
. then ( ( ) => {
76
80
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 ) ) ;
80
84
} ) ;
81
85
} )
82
86
. catch ( err => {
@@ -87,16 +91,16 @@ function handleInsertQuery(response: ServerResponse) {
87
91
88
92
function handleGetQuery ( response : ServerResponse ) {
89
93
const usersCollection : Collection = db . collection ( COLLECTION_NAME ) ;
90
- usersCollection .
91
- find ( { } )
94
+ usersCollection
95
+ . find ( { } )
92
96
. toArray ( )
93
97
. then ( ( ) => {
94
98
console . log ( '1 document served' ) ;
95
99
response . end ( ) ;
96
100
} )
97
101
. catch ( err => {
98
102
throw err ;
99
- } )
103
+ } ) ;
100
104
}
101
105
102
106
function handleCreateCollection ( response : ServerResponse ) {
@@ -108,7 +112,7 @@ function handleCreateCollection(response: ServerResponse) {
108
112
. catch ( err => {
109
113
console . log ( 'Error code:' , err . code ) ;
110
114
response . end ( err . message ) ;
111
- } ) ;
115
+ } ) ;
112
116
}
113
117
114
118
function handleNotFound ( response : ServerResponse ) {
0 commit comments