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

[Metric builder] Replace enum attributes values with typed constants #9683

Merged
merged 2 commits into from
May 4, 2022
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
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)
- `transformprocessor`: Add new `truncation` function to allow truncating string values in maps such as `attributes` or `resource.attributes` (#9546)
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