Skip to content

Commit 4d85b49

Browse files
committed
Add config and refactor jprofiler frmrk
1 parent 4277f8f commit 4d85b49

File tree

3 files changed

+89
-24
lines changed

3 files changed

+89
-24
lines changed

src/integration/frameworks_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
536536
deployment, logs, err := platform.Deploy.
537537
WithEnv(map[string]string{
538538
"BP_JAVA_VERSION": "11",
539-
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "'{ enabled: true }'",
539+
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "{ enabled: true }",
540540
}).
541541
Execute(name, filepath.Join(fixtures, "containers", "tomcat"))
542542
Expect(err).NotTo(HaveOccurred(), logs.String)
@@ -550,7 +550,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
550550
deployment, logs, err := platform.Deploy.
551551
WithEnv(map[string]string{
552552
"BP_JAVA_VERSION": "11",
553-
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "'{ enabled: false }'",
553+
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "{ enabled: false }",
554554
}).
555555
Execute(name, filepath.Join(fixtures, "containers", "tomcat"))
556556
Expect(err).NotTo(HaveOccurred(), logs.String)
@@ -851,7 +851,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
851851
deployment, logs, err := platform.Deploy.
852852
WithEnv(map[string]string{
853853
"BP_JAVA_VERSION": "11",
854-
"JBP_CONFIG_JPROFILER_PROFILER": "'{enabled: true}'",
854+
"JBP_CONFIG_JPROFILER_PROFILER": "{ enabled: true }",
855855
}).
856856
Execute(name, filepath.Join(fixtures, "containers", "spring_boot_staged"))
857857
Expect(err).NotTo(HaveOccurred(), logs.String)

src/java/frameworks/jprofiler_profiler.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ func NewJProfilerProfilerFramework(ctx *common.Context) *JProfilerProfilerFramew
3838
func (f *JProfilerProfilerFramework) Detect() (string, error) {
3939
// JProfiler is disabled by default
4040
// Check for JBP_CONFIG_JPROFILER_PROFILER='{enabled: true}'
41-
enabled := os.Getenv("JBP_CONFIG_JPROFILER_PROFILER")
42-
if enabled != "" {
43-
// Check if "enabled:true" in the agent options
44-
// We need case-insensitive check due to inconsistent casing
45-
if common.ContainsIgnoreCase(enabled, "enabled") && common.ContainsIgnoreCase(enabled, "true") {
46-
return "JProfiler Profiler", nil
47-
}
41+
config, err := f.loadConfig()
42+
if err != nil {
43+
f.context.Log.Warning("Failed to load jprofile profiler config: %s", err.Error())
44+
return "", nil // Don't fail the build
45+
}
46+
if config.isEnabled() {
47+
return "JProfiler Profiler", nil
4848
}
4949

5050
return "", nil
@@ -111,24 +111,20 @@ func (f *JProfilerProfilerFramework) Finalize() error {
111111
}
112112
runtimeAgentPath := filepath.Join(fmt.Sprintf("$DEPS_DIR/%s", depsIdx), relPath)
113113

114-
// Build agent options
115-
// Default options: port=8849, nowait (don't wait for profiler UI to connect)
116-
port := "8849"
117-
portConfig := os.Getenv("JBP_CONFIG_JPROFILER_PROFILER")
118-
if portConfig != "" && common.ContainsIgnoreCase(portConfig, "port") {
119-
// Simple extraction (would need proper YAML parsing in production)
120-
// For now, use default
114+
config, err := f.loadConfig()
115+
if err != nil {
116+
f.context.Log.Warning("Failed to load jprofile profiler config: %s", err.Error())
117+
return nil // Don't fail the build
121118
}
122119

123-
// Check for nowait option (default: true)
124-
nowait := "nowait"
125-
if portConfig != "" && common.ContainsIgnoreCase(portConfig, "nowait") && common.ContainsIgnoreCase(portConfig, "false") {
126-
nowait = ""
127-
}
120+
// Build agent options
121+
// Default options: port=8849, nowait (don't wait for profiler UI to connect)
122+
port := config.Port
123+
nowait := config.NoWait
128124

129125
// Build agent path with options
130126
var agentOptions string
131-
if nowait != "" {
127+
if nowait {
132128
agentOptions = fmt.Sprintf("port=%s,%s", port, nowait)
133129
} else {
134130
agentOptions = fmt.Sprintf("port=%s", port)
@@ -143,3 +139,35 @@ func (f *JProfilerProfilerFramework) Finalize() error {
143139
f.context.Log.Info("JProfiler Profiler configured (priority 30)")
144140
return nil
145141
}
142+
143+
func (f *JProfilerProfilerFramework) loadConfig() (*jProfilerConfig, error) {
144+
// initialize default values
145+
jpConfig := jProfilerConfig{
146+
Enabled: false,
147+
NoWait: true,
148+
Port: 8849,
149+
}
150+
config := os.Getenv("JBP_CONFIG_JPROFILER_PROFILER")
151+
if config != "" {
152+
yamlHandler := common.YamlHandler{}
153+
err := yamlHandler.ValidateFields([]byte(config), &jpConfig)
154+
if err != nil {
155+
f.context.Log.Warning("Unknown user config values: %s", err.Error())
156+
}
157+
// overlay JBP_CONFIG_JPROFILER_PROFILER over default values
158+
if err = yamlHandler.Unmarshal([]byte(config), &jpConfig); err != nil {
159+
return nil, fmt.Errorf("failed to parse JBP_CONFIG_JPROFILER_PROFILER: %w", err)
160+
}
161+
}
162+
return &jpConfig, nil
163+
}
164+
165+
type jProfilerConfig struct {
166+
Enabled bool `yaml:"enabled"`
167+
NoWait bool `yaml:"nowait"`
168+
Port int `yaml:"port"`
169+
}
170+
171+
func (j *jProfilerConfig) isEnabled() bool {
172+
return j.Enabled
173+
}

src/java/frameworks/sky_walking_agent.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (s *SkyWalkingAgentFramework) Finalize() error {
120120
opts = append(opts, fmt.Sprintf("-javaagent:%s", runtimeJarPath))
121121

122122
// Configure application name (default to space:application_name)
123-
appName := GetApplicationName(true)
123+
appName := s.getAppName()
124124
if appName != "" {
125125
opts = append(opts, fmt.Sprintf("-Dskywalking.agent.service_name=%s", appName))
126126
}
@@ -200,6 +200,19 @@ func (s *SkyWalkingAgentFramework) getCredentials() SkyWalkingCredentials {
200200
return creds
201201
}
202202

203+
func (s *SkyWalkingAgentFramework) getAppName() string {
204+
appName := GetApplicationName(true)
205+
if appName != "" {
206+
return appName
207+
}
208+
config, err := s.loadConfig()
209+
if err != nil {
210+
s.context.Log.Warning("Failed to load sky walking agent config: %s", err.Error())
211+
return ""
212+
}
213+
return config.DefaultApplicationName
214+
}
215+
203216
func (s *SkyWalkingAgentFramework) constructJarPath(agentDir string) error {
204217
// Find the installed agent JAR (in skywalking-agent subdirectory)
205218
jarPattern := filepath.Join(agentDir, "skywalking-agent", "skywalking-agent.jar")
@@ -209,3 +222,27 @@ func (s *SkyWalkingAgentFramework) constructJarPath(agentDir string) error {
209222
s.jarPath = jarPattern
210223
return nil
211224
}
225+
226+
func (s *SkyWalkingAgentFramework) loadConfig() (*skyWalkingAgentConfig, error) {
227+
// initialize default values
228+
swaConfig := skyWalkingAgentConfig{
229+
DefaultApplicationName: "",
230+
}
231+
config := os.Getenv("JBP_CONFIG_SKY_WALKING_AGENT")
232+
if config != "" {
233+
yamlHandler := common.YamlHandler{}
234+
err := yamlHandler.ValidateFields([]byte(config), &swaConfig)
235+
if err != nil {
236+
s.context.Log.Warning("Unknown user config values: %s", err.Error())
237+
}
238+
// overlay JBP_CONFIG_SKY_WALKING_AGENT over default values
239+
if err = yamlHandler.Unmarshal([]byte(config), &swaConfig); err != nil {
240+
return nil, fmt.Errorf("failed to parse JBP_CONFIG_SKY_WALKING_AGENT: %w", err)
241+
}
242+
}
243+
return &swaConfig, nil
244+
}
245+
246+
type skyWalkingAgentConfig struct {
247+
DefaultApplicationName string `yaml:"default_application_name"`
248+
}

0 commit comments

Comments
 (0)