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

The system info metrics now use OpenCensus #1114

Merged
merged 6 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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-cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: System information metrics now use OpenCensus and were moved out of the Prometheus service

The system information metrics are now based on OpenCensus instead of the Prometheus client library. Furthermore, its initialization was moved out of the Prometheus HTTP service to keep things clean.

https://github.com/cs3org/reva/pull/1114
5 changes: 4 additions & 1 deletion cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func main() {
flag.Parse()

// initialize the global system information
sysinfo.InitSystemInfo(&sysinfo.RevaVersion{Version: version, BuildDate: buildDate, GitCommit: gitCommit, GoVersion: goVersion})
if err := sysinfo.InitSystemInfo(&sysinfo.RevaVersion{Version: version, BuildDate: buildDate, GitCommit: gitCommit, GoVersion: goVersion}); err != nil {
fmt.Fprintf(os.Stderr, "error initializing system info: %s\n", err.Error())
// This is not really a fatal error, so don't panic
}

handleVersionFlag()
handleSignalFlag()
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ require (
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 // indirect
github.com/pkg/xattr v0.4.1
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/prometheus/client_golang v1.7.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 go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20u
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/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
15 changes: 1 addition & 14 deletions internal/http/services/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ import (
"contrib.go.opencensus.io/exporter/prometheus"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
promclient "github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"go.opencensus.io/stats/view"

"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/sysinfo"

// Initializes goroutines which periodically update stats
_ "github.com/cs3org/reva/pkg/metrics/reader/dummy"
)
Expand All @@ -48,23 +45,15 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)

conf.init()

registry := promclient.NewRegistry() // A Prometheus registry shared by OpenCensus and SysInfo
pe, err := prometheus.NewExporter(prometheus.Options{
Namespace: "revad",
Registry: registry,
})
if err != nil {
return nil, errors.Wrap(err, "prometheus: error creating exporter")
}

// Initialize the SysInfo Prometheus exporter
sysinfo, err := sysinfo.NewPrometheusExporter(registry)
if err != nil {
return nil, errors.Wrap(err, "prometheus: unable to create system info exporter")
}

view.RegisterExporter(pe)
return &svc{prefix: conf.Prefix, h: pe, sysinfo: sysinfo}, nil
return &svc{prefix: conf.Prefix, h: pe}, nil
}

type config struct {
Expand All @@ -80,8 +69,6 @@ func (c *config) init() {
type svc struct {
prefix string
h http.Handler

sysinfo *sysinfo.PrometheusExporter
}

func (s *svc) Prefix() string {
Expand Down
134 changes: 134 additions & 0 deletions pkg/sysinfo/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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 (
"context"
"fmt"
"reflect"
"strings"

"github.com/pkg/errors"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"

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

type sysInfoMetricsLabels = map[tag.Key]string

type sysInfoMetrics struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the struct and the global variable? Let's just get the labels and register them.

sysInfoStats *stats.Int64Measure
sysInfoView *view.View

labels sysInfoMetricsLabels

ctx context.Context
}

var (
metrics *sysInfoMetrics
)

func (m *sysInfoMetrics) init() error {
m.labels = m.getLabels("", SysInfo)

// Collect all labels and their values; the values are stored as mutators
tagKeys := make([]tag.Key, 0, len(m.labels))
mutators := make([]tag.Mutator, 0, len(m.labels))
for key, value := range m.labels {
tagKeys = append(tagKeys, key)
mutators = append(mutators, tag.Insert(key, value))
}

// Create the OpenCensus statistics and a corresponding view
m.sysInfoStats = stats.Int64("sys_info", "A metric with a constant '1' value labeled by various system information elements", stats.UnitDimensionless)
m.sysInfoView = &view.View{
Name: m.sysInfoStats.Name(),
Description: m.sysInfoStats.Description(),
Measure: m.sysInfoStats,
TagKeys: tagKeys,
Aggregation: view.LastValue(),
}

if err := view.Register(m.sysInfoView); err != nil {
return errors.Wrap(err, "unable to register the system info view")
}

// Create a new context to serve the metrics
if ctx, err := tag.New(context.Background(), mutators...); err == nil {
m.ctx = ctx
} else {
return errors.Wrap(err, "unable to create a context")
}

return nil
}

func (m *sysInfoMetrics) getLabels(root string, i interface{}) sysInfoMetricsLabels {
labels := sysInfoMetricsLabels{}

// 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 m.getLabels(fieldName, f.Interface()) {
labels[key] = val
}
} else { // Store the value of the field in the labels
key := tag.MustNewKey(fieldName)
labels[key] = fmt.Sprintf("%v", f)
}
}

return labels
}

func (m *sysInfoMetrics) record() {
// Just record a simple hardcoded '1' to expose the system info as a metric
stats.Record(m.ctx, m.sysInfoStats.M(1))
}

func registerSystemInfoMetrics() error {
// Create and initialize the local system info metrics object
metrics = &sysInfoMetrics{}
if err := metrics.init(); err != nil {
return errors.Wrap(err, "unable to initialize the system information metrics")
}

metrics.record()
return nil
}
103 changes: 0 additions & 103 deletions pkg/sysinfo/prometheus.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/sysinfo/reva.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package sysinfo
// RevaVersion stores version information about Reva.
type RevaVersion struct {
Version string `json:"version"`
BuildDate string `json:"build_date" sysinfo:"omitlabel"`
BuildDate string `json:"build_date"`
GitCommit string `json:"git_commit" sysinfo:"omitlabel"`
GoVersion string `json:"go_version" sysinfo:"omitlabel"`
}
29 changes: 19 additions & 10 deletions pkg/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/json"
"fmt"
"reflect"

"github.com/pkg/errors"
)

// SystemInformation stores general information about Reva and the system it's running on.
Expand All @@ -44,16 +46,6 @@ func (sysInfo *SystemInformation) ToJSON() (string, error) {
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()
Expand All @@ -71,3 +63,20 @@ func replaceEmptyInfoValues(i interface{}) {
}
}
}

// InitSystemInfo initializes the global system information object and also registers the corresponding metrics.
func InitSystemInfo(revaVersion *RevaVersion) error {
SysInfo = &SystemInformation{
Reva: revaVersion,
}

// Replace any empty values in the system information by more meaningful ones
replaceEmptyInfoValues(SysInfo)

// Register the system information metrics, as the necessary system info object has been filled out
if err := registerSystemInfoMetrics(); err != nil {
return errors.Wrap(err, "unable to register the system info metrics")
}

return nil
}