Skip to content

[baseserver] Track metrics for HTTP requests automatically #13040

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

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ components/**/*.pb.go linguist-generated=true
components/**/*_pb.js linguist-generated=true
components/**/*_pb.ts linguist-generated=true
components/**/*_pb.d.ts linguist-generated=true

**/go.sum linguist-generated=true
10 changes: 9 additions & 1 deletion components/common-go/baseserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
http_metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand All @@ -42,7 +45,12 @@ func New(name string, opts ...Option) (*Server, error) {
server.builtinServices = newBuiltinServices(server)

server.httpMux = http.NewServeMux()
server.http = &http.Server{Handler: server.httpMux}
server.http = &http.Server{Handler: std.Handler("", middleware.New(middleware.Config{
Recorder: http_metrics.NewRecorder(http_metrics.Config{
Prefix: "gitpod",
Registry: server.MetricsRegistry(),
}),
}), server.httpMux)}

err = server.initializeMetrics()
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions components/common-go/baseserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package baseserver_test
import (
"context"
"fmt"
"net/http"
"testing"

"github.com/gitpod-io/gitpod/common-go/baseserver"
Expand Down Expand Up @@ -110,3 +111,23 @@ func TestServer_Metrics_gRPC(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(expected)*1, count, "expected 1 count for each metric")
}

func TestServer_Metrics_HTTP(t *testing.T) {
srv := baseserver.NewForTests(t, baseserver.WithHTTP(&baseserver.ServerConfiguration{
Address: fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)),
}))

// At this point, there must be metrics registry available for use
require.NotNil(t, srv.MetricsRegistry())
// Let's start our server up
baseserver.StartServerForTests(t, srv)

_, err := http.Get(fmt.Sprintf("%s/foo/bar", srv.HTTPAddress()))
require.NoError(t, err)

registry := srv.MetricsRegistry()

count, err := testutil.GatherAndCount(registry, "gitpod_http_request_duration_seconds", "gitpod_http_response_size_bytes", "gitpod_http_requests_inflight")
require.NoError(t, err)
require.Equal(t, 3, count, "expecting 1 count for each above metric")
}
1 change: 1 addition & 0 deletions components/common-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/hashicorp/golang-lru v0.5.1
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb
github.com/slok/go-http-metrics v0.10.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
Expand Down
2 changes: 2 additions & 0 deletions components/common-go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/content-service-api/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/content-service-api/go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/content-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rs/xid v1.2.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions components/content-service/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/ide-service-api/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/ide-service-api/go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/ide-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/ide-service/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/public-api-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/public-api-server/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/public-api/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/public-api/go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/supervisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/fatih/gomodifytags v1.14.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/smartystreets/assertions v1.13.0 // indirect
golang.org/x/tools v0.1.5 // indirect
)
Expand Down
8 changes: 5 additions & 3 deletions components/supervisor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
Expand Down
3 changes: 3 additions & 0 deletions components/usage/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/ws-daemon/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
)

require (
Expand Down
2 changes: 2 additions & 0 deletions components/ws-daemon/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading