diff --git a/changelog/unreleased/sysinfo-http-service.md b/changelog/unreleased/sysinfo-http-service.md new file mode 100644 index 0000000000..c946275ea7 --- /dev/null +++ b/changelog/unreleased/sysinfo-http-service.md @@ -0,0 +1,5 @@ +Enhancement: System information HTTP service + +This service exposes system information via an HTTP endpoint. This currently only includes Reva version information but can be extended easily. The information are exposed in the form of Prometheus metrics so that we can gather these in a streamlined way. + +https://github.com/cs3org/reva/pull/1037 diff --git a/cmd/reva/main.go b/cmd/reva/main.go index acc0dacdb8..2afdb9b8a3 100644 --- a/cmd/reva/main.go +++ b/cmd/reva/main.go @@ -23,6 +23,8 @@ import ( "fmt" "os" "strings" + + "github.com/cs3org/reva/pkg/sysinfo" ) var ( @@ -41,6 +43,8 @@ func init() { } func main() { + // initiliaze the global system information + sysinfo.InitSystemInfo(&sysinfo.RevaVersion{Version: version, BuildDate: buildDate, GitCommit: gitCommit, GoVersion: goVersion}) cmds := []*command{ versionCommand(), diff --git a/cmd/revad/main.go b/cmd/revad/main.go index 32e6d15840..d803d5ca00 100644 --- a/cmd/revad/main.go +++ b/cmd/revad/main.go @@ -31,6 +31,8 @@ import ( "github.com/cs3org/reva/cmd/revad/internal/config" "github.com/cs3org/reva/cmd/revad/internal/grace" "github.com/cs3org/reva/cmd/revad/runtime" + "github.com/cs3org/reva/pkg/sysinfo" + "github.com/gofrs/uuid" ) @@ -50,6 +52,9 @@ var ( func main() { flag.Parse() + // initiliaze the global system information + sysinfo.InitSystemInfo(&sysinfo.RevaVersion{Version: version, BuildDate: buildDate, GitCommit: gitCommit, GoVersion: goVersion}) + handleVersionFlag() handleSignalFlag() diff --git a/docs/content/en/docs/config/http/services/sysinfo/_index.md b/docs/content/en/docs/config/http/services/sysinfo/_index.md new file mode 100644 index 0000000000..ff06a4f389 --- /dev/null +++ b/docs/content/en/docs/config/http/services/sysinfo/_index.md @@ -0,0 +1,15 @@ +--- +title: "sysinfo" +linkTitle: "sysinfo" +weight: 10 +description: > + Configuration for the system information service +--- + +{{% dir name="prefix" type="string" default="sysinfo" %}} +Endpoint of the system information service. +{{< highlight toml >}} +[http.services.sysinfo] +prefix = "/sysinfo" +{{< /highlight >}} +{{% /dir %}} diff --git a/go.mod b/go.mod index da84c1c6ed..3f6935625c 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/pkg/xattr v0.4.1 github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect + github.com/prometheus/client_golang v1.2.1 github.com/rs/cors v1.7.0 github.com/rs/zerolog v1.19.0 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index a1e113a9e1..43bc6c3756 100644 --- a/go.sum +++ b/go.sum @@ -676,6 +676,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.1.1/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= @@ -873,6 +874,7 @@ golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200331124033-c3d80250170d h1:nc5K6ox/4lTFbMVSL9WRR81ixkcwXThoiF6yf+R9scA= golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -991,6 +993,7 @@ google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvx google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940 h1:MRHtG0U6SnaUb+s+LhNE1qt1FQ1wlhqr5E4usBKC0uA= google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= @@ -1031,6 +1034,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo= gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/http/services/loader/loader.go b/internal/http/services/loader/loader.go index 9d35075393..06a4534a49 100644 --- a/internal/http/services/loader/loader.go +++ b/internal/http/services/loader/loader.go @@ -30,6 +30,7 @@ import ( _ "github.com/cs3org/reva/internal/http/services/owncloud/ocdav" _ "github.com/cs3org/reva/internal/http/services/owncloud/ocs" _ "github.com/cs3org/reva/internal/http/services/prometheus" + _ "github.com/cs3org/reva/internal/http/services/sysinfo" _ "github.com/cs3org/reva/internal/http/services/wellknown" // Add your own service here ) diff --git a/internal/http/services/sysinfo/prometheus.go b/internal/http/services/sysinfo/prometheus.go new file mode 100644 index 0000000000..98ddeb0821 --- /dev/null +++ b/internal/http/services/sysinfo/prometheus.go @@ -0,0 +1,94 @@ +// Copyright 2018-2020 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 sysinfo + +import ( + "fmt" + "net/http" + "reflect" + "strings" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + + "github.com/cs3org/reva/pkg/sysinfo" + "github.com/cs3org/reva/pkg/utils" +) + +type prometheusSysInfoHandler struct { + registry *prometheus.Registry + sysInfoMetric prometheus.GaugeFunc + + httpHandler http.Handler +} + +func (psysinfo *prometheusSysInfoHandler) init() error { + // Create all necessary Prometheus objects + psysinfo.registry = prometheus.NewRegistry() + psysinfo.sysInfoMetric = prometheus.NewGaugeFunc( + prometheus.GaugeOpts{ + Namespace: "revad", + Name: "sys_info", + Help: "A metric with a constant '1' value labeled by various system information elements", + ConstLabels: psysinfo.getLabels("", sysinfo.SysInfo), + }, + func() float64 { return 1 }, + ) + psysinfo.httpHandler = promhttp.HandlerFor(psysinfo.registry, promhttp.HandlerOpts{}) + + if err := psysinfo.registry.Register(psysinfo.sysInfoMetric); err != nil { + return fmt.Errorf("unable to register the system information metric: %v", err) + } + + return nil +} + +func (psysinfo *prometheusSysInfoHandler) getLabels(root string, i interface{}) prometheus.Labels { + labels := prometheus.Labels{} + + // Iterate over each field of the given interface, recursively collecting the values as labels + v := reflect.ValueOf(i).Elem() + for i := 0; i < v.NumField(); i++ { + // Check if the field was tagged with 'sysinfo:omitlabel'; if so, skip this field + tags := v.Type().Field(i).Tag.Get("sysinfo") + if strings.Contains(tags, "omitlabel") { + continue + } + + // Get the name of the field from the parent structure + fieldName := utils.ToSnakeCase(v.Type().Field(i).Name) + if len(root) > 0 { + fieldName = "_" + fieldName + } + fieldName = root + fieldName + + // Check if the field is either a struct or a pointer to a struct; in that case, process the field recursively + f := v.Field(i) + if f.Kind() == reflect.Struct || (f.Kind() == reflect.Ptr && f.Elem().Kind() == reflect.Struct) { + // Merge labels recursively + for key, val := range psysinfo.getLabels(fieldName, f.Interface()) { + labels[key] = val + } + } else { // Store the value of the field in the labels + labels[fieldName] = fmt.Sprintf("%v", f) + } + } + + return labels +} diff --git a/internal/http/services/sysinfo/sysinfo.go b/internal/http/services/sysinfo/sysinfo.go new file mode 100644 index 0000000000..b2c20154bf --- /dev/null +++ b/internal/http/services/sysinfo/sysinfo.go @@ -0,0 +1,135 @@ +// Copyright 2018-2020 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 sysinfo + +import ( + "net/http" + "strings" + + "github.com/mitchellh/mapstructure" + "github.com/pkg/errors" + "github.com/rs/zerolog" + + "github.com/cs3org/reva/pkg/appctx" + "github.com/cs3org/reva/pkg/rhttp/global" + "github.com/cs3org/reva/pkg/sysinfo" +) + +func init() { + global.Register(serviceName, New) +} + +type config struct { + Prefix string `mapstructure:"prefix"` +} + +type svc struct { + conf *config + + promHandler *prometheusSysInfoHandler +} + +const ( + serviceName = "sysinfo" +) + +// Close is called when this service is being stopped. +func (s *svc) Close() error { + return nil +} + +// Prefix returns the main endpoint of this service. +func (s *svc) Prefix() string { + return s.conf.Prefix +} + +// Unprotected returns all endpoints that can be queried without prior authorization. +func (s *svc) Unprotected() []string { + return []string{"/"} +} + +// Handler serves all HTTP requests. +func (s *svc) Handler() http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt := strings.ToLower(r.URL.Query().Get("format")) + if len(fmt) == 0 { + // No format was specified, so let Prometheus handle the request + s.promHandler.httpHandler.ServeHTTP(w, r) + } else { + // Otherwise, provide the system information in the requested format + data := "" + switch fmt { + case "json": + data = s.getJSONData() + default: + data = "Unsupported format" + } + + log := appctx.GetLogger(r.Context()) + if _, err := w.Write([]byte(data)); err != nil { + log.Err(err).Msg("error writing SysInfo response") + } + } + }) +} + +func (s *svc) getJSONData() string { + if data, err := sysinfo.SysInfo.ToJSON(); err == nil { + return data + } + + return "" +} + +func parseConfig(m map[string]interface{}) (*config, error) { + cfg := &config{} + if err := mapstructure.Decode(m, &cfg); err != nil { + return nil, errors.Wrap(err, "sysinfo: error decoding configuration") + } + applyDefaultConfig(cfg) + return cfg, nil +} + +func applyDefaultConfig(conf *config) { + if conf.Prefix == "" { + conf.Prefix = serviceName + } +} + +// New returns a new SysInfo service. +func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) { + // Prepare the configuration + conf, err := parseConfig(m) + if err != nil { + return nil, err + } + + // Create the Prometheus system information handler + promHandler := &prometheusSysInfoHandler{} + if err := promHandler.init(); err != nil { + return nil, err + } + + // Create the service + s := &svc{ + conf: conf, + promHandler: promHandler, + } + return s, nil +} diff --git a/pkg/sysinfo/reva.go b/pkg/sysinfo/reva.go new file mode 100644 index 0000000000..1420a5252c --- /dev/null +++ b/pkg/sysinfo/reva.go @@ -0,0 +1,27 @@ +// Copyright 2018-2020 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 sysinfo + +// RevaVersion stores version information about Reva. +type RevaVersion struct { + Version string `json:"version"` + BuildDate string `json:"build_date" sysinfo:"omitlabel"` + GitCommit string `json:"git_commit" sysinfo:"omitlabel"` + GoVersion string `json:"go_version" sysinfo:"omitlabel"` +} diff --git a/pkg/sysinfo/sysinfo.go b/pkg/sysinfo/sysinfo.go new file mode 100644 index 0000000000..8c8743df97 --- /dev/null +++ b/pkg/sysinfo/sysinfo.go @@ -0,0 +1,73 @@ +// Copyright 2018-2020 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 sysinfo + +import ( + "encoding/json" + "fmt" + "reflect" +) + +// SystemInformation stores general information about Reva and the system it's running on. +type SystemInformation struct { + // Reva holds the main Reva information + Reva *RevaVersion `json:"reva"` +} + +var ( + // SysInfo provides global system information. + SysInfo *SystemInformation = &SystemInformation{} +) + +// ToJSON converts the system information to JSON. +func (sysInfo *SystemInformation) ToJSON() (string, error) { + data, err := json.MarshalIndent(sysInfo, "", "\t") + if err != nil { + return "", fmt.Errorf("unable to marshal the system information: %v", err) + } + return string(data), nil +} + +// InitSystemInfo initializes the global system information object. +func InitSystemInfo(revaVersion *RevaVersion) { + SysInfo = &SystemInformation{ + Reva: revaVersion, + } + + // Replace any empty values in the system information by more meaningful ones + replaceEmptyInfoValues(SysInfo) +} + +func replaceEmptyInfoValues(i interface{}) { + // Iterate over each field of the given interface and search for "empty" values + v := reflect.ValueOf(i).Elem() + for i := 0; i < v.NumField(); i++ { + // Check if the field is either a struct or a pointer to a struct; in that case, process the field recursively + f := v.Field(i) + if f.Kind() == reflect.Struct || (f.Kind() == reflect.Ptr && f.Elem().Kind() == reflect.Struct) { + replaceEmptyInfoValues(f.Interface()) + } else if f.CanSet() { // Replace empty values with something more meaningful + if f.Kind() == reflect.String { + if len(f.String()) == 0 { + f.SetString("(Unknown)") + } + } + } + } +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index d885ca1ed5..c7888b55f2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -21,9 +21,15 @@ package utils import ( "net" "net/http" + "regexp" "strings" ) +var ( + matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") + matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") +) + // Skip evaluates whether a source endpoint contains any of the prefixes. // i.e: /a/b/c/d/e contains prefix /a/b/c func Skip(source string, prefixes []string) bool { @@ -55,3 +61,10 @@ func GetClientIP(r *http.Request) (string, error) { } return clientIP, nil } + +// ToSnakeCase converts a CamelCase string to a snake_case string. +func ToSnakeCase(str string) string { + snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") + snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") + return strings.ToLower(snake) +}