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

Rename labels to attributes in metric metadata #6220

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions cmd/mdatagen/documentation.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ These are the metrics available for this scraper.
| {{ $metricName }} | {{ $metricInfo.Description }} | {{ $metricInfo.Unit }} | {{ $metricInfo.Data.Type }} | <ul>
{{- end }}

{{- range $index, $labelName := $metricInfo.Labels }} <li>{{ $labelName }}</li> {{- end }} </ul> |
{{- range $index, $attributeName := $metricInfo.Attributes }} <li>{{ $attributeName }}</li> {{- end }} </ul> |
{{- end }}

## Attributes

| Name | Description |
| ---- | ----------- |
{{- range $labelName, $labelInfo := .Labels }}
| {{ $labelName }} | {{ $labelInfo.Description }} |
{{- range $attributeName, $attributeInfo := .Attributes }}
| {{ $attributeName }} | {{ $attributeInfo.Description }} |
{{- end }}
39 changes: 20 additions & 19 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (mn metricName) Render() (string, error) {
return formatIdentifier(string(mn), true)
}

type labelName string
type attributeName string

func (mn labelName) Render() (string, error) {
func (mn attributeName) Render() (string, error) {
return formatIdentifier(string(mn), true)
}

Expand All @@ -55,26 +55,26 @@ type metric struct {
// Date is set to generic metric data interface after validating.
Data MetricData `yaml:"-"`

// Labels is the list of labels that the metric emits.
Labels []labelName
// Attributes is the list of attributes that the metric emits.
Attributes []attributeName
}

type label struct {
// Description describes the purpose of the label.
type attribute struct {
// Description describes the purpose of the attribute.
Description string `validate:"notblank"`
// Value can optionally specify the value this label will have.
// For example, the label may have the identifier `MemState` to its
// Value can optionally specify the value this attribute will have.
// For example, the attribute may have the identifier `MemState` to its
// value may be `state` when used.
Value string
// Enum can optionally describe the set of values to which the label can belong.
// Enum can optionally describe the set of values to which the attribute can belong.
Enum []string
}

type metadata struct {
// Name of the component.
Name string `validate:"notblank"`
// Labels emitted by one or more metrics.
Labels map[labelName]label `validate:"dive"`
// Attributes emitted by one or more metrics.
Attributes map[attributeName]attribute `validate:"dive"`
// Metrics that can be emitted by the component.
Metrics map[metricName]metric `validate:"dive"`
}
Expand Down Expand Up @@ -120,13 +120,13 @@ func validateMetadata(out metadata) error {
return fmt.Errorf("failed registering translations: %v", err)
}

if err := v.RegisterTranslation("nosuchlabel", tr, func(ut ut.Translator) error {
return ut.Add("nosuchlabel", "unknown label value", true) // see universal-translator for details
if err := v.RegisterTranslation("nosuchattribute", tr, func(ut ut.Translator) error {
return ut.Add("nosuchattribute", "unknown attribute value", true) // see universal-translator for details
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("nosuchlabel", fe.Field())
t, _ := ut.T("nosuchattribute", fe.Field())
return t
}); err != nil {
return fmt.Errorf("failed registering nosuchlabel: %v", err)
return fmt.Errorf("failed registering nosuchattribute: %v", err)
}

v.RegisterStructValidation(metricValidation, metric{})
Expand Down Expand Up @@ -156,13 +156,14 @@ func validateMetadata(out metadata) error {

// metricValidation validates metric structs.
func metricValidation(sl validator.StructLevel) {
// Make sure that the labels are valid.
// Make sure that the attributes are valid.
md := sl.Top().Interface().(*metadata)
cur := sl.Current().Interface().(metric)

for _, l := range cur.Labels {
if _, ok := md.Labels[l]; !ok {
sl.ReportError(cur.Labels, fmt.Sprintf("Labels[%s]", string(l)), "Labels", "nosuchlabel", "")
for _, l := range cur.Attributes {
if _, ok := md.Attributes[l]; !ok {
sl.ReportError(cur.Attributes, fmt.Sprintf("Attributes[%s]", string(l)), "Attributes", "nosuchattribute",
"")
}
}
}
48 changes: 25 additions & 23 deletions cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
const (
allOptions = `
name: metricreceiver
labels:
freeFormLabel:
description: Label that can take on any value.
attributes:
freeFormAttribute:
description: Attribute that can take on any value.

freeFormLabelWithValue:
freeFormAttributeWithValue:
value: state
description: Label that has alternate value set.
description: Attribute that has alternate value set.

enumLabel:
description: Label with a known set of values.
enumAttribute:
description: Attribute with a known set of values.
enum: [red, green, blue]

metrics:
Expand All @@ -44,10 +44,10 @@ metrics:
type: sum
monotonic: true
aggregation: cumulative
labels: [freeFormLabel, freeFormLabelWithValue, enumLabel]
attributes: [freeFormAttribute, freeFormAttributeWithValue, enumAttribute]
`

unknownMetricLabel = `
unknownMetricAttribute = `
name: metricreceiver
metrics:
system.cpu.time:
Expand All @@ -57,7 +57,7 @@ metrics:
type: sum
monotonic: true
aggregation: cumulative
labels: [missing]
attributes: [missing]
`
unknownMetricType = `
name: metricreceiver
Expand All @@ -67,7 +67,7 @@ metrics:
unit: s
data:
type: invalid
labels:
attributes:
`
)

Expand All @@ -83,16 +83,16 @@ func Test_loadMetadata(t *testing.T) {
yml: allOptions,
want: metadata{
Name: "metricreceiver",
Labels: map[labelName]label{
"enumLabel": {
Description: "Label with a known set of values.",
Attributes: map[attributeName]attribute{
"enumAttribute": {
Description: "Attribute with a known set of values.",
Value: "",
Enum: []string{"red", "green", "blue"}},
"freeFormLabel": {
Description: "Label that can take on any value.",
"freeFormAttribute": {
Description: "Attribute that can take on any value.",
Value: ""},
"freeFormLabelWithValue": {
Description: "Label that has alternate value set.",
"freeFormAttributeWithValue": {
Description: "Attribute that has alternate value set.",
Value: "state"}},
Metrics: map[metricName]metric{
"system.cpu.time": {
Expand All @@ -104,14 +104,16 @@ func Test_loadMetadata(t *testing.T) {
Mono: Mono{Monotonic: true},
},
// YmlData: nil,
Labels: []labelName{"freeFormLabel", "freeFormLabelWithValue", "enumLabel"}}},
Attributes: []attributeName{"freeFormAttribute", "freeFormAttributeWithValue",
"enumAttribute"}}},
},
},
{
name: "unknown metric label",
yml: unknownMetricLabel,
want: metadata{},
wantErr: "error validating struct:\n\tmetadata.Metrics[system.cpu.time].Labels[missing]: unknown label value\n",
name: "unknown metric attribute",
yml: unknownMetricAttribute,
want: metadata{},
wantErr: "error validating struct:\n\tmetadata.Metrics[system.cpu.time]." +
"Attributes[missing]: unknown attribute value\n",
},
{
name: "unknownMetricType",
Expand Down
23 changes: 11 additions & 12 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ var Metrics = &metricStruct{
// manipulating those metrics. M is an alias for Metrics
var M = Metrics

{{- /* Renders label names. */}}
// Labels contains the possible metric labels that can be used.
var Labels = struct {
{{- range $name, $info := .Labels }}
{{- /* Renders attribute names. */}}
// Attributes contains the possible metric attributes that can be used.
var Attributes = struct {
{{- range $name, $info := .Attributes }}
// {{ $name.Render }} ({{ $info.Description }})
{{ $name.Render }} string
{{- end }}
}{
{{- range $name, $info := .Labels }}
{{- range $name, $info := .Attributes }}
{{- if $info.Value }}
"{{ $info.Value }}",
{{- else }}
Expand All @@ -123,16 +123,15 @@ var Labels = struct {
{{- end }}
}

// L contains the possible metric labels that can be used. L is an alias for
// Labels.
var L = Labels
// A is an alias for Attributes.
var A = Attributes

{{- /* Renders label enum values. */}}
{{- /* Renders attribute enum values. */}}

{{ range $name, $info := .Labels }}
{{ range $name, $info := .Attributes }}
{{ if $info.Enum }}
// Label{{ $name.Render }} are the possible values that the label "{{ $name }}" can have.
var Label{{ $name.Render }} = struct {
// Attribute{{ $name.Render }} are the possible values that the attribute "{{ $name }}" can have.
var Attribute{{ $name.Render }} = struct {
{{- range $info.Enum }}
{{ . | publicVar }} string
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func initializeCPUTimeDataPoint(dataPoint pdata.NumberDataPoint, startTime, now
attributes := dataPoint.Attributes()
// ignore cpu attribute if reporting "total" cpu usage
if cpuLabel != gopsCPUTotal {
attributes.InsertString(metadata.Labels.Cpu, cpuLabel)
attributes.InsertString(metadata.Attributes.Cpu, cpuLabel)
}
attributes.InsertString(metadata.Labels.State, stateLabel)
attributes.InsertString(metadata.Attributes.State, stateLabel)

dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
const cpuStatesLen = 8

func appendCPUTimeStateDataPoints(ddps pdata.NumberDataPointSlice, startTime, now pdata.Timestamp, cpuTime cpu.TimesStat) {
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.User, cpuTime.User)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.System, cpuTime.System)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Idle, cpuTime.Idle)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Interrupt, cpuTime.Irq)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Nice, cpuTime.Nice)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Softirq, cpuTime.Softirq)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Steal, cpuTime.Steal)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Wait, cpuTime.Iowait)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.User, cpuTime.User)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.System, cpuTime.System)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Idle, cpuTime.Idle)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Interrupt, cpuTime.Irq)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Nice, cpuTime.Nice)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Softirq, cpuTime.Softirq)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Steal, cpuTime.Steal)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Wait, cpuTime.Iowait)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
const cpuStatesLen = 4

func appendCPUTimeStateDataPoints(ddps pdata.NumberDataPointSlice, startTime, now pdata.Timestamp, cpuTime cpu.TimesStat) {
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.User, cpuTime.User)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.System, cpuTime.System)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Idle, cpuTime.Idle)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.LabelState.Interrupt, cpuTime.Irq)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.User, cpuTime.User)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.System, cpuTime.System)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Idle, cpuTime.Idle)
initializeCPUTimeDataPoint(ddps.AppendEmpty(), startTime, now, cpuTime.CPU, metadata.AttributeState.Interrupt, cpuTime.Irq)
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ func assertCPUMetricValid(t *testing.T, metric pdata.Metric, descriptor pdata.Me
internal.AssertSumMetricStartTimeEquals(t, metric, startTime)
}
assert.GreaterOrEqual(t, metric.Sum().DataPoints().Len(), 4*runtime.NumCPU())
internal.AssertSumMetricHasAttribute(t, metric, 0, metadata.Labels.Cpu)
internal.AssertSumMetricHasAttributeValue(t, metric, 0, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.User))
internal.AssertSumMetricHasAttributeValue(t, metric, 1, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.System))
internal.AssertSumMetricHasAttributeValue(t, metric, 2, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Idle))
internal.AssertSumMetricHasAttributeValue(t, metric, 3, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Interrupt))
internal.AssertSumMetricHasAttribute(t, metric, 0, metadata.Attributes.Cpu)
internal.AssertSumMetricHasAttributeValue(t, metric, 0, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.User))
internal.AssertSumMetricHasAttributeValue(t, metric, 1, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.System))
internal.AssertSumMetricHasAttributeValue(t, metric, 2, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Idle))
internal.AssertSumMetricHasAttributeValue(t, metric, 3, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Interrupt))
}

func assertCPUMetricHasLinuxSpecificStateLabels(t *testing.T, metric pdata.Metric) {
internal.AssertSumMetricHasAttributeValue(t, metric, 4, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Nice))
internal.AssertSumMetricHasAttributeValue(t, metric, 5, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Softirq))
internal.AssertSumMetricHasAttributeValue(t, metric, 6, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Steal))
internal.AssertSumMetricHasAttributeValue(t, metric, 7, metadata.Labels.State, pdata.NewAttributeValueString(metadata.LabelState.Wait))
internal.AssertSumMetricHasAttributeValue(t, metric, 4, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Nice))
internal.AssertSumMetricHasAttributeValue(t, metric, 5, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Softirq))
internal.AssertSumMetricHasAttributeValue(t, metric, 6, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Steal))
internal.AssertSumMetricHasAttributeValue(t, metric, 7, metadata.Attributes.State, pdata.NewAttributeValueString(metadata.AttributeState.Wait))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cpu

labels:
attributes:
cpu:
description: CPU number starting at 0.

Expand All @@ -16,4 +16,4 @@ metrics:
type: sum
aggregation: cumulative
monotonic: true
labels: [state]
attributes: [state]
Loading