-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a system information HTTP service (#1037)
- Loading branch information
1 parent
fc44548
commit 0e0ea3f
Showing
11 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
docs/content/en/docs/config/http/services/sysinfo/_index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
Oops, something went wrong.