forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add prometheus server package
- Loading branch information
Showing
3 changed files
with
49 additions
and
9 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
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,44 @@ | ||
package prometheus | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type Server struct { | ||
port int16 | ||
server *http.Server | ||
} | ||
|
||
// NewServer returns a new prometheus server instance | ||
func NewServer(port int16) *Server { | ||
return &Server{ | ||
port: port, | ||
} | ||
} | ||
|
||
// Start starts the prometheus server | ||
func (s *Server) Start() { | ||
// Server already started | ||
if s.server != nil { | ||
return | ||
} | ||
|
||
http.Handle("/metrics", promhttp.Handler()) | ||
s.server = &http.Server{ | ||
Addr: fmt.Sprintf(":%d", s.port), | ||
ReadHeaderTimeout: 3 * time.Second, | ||
} | ||
go s.server.ListenAndServe() | ||
} | ||
|
||
// Stop stops the prometheus server | ||
func (s *Server) Stop() { | ||
if s.server != nil { | ||
s.server.Close() | ||
s.server = nil | ||
} | ||
} |