Skip to content

Commit

Permalink
Merge pull request #15288 from andyxiexu/java-sdk
Browse files Browse the repository at this point in the history
Add google cloud heap profiling support to beam java sdk container
  • Loading branch information
y1chi authored Aug 6, 2021
2 parents f759a5c + 0ac5480 commit 10e3e57
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sdks/java/container/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var (
semiPersistDir = flag.String("semi_persist_dir", "/tmp", "Local semi-persistent directory (optional).")
)

const (
enableGoogleCloudProfilerOption = "enable_google_cloud_profiler"
enableGoogleCloudHeapSamplingOption = "enable_google_cloud_heap_sampling"
googleCloudProfilerAgentBaseArgs = "-agentpath:/opt/google_cloud_profiler/profiler_java_agent.so=-logtostderr,-cprof_service=%s,-cprof_service_version=%s"
googleCloudProfilerAgentHeapArgs = googleCloudProfilerAgentBaseArgs + ",-cprof_enable_heap_sampling,-cprof_heap_sampling_interval=2097152"
)

func main() {
flag.Parse()
if *id == "" {
Expand Down Expand Up @@ -151,12 +158,18 @@ func main() {
"-cp", strings.Join(cp, ":"),
}

enableGoogleCloudProfiler := strings.Contains(options, "enable_google_cloud_profiler")
enableGoogleCloudProfiler := strings.Contains(options, enableGoogleCloudProfilerOption)
enableGoogleCloudHeapSampling := strings.Contains(options, enableGoogleCloudHeapSamplingOption)
if enableGoogleCloudProfiler {
if metadata := info.GetMetadata(); metadata != nil {
if jobName, nameExists := metadata["job_name"]; nameExists {
if jobId, idExists := metadata["job_id"]; idExists {
args = append(args, fmt.Sprintf("-agentpath:/opt/google_cloud_profiler/profiler_java_agent.so=-cprof_service=%s,-cprof_service_version=%s", jobName, jobId))
if enableGoogleCloudHeapSampling {
args = append(args, fmt.Sprintf(googleCloudProfilerAgentHeapArgs, jobName, jobId))
} else {
args = append(args, fmt.Sprintf(googleCloudProfilerAgentBaseArgs, jobName, jobId))
}
log.Printf("Turning on Cloud Profiling. Profile heap: %t", enableGoogleCloudHeapSampling)
} else {
log.Println("Required job_id missing from metadata, profiling will not be enabled without it.")
}
Expand Down

0 comments on commit 10e3e57

Please sign in to comment.