11// Simple in-memory metrics store compatible with Edge Runtime
22class EdgeMetrics {
3- private counters : Map < string , number >
4- private histograms : Map < string , number [ ] >
3+ private counters : Map < string , number > ;
4+ private histograms : Map < string , number [ ] > ;
55
66 constructor ( ) {
7- this . counters = new Map ( )
8- this . histograms = new Map ( )
7+ this . counters = new Map ( ) ;
8+ this . histograms = new Map ( ) ;
99 }
1010
1111 // Counter methods
1212 incrementCounter ( name : string , labels : Record < string , string > ) {
13- const key = this . getKey ( name , labels )
14- const current = this . counters . get ( key ) || 0
15- this . counters . set ( key , current + 1 )
13+ const key = this . getKey ( name , labels ) ;
14+ const current = this . counters . get ( key ) || 0 ;
15+ this . counters . set ( key , current + 1 ) ;
1616 }
1717
1818 // Histogram methods
19- observeHistogram ( name : string , value : number , labels : Record < string , string > ) {
20- const key = this . getKey ( name , labels )
21- const values = this . histograms . get ( key ) || [ ]
22- values . push ( value )
23- this . histograms . set ( key , values )
19+ observeHistogram (
20+ name : string ,
21+ value : number ,
22+ labels : Record < string , string > ,
23+ ) {
24+ const key = this . getKey ( name , labels ) ;
25+ const values = this . histograms . get ( key ) || [ ] ;
26+ values . push ( value ) ;
27+ this . histograms . set ( key , values ) ;
2428 }
2529
2630 // Get metrics in Prometheus format
2731 getMetrics ( ) : string {
28- let output = ''
32+ let output = "" ;
2933
3034 // Counter metrics
3135 this . counters . forEach ( ( value , key ) => {
32- const [ name , labelStr ] = this . parseKey ( key )
33- output += `# TYPE ${ name } counter\n`
34- output += `${ name } ${ labelStr } ${ value } \n`
35- } )
36+ const [ name , labelStr ] = this . parseKey ( key ) ;
37+ output += `# TYPE ${ name } counter\n` ;
38+ output += `${ name } ${ labelStr } ${ value } \n` ;
39+ } ) ;
3640
3741 // Histogram metrics
3842 this . histograms . forEach ( ( values , key ) => {
39- const [ name , labelStr ] = this . parseKey ( key )
40- output += `# TYPE ${ name } histogram\n`
43+ const [ name , labelStr ] = this . parseKey ( key ) ;
44+ output += `# TYPE ${ name } histogram\n` ;
4145
4246 // Calculate quantiles
43- const sorted = values . sort ( ( a , b ) => a - b )
44- const count = sorted . length
45- const sum = sorted . reduce ( ( a , b ) => a + b , 0 )
47+ const sorted = values . sort ( ( a , b ) => a - b ) ;
48+ const count = sorted . length ;
49+ const sum = sorted . reduce ( ( a , b ) => a + b , 0 ) ;
4650
47- output += `${ name } _count${ labelStr } ${ count } \n`
48- output += `${ name } _sum${ labelStr } ${ sum } \n`
51+ output += `${ name } _count${ labelStr } ${ count } \n` ;
52+ output += `${ name } _sum${ labelStr } ${ sum } \n` ;
4953
5054 // Add bucket information
51- const buckets = [ 10 , 50 , 100 , 200 , 500 , 1000 , 2000 , 5000 ]
52- buckets . forEach ( bucket => {
53- const le = bucket
54- const count = sorted . filter ( v => v <= le ) . length
55- output += `${ name } _bucket${ this . addLe ( labelStr , le ) } ${ count } \n`
56- } )
57- } )
55+ const buckets = [ 10 , 50 , 100 , 200 , 500 , 1000 , 2000 , 5000 ] ;
56+ buckets . forEach ( ( bucket ) => {
57+ const le = bucket ;
58+ const count = sorted . filter ( ( v ) => v <= le ) . length ;
59+ output += `${ name } _bucket${ this . addLe ( labelStr , le ) } ${ count } \n` ;
60+ } ) ;
61+ } ) ;
5862
59- return output
63+ return output ;
6064 }
6165
6266 private getKey ( name : string , labels : Record < string , string > ) : string {
6367 const labelStr = Object . entries ( labels )
6468 . map ( ( [ k , v ] ) => `${ k } ="${ v } "` )
65- . join ( ',' )
66- return `${ name } {${ labelStr } }`
69+ . join ( "," ) ;
70+ return `${ name } {${ labelStr } }` ;
6771 }
6872
6973 private parseKey ( key : string ) : [ string , string ] {
70- const match = key . match ( / ^ ( .+ ) { ( .+ ) } $ / )
71- if ( ! match ) return [ key , '' ]
72- return [ match [ 1 ] , `{${ match [ 2 ] } }` ]
74+ const match = key . match ( / ^ ( .+ ) { ( .+ ) } $ / ) ;
75+ if ( ! match ) return [ key , "" ] ;
76+ return [ match [ 1 ] , `{${ match [ 2 ] } }` ] ;
7377 }
7478
7579 private addLe ( labelStr : string , le : number ) : string {
76- if ( labelStr === '' ) return `{le="${ le } "}`
77- return labelStr . replace ( '}' , `,le="${ le } "}` )
80+ if ( labelStr === "" ) return `{le="${ le } "}` ;
81+ return labelStr . replace ( "}" , `,le="${ le } "}` ) ;
7882 }
7983}
8084
81- export const metrics = new EdgeMetrics ( )
85+ export const metrics = new EdgeMetrics ( ) ;
0 commit comments