1- 'use strict' ;
1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
216
317import * as api from '@opentelemetry/api' ;
4- import { setupTracing } from './tracer'
5- setupTracing ( 'example-koa-server' ) ;
618
719// Adding Koa router (if desired)
8- import * as Router from "@koa/router" ;
9- import * as Koa from "koa"
20+ import * as Router from '@koa/router' ;
21+ import * as Koa from 'koa' ;
22+ // eslint-disable-next-line import/extensions
23+ import { setupTracing } from './tracer' ;
1024
25+ setupTracing ( 'example-koa-server' ) ;
1126
1227// Setup koa
1328const app = new Koa ( ) ;
1429const PORT = 8081 ;
1530const router = new Router ( ) ;
1631
1732// route definitions
18- router . get ( '/run_test' , runTest )
33+ router
34+ . get ( '/run_test' , runTest )
1935 . get ( '/post/new' , addPost )
2036 . get ( '/post/:id' , showNewPost ) ;
2137
@@ -26,15 +42,15 @@ async function setUp() {
2642
2743/**
2844 * Router functions: list, add, or show posts
29- */
45+ */
3046const posts = [ 'post 0' , 'post 1' , 'post 2' ] ;
3147
3248function addPost ( ctx : Koa . Context ) {
3349 const newPostId = posts . length ;
3450 posts . push ( `post ${ newPostId } ` ) ;
3551 const currentSpan = api . trace . getSpan ( api . context . active ( ) ) ;
3652 currentSpan ?. addEvent ( 'Added post' ) ;
37- currentSpan ?. setAttribute ( 'post.id' , newPostId )
53+ currentSpan ?. setAttribute ( 'post.id' , newPostId ) ;
3854 ctx . body = `Added post: ${ posts [ posts . length - 1 ] } ` ;
3955 ctx . redirect ( '/post/3' ) ;
4056}
@@ -45,14 +61,16 @@ async function showNewPost(ctx: Koa.Context) {
4561 const post = posts [ id ] ;
4662 if ( ! post ) ctx . throw ( 404 , 'Invalid post id' ) ;
4763 const syntheticDelay = 500 ;
48- await new Promise ( ( r ) => setTimeout ( r , syntheticDelay ) ) ;
64+ await new Promise ( r => {
65+ setTimeout ( r , syntheticDelay ) ;
66+ } ) ;
4967 ctx . body = post ;
5068}
5169
5270function runTest ( ctx : Koa . Context ) {
5371 console . log ( 'runTest' ) ;
5472 const currentSpan = api . trace . getSpan ( api . context . active ( ) ) ;
55- if ( currentSpan ) {
73+ if ( currentSpan ) {
5674 const { traceId } = currentSpan . spanContext ( ) ;
5775 console . log ( `traceid: ${ traceId } ` ) ;
5876 console . log ( `Jaeger URL: http://localhost:16686/trace/${ traceId } ` ) ;
@@ -62,14 +80,15 @@ function runTest(ctx: Koa.Context) {
6280 }
6381}
6482
65- async function noOp ( ctx : Koa . Context , next : Koa . Next ) {
83+ function noOp ( ctx : Koa . Context , next : Koa . Next ) {
6684 console . log ( 'Sample basic koa middleware' ) ;
6785 const syntheticDelay = 100 ;
68- await new Promise ( ( r ) => setTimeout ( r , syntheticDelay ) ) ;
69- next ( ) ;
86+ setTimeout ( next , syntheticDelay ) ;
7087}
7188
72- setUp ( ) . then ( ( ) => {
73- app . listen ( PORT ) ;
74- console . log ( `Listening on http://localhost:${ PORT } ` ) ;
75- } ) ;
89+ setUp ( )
90+ . then ( ( ) => {
91+ app . listen ( PORT ) ;
92+ console . log ( `Listening on http://localhost:${ PORT } ` ) ;
93+ } )
94+ . catch ( err => console . log ( err ) ) ;
0 commit comments