5
5
// This module handles the optional dependency on @opentelemetry /api
6
6
// It provides functionality for creating spans for decode and sendEncoded operations
7
7
8
- import { Errors } from 'io-ts' ;
9
- import type { SpanMetadata } from './types' ;
10
8
import type { Attributes , Span , Tracer } from '@opentelemetry/api' ;
11
- import * as PathReporter from 'io-ts/lib/PathReporter' ;
9
+
10
+ import type { Json , SpanMetadata } from './types' ;
12
11
13
12
let otelApi : any ;
14
13
let tracer : Tracer | undefined ;
15
14
16
- // Try to load @opentelemetry /api if available
15
+ // Load @opentelemetry /api, if available.
17
16
try {
18
17
otelApi = require ( '@opentelemetry/api' ) ;
19
18
if ( otelApi ) {
20
19
tracer = otelApi . trace . getTracer ( 'typed-express-router' ) ;
21
20
}
22
21
} catch ( e ) {
23
- // Optional dependency not available, tracing will be disabled
22
+ // Optional dependency not available, so tracing will be disabled.
24
23
tracer = undefined ;
25
24
}
26
25
27
26
export const ApiTsAttributes = {
28
27
/**
29
- * Route name in the API-TS spec
28
+ * The Operation ID of the HTTP request
30
29
*/
31
- API_TS_ROUTE_NAME : 'api_ts.route.name ' ,
30
+ API_TS_OPERATION_ID : 'api_ts.operation_id ' ,
32
31
33
32
/**
34
- * HTTP method for the route
33
+ * The method of the HTTP request
35
34
*/
36
- API_TS_ROUTE_METHOD : 'api_ts.route .method' ,
35
+ API_TS_METHOD : 'api_ts.method' ,
37
36
38
37
/**
39
- * Path for the route
38
+ * The path of the HTTP request
40
39
*/
41
- API_TS_ROUTE_PATH : 'api_ts.route .path' ,
40
+ API_TS_PATH : 'api_ts.path' ,
42
41
43
42
/**
44
- * Request status code
43
+ * Returned HTTP request status code
45
44
*/
46
45
API_TS_STATUS_CODE : 'api_ts.status_code' ,
47
46
} ;
@@ -56,15 +55,15 @@ export function createDefaultDecodeAttributes(metadata: SpanMetadata): Attribute
56
55
const attributes : Attributes = { } ;
57
56
58
57
if ( metadata . apiName ) {
59
- attributes [ ApiTsAttributes . API_TS_ROUTE_NAME ] = metadata . apiName ;
58
+ attributes [ ApiTsAttributes . API_TS_OPERATION_ID ] = metadata . apiName ;
60
59
}
61
60
62
61
if ( metadata . httpRoute ) {
63
62
if ( metadata . httpRoute . method ) {
64
- attributes [ ApiTsAttributes . API_TS_ROUTE_METHOD ] = metadata . httpRoute . method ;
63
+ attributes [ ApiTsAttributes . API_TS_METHOD ] = metadata . httpRoute . method ;
65
64
}
66
65
if ( metadata . httpRoute . path ) {
67
- attributes [ ApiTsAttributes . API_TS_ROUTE_PATH ] = metadata . httpRoute . path ;
66
+ attributes [ ApiTsAttributes . API_TS_PATH ] = metadata . httpRoute . path ;
68
67
}
69
68
}
70
69
@@ -81,15 +80,15 @@ export function createDefaultEncodeAttributes(metadata: SpanMetadata): Attribute
81
80
const attributes : Attributes = { } ;
82
81
83
82
if ( metadata . apiName ) {
84
- attributes [ ApiTsAttributes . API_TS_ROUTE_NAME ] = metadata . apiName ;
83
+ attributes [ ApiTsAttributes . API_TS_OPERATION_ID ] = metadata . apiName ;
85
84
}
86
85
87
86
if ( metadata . httpRoute ) {
88
87
if ( metadata . httpRoute . method ) {
89
- attributes [ ApiTsAttributes . API_TS_ROUTE_METHOD ] = metadata . httpRoute . method ;
88
+ attributes [ ApiTsAttributes . API_TS_METHOD ] = metadata . httpRoute . method ;
90
89
}
91
90
if ( metadata . httpRoute . path ) {
92
- attributes [ ApiTsAttributes . API_TS_ROUTE_PATH ] = metadata . httpRoute . path ;
91
+ attributes [ ApiTsAttributes . API_TS_PATH ] = metadata . httpRoute . path ;
93
92
}
94
93
}
95
94
@@ -148,22 +147,28 @@ export function recordSpanEncodeError(span: Span | undefined, error: unknown): v
148
147
/**
149
148
* Records errors on a decode span
150
149
* @param span The span to record the errors on
151
- * @param errors The errors to record
150
+ * @param error The JSON error value to record
151
+ * @param statusCode The HTTP status code
152
152
*/
153
- export function recordSpanDecodeError ( span : Span | undefined , errors : Errors ) : void {
153
+ export function recordSpanDecodeError (
154
+ span : Span | undefined ,
155
+ error : Json ,
156
+ statusCode : number ,
157
+ ) : void {
154
158
if ( ! span || ! otelApi ) {
155
159
return ;
156
160
}
157
- const validationErrors = PathReporter . failure ( errors ) ;
158
- const validationErrorMessage = validationErrors . join ( '\n' ) ;
159
- span . recordException ( new Error ( validationErrorMessage ) ) ;
161
+ setSpanAttributes ( span , {
162
+ [ ApiTsAttributes . API_TS_STATUS_CODE ] : statusCode ,
163
+ } ) ;
164
+ span . recordException ( JSON . stringify ( error , null , 2 ) ) ;
160
165
span . setStatus ( { code : otelApi . SpanStatusCode . ERROR } ) ;
161
166
}
162
167
163
168
/**
164
169
* Sets a span's attributes if it exists
165
170
* @param span The span to modify
166
- * @param errors The attributes to modify the span with
171
+ * @param attributes The attributes to modify the span with
167
172
*/
168
173
export function setSpanAttributes (
169
174
span : Span | undefined ,
0 commit comments