Skip to content
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

Added a system information HTTP service #1037

Merged
merged 3 commits into from
Aug 5, 2020
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
5 changes: 5 additions & 0 deletions changelog/unreleased/sysinfo-http-service.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"fmt"
"os"
"strings"

"github.com/cs3org/reva/pkg/sysinfo"
)

var (
Expand All @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()

Expand Down
15 changes: 15 additions & 0 deletions docs/content/en/docs/config/http/services/sysinfo/_index.md
Original file line number Diff line number Diff line change
@@ -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 %}}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/http/services/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
94 changes: 94 additions & 0 deletions internal/http/services/sysinfo/prometheus.go
Original file line number Diff line number Diff line change
@@ -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
}
135 changes: 135 additions & 0 deletions internal/http/services/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions pkg/sysinfo/reva.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading