Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CASMHMS-6324: Added support for ppprof builds #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.34.0
2.35.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Fixed - for any bug fixes
Security - in case of vulnerabilities
-->

## [2.35.0] - 2025-01-08

### Added

- Added support for ppprof builds

## [2.34.0] - 2024-12-06

### Fixed
Expand Down
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# (C) Copyright [2019-2022,2024] Hewlett Packard Enterprise Development LP
# (C) Copyright [2019-2022,2024-2025] Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -50,8 +50,15 @@ COPY vendor $GOPATH/src/github.com/Cray-HPE/hms-hmcollector/vendor
### Build Stage ###
FROM base AS builder

RUN set -ex \
&& go build -v -o /usr/local/bin/hmcollector github.com/Cray-HPE/hms-hmcollector/cmd/hmcollector
# Set profiling to disabled by default
ARG ENABLE_PPROF=true

# Conditionally build with the pprof tag if profiling is enabled
RUN if [ "$ENABLE_PPROF" = "true" ]; then \
set -ex && go build -v -tags pprof -o /usr/local/bin/hmcollector github.com/Cray-HPE/hms-hmcollector/cmd/hmcollector; \
else \
set -ex && go build -v -o /usr/local/bin/hmcollector github.com/Cray-HPE/hms-hmcollector/cmd/hmcollector; \
fi

## Final Stage ###

Expand Down
6 changes: 5 additions & 1 deletion cmd/hmcollector/rest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// (C) Copyright [2020-2024] Hewlett Packard Enterprise Development LP
// (C) Copyright [2020-2025] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -347,6 +347,10 @@ func doRest() {
// Health function for Kubernetes liveness/readiness.
http.HandleFunc("/health", doHealth)

// If the 'pprof' build tag is set, then this will register pprof handlers,
// otherwise this function is stubbed and will do nothing.
hmcollector.RegisterPProfHandlers()

go func() {
defer WaitGroup.Done()
err := srv.ListenAndServe()
Expand Down
53 changes: 53 additions & 0 deletions internal/hmcollector/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file contains the code to enable pprof profiling. It is only
// included in the build when the 'pprof' build tag is set in the Dockerfile.
//
//go:build pprof

/*
* (C) Copyright [2025] Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package hmcollector

import (
"net/http"
"net/http/pprof"
_ "net/http/pprof"
)

func RegisterPProfHandlers() {
// Main profiling entry point
http.HandleFunc("/debug/pprof/", pprof.Index) // Index listing all pprof endpoints

// Specific profiling handlers
http.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) // Command-line arguments
http.HandleFunc("/debug/pprof/profile", pprof.Profile) // CPU profile (default: 30 seconds)
http.HandleFunc("/debug/pprof/symbol", pprof.Symbol) // Symbol resolution for addresses
http.HandleFunc("/debug/pprof/trace", pprof.Trace) // Execution trace (default: 1 second)

// Additional profiling endpoints
http.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) // Heap allocation samples
http.Handle("/debug/pprof/block", pprof.Handler("block")) // Goroutine blocking events
http.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) // Stack traces of all goroutines
http.Handle("/debug/pprof/heap", pprof.Handler("heap")) // Memory heap profile
http.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) // Mutex contention profile
http.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) // Stack traces of thread creation
}
31 changes: 31 additions & 0 deletions internal/hmcollector/pprof_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file contains a stub implementation of the RegisterPProfHandlers()
// function which is a noop.  It is included in the build by default by
// way of the 'pprof' build tag not being set in the Dockerfile.
//
//go:build !pprof

/*
* (C) Copyright [2025] Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package hmcollector

func RegisterPProfHandlers() { }
Loading