-
Notifications
You must be signed in to change notification settings - Fork 112
/
metrics.go
106 lines (90 loc) · 3.45 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2018-2021 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/v2/pkg/utils"
)
type sysInfoMetricsLabels = map[tag.Key]string
func registerSystemInfoMetrics() error {
labels := getSystemInfoMetricsLabels("", SysInfo)
// Collect all labels and their values; the values are stored as mutators
tagKeys := make([]tag.Key, 0, len(labels))
mutators := make([]tag.Mutator, 0, len(labels))
for key, value := range labels {
tagKeys = append(tagKeys, key)
mutators = append(mutators, tag.Insert(key, value))
}
// Create the OpenCensus statistics and a corresponding view
sysInfoStats := stats.Int64("sys_info", "A metric with a constant '1' value labeled by various system information elements", stats.UnitDimensionless)
sysInfoView := &view.View{
Name: sysInfoStats.Name(),
Description: sysInfoStats.Description(),
Measure: sysInfoStats,
TagKeys: tagKeys,
Aggregation: view.LastValue(),
}
if err := view.Register(sysInfoView); err != nil {
return errors.Wrap(err, "unable to register the system info metrics view")
}
// Create a new context to serve the metrics
if ctx, err := tag.New(context.Background(), mutators...); err == nil {
// Just record a simple hardcoded '1' to expose the system info as a metric
stats.Record(ctx, sysInfoStats.M(1))
} else {
return errors.Wrap(err, "unable to create a context for the system info metrics")
}
return nil
}
func getSystemInfoMetricsLabels(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 getSystemInfoMetricsLabels(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
}