88
99const fs = require ( 'fs' )
1010const path = require ( 'path' )
11+ const { randomBytes } = require ( 'node:crypto' )
1112
1213class Config {
1314 constructor ( config ) {
@@ -19,6 +20,12 @@ class Config {
1920class ConfigBuilder {
2021 constructor ( agentStartupUserDefinedJson = { } ) {
2122 this . agentStartupUserDefinedJson = agentStartupUserDefinedJson
23+ this . handlers = [
24+ new AgentIdGenerator ( ) ,
25+ new ApplicationNameValidator ( )
26+ ]
27+ this . errorMessages = [ ]
28+ this . warnMessages = [ ]
2229 }
2330
2431 setDefaultJson ( json ) {
@@ -31,6 +38,11 @@ class ConfigBuilder {
3138 return this
3239 }
3340
41+ addHandler ( handler ) {
42+ this . handlers . push ( handler )
43+ return this
44+ }
45+
3446 build ( ) {
3547 if ( ! this . defaultJson ) {
3648 this . defaultJson = require ( './pinpoint-config-default' )
@@ -46,6 +58,11 @@ class ConfigBuilder {
4658 makeEnvironmentConfig ( ) ,
4759 this . agentStartupUserDefinedJson
4860 )
61+
62+ for ( const handler of this . handlers ) {
63+ const changes = handler . handle ( config )
64+ applyConfigChanges ( config , changes )
65+ }
4966 return new Config ( config )
5067 }
5168
@@ -64,7 +81,7 @@ class ConfigBuilder {
6481 const fileContent = fs . readFileSync ( configFilePath , 'utf8' )
6582 return JSON . parse ( fileContent )
6683 } catch ( e ) {
67- console . error ( ' Failed to read or parse pinpoint-config.json:' , e )
84+ this . warnMessages . push ( ` Failed to read or parse pinpoint-config.json at ${ configFilePath } : ${ e . message } ` )
6885 return { }
6986 }
7087 }
@@ -81,6 +98,47 @@ class ConfigBuilder {
8198 }
8299}
83100
101+ function applyConfigChanges ( target , changes ) {
102+ if ( ! changes || typeof changes !== 'object' ) {
103+ return
104+ }
105+
106+ for ( const [ key , value ] of Object . entries ( changes ) ) {
107+ if ( value === undefined ) {
108+ continue
109+ }
110+
111+ if ( isPlainObject ( value ) && isPlainObject ( target [ key ] ) ) {
112+ applyConfigChanges ( target [ key ] , value )
113+ } else {
114+ target [ key ] = value
115+ }
116+ }
117+ }
118+
119+ function isPlainObject ( value ) {
120+ return value !== null && typeof value === 'object' && ! Array . isArray ( value )
121+ }
122+
123+ class AgentIdGenerator {
124+ handle ( config ) {
125+ if ( config . agentId ?. length > 0 ) {
126+ return { }
127+ }
128+
129+ return { agentId : randomBytes ( 8 ) . toString ( 'hex' ) }
130+ }
131+ }
132+
133+ class ApplicationNameValidator {
134+ handle ( config ) {
135+ if ( config . applicationName ?. length > 1 ) {
136+ return { }
137+ }
138+ return { enable : false }
139+ }
140+ }
141+
84142function makeEnvironmentConfig ( ) {
85143 return {
86144 agentId : valueOfString ( 'PINPOINT_AGENT_ID' ) ,
@@ -95,7 +153,8 @@ function makeEnvironmentConfig() {
95153 traceExclusionUrl : makeTraceExclusionUrlEnvironmentConfig ( ) ,
96154 sqlStats : valueOfBoolean ( 'PINPOINT_PROFILER_SQL_STAT' )
97155 } ,
98- plugins : new PluginsEnvironmentConfigBuilder ( ) . build ( )
156+ plugins : makePluginsEnvironmentConfig ( ) ,
157+ enable : valueOfBoolean ( 'PINPOINT_ENABLE' )
99158 }
100159}
101160
@@ -135,7 +194,7 @@ function makeSamplingEnvironmentConfig() {
135194}
136195
137196function makeLogLevelsEnvironmentConfig ( ) {
138- const value = process . env [ envName ]
197+ const value = process . env [ 'PINPOINT_LOGGER_LEVELS' ]
139198 if ( typeof value !== 'string' || value . trim ( ) . length === 0 ) {
140199 return undefined
141200 }
0 commit comments