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

[chore] [cmd/mdatagen] Rename structs with Settings suffix to Config #21522

Merged
merged 1 commit into from
May 7, 2023
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
140 changes: 70 additions & 70 deletions cmd/mdatagen/internal/metadata/generated_metrics.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions cmd/mdatagen/internal/metadata/generated_metrics_test.go

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

78 changes: 39 additions & 39 deletions cmd/mdatagen/templates/metrics.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
{{- end }}
)

// MetricSettings provides common settings for a particular metric.
type MetricSettings struct {
// MetricConfig provides common config for a particular metric.
type MetricConfig struct {
Enabled bool `mapstructure:"enabled"`

enabledSetByUser bool
}

func (ms *MetricSettings) Unmarshal(parser *confmap.Conf) error {
func (ms *MetricConfig) Unmarshal(parser *confmap.Conf) error {
if parser == nil {
return nil
}
Expand All @@ -38,40 +38,40 @@ func (ms *MetricSettings) Unmarshal(parser *confmap.Conf) error {
return nil
}

// MetricsSettings provides settings for {{ .Type }} metrics.
type MetricsSettings struct {
// MetricsConfig provides config for {{ .Type }} metrics.
type MetricsConfig struct {
{{- range $name, $metric := .Metrics }}
{{ $name.Render }} MetricSettings `mapstructure:"{{ $name }}"`
{{ $name.Render }} MetricConfig `mapstructure:"{{ $name }}"`
{{- end }}
}

func DefaultMetricsSettings() MetricsSettings {
return MetricsSettings{
func DefaultMetricsConfig() MetricsConfig {
return MetricsConfig{
{{- range $name, $metric := .Metrics }}
{{ $name.Render }}: MetricSettings{
{{ $name.Render }}: MetricConfig{
Enabled: {{ $metric.Enabled }},
},
{{- end }}
}
}

{{ if .ResourceAttributes -}}
// ResourceAttributeSettings provides common settings for a particular resource attribute.
type ResourceAttributeSettings struct {
// ResourceAttributeConfig provides common config for a particular resource attribute.
type ResourceAttributeConfig struct {
Enabled bool `mapstructure:"enabled"`
}

// ResourceAttributesSettings provides settings for {{ .Type }} resource attributes.
type ResourceAttributesSettings struct {
// ResourceAttributesConfig provides config for {{ .Type }} resource attributes.
type ResourceAttributesConfig struct {
{{- range $name, $attr := .ResourceAttributes }}
{{ $name.Render }} ResourceAttributeSettings `mapstructure:"{{ $name }}"`
{{ $name.Render }} ResourceAttributeConfig `mapstructure:"{{ $name }}"`
{{- end }}
}

func DefaultResourceAttributesSettings() ResourceAttributesSettings {
return ResourceAttributesSettings{
func DefaultResourceAttributesConfig() ResourceAttributesConfig {
return ResourceAttributesConfig{
{{- range $name, $attr := .ResourceAttributes }}
{{ $name.Render }}: ResourceAttributeSettings {
{{ $name.Render }}: ResourceAttributeConfig {
Enabled: {{ $attr.Enabled }},
},
{{- end }}
Expand Down Expand Up @@ -114,8 +114,8 @@ var MapAttribute{{ $name.Render }} = map[string]Attribute{{ $name.Render }}{

{{ range $name, $metric := .Metrics -}}
type metric{{ $name.Render }} struct {
data pmetric.Metric // data buffer for generated metric.
settings MetricSettings // metric settings provided by user.
data pmetric.Metric // data buffer for generated metric.
config MetricConfig // metric config provided by user.
capacity int // max observed number of data points added to the metric.
}

Expand All @@ -138,7 +138,7 @@ func (m *metric{{ $name.Render }}) init() {

func (m *metric{{ $name.Render }}) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val {{ $metric.Data.MetricValueType.BasicType }}
{{- range $metric.Attributes -}}, {{ .RenderUnexported }}AttributeValue {{ (attributeInfo .).Type.Primitive }}{{ end }}) {
if !m.settings.Enabled {
if !m.config.Enabled {
return
}
dp := m.data.{{ $metric.Data.Type }}().DataPoints().AppendEmpty()
Expand Down Expand Up @@ -167,16 +167,16 @@ func (m *metric{{ $name.Render }}) updateCapacity() {

// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
func (m *metric{{ $name.Render }}) emit(metrics pmetric.MetricSlice) {
if m.settings.Enabled && m.data.{{ $metric.Data.Type }}().DataPoints().Len() > 0 {
if m.config.Enabled && m.data.{{ $metric.Data.Type }}().DataPoints().Len() > 0 {
m.updateCapacity()
m.data.MoveTo(metrics.AppendEmpty())
m.init()
}
}

func newMetric{{ $name.Render }}(settings MetricSettings) metric{{ $name.Render }} {
m := metric{{ $name.Render }}{settings: settings}
if settings.Enabled {
func newMetric{{ $name.Render }}(cfg MetricConfig) metric{{ $name.Render }} {
m := metric{{ $name.Render }}{config: cfg}
if cfg.Enabled {
m.data = pmetric.NewMetric()
m.init()
}
Expand All @@ -187,22 +187,22 @@ func newMetric{{ $name.Render }}(settings MetricSettings) metric{{ $name.Render

// MetricsBuilderConfig is a structural subset of an otherwise 1-1 copy of metadata.yaml
type MetricsBuilderConfig struct {
Metrics MetricsSettings `mapstructure:"metrics"`
Metrics MetricsConfig `mapstructure:"metrics"`
{{- if .ResourceAttributes }}
ResourceAttributes ResourceAttributesSettings `mapstructure:"resource_attributes"`
ResourceAttributes ResourceAttributesConfig `mapstructure:"resource_attributes"`
{{- end }}
}

// MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
// required to produce metric representation defined in metadata and user settings.
// required to produce metric representation defined in metadata and user config.
type MetricsBuilder struct {
startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
metricsCapacity int // maximum observed number of metrics per resource.
resourceCapacity int // maximum observed number of resource attributes.
metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
buildInfo component.BuildInfo // contains version information
{{- if .ResourceAttributes }}
resourceAttributesSettings ResourceAttributesSettings
resourceAttributesConfig ResourceAttributesConfig
{{- end }}
{{- range $name, $metric := .Metrics }}
metric{{ $name.Render }} metric{{ $name.Render }}
Expand All @@ -221,9 +221,9 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption {

func DefaultMetricsBuilderConfig() MetricsBuilderConfig {
return MetricsBuilderConfig {
Metrics: DefaultMetricsSettings(),
Metrics: DefaultMetricsConfig(),
{{- if .ResourceAttributes }}
ResourceAttributes: DefaultResourceAttributesSettings(),
ResourceAttributes: DefaultResourceAttributesConfig(),
{{- end }}
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSetting
metricsBuffer: pmetric.NewMetrics(),
buildInfo: settings.BuildInfo,
{{- if .ResourceAttributes }}
resourceAttributesSettings: mbc.ResourceAttributes,
resourceAttributesConfig: mbc.ResourceAttributes,
{{- end }}
{{- range $name, $metric := .Metrics }}
metric{{ $name.Render }}: newMetric{{ $name.Render }}(mbc.Metrics.{{ $name.Render }}),
Expand All @@ -274,21 +274,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
}

// ResourceMetricsOption applies changes to provided resource metrics.
type ResourceMetricsOption func({{ if .ResourceAttributes }}ResourceAttributesSettings, {{ end }}pmetric.ResourceMetrics)
type ResourceMetricsOption func({{ if .ResourceAttributes }}ResourceAttributesConfig, {{ end }}pmetric.ResourceMetrics)

{{- range $name, $attr := .ResourceAttributes }}
{{- range $attr.Enum }}
// With{{ $name.Render }}{{ . | publicVar }} sets "{{ $name }}={{ . }}" attribute for current resource.
func With{{ $name.Render }}{{ . | publicVar }}(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) {
if ras.{{ $name.Render }}.Enabled {
func With{{ $name.Render }}{{ . | publicVar }}(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) {
if rac.{{ $name.Render }}.Enabled {
rm.Resource().Attributes().PutStr("{{ attributeName $name}}", "{{ . }}")
}
}
{{- else }}
// With{{ $name.Render }} sets provided value as "{{ $name }}" attribute for current resource.
func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOption {
return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) {
if ras.{{ $name.Render }}.Enabled {
return func(rac ResourceAttributesConfig, rm pmetric.ResourceMetrics) {
if rac.{{ $name.Render }}.Enabled {
{{- if eq $attr.Type.Primitive "[]byte" }}
rm.Resource().Attributes().PutEmptyBytes("{{ attributeName $name}}").FromRaw(val)
{{- else if eq $attr.Type.Primitive "[]any" }}
Expand All @@ -307,7 +307,7 @@ func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOptio
// WithStartTimeOverride overrides start time for all the resource metrics data points.
// This option should be only used if different start time has to be set on metrics coming from different resources.
func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption {
return func({{ if .ResourceAttributes }}_ ResourceAttributesSettings, {{ end }}rm pmetric.ResourceMetrics) {
return func({{ if .ResourceAttributes }}_ ResourceAttributesConfig, {{ end }}rm pmetric.ResourceMetrics) {
var dps pmetric.NumberDataPointSlice
metrics := rm.ScopeMetrics().At(0).Metrics()
for i := 0; i < metrics.Len(); i++ {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {
{{- end }}

for _, op := range rmo {
op({{ if .ResourceAttributes }}mb.resourceAttributesSettings, {{ end }}rm)
op({{ if .ResourceAttributes }}mb.resourceAttributesConfig, {{ end }}rm)
}
if ils.Metrics().Len() > 0 {
mb.updateCapacity(rm)
Expand All @@ -354,7 +354,7 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {

// Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for
// recording another set of metrics. This function will be responsible for applying all the transformations required to
// produce metric representation defined in metadata and user settings, e.g. delta or cumulative.
// produce metric representation defined in metadata and user config, e.g. delta or cumulative.
func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics {
mb.EmitForResource(rmo...)
metrics := mb.metricsBuffer
Expand Down
4 changes: 2 additions & 2 deletions cmd/mdatagen/templates/metrics_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func TestMetricsBuilder(t *testing.T) {
{{- range $name, $info := .ResourceAttributes }}
attrVal, ok {{ $assignSign }} rm.Resource().Attributes().Get("{{ $name }}")
attrCount++
assert.Equal(t, mb.resourceAttributesSettings.{{ $name.Render }}.Enabled, ok)
if mb.resourceAttributesSettings.{{ $name.Render }}.Enabled {
assert.Equal(t, mb.resourceAttributesConfig.{{ $name.Render }}.Enabled, ok)
if mb.resourceAttributesConfig.{{ $name.Render }}.Enabled {
enabledAttrCount++
{{- if $info.Enum }}
assert.Equal(t, "{{ index $info.Enum 0 }}", attrVal.Str())
Expand Down
2 changes: 1 addition & 1 deletion receiver/activedirectorydsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

assert.NoError(t, component.ValidateConfig(cfg))
if diff := cmp.Diff(tt.expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricsBuilderConfig{}), cmpopts.IgnoreUnexported(metadata.MetricSettings{})); diff != "" {
if diff := cmp.Diff(tt.expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricsBuilderConfig{}), cmpopts.IgnoreUnexported(metadata.MetricConfig{})); diff != "" {
t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
}
})
Expand Down
Loading