refactor: move handler register to apps

This commit is contained in:
Jon Lundy
2022-08-19 13:57:07 -06:00
parent 814c974e93
commit 0a964cb631
5 changed files with 43 additions and 88 deletions

View File

@@ -31,11 +31,9 @@ func Meter(ctx context.Context) metric.Meter {
}
return global.Meter("")
}
func PromHTTP(ctx context.Context) http.Handler {
if t := fromContext[contextKey, *prometheus.Exporter](ctx, promHTTPKey); t != nil {
return t
}
return http.NotFoundHandler()
func NewHTTP(ctx context.Context) *httpHandle {
t := fromContext[contextKey, *prometheus.Exporter](ctx, promHTTPKey)
return &httpHandle{t}
}
func initMetrics(ctx context.Context, name string) (context.Context, func() error) {
@@ -88,3 +86,14 @@ func initMetrics(ctx context.Context, name string) (context.Context, func() erro
return cont.Stop(ctx)
}
}
type httpHandle struct {
exp *prometheus.Exporter
}
func (h *httpHandle) RegisterHTTP(mux *http.ServeMux) {
if h.exp == nil {
return
}
mux.Handle("/metrics", h.exp)
}