@@ -7,8 +7,8 @@ import { resourceFromAttributes } from "@opentelemetry/resources";
77import { NodeSDK } from "@opentelemetry/sdk-node" ;
88import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-node" ;
99import {
10- ATTR_SERVICE_NAME ,
11- ATTR_SERVICE_VERSION ,
10+ ATTR_SERVICE_NAME ,
11+ ATTR_SERVICE_VERSION ,
1212} from "@opentelemetry/semantic-conventions" ;
1313import pkg from "../../package.json" ;
1414
@@ -18,135 +18,135 @@ let sdk: NodeSDK | null = null;
1818 * Initialize OpenTelemetry
1919 */
2020export function initTracing ( ) : void {
21- if ( sdk ) {
22- return ;
23- }
21+ if ( sdk ) {
22+ return ;
23+ }
2424
25- const exporter = new OTLPTraceExporter ( {
26- url : "https://api.axiom.co/v1/traces" ,
27- headers : {
28- Authorization : `Bearer ${ process . env . AXIOM_TOKEN } ` ,
29- "X-Axiom-Dataset" : process . env . AXIOM_DATASET ?? "api" ,
30- } ,
31- } ) ;
25+ const exporter = new OTLPTraceExporter ( {
26+ url : "https://api.axiom.co/v1/traces" ,
27+ headers : {
28+ Authorization : `Bearer ${ process . env . AXIOM_TOKEN } ` ,
29+ "X-Axiom-Dataset" : process . env . AXIOM_DATASET ?? "api" ,
30+ } ,
31+ } ) ;
3232
33- sdk = new NodeSDK ( {
34- resource : resourceFromAttributes ( {
35- [ ATTR_SERVICE_NAME ] : "api" ,
36- [ ATTR_SERVICE_VERSION ] : pkg . version ,
37- } ) ,
38- spanProcessor : new BatchSpanProcessor ( exporter , {
39- scheduledDelayMillis : 1000 ,
40- exportTimeoutMillis : 30_000 ,
41- maxExportBatchSize : 512 ,
42- maxQueueSize : 2048 ,
43- } ) ,
44- instrumentations : [
45- new HttpInstrumentation ( {
46- ignoreIncomingRequestHook : ( req : { url ?: string } ) => {
47- // Don't trace health checks
48- return req . url ?. includes ( "/health" ) ?? false ;
49- } ,
50- } ) ,
51- new PgInstrumentation ( ) ,
52- createORPCInstrumentation ( ) ,
53- ] ,
54- } ) ;
33+ sdk = new NodeSDK ( {
34+ resource : resourceFromAttributes ( {
35+ [ ATTR_SERVICE_NAME ] : "api" ,
36+ [ ATTR_SERVICE_VERSION ] : pkg . version ,
37+ } ) ,
38+ spanProcessor : new BatchSpanProcessor ( exporter , {
39+ scheduledDelayMillis : 1000 ,
40+ exportTimeoutMillis : 30_000 ,
41+ maxExportBatchSize : 512 ,
42+ maxQueueSize : 2048 ,
43+ } ) ,
44+ instrumentations : [
45+ new HttpInstrumentation ( {
46+ ignoreIncomingRequestHook : ( req : { url ?: string } ) => {
47+ // Don't trace health checks
48+ return req . url ?. includes ( "/health" ) ?? false ;
49+ } ,
50+ } ) ,
51+ new PgInstrumentation ( ) ,
52+ createORPCInstrumentation ( ) ,
53+ ] ,
54+ } ) ;
5555
56- sdk . start ( ) ;
56+ sdk . start ( ) ;
5757}
5858
5959export async function shutdownTracing ( ) : Promise < void > {
60- if ( sdk ) {
61- await sdk . shutdown ( ) ;
62- sdk = null ;
63- }
60+ if ( sdk ) {
61+ await sdk . shutdown ( ) ;
62+ sdk = null ;
63+ }
6464}
6565
6666/**
6767 * Get tracer
6868 */
6969function getTracer ( ) {
70- return trace . getTracer ( "api" ) ;
70+ return trace . getTracer ( "api" ) ;
7171}
7272
7373/**
7474 * Create a span - replaces @elysiajs/opentelemetry record
7575 */
7676export function record < T > ( name : string , fn : ( ) => Promise < T > | T ) : Promise < T > {
77- const tracer = getTracer ( ) ;
78- return tracer . startActiveSpan ( name , async ( span : Span ) => {
79- try {
80- const result = await fn ( ) ;
81- span . setStatus ( { code : SpanStatusCode . OK } ) ;
82- return result ;
83- } catch ( error ) {
84- span . setStatus ( {
85- code : SpanStatusCode . ERROR ,
86- message : error instanceof Error ? error . message : String ( error ) ,
87- } ) ;
88- span . recordException (
89- error instanceof Error ? error : new Error ( String ( error ) )
90- ) ;
91- throw error ;
92- } finally {
93- span . end ( ) ;
94- }
95- } ) ;
77+ const tracer = getTracer ( ) ;
78+ return tracer . startActiveSpan ( name , async ( span : Span ) => {
79+ try {
80+ const result = await fn ( ) ;
81+ span . setStatus ( { code : SpanStatusCode . OK } ) ;
82+ return result ;
83+ } catch ( error ) {
84+ span . setStatus ( {
85+ code : SpanStatusCode . ERROR ,
86+ message : error instanceof Error ? error . message : String ( error ) ,
87+ } ) ;
88+ span . recordException (
89+ error instanceof Error ? error : new Error ( String ( error ) )
90+ ) ;
91+ throw error ;
92+ } finally {
93+ span . end ( ) ;
94+ }
95+ } ) ;
9696}
9797
9898/**
9999 * Set attributes on active span - replaces @elysiajs/opentelemetry setAttributes
100100 */
101101export function setAttributes (
102- attributes : Record < string , string | number | boolean >
102+ attributes : Record < string , string | number | boolean >
103103) : void {
104- const span = trace . getActiveSpan ( ) ;
105- if ( span ) {
106- for ( const [ key , value ] of Object . entries ( attributes ) ) {
107- span . setAttribute ( key , value ) ;
108- }
109- }
104+ const span = trace . getActiveSpan ( ) ;
105+ if ( span ) {
106+ for ( const [ key , value ] of Object . entries ( attributes ) ) {
107+ span . setAttribute ( key , value ) ;
108+ }
109+ }
110110}
111111
112112/**
113113 * Start HTTP request span and set it as active
114114 * Returns both the span and the context with the span set as active
115115 */
116116export function startRequestSpan (
117- method : string ,
118- path : string ,
119- route ?: string
117+ method : string ,
118+ path : string ,
119+ route ?: string
120120) : { span : Span ; activeContext : ReturnType < typeof context . active > } {
121- const tracer = getTracer ( ) ;
122- const span = tracer . startSpan ( `${ method } ${ route ?? path } ` , {
123- kind : 1 , // SERVER
124- attributes : {
125- "http.method" : method ,
126- "http.route" : route ?? path ,
127- "http.target" : path ,
128- } ,
129- } ) ;
121+ const tracer = getTracer ( ) ;
122+ const span = tracer . startSpan ( `${ method } ${ route ?? path } ` , {
123+ kind : 1 , // SERVER
124+ attributes : {
125+ "http.method" : method ,
126+ "http.route" : route ?? path ,
127+ "http.target" : path ,
128+ } ,
129+ } ) ;
130130
131- // Create context with this span as active
132- const activeContext = trace . setSpan ( context . active ( ) , span ) ;
131+ // Create context with this span as active
132+ const activeContext = trace . setSpan ( context . active ( ) , span ) ;
133133
134- return { span, activeContext } ;
134+ return { span, activeContext } ;
135135}
136136
137137/**
138138 * End HTTP request span
139139 */
140140export function endRequestSpan (
141- span : Span ,
142- statusCode : number ,
143- startTime : number
141+ span : Span ,
142+ statusCode : number ,
143+ startTime : number
144144) : void {
145- span . setAttribute ( "http.status_code" , statusCode ) ;
146- span . setAttribute ( "http.response.duration_ms" , Date . now ( ) - startTime ) ;
147- span . setStatus ( {
148- code : statusCode >= 400 ? SpanStatusCode . ERROR : SpanStatusCode . OK ,
149- message : statusCode >= 400 ? `HTTP ${ statusCode } ` : undefined ,
150- } ) ;
151- span . end ( ) ;
145+ span . setAttribute ( "http.status_code" , statusCode ) ;
146+ span . setAttribute ( "http.response.duration_ms" , Date . now ( ) - startTime ) ;
147+ span . setStatus ( {
148+ code : statusCode >= 400 ? SpanStatusCode . ERROR : SpanStatusCode . OK ,
149+ message : statusCode >= 400 ? `HTTP ${ statusCode } ` : undefined ,
150+ } ) ;
151+ span . end ( ) ;
152152}
0 commit comments