-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a157df1
commit 1cbcfab
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pprof | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
_ "net/http/pprof" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// PprofConfig is the configuration for the pprof service. | ||
type PprofConfig struct { | ||
// Enabled controls whether the pprof service is enabled. | ||
Enabled bool `json:"enabled"` | ||
|
||
// ListenAddress is the address to listen on. | ||
ListenAddress string `json:"listenAddress"` | ||
} | ||
|
||
// NewService creates a new pprof service. | ||
func (pc *PprofConfig) NewService(logger *zap.Logger) *Service { | ||
return &Service{ | ||
logger: logger, | ||
server: http.Server{ | ||
Addr: pc.ListenAddress, | ||
}, | ||
} | ||
} | ||
|
||
// Service implements [service.Service]. | ||
type Service struct { | ||
logger *zap.Logger | ||
server http.Server | ||
} | ||
|
||
// String implements [service.Service.String]. | ||
func (s *Service) String() string { | ||
return "pprof" | ||
} | ||
|
||
// Start implements [service.Service.Start]. | ||
func (s *Service) Start(ctx context.Context) error { | ||
var lc net.ListenConfig | ||
ln, err := lc.Listen(ctx, "tcp", s.server.Addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
if err := s.server.Serve(ln); err != nil && err != http.ErrServerClosed { | ||
s.logger.Error("Failed to serve pprof", zap.Error(err)) | ||
} | ||
}() | ||
|
||
s.logger.Info("Started pprof", zap.String("listenAddress", s.server.Addr)) | ||
return nil | ||
} | ||
|
||
// Stop implements [service.Service.Stop]. | ||
func (s *Service) Stop() error { | ||
return s.server.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters