1
+ import {
2
+ getServiceName ,
3
+ getXRayTraceIdFromEnv ,
4
+ isDevMode ,
5
+ isRequestXRaySampled ,
6
+ } from '../envUtils.js' ;
1
7
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js' ;
2
8
3
9
/**
@@ -46,7 +52,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
46
52
* Get the value of the `POWERTOOLS_SERVICE_NAME` environment variable.
47
53
*/
48
54
public getServiceName ( ) : string {
49
- return this . get ( this . serviceNameVariable ) ;
55
+ return getServiceName ( ) ;
50
56
}
51
57
52
58
/**
@@ -58,9 +64,7 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
58
64
* The actual Trace ID is: `1-5759e988-bd862e3fe1be46a994272793`.
59
65
*/
60
66
public getXrayTraceId ( ) : string | undefined {
61
- const xRayTraceData = this . getXrayTraceData ( ) ;
62
-
63
- return xRayTraceData ?. Root ;
67
+ return getXRayTraceIdFromEnv ( ) ;
64
68
}
65
69
66
70
/**
@@ -70,16 +74,14 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
70
74
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
71
75
*/
72
76
public getXrayTraceSampled ( ) : boolean {
73
- const xRayTraceData = this . getXrayTraceData ( ) ;
74
-
75
- return xRayTraceData ?. Sampled === '1' ;
77
+ return isRequestXRaySampled ( ) ;
76
78
}
77
79
78
80
/**
79
81
* Determine if the current invocation is running in a development environment.
80
82
*/
81
83
public isDevMode ( ) : boolean {
82
- return this . isValueTrue ( this . get ( this . devModeVariable ) ) ;
84
+ return isDevMode ( ) ;
83
85
}
84
86
85
87
/**
@@ -103,29 +105,6 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
103
105
104
106
return falsyValues . includes ( value . toLowerCase ( ) ) ;
105
107
}
106
-
107
- /**
108
- * Get the AWS X-Ray Trace data from the environment variable.
109
- *
110
- * The method parses the environment variable `_X_AMZN_TRACE_ID` and returns an object with the key-value pairs.
111
- */
112
- private getXrayTraceData ( ) : Record < string , string > | undefined {
113
- const xRayTraceEnv = this . get ( this . xRayTraceIdVariable ) ;
114
-
115
- if ( xRayTraceEnv === '' ) return undefined ;
116
-
117
- if ( ! xRayTraceEnv . includes ( '=' ) ) return { Root : xRayTraceEnv } ;
118
-
119
- const xRayTraceData : Record < string , string > = { } ;
120
-
121
- for ( const field of xRayTraceEnv . split ( ';' ) ) {
122
- const [ key , value ] = field . split ( '=' ) ;
123
-
124
- xRayTraceData [ key ] = value ;
125
- }
126
-
127
- return xRayTraceData ;
128
- }
129
108
}
130
109
131
110
export { EnvironmentVariablesService } ;
0 commit comments