-
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.
Creates a package for metrics where handlers are moved
- Loading branch information
1 parent
c83cf5b
commit 8ae91a9
Showing
5 changed files
with
84 additions
and
18 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
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,32 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type MetricsHandler interface { | ||
http.Handler | ||
} | ||
|
||
// PrometheusHandler implements the MetricsHandler interface. | ||
type PrometheusHandler struct { | ||
registry *prometheus.Registry | ||
} | ||
|
||
// Returns a new PrometheusHandler. | ||
func NewPrometheusHandler() MetricsHandler { | ||
registry := prometheus.NewRegistry() | ||
registry.MustRegister(collectors.NewGoCollector()) | ||
return &PrometheusHandler{ | ||
registry: registry, | ||
} | ||
} | ||
|
||
// ServeHTTP implements the http.Handler interface, allowing the PrometheusHandler to handle HTTP requests. | ||
func (p *PrometheusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{}).ServeHTTP(w, r) | ||
} |
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,33 @@ | ||
package metrics_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
metrics "github.com/canonical/gocert/internal/metrics" | ||
) | ||
|
||
// TestPrometheusHandler tests that the Prometheus metrics handler responds correctly to an HTTP request. | ||
func TestPrometheusHandler(t *testing.T) { | ||
handler := metrics.NewPrometheusHandler() | ||
|
||
request, err := http.NewRequest("GET", "/", nil) | ||
if err != nil { | ||
t.Fatalf("could not create request: %v", err) | ||
} | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, request) | ||
|
||
if status := recorder.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
if recorder.Body.String() == "" { | ||
t.Errorf("handler returned an empty body") | ||
} | ||
if !strings.Contains(recorder.Body.String(), "go_goroutines") { | ||
t.Errorf("handler returned an empty body") | ||
} | ||
} |