-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
4 changed files
with
133 additions
and
26 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,21 @@ | ||
// Package prom provides prometheus metrics handler options for httpserver module. | ||
package prom | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/elisasre/go-common/metrics" | ||
"github.com/elisasre/go-common/service/module/httpserver" | ||
) | ||
|
||
// WithMetrics replaces servers handler with http.Handler which is instrumented with /metrics endpoint. | ||
// This option is meant be used with stand alone metrics server, not embedded inside application server. | ||
// For serving metrics endpoint inside your application web server check lower level functionalities from metrics. | ||
func WithMetrics(p *metrics.Prometheus) httpserver.Opt { | ||
return func(s *httpserver.Server) error { | ||
if err := p.Init(); err != nil { | ||
return fmt.Errorf("failed to initialize prometheus handler: %w", err) | ||
} | ||
return httpserver.WithHandler(metrics.NewPrometheusHandler(p))(s) | ||
} | ||
} |
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,64 @@ | ||
package prom_test | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/elisasre/go-common/metrics" | ||
"github.com/elisasre/go-common/service/module/httpserver" | ||
"github.com/elisasre/go-common/service/module/httpserver/prom" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInitError(t *testing.T) { | ||
c := collectors.NewGoCollector() | ||
srv := httpserver.New( | ||
httpserver.WithServer(&http.Server{ReadHeaderTimeout: time.Second}), | ||
httpserver.WithAddr("127.0.0.1:0"), | ||
prom.WithMetrics(metrics.New(c)), | ||
) | ||
|
||
require.Error(t, srv.Init()) | ||
} | ||
|
||
func TestServer(t *testing.T) { | ||
srv := httpserver.New( | ||
httpserver.WithServer(&http.Server{ReadHeaderTimeout: time.Second}), | ||
httpserver.WithAddr("127.0.0.1:0"), | ||
prom.WithMetrics(metrics.New()), | ||
) | ||
|
||
require.NotEmpty(t, srv.Name()) | ||
require.NoError(t, srv.Init()) | ||
url := srv.URL() + "/metrics" | ||
wg := &multierror.Group{} | ||
wg.Go(srv.Run) | ||
|
||
assertOK(t, url) | ||
|
||
assert.NoError(t, srv.Stop()) | ||
err := wg.Wait().ErrorOrNil() | ||
require.NoError(t, err) | ||
} | ||
|
||
func assertOK(t testing.TB, url string) { | ||
resp, err := http.Get(url) //nolint:gosec | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, "200 OK", resp.Status) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
assert.NoError(t, resp.Body.Close()) | ||
assert.NotEmpty(t, data) | ||
} |