-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move proxy metrics to a separate server #352
Changes from 5 commits
5617ca5
81ebcf3
95276cf
f2c2154
d62a38e
5dd6bd8
2dd56ad
4277e29
ee39648
2fd62ba
6889cf6
dde36ba
a51ed78
fbfb228
53d0e59
6ddf215
94a372a
fdc83f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
glog "github.com/labstack/gommon/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"net/http" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"time" | ||
) | ||
|
||
var log = logf.Log.WithName("registration_metrics") | ||
|
@@ -12,6 +17,7 @@ | |
MetricLabelRejected = "Rejected" | ||
MetricsLabelVerbGet = "Get" | ||
MetricsLabelVerbList = "List" | ||
MetricsPort = "8082" | ||
) | ||
|
||
// histogram with labels | ||
|
@@ -58,9 +64,37 @@ | |
// RegisterCustomMetrics registers the custom metrics | ||
func RegisterCustomMetrics() { | ||
Reg = prometheus.NewRegistry() | ||
if len(allHistogramVecs) == 0 { | ||
log.Info("No Histograms to register") | ||
} | ||
// register metrics | ||
for _, v := range allHistogramVecs { | ||
Reg.MustRegister(v) | ||
} | ||
log.Info("custom metrics registered") | ||
} | ||
|
||
//nolint:unparam | ||
func prometheusHandler(ctx echo.Context) error { | ||
h := promhttp.HandlerFor(Reg, promhttp.HandlerOpts{DisableCompression: true, Registry: Reg}) | ||
h.ServeHTTP(ctx.Response().Writer, ctx.Request()) | ||
return nil | ||
} | ||
|
||
func StartMetricsServer() *http.Server { | ||
// start server | ||
router := echo.New() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
router.Logger.SetLevel(glog.INFO) | ||
router.GET("/metrics", prometheusHandler) | ||
|
||
log.Info("Starting the Registration-Service Metrics server...") | ||
srv := &http.Server{Addr: ":" + MetricsPort, Handler: router, ReadHeaderTimeout: 2 * time.Second} | ||
// listen concurrently to allow for graceful shutdown | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil { | ||
log.Error(err, err.Error()) | ||
} | ||
}() | ||
|
||
return srv | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
promtestutil "github.com/prometheus/client_golang/prometheus/testutil" | ||
clientmodel "github.com/prometheus/client_model/go" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
@@ -64,6 +67,31 @@ func TestRegisterCustomMetrics(t *testing.T) { | |
} | ||
} | ||
|
||
func TestMetricsHandler(t *testing.T) { | ||
// Create a request to pass to our handler. We don't have any query parameters, so we'll | ||
// pass 'nil' as the third parameter. | ||
req, err := http.NewRequest(http.MethodGet, "/metrics", nil) | ||
require.NoError(t, err) | ||
|
||
// Create handler instance. | ||
RegisterCustomMetrics() | ||
|
||
t.Run("valid metrics json", func(t *testing.T) { | ||
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. | ||
e := echo.New() | ||
rec := httptest.NewRecorder() | ||
ctx := e.NewContext(req, rec) | ||
|
||
//when | ||
err := prometheusHandler(ctx) | ||
require.NoError(t, err) | ||
// then | ||
// check the status code is what we expect, and the content-type | ||
require.Equal(t, http.StatusOK, rec.Code) | ||
require.Equal(t, "text/plain; version=0.0.4; charset=utf-8", rec.Header().Get("Content-Type")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it make sense to verify that the body also contains the actual registered metric? you know, just check that the name of the metric is there, no need to verify the actual values... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
}) | ||
} | ||
|
||
var expectedResponseMetadata = ` | ||
# HELP sandbox_test_histogram_vec test histogram description | ||
# TYPE sandbox_test_histogram_vec histogram` | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is used only in dev environments. The one which is used in production (and e2e tests) are defined here: https://github.com/codeready-toolchain/host-operator/blob/master/deploy/registration-service/registration-service.yaml
Please create a PR to update that file too.
But what I'm wonder is why the e2e-test still pass? Do we have any e2e tests for this metric service? Without updating https://github.com/codeready-toolchain/host-operator/blob/master/deploy/registration-service/registration-service.yaml the e2e-tests for this PR should fail...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we also add names to the container ports (see for example https://kubernetes.io/docs/concepts/services-networking/service/#field-spec-ports) to make them more explicit, and reuse these names in the services as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually not used even in dev environment. This file is a legacy thing from the time when we were able to deploy reg-service on its own - we don't do that anymore (or at least I'm not aware of anyone who would be doing that)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but anyway, Alexey is right that you need to create PR in host-repo with this change. Let's do it in two steps:
Service
is created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply,
@MatousJobanek @alexeykazakov Thanks for your comments and catching the missing host-repo changes. I did have the changes in host-repo, that's how i was able to test it locally indeed, but had missed creating the PR on host-repo. I've updated the host-operator and e2e PR in the description.