Skip to content

Commit 9e789b5

Browse files
author
Xuewei Zhang
committed
Refactor on metrics so that names for all the views are tracked
1 parent 0f2fce5 commit 9e789b5

File tree

8 files changed

+292
-159
lines changed

8 files changed

+292
-159
lines changed

pkg/problemmetrics/problem_metrics.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func NewProblemMetricsManagerOrDie() *ProblemMetricsManager {
4949

5050
var err error
5151
pmm.problemCounter, err = metrics.NewInt64Metric(
52-
"problem_counter",
52+
metrics.ProblemCounterID,
53+
string(metrics.ProblemCounterID),
5354
"Number of times a specific type of problem have occurred.",
5455
"1",
5556
metrics.Sum,
@@ -59,7 +60,8 @@ func NewProblemMetricsManagerOrDie() *ProblemMetricsManager {
5960
}
6061

6162
pmm.problemGauge, err = metrics.NewInt64Metric(
62-
"problem_gauge",
63+
metrics.ProblemGaugeID,
64+
string(metrics.ProblemGaugeID),
6365
"Whether a specific type of problem is affecting the node or not.",
6466
"1",
6567
metrics.LastValue,

pkg/systemstatsmonitor/disk_collector.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func NewDiskCollectorOrDie(diskConfig *ssmtypes.DiskStatsConfig) *diskCollector
4949

5050
// Use metrics.Sum aggregation method to ensure the metric is a counter/cumulative metric.
5151
dc.mIOTime, err = metrics.NewInt64Metric(
52-
diskConfig.MetricsConfigs["disk/io_time"].DisplayName,
52+
metrics.DiskIOTimeID,
53+
diskConfig.MetricsConfigs[string(metrics.DiskIOTimeID)].DisplayName,
5354
"The IO time spent on the disk",
5455
"second",
5556
metrics.Sum,
@@ -60,7 +61,8 @@ func NewDiskCollectorOrDie(diskConfig *ssmtypes.DiskStatsConfig) *diskCollector
6061

6162
// Use metrics.Sum aggregation method to ensure the metric is a counter/cumulative metric.
6263
dc.mWeightedIO, err = metrics.NewInt64Metric(
63-
diskConfig.MetricsConfigs["disk/weighted_io"].DisplayName,
64+
metrics.DiskWeightedIOID,
65+
diskConfig.MetricsConfigs[string(metrics.DiskWeightedIOID)].DisplayName,
6466
"The weighted IO on the disk",
6567
"second",
6668
metrics.Sum,
@@ -70,7 +72,8 @@ func NewDiskCollectorOrDie(diskConfig *ssmtypes.DiskStatsConfig) *diskCollector
7072
}
7173

7274
dc.mAvgQueueLen, err = metrics.NewFloat64Metric(
73-
diskConfig.MetricsConfigs["disk/avg_queue_len"].DisplayName,
75+
metrics.DiskAvgQueueLenID,
76+
diskConfig.MetricsConfigs[string(metrics.DiskAvgQueueLenID)].DisplayName,
7477
"The average queue length on the disk",
7578
"second",
7679
metrics.LastValue,

pkg/systemstatsmonitor/host_collector.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
)
2727

2828
type hostCollector struct {
29-
tags map[string]string
30-
uptime *metrics.Int64Metric
29+
tags map[string]string
30+
uptime *metrics.Int64Metric
3131
}
3232

3333
func NewHostCollectorOrDie(hostConfig *ssmtypes.HostStatsConfig) *hostCollector {
34-
hc := hostCollector{map[string]string{}, nil, 0}
34+
hc := hostCollector{map[string]string{}, nil}
3535

3636
kernelVersion, err := host.KernelVersion()
3737
if err != nil {
@@ -48,7 +48,8 @@ func NewHostCollectorOrDie(hostConfig *ssmtypes.HostStatsConfig) *hostCollector
4848
// Use metrics.Sum aggregation method to ensure the metric is a counter/cumulative metric.
4949
if hostConfig.MetricsConfigs["host/uptime"].DisplayName != "" {
5050
hc.uptime, err = metrics.NewInt64Metric(
51-
hostConfig.MetricsConfigs["host/uptime"].DisplayName,
51+
metrics.HostUptimeID,
52+
hostConfig.MetricsConfigs[string(metrics.HostUptimeID)].DisplayName,
5253
"The uptime of the operating system",
5354
"second",
5455
metrics.LastValue,
@@ -75,4 +76,4 @@ func (hc *hostCollector) collect() {
7576
if hc.uptime != nil {
7677
hc.uptime.Record(hc.tags, int64(uptime))
7778
}
78-
}
79+
}

pkg/types/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,19 @@ type ProblemDaemonHandler struct {
131131
// CmdOptionDescription explains how to configure the problem daemon from command line arguments.
132132
CmdOptionDescription string
133133
}
134+
135+
// ExporterType is the type of the exporter.
136+
type ExporterType string
137+
138+
// ExporterConfigPathMap represents configurations on all types of exporters:
139+
// 1) Each key represents a type of exporter.
140+
// 2) Each value represents the config file path for the exporter.
141+
type ExporterConfigPathMap map[ExporterType]*string
142+
143+
// ExporterHandler represents the initialization handler for a type of exporter.
144+
type ExporterHandler struct {
145+
// CreateExporterOrDie initializes an exporter, panic if error occurs.
146+
CreateExporterOrDie func(string) Exporter
147+
// CmdOptionDescription explains how to configure the exporter from command line arguments.
148+
CmdOptionDescription string
149+
}

pkg/util/metrics/helpers.go

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ limitations under the License.
1616
package metrics
1717

1818
import (
19-
"context"
2019
"fmt"
2120
"strings"
2221
"sync"
2322

2423
pcm "github.com/prometheus/client_model/go"
2524
"github.com/prometheus/common/expfmt"
26-
"go.opencensus.io/stats"
27-
"go.opencensus.io/stats/view"
2825
"go.opencensus.io/tag"
2926
)
3027

@@ -47,152 +44,6 @@ const (
4744
Sum Aggregation = "Sum"
4845
)
4946

50-
// Int64MetricRepresentation represents a snapshot of an int64 metrics.
51-
// This is used for inspecting metric internals.
52-
type Int64MetricRepresentation struct {
53-
// Name is the metric name.
54-
Name string
55-
// Labels contains all metric labels in key-value pair format.
56-
Labels map[string]string
57-
// Value is the value of the metric.
58-
Value int64
59-
}
60-
61-
// Int64Metric represents an int64 metric.
62-
type Int64Metric struct {
63-
name string
64-
measure *stats.Int64Measure
65-
}
66-
67-
// NewInt64Metric create a Int64Metric metric, returns nil when name is empty.
68-
func NewInt64Metric(name string, description string, unit string, aggregation Aggregation, tagNames []string) (*Int64Metric, error) {
69-
if name == "" {
70-
return nil, nil
71-
}
72-
73-
tagKeys, err := getTagKeysFromNames(tagNames)
74-
if err != nil {
75-
return nil, fmt.Errorf("failed to create metric %q because of tag creation failure: %v", name, err)
76-
}
77-
78-
var aggregationMethod *view.Aggregation
79-
switch aggregation {
80-
case LastValue:
81-
aggregationMethod = view.LastValue()
82-
case Sum:
83-
aggregationMethod = view.Sum()
84-
default:
85-
return nil, fmt.Errorf("unknown aggregation option %q", aggregation)
86-
}
87-
88-
measure := stats.Int64(name, description, unit)
89-
newView := &view.View{
90-
Name: name,
91-
Measure: measure,
92-
Description: description,
93-
Aggregation: aggregationMethod,
94-
TagKeys: tagKeys,
95-
}
96-
view.Register(newView)
97-
98-
metric := Int64Metric{name, measure}
99-
return &metric, nil
100-
}
101-
102-
// Record records a measurement for the metric, with provided tags as metric labels.
103-
func (metric *Int64Metric) Record(tags map[string]string, measurement int64) error {
104-
var mutators []tag.Mutator
105-
106-
tagMapMutex.RLock()
107-
defer tagMapMutex.RUnlock()
108-
109-
for tagName, tagValue := range tags {
110-
tagKey, ok := tagMap[tagName]
111-
if !ok {
112-
return fmt.Errorf("referencing none existing tag %q in metric %q", tagName, metric.name)
113-
}
114-
mutators = append(mutators, tag.Upsert(tagKey, tagValue))
115-
}
116-
117-
return stats.RecordWithTags(
118-
context.Background(),
119-
mutators,
120-
metric.measure.M(measurement))
121-
}
122-
123-
// Float64MetricRepresentation represents a snapshot of a float64 metrics.
124-
// This is used for inspecting metric internals.
125-
type Float64MetricRepresentation struct {
126-
// Name is the metric name.
127-
Name string
128-
// Labels contains all metric labels in key-value pair format.
129-
Labels map[string]string
130-
// Value is the value of the metric.
131-
Value float64
132-
}
133-
134-
// Float64Metric represents an float64 metric.
135-
type Float64Metric struct {
136-
name string
137-
measure *stats.Float64Measure
138-
}
139-
140-
// NewFloat64Metric create a Float64Metric metrics, returns nil when name is empty.
141-
func NewFloat64Metric(name string, description string, unit string, aggregation Aggregation, tagNames []string) (*Float64Metric, error) {
142-
if name == "" {
143-
return nil, nil
144-
}
145-
146-
tagKeys, err := getTagKeysFromNames(tagNames)
147-
if err != nil {
148-
return nil, fmt.Errorf("failed to create metric %q because of tag creation failure: %v", name, err)
149-
}
150-
151-
var aggregationMethod *view.Aggregation
152-
switch aggregation {
153-
case LastValue:
154-
aggregationMethod = view.LastValue()
155-
case Sum:
156-
aggregationMethod = view.Sum()
157-
default:
158-
return nil, fmt.Errorf("unknown aggregation option %q", aggregation)
159-
}
160-
161-
measure := stats.Float64(name, description, unit)
162-
newView := &view.View{
163-
Name: name,
164-
Measure: measure,
165-
Description: description,
166-
Aggregation: aggregationMethod,
167-
TagKeys: tagKeys,
168-
}
169-
view.Register(newView)
170-
171-
metric := Float64Metric{name, measure}
172-
return &metric, nil
173-
}
174-
175-
// Record records a measurement for the metric, with provided tags as metric labels.
176-
func (metric *Float64Metric) Record(tags map[string]string, measurement float64) error {
177-
var mutators []tag.Mutator
178-
179-
tagMapMutex.RLock()
180-
defer tagMapMutex.RUnlock()
181-
182-
for tagName, tagValue := range tags {
183-
tagKey, ok := tagMap[tagName]
184-
if !ok {
185-
return fmt.Errorf("referencing none existing tag %q in metric %q", tagName, metric.name)
186-
}
187-
mutators = append(mutators, tag.Upsert(tagKey, tagValue))
188-
}
189-
190-
return stats.RecordWithTags(
191-
context.Background(),
192-
mutators,
193-
metric.measure.M(measurement))
194-
}
195-
19647
func getTagKeysFromNames(tagNames []string) ([]tag.Key, error) {
19748
tagMapMutex.Lock()
19849
defer tagMapMutex.Unlock()

pkg/util/metrics/metric.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package metrics
17+
18+
import (
19+
"sync"
20+
)
21+
22+
const (
23+
ProblemCounterID MetricID = "problem_counter"
24+
ProblemGaugeID MetricID = "problem_gauge"
25+
DiskIOTimeID MetricID = "disk/io_time"
26+
DiskWeightedIOID MetricID = "disk/weighted_io"
27+
DiskAvgQueueLenID MetricID = "disk/avg_queue_len"
28+
HostUptimeID MetricID = "host/uptime"
29+
)
30+
31+
var MetricMap MetricMapping
32+
33+
func init() {
34+
MetricMap.mapMutex.Lock()
35+
defer MetricMap.mapMutex.Unlock()
36+
37+
MetricMap.viewNameToMetricIDMap = make(map[string]MetricID)
38+
}
39+
40+
type MetricID string
41+
42+
type MetricMapping struct {
43+
viewNameToMetricIDMap map[string]MetricID
44+
mapMutex sync.RWMutex
45+
}
46+
47+
func (mm *MetricMapping) AddMapping(metricID MetricID, viewName string) {
48+
mm.mapMutex.Lock()
49+
defer mm.mapMutex.Unlock()
50+
51+
mm.viewNameToMetricIDMap[viewName] = metricID
52+
}
53+
54+
func (mm *MetricMapping) ViewNameToMetricID(viewName string) (MetricID, bool) {
55+
mm.mapMutex.RLock()
56+
defer mm.mapMutex.RUnlock()
57+
58+
id, ok := mm.viewNameToMetricIDMap[viewName]
59+
return id, ok
60+
}

0 commit comments

Comments
 (0)