1- import { IncomingMessage , ServerResponse } from 'http' ;
21import { SpanContext } from './span-context' ;
32import * as Tags from './tags' ;
43import * as Hdrs from './headers' ;
54import { Tracer } from './tracer' ;
6- import { SpanOptions , SpanTags } from './shared' ;
7- import FetchTemp , { Request , RequestInit , Headers } from 'node-fetch' ;
8- type Fetch = typeof FetchTemp ;
5+ import { SpanOptions , SpanTags , HttpRequest , HttpResponse } from './shared' ;
96
107interface SetupHttpTracingOptions {
118 name ?: string ;
129 tracer : Tracer ;
13- req : IncomingMessage ;
14- res : ServerResponse ;
15- fetch ?: Fetch ;
10+ req : HttpRequest ;
11+ res : HttpResponse ;
1612}
1713
1814export function setupHttpTracing ( options : SetupHttpTracingOptions ) {
19- const { name = 'setupHttpTracing' , tracer, req, res, fetch } = options ;
15+ const { name = 'setupHttpTracing' , tracer, req, res } = options ;
2016 const spanOptions = getSpanOptions ( req ) ;
2117 const span = tracer . startSpan ( name , spanOptions ) ;
2218 const spanContext = span . context ( ) ;
@@ -30,17 +26,15 @@ export function setupHttpTracing(options: SetupHttpTracingOptions) {
3026 span . finish ( ) ;
3127 } ) ;
3228
33- const fetchTracing = setupFetch ( fetch , spanContext ) ;
34-
35- return { spanContext, fetch : fetchTracing } ;
29+ return spanContext ;
3630}
3731
38- function getFirstHeader ( req : IncomingMessage , key : string ) {
32+ function getFirstHeader ( req : HttpRequest , key : string ) : string | undefined {
3933 const value = req . headers [ key ] ;
4034 return Array . isArray ( value ) ? value [ 0 ] : value ;
4135}
4236
43- function getSpanOptions ( req : IncomingMessage ) {
37+ function getSpanOptions ( req : HttpRequest ) : SpanOptions {
4438 const tags : SpanTags = { } ;
4539 tags [ Tags . HTTP_METHOD ] = req . method ;
4640 tags [ Tags . HTTP_URL ] = req . url ;
@@ -57,52 +51,5 @@ function getSpanOptions(req: IncomingMessage) {
5751 childOf = new SpanContext ( traceId , parentId , tags ) ;
5852 }
5953
60- const options : SpanOptions = { tags, childOf } ;
61- return options ;
62- }
63-
64- function setupFetch ( fetch : Fetch | undefined , spanContext : SpanContext ) {
65- let fetchOriginal : Fetch ;
66- if ( fetch ) {
67- fetchOriginal = fetch ;
68- } else {
69- fetchOriginal = require ( 'node-fetch' ) ;
70- }
71-
72- function fetchTracing ( url : string | Request , opts ?: RequestInit ) {
73- if ( ! opts ) {
74- opts = { headers : new Headers ( ) } ;
75- }
76- const headers =
77- opts . headers instanceof Headers
78- ? opts . headers
79- : new Headers ( opts . headers as any ) ;
80-
81- const traceId = spanContext . toTraceId ( ) ;
82- const parentId = spanContext . toSpanId ( ) ;
83- const priority = spanContext . getTag ( Tags . SAMPLING_PRIORITY ) ;
84-
85- headers . set ( Hdrs . TRACE_ID , traceId ) ;
86- if ( typeof parentId !== 'undefined' ) {
87- headers . set ( Hdrs . PARENT_ID , parentId ) ;
88- }
89- if ( typeof priority !== 'undefined' ) {
90- headers . set ( Hdrs . PRIORITY , priority ) ;
91- }
92-
93- return fetchOriginal ( url , opts ) ;
94- }
95-
96- fetchTracing . default = fetchTracing ;
97- fetchTracing . isRedirect = fetchOriginal . isRedirect ;
98-
99- // TS doesn't know about decorated runtime data
100- // so we copy from the original just to be safe.
101- for ( const key of Object . keys ( fetchOriginal ) ) {
102- const tracing = fetchTracing as any ;
103- const original = fetchOriginal as any ;
104- tracing [ key ] = original [ key ] ;
105- }
106-
107- return fetchTracing ;
54+ return { tags, childOf } ;
10855}
0 commit comments