From ce0bd1ea8b8937bcddec56cdc2941402309817dc Mon Sep 17 00:00:00 2001 From: Hugo Labrador Date: Wed, 7 Feb 2024 17:03:17 +0100 Subject: [PATCH] Add pprof http service and demote prom interceptor to not run by default (#4508) * add pprof http service * disable loading prom as default interceptor --- changelog/unreleased/pprof.md | 6 ++ cmd/revad/runtime/http.go | 2 - internal/http/services/loader/loader.go | 1 + internal/http/services/pprof/pprof.go | 80 +++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/pprof.md create mode 100644 internal/http/services/pprof/pprof.go diff --git a/changelog/unreleased/pprof.md b/changelog/unreleased/pprof.md new file mode 100644 index 0000000000..3c29d646b5 --- /dev/null +++ b/changelog/unreleased/pprof.md @@ -0,0 +1,6 @@ +Enhancement: Add pprof http service + +This service is useful to trigger diagnostics +on running processes + +https://github.com/cs3org/reva/pull/4508 diff --git a/cmd/revad/runtime/http.go b/cmd/revad/runtime/http.go index 1de255cdc1..9a919756c5 100644 --- a/cmd/revad/runtime/http.go +++ b/cmd/revad/runtime/http.go @@ -25,7 +25,6 @@ import ( "github.com/cs3org/reva/internal/http/interceptors/appctx" "github.com/cs3org/reva/internal/http/interceptors/auth" "github.com/cs3org/reva/internal/http/interceptors/log" - "github.com/cs3org/reva/internal/http/interceptors/metrics" "github.com/cs3org/reva/internal/http/interceptors/trace" "github.com/cs3org/reva/pkg/rhttp/global" "github.com/pkg/errors" @@ -72,7 +71,6 @@ func initHTTPMiddlewares(conf map[string]map[string]any, unprotected []string, l authMiddle, log.New(), appctx.New(*logger), - metrics.New(), trace.New(), } diff --git a/internal/http/services/loader/loader.go b/internal/http/services/loader/loader.go index 405d8ff5df..d7d54f11e3 100644 --- a/internal/http/services/loader/loader.go +++ b/internal/http/services/loader/loader.go @@ -34,6 +34,7 @@ import ( _ "github.com/cs3org/reva/internal/http/services/owncloud/ocs" _ "github.com/cs3org/reva/internal/http/services/pingpong" _ "github.com/cs3org/reva/internal/http/services/plugins" + _ "github.com/cs3org/reva/internal/http/services/pprof" _ "github.com/cs3org/reva/internal/http/services/preferences" _ "github.com/cs3org/reva/internal/http/services/prometheus" _ "github.com/cs3org/reva/internal/http/services/reverseproxy" diff --git a/internal/http/services/pprof/pprof.go b/internal/http/services/pprof/pprof.go new file mode 100644 index 0000000000..c0773e53c7 --- /dev/null +++ b/internal/http/services/pprof/pprof.go @@ -0,0 +1,80 @@ +// Copyright 2018-2024 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package pprof + +import ( + "context" + "net/http" + "net/http/pprof" + + "github.com/cs3org/reva/pkg/rhttp/global" + "github.com/cs3org/reva/pkg/utils/cfg" +) + +func init() { + global.Register("pprof", New) +} + +// New returns a new pprof service. +func New(ctx context.Context, m map[string]interface{}) (global.Service, error) { + var c config + if err := cfg.Decode(m, &c); err != nil { + return nil, err + } + + c.ApplyDefaults() + + return &svc{conf: &c}, nil +} + +// Close performs cleanup. +func (s *svc) Close() error { + return nil +} + +type config struct { + Prefix string `mapstructure:"prefix"` +} + +func (c *config) ApplyDefaults() { + // pprof is always exposed at /debug + c.Prefix = "debug" +} + +type svc struct { + conf *config +} + +func (s *svc) Prefix() string { + return s.conf.Prefix +} + +func (s *svc) Unprotected() []string { + return []string{"/"} +} + +func (s *svc) Handler() http.Handler { + mux := http.NewServeMux() + // example: /debug/pprof/profile + mux.HandleFunc("/pprof/", pprof.Index) + mux.HandleFunc("/pprof/profile", pprof.Profile) + mux.HandleFunc("/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/pprof/trace", pprof.Trace) + return mux +}