Skip to content

Commit

Permalink
Added a system information HTTP service (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-WWU-IT authored Aug 5, 2020
1 parent fc44548 commit 0e0ea3f
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 0 deletions.
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
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

0 comments on commit 0e0ea3f

Please sign in to comment.