Skip to content

Commit

Permalink
[Metric builder] Replace enum attributes values with typed constants (#…
Browse files Browse the repository at this point in the history
…9683)

`Record...DataPoint` functions should take one of predefined attribute values instead of any string if the attribute has enum field defined in metadata.yaml. This significantly reduces chance of passing wrong attribute values and helps to ensure accuracy of the automatically generated documentation.
  • Loading branch information
dmitryax authored May 4, 2022
1 parent e7fb865 commit 99d2204
Show file tree
Hide file tree
Showing 74 changed files with 4,613 additions and 2,336 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### 💡 Enhancements 💡

- `cmd/mdatagen`: Replace enum attributes values with typed constants (#9683)
- `k8sclusterreceiver`: Validate that k8s API supports a resource before setting up a watcher for it (#9523)
- `internal/stanza`: Add support for `remove` operator (#9524)
- `k8sattributesprocessor`: Support regex capture groups in tag_name (#9525)
Expand Down
3 changes: 3 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func generateMetrics(ymlDir string, thisDir string, md metadata, useExpGen bool)
"publicVar": func(s string) (string, error) {
return formatIdentifier(s, true)
},
"attributeInfo": func(an attributeName) attribute {
return md.Attributes[an]
},
"parseImportsRequired": func(metrics map[metricName]metric) bool {
for _, m := range metrics {
if m.Data().HasMetricInputType() {
Expand Down
60 changes: 42 additions & 18 deletions cmd/mdatagen/metrics_v2.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ func DefaultMetricsSettings() MetricsSettings {
}
}

{{ range $name, $info := .Attributes }}
{{- if $info.Enum -}}
// Attribute{{ $name.Render }} specifies the a value {{ $name }} attribute.
type Attribute{{ $name.Render }} int

const (
_ Attribute{{ $name.Render }} = iota
{{- range $info.Enum }}
Attribute{{ $name.Render }}{{ . | publicVar }}
{{- end }}
)

// String returns the string representation of the Attribute{{ $name.Render }}.
func (av Attribute{{ $name.Render }}) String() string {
switch av {
{{- range $info.Enum }}
case Attribute{{ $name.Render }}{{ . | publicVar }}:
return "{{ . }}"
{{- end }}
}
return ""
}

// MapAttribute{{ $name.Render }} is a helper map of string to Attribute{{ $name.Render }} attribute value.
var MapAttribute{{ $name.Render }} = map[string]Attribute{{ $name.Render }}{
{{- range $info.Enum }}
"{{ . }}": Attribute{{ $name.Render }}{{ . | publicVar }},
{{- end }}
}

{{ end }}
{{- end }}

{{ range $name, $metric := .Metrics -}}
type metric{{ $name.Render }} struct {
data pmetric.Metric // data buffer for generated metric.
Expand Down Expand Up @@ -206,7 +239,9 @@ func (mb *MetricsBuilder) Record{{ $name.Render }}DataPoint(ts pcommon.Timestamp
{{- else }}
{{- if $metric.Data.HasMetricValueType }}, val {{ $metric.Data.MetricValueType.BasicType }}{{- end }}
{{- end -}}
{{- range $metric.Attributes -}} , {{ .RenderUnexported }}AttributeValue string{{ end }})
{{- range $metric.Attributes -}}
, {{ .RenderUnexported }}AttributeValue {{ if (attributeInfo .).Enum }}Attribute{{ .Render }}{{ else }}string{{ end }}
{{- end }})
{{- if $metric.Data.HasMetricInputType }} error{{ end }} {
{{- if $metric.Data.HasMetricInputType }}
{{- if $metric.Data.HasMetricValueType }}
Expand All @@ -225,15 +260,19 @@ func (mb *MetricsBuilder) Record{{ $name.Render }}DataPoint(ts pcommon.Timestamp
} else {
mb.metric{{ $name.Render }}.recordDataPoint(mb.startTime, ts
{{- if $metric.Data.HasMetricValueType }}, i {{ end }}
{{- range $metric.Attributes -}} , {{ .RenderUnexported }}AttributeValue{{ end }})
{{- range $metric.Attributes -}}
, {{ .RenderUnexported }}AttributeValue{{ if (attributeInfo .).Enum }}.String(){{ end }}
{{- end }})
}
{{- end }}
return nil
{{- end }}
{{- else }}
mb.metric{{ $name.Render }}.recordDataPoint(mb.startTime, ts
{{- if $metric.Data.HasMetricValueType }}, val {{ end }}
{{- range $metric.Attributes -}} , {{ .RenderUnexported }}AttributeValue{{ end }})
{{- range $metric.Attributes -}}
, {{ .RenderUnexported }}AttributeValue{{ if (attributeInfo .).Enum }}.String(){{ end }}
{{- end }})
{{- end }}
}
{{ end }}
Expand Down Expand Up @@ -265,18 +304,3 @@ var Attributes = struct {

// A is an alias for Attributes.
var A = Attributes

{{ range $name, $info := .Attributes -}}
{{ if $info.Enum -}}
// Attribute{{ $name.Render }} are the possible values that the attribute "{{ $name }}" can have.
var Attribute{{ $name.Render }} = struct {
{{- range $info.Enum }}
{{ . | publicVar }} string
{{- end }}
}{
{{- range $info.Enum }}
"{{ . }}",
{{- end }}
}
{{ end }}
{{ end }}
Loading

0 comments on commit 99d2204

Please sign in to comment.