Skip to content

Commit 7c023e6

Browse files
authored
Add new labels to hardware_model metric (#498)
* Add new labels to metric * Update tests * Release notes.
1 parent 4566ed9 commit 7c023e6

File tree

6 files changed

+56
-34
lines changed

6 files changed

+56
-34
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
10+
## [v1.10.4]
811
### Fixed
9-
- added default port for creating a xresolver connection [#499](https://github.com/xmidt-org/webpa-common/pull/499)
12+
- Added default port for creating a xresolver connection. [#499](https://github.com/xmidt-org/webpa-common/pull/499)
13+
14+
### Added
15+
- Add partner and firmware label to existing `hardware_model` gauge. [#498](https://github.com/xmidt-org/webpa-common/pull/498)
1016

1117
### Changed
1218
- Update word from blacklist to denylist. [#495](https://github.com/xmidt-org/webpa-common/pull/495)
1319
- Update references to the main branch. [#497](https://github.com/xmidt-org/webpa-common/pull/497)
1420

21+
1522
## [v1.10.3]
1623
### Fixed
1724
- Fixed xresolver failing to create route when default port is used. [#494](https://github.com/xmidt-org/webpa-common/pull/494)
@@ -116,7 +123,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
116123
- The first official release. We will be better about documenting changes
117124
moving forward.
118125

119-
[Unreleased]: https://github.com/xmidt-org/webpa-common/compare/v1.10.3...HEAD
126+
[Unreleased]: https://github.com/xmidt-org/webpa-common/compare/v1.10.4...HEAD
127+
[v1.10.4]: https://github.com/xmidt-org/webpa-common/compare/v1.10.3...v1.10.4
120128
[v1.10.3]: https://github.com/xmidt-org/webpa-common/compare/v1.10.2...v1.10.3
121129
[v1.10.2]: https://github.com/xmidt-org/webpa-common/compare/v1.10.1...v1.10.2
122130
[v1.10.1]: https://github.com/xmidt-org/webpa-common/compare/v1.10.0...v1.10.1

convey/conveymetric/metric.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,53 @@ import (
55
"github.com/xmidt-org/webpa-common/convey"
66
)
77

8-
// UnknownLabel is a constant for when key/tag can not be found in the C JSON
9-
const UnknownLabel = "unknown"
8+
// UnknownLabelValue is a constant for when key/tag can not be found in the C JSON.
9+
const UnknownLabelValue = "unknown"
1010

11-
// Closure will be returned after Update(), this should be used to update the struct, aka decrement the count
11+
// Closure will be returned after Update(), this should be used to update the struct, aka decrement the count.
1212
type Closure func()
1313

14-
// Interface provides a way of updating an internal resource
14+
// TagLabelPair is a convenient structure for inputs to create a new convey metric.
15+
type TagLabelPair struct {
16+
Tag string
17+
Label string
18+
}
19+
20+
// Interface provides a way of updating an internal resource.
1521
type Interface interface {
1622
// Update takes the convey JSON to update internal struct, and return a closure to update the struct again, or an
1723
// error
1824
//
1925
// Note: Closure should only be called once.
20-
Update(data convey.C) (Closure, error)
26+
Update(data convey.C, labelPairs ...string) (Closure, error)
2127
}
2228

2329
// NewConveyMetric produces an Interface where gauge is the internal structure to update, tag is the key in the C JSON
2430
// to update the gauge, and label is the `key` for the gauge cardinality.
2531
//
2632
// Note: The Gauge must have the label as one of the constant labels, (aka. the name of the gauge)
27-
func NewConveyMetric(gauge metrics.Gauge, tag string, label string) Interface {
33+
func NewConveyMetric(gauge metrics.Gauge, pairs ...TagLabelPair) Interface {
2834
return &cMetric{
29-
tag: tag,
30-
label: label,
35+
pairs: pairs,
3136
gauge: gauge,
3237
}
3338
}
3439

3540
// cMetric is the internal Interface implementation
3641
type cMetric struct {
37-
tag string
38-
label string
42+
pairs []TagLabelPair
3943
gauge metrics.Gauge
4044
}
4145

42-
func (m *cMetric) Update(data convey.C) (Closure, error) {
43-
key := UnknownLabel
44-
if item, ok := data[m.tag].(string); ok {
45-
key = item
46+
func (m *cMetric) Update(data convey.C, baseLabelPairs ...string) (Closure, error) {
47+
labelPairs := baseLabelPairs
48+
for _, pair := range m.pairs {
49+
labelValue := UnknownLabelValue
50+
if item, ok := data[pair.Tag].(string); ok {
51+
labelValue = item
52+
}
53+
labelPairs = append(labelPairs, pair.Label, labelValue)
4654
}
47-
48-
m.gauge.With(m.label, key).Add(1.0)
49-
return func() { m.gauge.With(m.label, key).Add(-1.0) }, nil
55+
m.gauge.With(labelPairs...).Add(1.0)
56+
return func() { m.gauge.With(labelPairs...).Add(-1.0) }, nil
5057
}

convey/conveymetric/metric_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package conveymetric
22

33
import (
4+
"testing"
5+
46
"github.com/xmidt-org/webpa-common/convey"
57
"github.com/xmidt-org/webpa-common/xmetrics"
68
"github.com/xmidt-org/webpa-common/xmetrics/xmetricstest"
7-
"testing"
89

910
"github.com/stretchr/testify/assert"
1011
)
1112

1213
func TestConveyMetric(t *testing.T) {
1314
assert := assert.New(t)
1415

15-
//namespace, subsystem, name := "test", "basic", "hardware"
16-
1716
gauge := xmetricstest.NewGauge("hardware")
1817

19-
conveyMetric := NewConveyMetric(gauge, "hw-model", "model")
18+
conveyMetric := NewConveyMetric(gauge, []TagLabelPair{{"hw-model", "model"}, {"fw-name", "firmware"}}...)
2019

21-
dec, err := conveyMetric.Update(convey.C{"data": "neat", "hw-model": "hardware123abc"})
20+
dec, err := conveyMetric.Update(convey.C{"data": "neat", "hw-model": "hardware123abc", "fw-name": "firmware-xyz"})
2221
assert.NoError(err)
23-
assert.Equal(float64(1), gauge.With("model", "hardware123abc").(xmetrics.Valuer).Value())
22+
assert.Equal(float64(1), gauge.With("model", "hardware123abc", "firmware", "firmware-xyz").(xmetrics.Valuer).Value())
2423
// remove the update
2524
dec()
2625

27-
assert.Equal(float64(0), gauge.With("model", "hardware123abc").(xmetrics.Valuer).Value())
26+
assert.Equal(float64(0), gauge.With("model", "hardware123abc", "firmware", "firmware-xyz").(xmetrics.Valuer).Value())
2827

2928
// try with no `hw_model`
30-
dec, err = conveyMetric.Update(convey.C{"data": "neat"})
31-
assert.Equal(float64(1), gauge.With("model", UnknownLabel).(xmetrics.Valuer).Value())
29+
dec, err = conveyMetric.Update(convey.C{"data": "neat", "fw-name": "firmware-abc"})
30+
t.Logf("%v+", gauge)
31+
assert.Equal(float64(1), gauge.With("model", UnknownLabelValue, "firmware", "firmware-abc").(xmetrics.Valuer).Value())
3232

3333
// remove the update
3434
dec()
35-
assert.Equal(float64(0), gauge.With("model", UnknownLabel).(xmetrics.Valuer).Value())
35+
assert.Equal(float64(0), gauge.With("model", UnknownLabelValue, "firmware", "firmware-abc").(xmetrics.Valuer).Value())
3636
}

device/manager.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ func NewManager(o *Options) Manager {
103103
Limit: o.maxDevices(),
104104
Measures: measures,
105105
}),
106-
conveyHWMetric: conveymetric.NewConveyMetric(measures.Models, "hw-model", "model"),
106+
conveyHWMetric: conveymetric.NewConveyMetric(measures.Models, []conveymetric.TagLabelPair{
107+
{
108+
Tag: "hw-model",
109+
Label: "model",
110+
},
111+
{
112+
Tag: "fw-name",
113+
Label: "firmware",
114+
}}...),
107115

108116
deviceMessageQueueSize: o.deviceMessageQueueSize(),
109117
pingPeriod: o.pingPeriod(),
@@ -208,8 +216,7 @@ func (m *manager) Connect(response http.ResponseWriter, request *http.Request, r
208216
d.errorLog.Log(logging.MessageKey(), "unable to marshal the convey header", logging.ErrorKey(), err)
209217
}
210218
}
211-
212-
metricClosure, err := m.conveyHWMetric.Update(cvy)
219+
metricClosure, err := m.conveyHWMetric.Update(cvy, "partnerid", metadata.PartnerIDClaim())
213220
if err != nil {
214221
d.errorLog.Log(logging.MessageKey(), "failed to update convey metrics", logging.ErrorKey(), err)
215222
}

device/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func TestGaugeCardinality(t *testing.T) {
386386
assert.NoError(err)
387387

388388
assert.NotPanics(func() {
389-
dec, err := m.(*manager).conveyHWMetric.Update(convey.C{"hw-model": "cardinality", "model": "f"})
389+
dec, err := m.(*manager).conveyHWMetric.Update(convey.C{"hw-model": "cardinality", "fw-name": "firmware-number", "model": "f"}, "partnerid", "comcast")
390390
assert.NoError(err)
391391
dec()
392392
})

device/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Metrics() []xmetrics.Metric {
5656
{
5757
Name: ModelGauge,
5858
Type: "gauge",
59-
LabelNames: []string{"model"},
59+
LabelNames: []string{"model", "partnerid", "firmware"},
6060
},
6161
}
6262
}

0 commit comments

Comments
 (0)