From d86b6516d1a82f3e73f655d2a7fa8a5dbe653e01 Mon Sep 17 00:00:00 2001 From: hughesjj Date: Thu, 5 Jan 2023 19:32:23 -0800 Subject: [PATCH] Add enabled flag to resource attributes --- .../add_resource_attribute_settings.yaml | 18 ++ Makefile | 1 + cmd/mdatagen/README.md | 5 +- cmd/mdatagen/documentation.md | 9 +- .../internal/metadata/generated_metrics.go | 83 +++++++- .../metadata/generated_metrics_test.go | 33 ++- cmd/mdatagen/loader.go | 2 + cmd/mdatagen/loader_test.go | 9 + cmd/mdatagen/metadata.yaml | 7 + cmd/mdatagen/metric-metadata.yaml | 2 + cmd/mdatagen/templates/documentation.md.tmpl | 6 +- cmd/mdatagen/templates/metrics.go.tmpl | 71 ++++++- cmd/mdatagen/templates/metrics_test.go.tmpl | 29 ++- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- receiver/aerospikereceiver/documentation.md | 8 +- .../internal/metadata/generated_metrics.go | 64 +++++- .../metadata/generated_metrics_test.go | 23 ++- receiver/aerospikereceiver/metadata.yaml | 2 + receiver/apachereceiver/documentation.md | 8 +- .../internal/metadata/generated_metrics.go | 64 +++++- .../metadata/generated_metrics_test.go | 23 ++- receiver/apachereceiver/metadata.yaml | 2 + receiver/bigipreceiver/documentation.md | 18 +- .../internal/metadata/generated_metrics.go | 114 ++++++++-- .../metadata/generated_metrics_test.go | 63 ++++-- receiver/bigipreceiver/metadata.yaml | 7 + .../internal/metadata/generated_metrics.go | 87 +++++--- .../metadata/generated_metrics_test.go | 5 +- receiver/couchdbreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/couchdbreceiver/metadata.yaml | 1 + receiver/dockerstatsreceiver/documentation.md | 14 +- .../internal/metadata/generated_metrics.go | 104 ++++++++-- .../metadata/generated_metrics_test.go | 47 +++-- receiver/dockerstatsreceiver/metadata.yaml | 5 + receiver/dockerstatsreceiver/receiver_v2.go | 4 +- .../elasticsearchreceiver/documentation.md | 12 +- .../internal/metadata/generated_metrics.go | 90 ++++++-- .../metadata/generated_metrics_test.go | 39 +++- receiver/elasticsearchreceiver/metadata.yaml | 4 + .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../flinkmetricsreceiver/documentation.md | 16 +- .../internal/metadata/generated_metrics.go | 110 ++++++++-- .../metadata/generated_metrics_test.go | 55 +++-- receiver/flinkmetricsreceiver/metadata.yaml | 6 + receiver/haproxyreceiver/documentation.md | 18 +- .../internal/metadata/generated_metrics.go | 114 ++++++++-- .../metadata/generated_metrics_test.go | 63 ++++-- receiver/haproxyreceiver/metadata.yaml | 7 + .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../scraper/processscraper/documentation.md | 18 +- .../internal/metadata/generated_metrics.go | 114 ++++++++-- .../metadata/generated_metrics_test.go | 63 ++++-- .../scraper/processscraper/metadata.yaml | 7 + .../internal/metadata/generated_metrics.go | 71 +++++-- .../metadata/generated_metrics_test.go | 5 +- receiver/iisreceiver/documentation.md | 8 +- .../internal/metadata/generated_metrics.go | 64 +++++- .../metadata/generated_metrics_test.go | 23 ++- receiver/iisreceiver/metadata.yaml | 2 + .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../kubeletstatsreceiver/documentation.md | 34 +-- .../internal/kubelet/metadata_test.go | 6 +- .../internal/kubelet/volume_test.go | 3 +- .../internal/metadata/generated_metrics.go | 194 +++++++++++++++--- .../metadata/generated_metrics_test.go | 127 +++++++++--- receiver/kubeletstatsreceiver/metadata.yaml | 15 ++ .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- .../mongodbatlasreceiver/documentation.md | 22 +- .../internal/metadata/generated_metrics.go | 134 ++++++++++-- .../metadata/generated_metrics_test.go | 79 +++++-- receiver/mongodbatlasreceiver/metadata.yaml | 9 + receiver/mongodbreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/mongodbreceiver/metadata.yaml | 1 + receiver/mysqlreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/mysqlreceiver/metadata.yaml | 1 + .../internal/metadata/generated_metrics.go | 43 +++- .../metadata/generated_metrics_test.go | 5 +- receiver/nsxtreceiver/documentation.md | 12 +- .../internal/metadata/generated_metrics.go | 84 +++++++- .../metadata/generated_metrics_test.go | 39 +++- receiver/nsxtreceiver/metadata.yaml | 4 + receiver/oracledbreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/oracledbreceiver/metadata.yaml | 1 + receiver/postgresqlreceiver/documentation.md | 10 +- .../internal/metadata/generated_metrics.go | 74 ++++++- .../metadata/generated_metrics_test.go | 31 ++- receiver/postgresqlreceiver/metadata.yaml | 3 + receiver/rabbitmqreceiver/documentation.md | 10 +- .../internal/metadata/generated_metrics.go | 74 ++++++- .../metadata/generated_metrics_test.go | 31 ++- receiver/rabbitmqreceiver/metadata.yaml | 3 + receiver/redisreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/redisreceiver/metadata.yaml | 1 + receiver/riakreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/riakreceiver/metadata.yaml | 1 + receiver/saphanareceiver/documentation.md | 8 +- .../internal/metadata/generated_metrics.go | 64 +++++- .../metadata/generated_metrics_test.go | 23 ++- receiver/saphanareceiver/metadata.yaml | 2 + receiver/snowflakereceiver/metadata.yaml | 1 + receiver/sqlserverreceiver/documentation.md | 6 +- .../internal/metadata/generated_metrics.go | 54 ++++- .../metadata/generated_metrics_test.go | 15 +- receiver/sqlserverreceiver/metadata.yaml | 1 + receiver/vcenterreceiver/documentation.md | 16 +- .../internal/metadata/generated_metrics.go | 104 ++++++++-- .../metadata/generated_metrics_test.go | 55 +++-- receiver/vcenterreceiver/metadata.yaml | 6 + receiver/zookeeperreceiver/documentation.md | 8 +- .../internal/metadata/generated_metrics.go | 64 +++++- .../metadata/generated_metrics_test.go | 23 ++- receiver/zookeeperreceiver/metadata.yaml | 2 + 143 files changed, 3545 insertions(+), 831 deletions(-) create mode 100644 .chloggen/add_resource_attribute_settings.yaml diff --git a/.chloggen/add_resource_attribute_settings.yaml b/.chloggen/add_resource_attribute_settings.yaml new file mode 100644 index 000000000000..a3a3862d8a3c --- /dev/null +++ b/.chloggen/add_resource_attribute_settings.yaml @@ -0,0 +1,18 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: mdatagen + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Adds ability to enable/disable resource attributes in output and sets all existing resource attributes to enabled by default. + +# One or more tracking issues related to the change +issues: [16373] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + We may change NewMetricBuilder in the future from taking in ResourceMetrics to taking in a MetricsBuildingConfiguration, + but for now changing the settings requires a call to WithResourceAttributeSettings. diff --git a/Makefile b/Makefile index 980ac5c39794..0e1a81e3b699 100644 --- a/Makefile +++ b/Makefile @@ -249,6 +249,7 @@ generate: mdatagen-test: cd cmd/mdatagen && $(GOCMD) install . cd cmd/mdatagen && $(GOCMD) generate ./... + cd cmd/mdatagen && $(GOCMD) test ./... .PHONY: chlog-install chlog-install: diff --git a/cmd/mdatagen/README.md b/cmd/mdatagen/README.md index 8a7c446ef86c..4a9ba37ec3ed 100644 --- a/cmd/mdatagen/README.md +++ b/cmd/mdatagen/README.md @@ -10,7 +10,7 @@ See [metric-metadata.yaml](metric-metadata.yaml) for file format documentation. If adding a new receiver a `doc.go` file should also be added to trigger the generation. See below for details. -## Build +## Generating `make generate` triggers the following actions: @@ -33,4 +33,5 @@ In order to introduce support of a new functionality in metadata.yaml: 2. Add usage of the new functionality in (metadata.yaml)[./metadata.yaml]. 3. Run `make mdatagen-test`. 4. Make sure all tests are passing including (generated tests)[./internal/metadata/generated_metrics_test.go]. -5. Run `make generate`. \ No newline at end of file +5. Run `make generate`. + diff --git a/cmd/mdatagen/documentation.md b/cmd/mdatagen/documentation.md index 19ab22c906ed..f3af494d5b3b 100644 --- a/cmd/mdatagen/documentation.md +++ b/cmd/mdatagen/documentation.md @@ -67,7 +67,8 @@ metrics: ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | -| string.resource.attr | Resource attribute with any string value. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| optional.resource.attr | explicitly disabled ResourceAttribute | Any Str | false | +| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true | +| string.resource.attr | Resource attribute with any string value. | Any Str | true | diff --git a/cmd/mdatagen/internal/metadata/generated_metrics.go b/cmd/mdatagen/internal/metadata/generated_metrics.go index b24877a64c2d..28c5a1edf1c5 100644 --- a/cmd/mdatagen/internal/metadata/generated_metrics.go +++ b/cmd/mdatagen/internal/metadata/generated_metrics.go @@ -53,6 +53,46 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for testreceiver metrics. +type ResourceAttributesSettings struct { + OptionalResourceAttr ResourceAttributeSettings `mapstructure:"optional.resource.attr"` + StringEnumResourceAttr ResourceAttributeSettings `mapstructure:"string.enum.resource.attr"` + StringResourceAttr ResourceAttributeSettings `mapstructure:"string.resource.attr"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + OptionalResourceAttr: ResourceAttributeSettings{ + Enabled: false, + }, + StringEnumResourceAttr: ResourceAttributeSettings{ + Enabled: true, + }, + StringResourceAttr: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeEnumAttr specifies the a value enum_attr attribute. type AttributeEnumAttr int @@ -249,6 +289,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricDefaultMetric metricDefaultMetric metricDefaultMetricToBeRemoved metricDefaultMetricToBeRemoved metricOptionalMetric metricOptionalMetric @@ -264,6 +305,13 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { if !ms.DefaultMetric.enabledSetByUser { settings.Logger.Warn("[WARNING] Please set `enabled` field explicitly for `default.metric`: This metric will be disabled by default soon.") @@ -278,6 +326,7 @@ func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, opt startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricDefaultMetric: newMetricDefaultMetric(ms.DefaultMetric), metricDefaultMetricToBeRemoved: newMetricDefaultMetricToBeRemoved(ms.DefaultMetricToBeRemoved), metricOptionalMetric: newMetricOptionalMetric(ms.OptionalMetric), @@ -299,29 +348,44 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) + +// WithOptionalResourceAttr sets provided value as "optional.resource.attr" attribute for current resource. +func WithOptionalResourceAttr(val string) ResourceMetricsOption { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.OptionalResourceAttr.Enabled { + rm.Resource().Attributes().PutStr("optional.resource.attr", val) + } + } +} // WithStringEnumResourceAttrOne sets "string.enum.resource.attr=one" attribute for current resource. -func WithStringEnumResourceAttrOne(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("string.enum.resource.attr", "one") +func WithStringEnumResourceAttrOne(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.StringEnumResourceAttr.Enabled { + rm.Resource().Attributes().PutStr("string.enum.resource.attr", "one") + } } // WithStringEnumResourceAttrTwo sets "string.enum.resource.attr=two" attribute for current resource. -func WithStringEnumResourceAttrTwo(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("string.enum.resource.attr", "two") +func WithStringEnumResourceAttrTwo(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.StringEnumResourceAttr.Enabled { + rm.Resource().Attributes().PutStr("string.enum.resource.attr", "two") + } } // WithStringResourceAttr sets provided value as "string.resource.attr" attribute for current resource. func WithStringResourceAttr(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("string.resource.attr", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.StringResourceAttr.Enabled { + rm.Resource().Attributes().PutStr("string.resource.attr", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -354,8 +418,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricDefaultMetric.emit(ils.Metrics()) mb.metricDefaultMetricToBeRemoved.emit(ils.Metrics()) mb.metricOptionalMetric.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/cmd/mdatagen/internal/metadata/generated_metrics_test.go b/cmd/mdatagen/internal/metadata/generated_metrics_test.go index 3063befd535e..7dad4c59f23d 100644 --- a/cmd/mdatagen/internal/metadata/generated_metrics_test.go +++ b/cmd/mdatagen/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -81,7 +80,7 @@ func TestMetricsBuilder(t *testing.T) { allMetricsCount++ mb.RecordOptionalMetricDataPoint(ts, 1, "attr-val", true) - metrics := mb.Emit(WithStringEnumResourceAttrOne, WithStringResourceAttr("attr-val")) + metrics := mb.Emit(WithOptionalResourceAttr("attr-val"), WithStringEnumResourceAttrOne, WithStringResourceAttr("attr-val")) if test.metricsSet == testMetricsSetNo { assert.Equal(t, 0, metrics.ResourceMetrics().Len()) @@ -91,15 +90,33 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.OptionalResourceAttr.Enabled + attrVal, ok := rm.Resource().Attributes().Get("optional.resource.attr") attrCount++ - attrVal, ok := rm.Resource().Attributes().Get("string.enum.resource.attr") - assert.True(t, ok) - assert.Equal(t, "one", attrVal.Str()) + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.StringEnumResourceAttr.Enabled + attrVal, ok = rm.Resource().Attributes().Get("string.enum.resource.attr") attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.Equal(t, "one", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.StringResourceAttr.Enabled attrVal, ok = rm.Resource().Attributes().Get("string.resource.attr") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 3) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/cmd/mdatagen/loader.go b/cmd/mdatagen/loader.go index e3fbc4122f64..083e5f0dcabc 100644 --- a/cmd/mdatagen/loader.go +++ b/cmd/mdatagen/loader.go @@ -186,6 +186,8 @@ type attribute struct { Description string `mapstructure:"description"` // NameOverride can be used to override the attribute name. NameOverride string `mapstructure:"name_override"` + // Enabled defines whether the attribute is enabled by default. + Enabled bool `yaml:"enabled" validate:"required"` // Enum can optionally describe the set of values to which the attribute can belong. Enum []string // Type is an attribute type. diff --git a/cmd/mdatagen/loader_test.go b/cmd/mdatagen/loader_test.go index 689226f3e07a..e227a3ebb007 100644 --- a/cmd/mdatagen/loader_test.go +++ b/cmd/mdatagen/loader_test.go @@ -36,17 +36,26 @@ func Test_loadMetadata(t *testing.T) { ResourceAttributes: map[attributeName]attribute{ "string.resource.attr": { Description: "Resource attribute with any string value.", + Enabled: true, Type: ValueType{ ValueType: pcommon.ValueTypeStr, }, }, "string.enum.resource.attr": { Description: "Resource attribute with a known set of string values.", + Enabled: true, Enum: []string{"one", "two"}, Type: ValueType{ ValueType: pcommon.ValueTypeStr, }, }, + "optional.resource.attr": { + Description: "explicitly disabled ResourceAttribute", + Enabled: false, + Type: ValueType{ + ValueType: pcommon.ValueTypeStr, + }, + }, }, Attributes: map[attributeName]attribute{ "enum_attr": { diff --git a/cmd/mdatagen/metadata.yaml b/cmd/mdatagen/metadata.yaml index 6289e47ca1f9..570c192536f9 100644 --- a/cmd/mdatagen/metadata.yaml +++ b/cmd/mdatagen/metadata.yaml @@ -8,11 +8,18 @@ resource_attributes: string.resource.attr: description: Resource attribute with any string value. type: string + enabled: true string.enum.resource.attr: description: Resource attribute with a known set of string values. type: string enum: [one, two] + enabled: true + + optional.resource.attr: + description: explicitly disabled ResourceAttribute + type: string + enabled: false attributes: string_attr: diff --git a/cmd/mdatagen/metric-metadata.yaml b/cmd/mdatagen/metric-metadata.yaml index 02124cbf245a..f3a55b16b7a8 100644 --- a/cmd/mdatagen/metric-metadata.yaml +++ b/cmd/mdatagen/metric-metadata.yaml @@ -10,6 +10,8 @@ resource_attributes: : # Required: description of the attribute. description: + # Optional: Whether the resource attribute is emitted by default. + enabled: bool # Required: attribute value type. type: diff --git a/cmd/mdatagen/templates/documentation.md.tmpl b/cmd/mdatagen/templates/documentation.md.tmpl index bdbf0b1436dd..7a1d9f6ffb6a 100644 --- a/cmd/mdatagen/templates/documentation.md.tmpl +++ b/cmd/mdatagen/templates/documentation.md.tmpl @@ -83,11 +83,11 @@ metrics: ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | {{- range $attributeName, $attribute := .ResourceAttributes }} | {{ $attributeName }} | {{ $attribute.Description }} | -{{- if $attribute.Enum }} {{ $attribute.Type }}: ``{{ stringsJoin $attribute.Enum "``, ``" }}``{{ else }} Any {{ $attribute.Type }}{{ end }} | +{{- if $attribute.Enum }} {{ $attribute.Type }}: ``{{ stringsJoin $attribute.Enum "``, ``" }}``{{ else }} Any {{ $attribute.Type }}{{ end }} | {{ $attribute.Enabled }} | {{- end }} {{- end }} diff --git a/cmd/mdatagen/templates/metrics.go.tmpl b/cmd/mdatagen/templates/metrics.go.tmpl index 26e5602a7053..7f3f8600da7a 100644 --- a/cmd/mdatagen/templates/metrics.go.tmpl +++ b/cmd/mdatagen/templates/metrics.go.tmpl @@ -55,6 +55,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for {{ .Name }} metrics. +type ResourceAttributesSettings struct { + {{- range $name, $attr := .ResourceAttributes }} + {{ $name.Render }} ResourceAttributeSettings `mapstructure:"{{ $name }}"` + {{- end }} +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + {{- range $name, $attr := .ResourceAttributes }} + {{ $name.Render }}: ResourceAttributeSettings { + Enabled: {{ $attr.Enabled }}, + }, + {{- end }} + } +} + {{ range $name, $info := .Attributes }} {{- if $info.Enum -}} // Attribute{{ $name.Render }} specifies the a value {{ $name }} attribute. @@ -165,6 +201,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings {{- range $name, $metric := .Metrics }} metric{{ $name.Render }} metric{{ $name.Render }} {{- end }} @@ -179,6 +216,12 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { mb.startTime = startTime } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { {{- range $name, $metric := .Metrics }} @@ -202,6 +245,7 @@ func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, opt startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), {{- range $name, $metric := .Metrics }} metric{{ $name.Render }}: newMetric{{ $name.Render }}(ms.{{ $name.Render }}), {{- end }} @@ -223,23 +267,27 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) {{- range $name, $attr := .ResourceAttributes }} {{- range $attr.Enum }} // With{{ $name.Render }}{{ . | publicVar }} sets "{{ $name }}={{ . }}" attribute for current resource. -func With{{ $name.Render }}{{ . | publicVar }}(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("{{ attributeName $name}}", "{{ . }}") +func With{{ $name.Render }}{{ . | publicVar }}(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.{{ $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(rm pmetric.ResourceMetrics) { - {{- if eq $attr.Type.Primitive "[]byte" }} - rm.Resource().Attributes().PutEmptyBytes("{{ attributeName $name}}").FromRaw(val) - {{- else }} - rm.Resource().Attributes().Put{{ $attr.Type }}("{{ attributeName $name}}", val) - {{- end }} + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.{{ $name.Render }}.Enabled { + {{- if eq $attr.Type.Primitive "[]byte" }} + rm.Resource().Attributes().PutEmptyBytes("{{ attributeName $name}}").FromRaw(val) + {{- else }} + rm.Resource().Attributes().Put{{ $attr.Type }}("{{ attributeName $name}}", val) + {{- end }} + } } } {{- end }} @@ -248,7 +296,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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -283,8 +331,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { {{- range $name, $metric := .Metrics }} mb.metric{{- $name.Render }}.emit(ils.Metrics()) {{- end }} + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/cmd/mdatagen/templates/metrics_test.go.tmpl b/cmd/mdatagen/templates/metrics_test.go.tmpl index 3464db2f88bd..25f1f4445c01 100644 --- a/cmd/mdatagen/templates/metrics_test.go.tmpl +++ b/cmd/mdatagen/templates/metrics_test.go.tmpl @@ -1,5 +1,5 @@ // Code generated by mdatagen. DO NOT EDIT. - +{{- $sep := "" }}{{- $assignSign := ":=" }} package {{ .Package }} import ( @@ -88,7 +88,7 @@ func TestMetricsBuilder(t *testing.T) { {{- end }} metrics := mb.Emit( - {{- $sep := "" }} + {{- $sep = "" }} {{- range $name, $info := .ResourceAttributes -}} {{- $sep }}With{{ $name.Render }} {{- if $info.Enum }}{{ index $info.Enum 0 | publicVar }}{{ else }}({{- $info.Type.TestValue }}){{ end }} @@ -103,20 +103,27 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) - {{- $assignSign := ":=" }} + {{- $assignSign = ":=" }} attrCount := 0 + enabledAttrCount := 0 {{- range $name, $info := .ResourceAttributes }} - attrCount++ + enabled {{ $assignSign }} mb.resourceAttributesSettings.{{ $name.Render }}.Enabled attrVal, ok {{ $assignSign }} rm.Resource().Attributes().Get("{{ $name }}") - assert.True(t, ok) - {{- if $info.Enum }} - assert.Equal(t, "{{ index $info.Enum 0 }}", attrVal.Str()) - {{- else }} - assert.EqualValues(t, {{ $info.Type.TestValue }}, attrVal.{{ $info.Type }}()) - {{- end }} + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + {{- if $info.Enum }} + assert.Equal(t, "{{ index $info.Enum 0 }}", attrVal.Str()) + {{- else }} + assert.EqualValues(t, {{ $info.Type.TestValue }}, attrVal.{{ $info.Type }}()) + {{- end }} + } + {{- $assignSign = "=" }} {{- end }} - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, {{ len .ResourceAttributes }}) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics.go b/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics.go index faf3fc489224..dc22752ecdd1 100644 --- a/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics.go +++ b/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics.go @@ -112,6 +112,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for activedirectorydsreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeBindType specifies the a value bind_type attribute. type AttributeBindType int @@ -1242,6 +1269,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricActiveDirectoryDsBindRate metricActiveDirectoryDsBindRate metricActiveDirectoryDsLdapBindLastSuccessfulTime metricActiveDirectoryDsLdapBindLastSuccessfulTime metricActiveDirectoryDsLdapBindRate metricActiveDirectoryDsLdapBindRate @@ -1272,11 +1300,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricActiveDirectoryDsBindRate: newMetricActiveDirectoryDsBindRate(ms.ActiveDirectoryDsBindRate), metricActiveDirectoryDsLdapBindLastSuccessfulTime: newMetricActiveDirectoryDsLdapBindLastSuccessfulTime(ms.ActiveDirectoryDsLdapBindLastSuccessfulTime), metricActiveDirectoryDsLdapBindRate: newMetricActiveDirectoryDsLdapBindRate(ms.ActiveDirectoryDsLdapBindRate), @@ -1313,12 +1349,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1365,8 +1401,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricActiveDirectoryDsSecurityDescriptorPropagationsEventQueued.emit(ils.Metrics()) mb.metricActiveDirectoryDsSuboperationRate.emit(ils.Metrics()) mb.metricActiveDirectoryDsThreadCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics_test.go b/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics_test.go index aeebbf7f203d..a83a92913617 100644 --- a/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/activedirectorydsreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -140,7 +139,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/aerospikereceiver/documentation.md b/receiver/aerospikereceiver/documentation.md index 0481c39cfb98..398fe6935ded 100644 --- a/receiver/aerospikereceiver/documentation.md +++ b/receiver/aerospikereceiver/documentation.md @@ -193,7 +193,7 @@ Number of queries which ran more than query untracked_time (default 1 sec), Aero ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| aerospike.namespace | Name of the Aerospike namespace | Any Str | -| aerospike.node.name | Name of the Aerospike node collected from | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| aerospike.namespace | Name of the Aerospike namespace | Any Str | true | +| aerospike.node.name | Name of the Aerospike node collected from | Any Str | true | diff --git a/receiver/aerospikereceiver/internal/metadata/generated_metrics.go b/receiver/aerospikereceiver/internal/metadata/generated_metrics.go index 6735973920d1..a544c6c999fd 100644 --- a/receiver/aerospikereceiver/internal/metadata/generated_metrics.go +++ b/receiver/aerospikereceiver/internal/metadata/generated_metrics.go @@ -98,6 +98,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for aerospikereceiver metrics. +type ResourceAttributesSettings struct { + AerospikeNamespace ResourceAttributeSettings `mapstructure:"aerospike.namespace"` + AerospikeNodeName ResourceAttributeSettings `mapstructure:"aerospike.node.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + AerospikeNamespace: ResourceAttributeSettings{ + Enabled: true, + }, + AerospikeNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeConnectionOp specifies the a value connection_op attribute. type AttributeConnectionOp int @@ -1163,6 +1199,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricAerospikeNamespaceDiskAvailable metricAerospikeNamespaceDiskAvailable metricAerospikeNamespaceGeojsonRegionQueryCells metricAerospikeNamespaceGeojsonRegionQueryCells metricAerospikeNamespaceGeojsonRegionQueryFalsePositive metricAerospikeNamespaceGeojsonRegionQueryFalsePositive @@ -1189,11 +1226,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricAerospikeNamespaceDiskAvailable: newMetricAerospikeNamespaceDiskAvailable(ms.AerospikeNamespaceDiskAvailable), metricAerospikeNamespaceGeojsonRegionQueryCells: newMetricAerospikeNamespaceGeojsonRegionQueryCells(ms.AerospikeNamespaceGeojsonRegionQueryCells), metricAerospikeNamespaceGeojsonRegionQueryFalsePositive: newMetricAerospikeNamespaceGeojsonRegionQueryFalsePositive(ms.AerospikeNamespaceGeojsonRegionQueryFalsePositive), @@ -1226,26 +1271,30 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithAerospikeNamespace sets provided value as "aerospike.namespace" attribute for current resource. func WithAerospikeNamespace(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("aerospike.namespace", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.AerospikeNamespace.Enabled { + rm.Resource().Attributes().PutStr("aerospike.namespace", val) + } } } // WithAerospikeNodeName sets provided value as "aerospike.node.name" attribute for current resource. func WithAerospikeNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("aerospike.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.AerospikeNodeName.Enabled { + rm.Resource().Attributes().PutStr("aerospike.node.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1288,8 +1337,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricAerospikeNodeConnectionOpen.emit(ils.Metrics()) mb.metricAerospikeNodeMemoryFree.emit(ils.Metrics()) mb.metricAerospikeNodeQueryTracked.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/aerospikereceiver/internal/metadata/generated_metrics_test.go b/receiver/aerospikereceiver/internal/metadata/generated_metrics_test.go index 01ab50ae1680..ea658f427c32 100644 --- a/receiver/aerospikereceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/aerospikereceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -124,15 +123,25 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.AerospikeNamespace.Enabled attrVal, ok := rm.Resource().Attributes().Get("aerospike.namespace") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.AerospikeNodeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("aerospike.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 2) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/aerospikereceiver/metadata.yaml b/receiver/aerospikereceiver/metadata.yaml index 822dba18222c..070a926a5aa1 100644 --- a/receiver/aerospikereceiver/metadata.yaml +++ b/receiver/aerospikereceiver/metadata.yaml @@ -3,9 +3,11 @@ name: aerospikereceiver resource_attributes: aerospike.node.name: description: Name of the Aerospike node collected from + enabled: true type: string aerospike.namespace: description: Name of the Aerospike namespace + enabled: true type: string attributes: diff --git a/receiver/apachereceiver/documentation.md b/receiver/apachereceiver/documentation.md index 9fb420cbe24d..283b4c3d3476 100644 --- a/receiver/apachereceiver/documentation.md +++ b/receiver/apachereceiver/documentation.md @@ -131,7 +131,7 @@ The number of workers currently attached to the HTTP server. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| apache.server.name | The name of the Apache HTTP server. | Any Str | -| apache.server.port | The port of the Apache HTTP server. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| apache.server.name | The name of the Apache HTTP server. | Any Str | true | +| apache.server.port | The port of the Apache HTTP server. | Any Str | true | diff --git a/receiver/apachereceiver/internal/metadata/generated_metrics.go b/receiver/apachereceiver/internal/metadata/generated_metrics.go index 680c1baaa817..86678444a588 100644 --- a/receiver/apachereceiver/internal/metadata/generated_metrics.go +++ b/receiver/apachereceiver/internal/metadata/generated_metrics.go @@ -90,6 +90,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for apachereceiver metrics. +type ResourceAttributesSettings struct { + ApacheServerName ResourceAttributeSettings `mapstructure:"apache.server.name"` + ApacheServerPort ResourceAttributeSettings `mapstructure:"apache.server.port"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + ApacheServerName: ResourceAttributeSettings{ + Enabled: true, + }, + ApacheServerPort: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeCPULevel specifies the a value cpu_level attribute. type AttributeCPULevel int @@ -853,6 +889,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricApacheCPULoad metricApacheCPULoad metricApacheCPUTime metricApacheCPUTime metricApacheCurrentConnections metricApacheCurrentConnections @@ -877,11 +914,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricApacheCPULoad: newMetricApacheCPULoad(ms.ApacheCPULoad), metricApacheCPUTime: newMetricApacheCPUTime(ms.ApacheCPUTime), metricApacheCurrentConnections: newMetricApacheCurrentConnections(ms.ApacheCurrentConnections), @@ -912,26 +957,30 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithApacheServerName sets provided value as "apache.server.name" attribute for current resource. func WithApacheServerName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("apache.server.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ApacheServerName.Enabled { + rm.Resource().Attributes().PutStr("apache.server.name", val) + } } } // WithApacheServerPort sets provided value as "apache.server.port" attribute for current resource. func WithApacheServerPort(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("apache.server.port", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ApacheServerPort.Enabled { + rm.Resource().Attributes().PutStr("apache.server.port", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -972,8 +1021,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricApacheTraffic.emit(ils.Metrics()) mb.metricApacheUptime.emit(ils.Metrics()) mb.metricApacheWorkers.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/apachereceiver/internal/metadata/generated_metrics_test.go b/receiver/apachereceiver/internal/metadata/generated_metrics_test.go index 953a0ab693d3..135741cae795 100644 --- a/receiver/apachereceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/apachereceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -116,15 +115,25 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.ApacheServerName.Enabled attrVal, ok := rm.Resource().Attributes().Get("apache.server.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ApacheServerPort.Enabled attrVal, ok = rm.Resource().Attributes().Get("apache.server.port") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 2) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/apachereceiver/metadata.yaml b/receiver/apachereceiver/metadata.yaml index a8faf387671b..10ab12cc304e 100644 --- a/receiver/apachereceiver/metadata.yaml +++ b/receiver/apachereceiver/metadata.yaml @@ -4,9 +4,11 @@ resource_attributes: apache.server.name: description: The name of the Apache HTTP server. type: string + enabled: true apache.server.port: description: The port of the Apache HTTP server. type: string + enabled: true attributes: workers_state: diff --git a/receiver/bigipreceiver/documentation.md b/receiver/bigipreceiver/documentation.md index 76c08cb20be0..fe9311c334a2 100644 --- a/receiver/bigipreceiver/documentation.md +++ b/receiver/bigipreceiver/documentation.md @@ -332,12 +332,12 @@ Number of requests to the virtual server. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| bigip.node.ip_address | The IP Address of the Big-IP Node. | Any Str | -| bigip.node.name | The name of the Big-IP Node. | Any Str | -| bigip.pool.name | The name of the Big-IP Pool. | Any Str | -| bigip.pool_member.ip_address | The IP Address of the Big-IP Pool Member. | Any Str | -| bigip.pool_member.name | The name of the Big-IP Pool Member. | Any Str | -| bigip.virtual_server.destination | The destination for the Big-IP Virtual Server. | Any Str | -| bigip.virtual_server.name | The name of the Big-IP Virtual Server. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| bigip.node.ip_address | The IP Address of the Big-IP Node. | Any Str | true | +| bigip.node.name | The name of the Big-IP Node. | Any Str | true | +| bigip.pool.name | The name of the Big-IP Pool. | Any Str | true | +| bigip.pool_member.ip_address | The IP Address of the Big-IP Pool Member. | Any Str | true | +| bigip.pool_member.name | The name of the Big-IP Pool Member. | Any Str | true | +| bigip.virtual_server.destination | The destination for the Big-IP Virtual Server. | Any Str | true | +| bigip.virtual_server.name | The name of the Big-IP Virtual Server. | Any Str | true | diff --git a/receiver/bigipreceiver/internal/metadata/generated_metrics.go b/receiver/bigipreceiver/internal/metadata/generated_metrics.go index d8b3c7a00f1d..783bc1ed5507 100644 --- a/receiver/bigipreceiver/internal/metadata/generated_metrics.go +++ b/receiver/bigipreceiver/internal/metadata/generated_metrics.go @@ -148,6 +148,62 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for bigipreceiver metrics. +type ResourceAttributesSettings struct { + BigipNodeIPAddress ResourceAttributeSettings `mapstructure:"bigip.node.ip_address"` + BigipNodeName ResourceAttributeSettings `mapstructure:"bigip.node.name"` + BigipPoolName ResourceAttributeSettings `mapstructure:"bigip.pool.name"` + BigipPoolMemberIPAddress ResourceAttributeSettings `mapstructure:"bigip.pool_member.ip_address"` + BigipPoolMemberName ResourceAttributeSettings `mapstructure:"bigip.pool_member.name"` + BigipVirtualServerDestination ResourceAttributeSettings `mapstructure:"bigip.virtual_server.destination"` + BigipVirtualServerName ResourceAttributeSettings `mapstructure:"bigip.virtual_server.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + BigipNodeIPAddress: ResourceAttributeSettings{ + Enabled: true, + }, + BigipNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + BigipPoolName: ResourceAttributeSettings{ + Enabled: true, + }, + BigipPoolMemberIPAddress: ResourceAttributeSettings{ + Enabled: true, + }, + BigipPoolMemberName: ResourceAttributeSettings{ + Enabled: true, + }, + BigipVirtualServerDestination: ResourceAttributeSettings{ + Enabled: true, + }, + BigipVirtualServerName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeActiveStatus specifies the a value active.status attribute. type AttributeActiveStatus int @@ -1659,6 +1715,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricBigipNodeAvailability metricBigipNodeAvailability metricBigipNodeConnectionCount metricBigipNodeConnectionCount metricBigipNodeDataTransmitted metricBigipNodeDataTransmitted @@ -1698,11 +1755,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricBigipNodeAvailability: newMetricBigipNodeAvailability(ms.BigipNodeAvailability), metricBigipNodeConnectionCount: newMetricBigipNodeConnectionCount(ms.BigipNodeConnectionCount), metricBigipNodeDataTransmitted: newMetricBigipNodeDataTransmitted(ms.BigipNodeDataTransmitted), @@ -1748,61 +1813,75 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithBigipNodeIPAddress sets provided value as "bigip.node.ip_address" attribute for current resource. func WithBigipNodeIPAddress(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.node.ip_address", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipNodeIPAddress.Enabled { + rm.Resource().Attributes().PutStr("bigip.node.ip_address", val) + } } } // WithBigipNodeName sets provided value as "bigip.node.name" attribute for current resource. func WithBigipNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipNodeName.Enabled { + rm.Resource().Attributes().PutStr("bigip.node.name", val) + } } } // WithBigipPoolName sets provided value as "bigip.pool.name" attribute for current resource. func WithBigipPoolName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.pool.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipPoolName.Enabled { + rm.Resource().Attributes().PutStr("bigip.pool.name", val) + } } } // WithBigipPoolMemberIPAddress sets provided value as "bigip.pool_member.ip_address" attribute for current resource. func WithBigipPoolMemberIPAddress(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.pool_member.ip_address", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipPoolMemberIPAddress.Enabled { + rm.Resource().Attributes().PutStr("bigip.pool_member.ip_address", val) + } } } // WithBigipPoolMemberName sets provided value as "bigip.pool_member.name" attribute for current resource. func WithBigipPoolMemberName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.pool_member.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipPoolMemberName.Enabled { + rm.Resource().Attributes().PutStr("bigip.pool_member.name", val) + } } } // WithBigipVirtualServerDestination sets provided value as "bigip.virtual_server.destination" attribute for current resource. func WithBigipVirtualServerDestination(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.virtual_server.destination", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipVirtualServerDestination.Enabled { + rm.Resource().Attributes().PutStr("bigip.virtual_server.destination", val) + } } } // WithBigipVirtualServerName sets provided value as "bigip.virtual_server.name" attribute for current resource. func WithBigipVirtualServerName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("bigip.virtual_server.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.BigipVirtualServerName.Enabled { + rm.Resource().Attributes().PutStr("bigip.virtual_server.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1858,8 +1937,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricBigipVirtualServerEnabled.emit(ils.Metrics()) mb.metricBigipVirtualServerPacketCount.emit(ils.Metrics()) mb.metricBigipVirtualServerRequestCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/bigipreceiver/internal/metadata/generated_metrics_test.go b/receiver/bigipreceiver/internal/metadata/generated_metrics_test.go index a915a62f8b58..a1570c14ee81 100644 --- a/receiver/bigipreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/bigipreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -176,35 +175,65 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.BigipNodeIPAddress.Enabled attrVal, ok := rm.Resource().Attributes().Get("bigip.node.ip_address") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipNodeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipPoolName.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.pool.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipPoolMemberIPAddress.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.pool_member.ip_address") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipPoolMemberName.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.pool_member.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipVirtualServerDestination.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.virtual_server.destination") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.BigipVirtualServerName.Enabled attrVal, ok = rm.Resource().Attributes().Get("bigip.virtual_server.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 7) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/bigipreceiver/metadata.yaml b/receiver/bigipreceiver/metadata.yaml index 03324870afa0..3fac119c2cdd 100644 --- a/receiver/bigipreceiver/metadata.yaml +++ b/receiver/bigipreceiver/metadata.yaml @@ -4,24 +4,31 @@ resource_attributes: bigip.virtual_server.name: description: The name of the Big-IP Virtual Server. type: string + enabled: true bigip.virtual_server.destination: description: The destination for the Big-IP Virtual Server. type: string + enabled: true bigip.pool.name: description: The name of the Big-IP Pool. type: string + enabled: true bigip.pool_member.name: description: The name of the Big-IP Pool Member. type: string + enabled: true bigip.pool_member.ip_address: description: The IP Address of the Big-IP Pool Member. type: string + enabled: true bigip.node.name: description: The name of the Big-IP Node. type: string + enabled: true bigip.node.ip_address: description: The IP Address of the Big-IP Node. type: string + enabled: true attributes: direction: diff --git a/receiver/chronyreceiver/internal/metadata/generated_metrics.go b/receiver/chronyreceiver/internal/metadata/generated_metrics.go index 9dbe3570597e..204c36e79218 100644 --- a/receiver/chronyreceiver/internal/metadata/generated_metrics.go +++ b/receiver/chronyreceiver/internal/metadata/generated_metrics.go @@ -68,6 +68,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for chrony receiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeLeapStatus specifies the a value leap.status attribute. type AttributeLeapStatus int @@ -458,18 +485,19 @@ func newMetricNtpTimeRootDelay(settings MetricSettings) metricNtpTimeRootDelay { // 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. 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 - metricNtpFrequencyOffset metricNtpFrequencyOffset - metricNtpSkew metricNtpSkew - metricNtpStratum metricNtpStratum - metricNtpTimeCorrection metricNtpTimeCorrection - metricNtpTimeLastOffset metricNtpTimeLastOffset - metricNtpTimeRmsOffset metricNtpTimeRmsOffset - metricNtpTimeRootDelay metricNtpTimeRootDelay + 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 + resourceAttributesSettings ResourceAttributesSettings + metricNtpFrequencyOffset metricNtpFrequencyOffset + metricNtpSkew metricNtpSkew + metricNtpStratum metricNtpStratum + metricNtpTimeCorrection metricNtpTimeCorrection + metricNtpTimeLastOffset metricNtpTimeLastOffset + metricNtpTimeRmsOffset metricNtpTimeRmsOffset + metricNtpTimeRootDelay metricNtpTimeRootDelay } // metricBuilderOption applies changes to default metrics builder. @@ -482,18 +510,26 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, - metricNtpFrequencyOffset: newMetricNtpFrequencyOffset(ms.NtpFrequencyOffset), - metricNtpSkew: newMetricNtpSkew(ms.NtpSkew), - metricNtpStratum: newMetricNtpStratum(ms.NtpStratum), - metricNtpTimeCorrection: newMetricNtpTimeCorrection(ms.NtpTimeCorrection), - metricNtpTimeLastOffset: newMetricNtpTimeLastOffset(ms.NtpTimeLastOffset), - metricNtpTimeRmsOffset: newMetricNtpTimeRmsOffset(ms.NtpTimeRmsOffset), - metricNtpTimeRootDelay: newMetricNtpTimeRootDelay(ms.NtpTimeRootDelay), + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), + metricNtpFrequencyOffset: newMetricNtpFrequencyOffset(ms.NtpFrequencyOffset), + metricNtpSkew: newMetricNtpSkew(ms.NtpSkew), + metricNtpStratum: newMetricNtpStratum(ms.NtpStratum), + metricNtpTimeCorrection: newMetricNtpTimeCorrection(ms.NtpTimeCorrection), + metricNtpTimeLastOffset: newMetricNtpTimeLastOffset(ms.NtpTimeLastOffset), + metricNtpTimeRmsOffset: newMetricNtpTimeRmsOffset(ms.NtpTimeRmsOffset), + metricNtpTimeRootDelay: newMetricNtpTimeRootDelay(ms.NtpTimeRootDelay), } for _, op := range options { op(mb) @@ -512,12 +548,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -553,8 +589,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricNtpTimeLastOffset.emit(ils.Metrics()) mb.metricNtpTimeRmsOffset.emit(ils.Metrics()) mb.metricNtpTimeRootDelay.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/chronyreceiver/internal/metadata/generated_metrics_test.go b/receiver/chronyreceiver/internal/metadata/generated_metrics_test.go index 979b7eb44beb..95a04ec44b01 100644 --- a/receiver/chronyreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/chronyreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -92,7 +91,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/couchdbreceiver/documentation.md b/receiver/couchdbreceiver/documentation.md index a31feaddf959..5499a7cc548a 100644 --- a/receiver/couchdbreceiver/documentation.md +++ b/receiver/couchdbreceiver/documentation.md @@ -102,6 +102,6 @@ The number of views read. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| couchdb.node.name | The name of the node. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| couchdb.node.name | The name of the node. | Any Str | true | diff --git a/receiver/couchdbreceiver/internal/metadata/generated_metrics.go b/receiver/couchdbreceiver/internal/metadata/generated_metrics.go index 20360b5e93e2..4c2a103e213b 100644 --- a/receiver/couchdbreceiver/internal/metadata/generated_metrics.go +++ b/receiver/couchdbreceiver/internal/metadata/generated_metrics.go @@ -72,6 +72,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for couchdbreceiver metrics. +type ResourceAttributesSettings struct { + CouchdbNodeName ResourceAttributeSettings `mapstructure:"couchdb.node.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + CouchdbNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeHTTPMethod specifies the a value http.method attribute. type AttributeHTTPMethod int @@ -592,6 +624,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricCouchdbAverageRequestTime metricCouchdbAverageRequestTime metricCouchdbDatabaseOpen metricCouchdbDatabaseOpen metricCouchdbDatabaseOperations metricCouchdbDatabaseOperations @@ -612,11 +645,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricCouchdbAverageRequestTime: newMetricCouchdbAverageRequestTime(ms.CouchdbAverageRequestTime), metricCouchdbDatabaseOpen: newMetricCouchdbDatabaseOpen(ms.CouchdbDatabaseOpen), metricCouchdbDatabaseOperations: newMetricCouchdbDatabaseOperations(ms.CouchdbDatabaseOperations), @@ -643,19 +684,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithCouchdbNodeName sets provided value as "couchdb.node.name" attribute for current resource. func WithCouchdbNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("couchdb.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.CouchdbNodeName.Enabled { + rm.Resource().Attributes().PutStr("couchdb.node.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -692,8 +735,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricCouchdbHttpdRequests.emit(ils.Metrics()) mb.metricCouchdbHttpdResponses.emit(ils.Metrics()) mb.metricCouchdbHttpdViews.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/couchdbreceiver/internal/metadata/generated_metrics_test.go b/receiver/couchdbreceiver/internal/metadata/generated_metrics_test.go index 0d4210dcd0be..b6db99487610 100644 --- a/receiver/couchdbreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/couchdbreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -100,11 +99,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.CouchdbNodeName.Enabled attrVal, ok := rm.Resource().Attributes().Get("couchdb.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/couchdbreceiver/metadata.yaml b/receiver/couchdbreceiver/metadata.yaml index d701e3c0dab1..8e8117c9d127 100644 --- a/receiver/couchdbreceiver/metadata.yaml +++ b/receiver/couchdbreceiver/metadata.yaml @@ -4,6 +4,7 @@ resource_attributes: couchdb.node.name: description: The name of the node. type: string + enabled: true attributes: http.method: diff --git a/receiver/dockerstatsreceiver/documentation.md b/receiver/dockerstatsreceiver/documentation.md index e6a8e1dec462..b735bf4ab7e2 100644 --- a/receiver/dockerstatsreceiver/documentation.md +++ b/receiver/dockerstatsreceiver/documentation.md @@ -676,10 +676,10 @@ Packets sent. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| container.hostname | The hostname of the container. | Any Str | -| container.id | The ID of the container. | Any Str | -| container.image.name | The name of the docker image in use by the container. | Any Str | -| container.name | The name of the container. | Any Str | -| container.runtime | The runtime of the container. For this receiver, it will always be 'docker'. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| container.hostname | The hostname of the container. | Any Str | true | +| container.id | The ID of the container. | Any Str | true | +| container.image.name | The name of the docker image in use by the container. | Any Str | true | +| container.name | The name of the container. | Any Str | true | +| container.runtime | The runtime of the container. For this receiver, it will always be 'docker'. | Any Str | true | diff --git a/receiver/dockerstatsreceiver/internal/metadata/generated_metrics.go b/receiver/dockerstatsreceiver/internal/metadata/generated_metrics.go index 5593a44ac22a..930918097ca3 100644 --- a/receiver/dockerstatsreceiver/internal/metadata/generated_metrics.go +++ b/receiver/dockerstatsreceiver/internal/metadata/generated_metrics.go @@ -293,6 +293,54 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for dockerstatsreceiver metrics. +type ResourceAttributesSettings struct { + ContainerHostname ResourceAttributeSettings `mapstructure:"container.hostname"` + ContainerID ResourceAttributeSettings `mapstructure:"container.id"` + ContainerImageName ResourceAttributeSettings `mapstructure:"container.image.name"` + ContainerName ResourceAttributeSettings `mapstructure:"container.name"` + ContainerRuntime ResourceAttributeSettings `mapstructure:"container.runtime"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + ContainerHostname: ResourceAttributeSettings{ + Enabled: true, + }, + ContainerID: ResourceAttributeSettings{ + Enabled: true, + }, + ContainerImageName: ResourceAttributeSettings{ + Enabled: true, + }, + ContainerName: ResourceAttributeSettings{ + Enabled: true, + }, + ContainerRuntime: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + type metricContainerBlockioIoMergedRecursive struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -3560,6 +3608,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricContainerBlockioIoMergedRecursive metricContainerBlockioIoMergedRecursive metricContainerBlockioIoQueuedRecursive metricContainerBlockioIoQueuedRecursive metricContainerBlockioIoServiceBytesRecursive metricContainerBlockioIoServiceBytesRecursive @@ -3635,13 +3684,21 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, - metricContainerBlockioIoMergedRecursive: newMetricContainerBlockioIoMergedRecursive(ms.ContainerBlockioIoMergedRecursive), - metricContainerBlockioIoQueuedRecursive: newMetricContainerBlockioIoQueuedRecursive(ms.ContainerBlockioIoQueuedRecursive), + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), + metricContainerBlockioIoMergedRecursive: newMetricContainerBlockioIoMergedRecursive(ms.ContainerBlockioIoMergedRecursive), + metricContainerBlockioIoQueuedRecursive: newMetricContainerBlockioIoQueuedRecursive(ms.ContainerBlockioIoQueuedRecursive), metricContainerBlockioIoServiceBytesRecursive: newMetricContainerBlockioIoServiceBytesRecursive(ms.ContainerBlockioIoServiceBytesRecursive), metricContainerBlockioIoServiceTimeRecursive: newMetricContainerBlockioIoServiceTimeRecursive(ms.ContainerBlockioIoServiceTimeRecursive), metricContainerBlockioIoServicedRecursive: newMetricContainerBlockioIoServicedRecursive(ms.ContainerBlockioIoServicedRecursive), @@ -3721,47 +3778,57 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithContainerHostname sets provided value as "container.hostname" attribute for current resource. func WithContainerHostname(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.hostname", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerHostname.Enabled { + rm.Resource().Attributes().PutStr("container.hostname", val) + } } } // WithContainerID sets provided value as "container.id" attribute for current resource. func WithContainerID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerID.Enabled { + rm.Resource().Attributes().PutStr("container.id", val) + } } } // WithContainerImageName sets provided value as "container.image.name" attribute for current resource. func WithContainerImageName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.image.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerImageName.Enabled { + rm.Resource().Attributes().PutStr("container.image.name", val) + } } } // WithContainerName sets provided value as "container.name" attribute for current resource. func WithContainerName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerName.Enabled { + rm.Resource().Attributes().PutStr("container.name", val) + } } } // WithContainerRuntime sets provided value as "container.runtime" attribute for current resource. func WithContainerRuntime(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.runtime", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerRuntime.Enabled { + rm.Resource().Attributes().PutStr("container.runtime", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -3854,8 +3921,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricContainerNetworkIoUsageTxDropped.emit(ils.Metrics()) mb.metricContainerNetworkIoUsageTxErrors.emit(ils.Metrics()) mb.metricContainerNetworkIoUsageTxPackets.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/dockerstatsreceiver/internal/metadata/generated_metrics_test.go b/receiver/dockerstatsreceiver/internal/metadata/generated_metrics_test.go index 267ae47a6641..c4d06772f02f 100644 --- a/receiver/dockerstatsreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/dockerstatsreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -270,27 +269,49 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.ContainerHostname.Enabled attrVal, ok := rm.Resource().Attributes().Get("container.hostname") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ContainerID.Enabled attrVal, ok = rm.Resource().Attributes().Get("container.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ContainerImageName.Enabled attrVal, ok = rm.Resource().Attributes().Get("container.image.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ContainerName.Enabled attrVal, ok = rm.Resource().Attributes().Get("container.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ContainerRuntime.Enabled attrVal, ok = rm.Resource().Attributes().Get("container.runtime") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 5) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/dockerstatsreceiver/metadata.yaml b/receiver/dockerstatsreceiver/metadata.yaml index 801eb17b11b3..44696038fae8 100644 --- a/receiver/dockerstatsreceiver/metadata.yaml +++ b/receiver/dockerstatsreceiver/metadata.yaml @@ -7,18 +7,23 @@ resource_attributes: container.runtime: description: "The runtime of the container. For this receiver, it will always be 'docker'." type: string + enabled: true container.id: description: "The ID of the container." type: string + enabled: true container.image.name: description: "The name of the docker image in use by the container." type: string + enabled: true container.name: description: "The name of the container." type: string + enabled: true container.hostname: description: "The hostname of the container." type: string + enabled: true attributes: core: diff --git a/receiver/dockerstatsreceiver/receiver_v2.go b/receiver/dockerstatsreceiver/receiver_v2.go index ae4fdbf9ddf6..7b6947b13ac1 100644 --- a/receiver/dockerstatsreceiver/receiver_v2.go +++ b/receiver/dockerstatsreceiver/receiver_v2.go @@ -98,14 +98,14 @@ func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *d for k, label := range r.config.EnvVarsToMetricLabels { if v := container.EnvMap[k]; v != "" { - resourceMetricsOptions = append(resourceMetricsOptions, func(rm pmetric.ResourceMetrics) { + resourceMetricsOptions = append(resourceMetricsOptions, func(ras metadata.ResourceAttributesSettings, rm pmetric.ResourceMetrics) { rm.Resource().Attributes().PutStr(label, v) }) } } for k, label := range r.config.ContainerLabelsToMetricLabels { if v := container.Config.Labels[k]; v != "" { - resourceMetricsOptions = append(resourceMetricsOptions, func(rm pmetric.ResourceMetrics) { + resourceMetricsOptions = append(resourceMetricsOptions, func(ras metadata.ResourceAttributesSettings, rm pmetric.ResourceMetrics) { rm.Resource().Attributes().PutStr(label, v) }) } diff --git a/receiver/elasticsearchreceiver/documentation.md b/receiver/elasticsearchreceiver/documentation.md index ff1257754727..f6c207c32aff 100644 --- a/receiver/elasticsearchreceiver/documentation.md +++ b/receiver/elasticsearchreceiver/documentation.md @@ -1045,9 +1045,9 @@ Fraction of heap memory usage ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| elasticsearch.cluster.name | The name of the elasticsearch cluster. | Any Str | -| elasticsearch.index.name | The name of the elasticsearch index. | Any Str | -| elasticsearch.node.name | The name of the elasticsearch node. | Any Str | -| elasticsearch.node.version | The version of the elasticsearch node. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| elasticsearch.cluster.name | The name of the elasticsearch cluster. | Any Str | true | +| elasticsearch.index.name | The name of the elasticsearch index. | Any Str | true | +| elasticsearch.node.name | The name of the elasticsearch node. | Any Str | true | +| elasticsearch.node.version | The version of the elasticsearch node. | Any Str | true | diff --git a/receiver/elasticsearchreceiver/internal/metadata/generated_metrics.go b/receiver/elasticsearchreceiver/internal/metadata/generated_metrics.go index 91fdfd47d1a3..3795fd18c480 100644 --- a/receiver/elasticsearchreceiver/internal/metadata/generated_metrics.go +++ b/receiver/elasticsearchreceiver/internal/metadata/generated_metrics.go @@ -404,6 +404,50 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for elasticsearchreceiver metrics. +type ResourceAttributesSettings struct { + ElasticsearchClusterName ResourceAttributeSettings `mapstructure:"elasticsearch.cluster.name"` + ElasticsearchIndexName ResourceAttributeSettings `mapstructure:"elasticsearch.index.name"` + ElasticsearchNodeName ResourceAttributeSettings `mapstructure:"elasticsearch.node.name"` + ElasticsearchNodeVersion ResourceAttributeSettings `mapstructure:"elasticsearch.node.version"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + ElasticsearchClusterName: ResourceAttributeSettings{ + Enabled: true, + }, + ElasticsearchIndexName: ResourceAttributeSettings{ + Enabled: true, + }, + ElasticsearchNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + ElasticsearchNodeVersion: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeCacheName specifies the a value cache_name attribute. type AttributeCacheName int @@ -5644,6 +5688,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricElasticsearchBreakerMemoryEstimated metricElasticsearchBreakerMemoryEstimated metricElasticsearchBreakerMemoryLimit metricElasticsearchBreakerMemoryLimit metricElasticsearchBreakerTripped metricElasticsearchBreakerTripped @@ -5747,11 +5792,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricElasticsearchBreakerMemoryEstimated: newMetricElasticsearchBreakerMemoryEstimated(ms.ElasticsearchBreakerMemoryEstimated), metricElasticsearchBreakerMemoryLimit: newMetricElasticsearchBreakerMemoryLimit(ms.ElasticsearchBreakerMemoryLimit), metricElasticsearchBreakerTripped: newMetricElasticsearchBreakerTripped(ms.ElasticsearchBreakerTripped), @@ -5861,40 +5914,48 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithElasticsearchClusterName sets provided value as "elasticsearch.cluster.name" attribute for current resource. func WithElasticsearchClusterName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("elasticsearch.cluster.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ElasticsearchClusterName.Enabled { + rm.Resource().Attributes().PutStr("elasticsearch.cluster.name", val) + } } } // WithElasticsearchIndexName sets provided value as "elasticsearch.index.name" attribute for current resource. func WithElasticsearchIndexName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("elasticsearch.index.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ElasticsearchIndexName.Enabled { + rm.Resource().Attributes().PutStr("elasticsearch.index.name", val) + } } } // WithElasticsearchNodeName sets provided value as "elasticsearch.node.name" attribute for current resource. func WithElasticsearchNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("elasticsearch.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ElasticsearchNodeName.Enabled { + rm.Resource().Attributes().PutStr("elasticsearch.node.name", val) + } } } // WithElasticsearchNodeVersion sets provided value as "elasticsearch.node.version" attribute for current resource. func WithElasticsearchNodeVersion(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("elasticsearch.node.version", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ElasticsearchNodeVersion.Enabled { + rm.Resource().Attributes().PutStr("elasticsearch.node.version", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -6014,8 +6075,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricJvmMemoryPoolMax.emit(ils.Metrics()) mb.metricJvmMemoryPoolUsed.emit(ils.Metrics()) mb.metricJvmThreadsCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/elasticsearchreceiver/internal/metadata/generated_metrics_test.go b/receiver/elasticsearchreceiver/internal/metadata/generated_metrics_test.go index f06006c9a815..162cfb14406c 100644 --- a/receiver/elasticsearchreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/elasticsearchreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -411,23 +410,41 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.ElasticsearchClusterName.Enabled attrVal, ok := rm.Resource().Attributes().Get("elasticsearch.cluster.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ElasticsearchIndexName.Enabled attrVal, ok = rm.Resource().Attributes().Get("elasticsearch.index.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ElasticsearchNodeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("elasticsearch.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ElasticsearchNodeVersion.Enabled attrVal, ok = rm.Resource().Attributes().Get("elasticsearch.node.version") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 4) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/elasticsearchreceiver/metadata.yaml b/receiver/elasticsearchreceiver/metadata.yaml index b9b85775fb4d..07f1a07bf001 100644 --- a/receiver/elasticsearchreceiver/metadata.yaml +++ b/receiver/elasticsearchreceiver/metadata.yaml @@ -4,15 +4,19 @@ resource_attributes: elasticsearch.cluster.name: description: The name of the elasticsearch cluster. type: string + enabled: true elasticsearch.node.name: description: The name of the elasticsearch node. type: string + enabled: true elasticsearch.node.version: description: The version of the elasticsearch node. type: string + enabled: true elasticsearch.index.name: description: The name of the elasticsearch index. type: string + enabled: true attributes: cache_name: diff --git a/receiver/expvarreceiver/internal/metadata/generated_metrics.go b/receiver/expvarreceiver/internal/metadata/generated_metrics.go index 6548b4351297..8a287615dbb3 100644 --- a/receiver/expvarreceiver/internal/metadata/generated_metrics.go +++ b/receiver/expvarreceiver/internal/metadata/generated_metrics.go @@ -144,6 +144,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for expvarreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + type metricProcessRuntimeMemstatsBuckHashSys struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -1474,6 +1501,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricProcessRuntimeMemstatsBuckHashSys metricProcessRuntimeMemstatsBuckHashSys metricProcessRuntimeMemstatsFrees metricProcessRuntimeMemstatsFrees metricProcessRuntimeMemstatsGcCPUFraction metricProcessRuntimeMemstatsGcCPUFraction @@ -1512,11 +1540,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricProcessRuntimeMemstatsBuckHashSys: newMetricProcessRuntimeMemstatsBuckHashSys(ms.ProcessRuntimeMemstatsBuckHashSys), metricProcessRuntimeMemstatsFrees: newMetricProcessRuntimeMemstatsFrees(ms.ProcessRuntimeMemstatsFrees), metricProcessRuntimeMemstatsGcCPUFraction: newMetricProcessRuntimeMemstatsGcCPUFraction(ms.ProcessRuntimeMemstatsGcCPUFraction), @@ -1561,12 +1597,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1621,8 +1657,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricProcessRuntimeMemstatsStackSys.emit(ils.Metrics()) mb.metricProcessRuntimeMemstatsSys.emit(ils.Metrics()) mb.metricProcessRuntimeMemstatsTotalAlloc.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/expvarreceiver/internal/metadata/generated_metrics_test.go b/receiver/expvarreceiver/internal/metadata/generated_metrics_test.go index a10965f1b272..79ce77d5c74a 100644 --- a/receiver/expvarreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/expvarreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -170,7 +169,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/flinkmetricsreceiver/documentation.md b/receiver/flinkmetricsreceiver/documentation.md index c6a2779fafba..a8e17c0f5fd6 100644 --- a/receiver/flinkmetricsreceiver/documentation.md +++ b/receiver/flinkmetricsreceiver/documentation.md @@ -283,11 +283,11 @@ The number of records a task has. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| flink.job.name | The job name. | Any Str | -| flink.resource.type | The flink scope type in which a metric belongs to. | Str: ``jobmanager``, ``taskmanager`` | -| flink.subtask.index | The subtask index. | Any Str | -| flink.task.name | The task name. | Any Str | -| flink.taskmanager.id | The taskmanager ID. | Any Str | -| host.name | The host name. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| flink.job.name | The job name. | Any Str | true | +| flink.resource.type | The flink scope type in which a metric belongs to. | Str: ``jobmanager``, ``taskmanager`` | true | +| flink.subtask.index | The subtask index. | Any Str | true | +| flink.task.name | The task name. | Any Str | true | +| flink.taskmanager.id | The taskmanager ID. | Any Str | true | +| host.name | The host name. | Any Str | true | diff --git a/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics.go b/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics.go index 93ed04a9f960..9c97c9fb5e86 100644 --- a/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics.go +++ b/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics.go @@ -158,6 +158,58 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for flinkmetricsreceiver metrics. +type ResourceAttributesSettings struct { + FlinkJobName ResourceAttributeSettings `mapstructure:"flink.job.name"` + FlinkResourceType ResourceAttributeSettings `mapstructure:"flink.resource.type"` + FlinkSubtaskIndex ResourceAttributeSettings `mapstructure:"flink.subtask.index"` + FlinkTaskName ResourceAttributeSettings `mapstructure:"flink.task.name"` + FlinkTaskmanagerID ResourceAttributeSettings `mapstructure:"flink.taskmanager.id"` + HostName ResourceAttributeSettings `mapstructure:"host.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + FlinkJobName: ResourceAttributeSettings{ + Enabled: true, + }, + FlinkResourceType: ResourceAttributeSettings{ + Enabled: true, + }, + FlinkSubtaskIndex: ResourceAttributeSettings{ + Enabled: true, + }, + FlinkTaskName: ResourceAttributeSettings{ + Enabled: true, + }, + FlinkTaskmanagerID: ResourceAttributeSettings{ + Enabled: true, + }, + HostName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeCheckpoint specifies the a value checkpoint attribute. type AttributeCheckpoint int @@ -1744,6 +1796,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricFlinkJobCheckpointCount metricFlinkJobCheckpointCount metricFlinkJobCheckpointInProgress metricFlinkJobCheckpointInProgress metricFlinkJobLastCheckpointSize metricFlinkJobLastCheckpointSize @@ -1785,11 +1838,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricFlinkJobCheckpointCount: newMetricFlinkJobCheckpointCount(ms.FlinkJobCheckpointCount), metricFlinkJobCheckpointInProgress: newMetricFlinkJobCheckpointInProgress(ms.FlinkJobCheckpointInProgress), metricFlinkJobLastCheckpointSize: newMetricFlinkJobLastCheckpointSize(ms.FlinkJobLastCheckpointSize), @@ -1837,57 +1898,71 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithFlinkJobName sets provided value as "flink.job.name" attribute for current resource. func WithFlinkJobName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.job.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkJobName.Enabled { + rm.Resource().Attributes().PutStr("flink.job.name", val) + } } } // WithFlinkResourceTypeJobmanager sets "flink.resource.type=jobmanager" attribute for current resource. -func WithFlinkResourceTypeJobmanager(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.resource.type", "jobmanager") +func WithFlinkResourceTypeJobmanager(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkResourceType.Enabled { + rm.Resource().Attributes().PutStr("flink.resource.type", "jobmanager") + } } // WithFlinkResourceTypeTaskmanager sets "flink.resource.type=taskmanager" attribute for current resource. -func WithFlinkResourceTypeTaskmanager(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.resource.type", "taskmanager") +func WithFlinkResourceTypeTaskmanager(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkResourceType.Enabled { + rm.Resource().Attributes().PutStr("flink.resource.type", "taskmanager") + } } // WithFlinkSubtaskIndex sets provided value as "flink.subtask.index" attribute for current resource. func WithFlinkSubtaskIndex(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.subtask.index", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkSubtaskIndex.Enabled { + rm.Resource().Attributes().PutStr("flink.subtask.index", val) + } } } // WithFlinkTaskName sets provided value as "flink.task.name" attribute for current resource. func WithFlinkTaskName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.task.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkTaskName.Enabled { + rm.Resource().Attributes().PutStr("flink.task.name", val) + } } } // WithFlinkTaskmanagerID sets provided value as "flink.taskmanager.id" attribute for current resource. func WithFlinkTaskmanagerID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("flink.taskmanager.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FlinkTaskmanagerID.Enabled { + rm.Resource().Attributes().PutStr("flink.taskmanager.id", val) + } } } // WithHostName sets provided value as "host.name" attribute for current resource. func WithHostName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("host.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HostName.Enabled { + rm.Resource().Attributes().PutStr("host.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1945,8 +2020,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricFlinkOperatorRecordCount.emit(ils.Metrics()) mb.metricFlinkOperatorWatermarkOutput.emit(ils.Metrics()) mb.metricFlinkTaskRecordCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics_test.go b/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics_test.go index f193961300eb..de603595a1f6 100644 --- a/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/flinkmetricsreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -184,31 +183,57 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.FlinkJobName.Enabled attrVal, ok := rm.Resource().Attributes().Get("flink.job.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.FlinkResourceType.Enabled attrVal, ok = rm.Resource().Attributes().Get("flink.resource.type") - assert.True(t, ok) - assert.Equal(t, "jobmanager", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.Equal(t, "jobmanager", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.FlinkSubtaskIndex.Enabled attrVal, ok = rm.Resource().Attributes().Get("flink.subtask.index") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.FlinkTaskName.Enabled attrVal, ok = rm.Resource().Attributes().Get("flink.task.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.FlinkTaskmanagerID.Enabled attrVal, ok = rm.Resource().Attributes().Get("flink.taskmanager.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HostName.Enabled attrVal, ok = rm.Resource().Attributes().Get("host.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 6) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/flinkmetricsreceiver/metadata.yaml b/receiver/flinkmetricsreceiver/metadata.yaml index cffae5e9786e..8b2df335c3c9 100644 --- a/receiver/flinkmetricsreceiver/metadata.yaml +++ b/receiver/flinkmetricsreceiver/metadata.yaml @@ -5,21 +5,27 @@ resource_attributes: host.name: description: The host name. type: string + enabled: true flink.taskmanager.id: description: The taskmanager ID. type: string + enabled: true flink.job.name: description: The job name. type: string + enabled: true flink.task.name: description: The task name. type: string + enabled: true flink.subtask.index: description: The subtask index. type: string + enabled: true flink.resource.type: description: The flink scope type in which a metric belongs to. type: string + enabled: true enum: [ jobmanager, taskmanager ] attributes: diff --git a/receiver/haproxyreceiver/documentation.md b/receiver/haproxyreceiver/documentation.md index 4ff212e7e772..57f41c25a736 100644 --- a/receiver/haproxyreceiver/documentation.md +++ b/receiver/haproxyreceiver/documentation.md @@ -60,12 +60,12 @@ Current sessions. Corresponds to HAProxy's `scur` metric. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| haproxy.addr | address:port or "unix". IPv6 has brackets around the address. | Any Str | -| haproxy.algo | load balancing algorithm | Any Str | -| haproxy.iid | unique proxy id | Any Str | -| haproxy.pid | process id (0 for first instance, 1 for second, ...) | Any Str | -| haproxy.sid | server id (unique inside a proxy) | Any Str | -| haproxy.type | (0=frontend, 1=backend, 2=server, 3=socket/listener) | Any Str | -| haproxy.url | The path to the HAProxy socket or HTTP URL. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| haproxy.addr | address:port or "unix". IPv6 has brackets around the address. | Any Str | true | +| haproxy.algo | load balancing algorithm | Any Str | true | +| haproxy.iid | unique proxy id | Any Str | true | +| haproxy.pid | process id (0 for first instance, 1 for second, ...) | Any Str | true | +| haproxy.sid | server id (unique inside a proxy) | Any Str | true | +| haproxy.type | (0=frontend, 1=backend, 2=server, 3=socket/listener) | Any Str | true | +| haproxy.url | The path to the HAProxy socket or HTTP URL. | Any Str | true | diff --git a/receiver/haproxyreceiver/internal/metadata/generated_metrics.go b/receiver/haproxyreceiver/internal/metadata/generated_metrics.go index 0d1ee6d5fb6d..73c7461015d3 100644 --- a/receiver/haproxyreceiver/internal/metadata/generated_metrics.go +++ b/receiver/haproxyreceiver/internal/metadata/generated_metrics.go @@ -58,6 +58,62 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for haproxyreceiver metrics. +type ResourceAttributesSettings struct { + HaproxyAddr ResourceAttributeSettings `mapstructure:"haproxy.addr"` + HaproxyAlgo ResourceAttributeSettings `mapstructure:"haproxy.algo"` + HaproxyIid ResourceAttributeSettings `mapstructure:"haproxy.iid"` + HaproxyPid ResourceAttributeSettings `mapstructure:"haproxy.pid"` + HaproxySid ResourceAttributeSettings `mapstructure:"haproxy.sid"` + HaproxyType ResourceAttributeSettings `mapstructure:"haproxy.type"` + HaproxyURL ResourceAttributeSettings `mapstructure:"haproxy.url"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + HaproxyAddr: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxyAlgo: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxyIid: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxyPid: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxySid: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxyType: ResourceAttributeSettings{ + Enabled: true, + }, + HaproxyURL: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + type metricHaproxyConnectionRate struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -270,6 +326,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricHaproxyConnectionRate metricHaproxyConnectionRate metricHaproxyIdlePercent metricHaproxyIdlePercent metricHaproxyRequests metricHaproxyRequests @@ -286,11 +343,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricHaproxyConnectionRate: newMetricHaproxyConnectionRate(ms.HaproxyConnectionRate), metricHaproxyIdlePercent: newMetricHaproxyIdlePercent(ms.HaproxyIdlePercent), metricHaproxyRequests: newMetricHaproxyRequests(ms.HaproxyRequests), @@ -313,61 +378,75 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithHaproxyAddr sets provided value as "haproxy.addr" attribute for current resource. func WithHaproxyAddr(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.addr", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyAddr.Enabled { + rm.Resource().Attributes().PutStr("haproxy.addr", val) + } } } // WithHaproxyAlgo sets provided value as "haproxy.algo" attribute for current resource. func WithHaproxyAlgo(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.algo", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyAlgo.Enabled { + rm.Resource().Attributes().PutStr("haproxy.algo", val) + } } } // WithHaproxyIid sets provided value as "haproxy.iid" attribute for current resource. func WithHaproxyIid(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.iid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyIid.Enabled { + rm.Resource().Attributes().PutStr("haproxy.iid", val) + } } } // WithHaproxyPid sets provided value as "haproxy.pid" attribute for current resource. func WithHaproxyPid(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.pid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyPid.Enabled { + rm.Resource().Attributes().PutStr("haproxy.pid", val) + } } } // WithHaproxySid sets provided value as "haproxy.sid" attribute for current resource. func WithHaproxySid(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.sid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxySid.Enabled { + rm.Resource().Attributes().PutStr("haproxy.sid", val) + } } } // WithHaproxyType sets provided value as "haproxy.type" attribute for current resource. func WithHaproxyType(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.type", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyType.Enabled { + rm.Resource().Attributes().PutStr("haproxy.type", val) + } } } // WithHaproxyURL sets provided value as "haproxy.url" attribute for current resource. func WithHaproxyURL(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("haproxy.url", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.HaproxyURL.Enabled { + rm.Resource().Attributes().PutStr("haproxy.url", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -400,8 +479,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricHaproxyIdlePercent.emit(ils.Metrics()) mb.metricHaproxyRequests.emit(ils.Metrics()) mb.metricHaproxySessionsCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/haproxyreceiver/internal/metadata/generated_metrics_test.go b/receiver/haproxyreceiver/internal/metadata/generated_metrics_test.go index 8fac8bd393d8..2a0f4c7791cb 100644 --- a/receiver/haproxyreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/haproxyreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -84,35 +83,65 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.HaproxyAddr.Enabled attrVal, ok := rm.Resource().Attributes().Get("haproxy.addr") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxyAlgo.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.algo") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxyIid.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.iid") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxyPid.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.pid") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxySid.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.sid") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxyType.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.type") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.HaproxyURL.Enabled attrVal, ok = rm.Resource().Attributes().Get("haproxy.url") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 7) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/haproxyreceiver/metadata.yaml b/receiver/haproxyreceiver/metadata.yaml index 302d6f5e7889..c1c3c31f27ad 100644 --- a/receiver/haproxyreceiver/metadata.yaml +++ b/receiver/haproxyreceiver/metadata.yaml @@ -3,24 +3,31 @@ name: haproxyreceiver resource_attributes: haproxy.url: description: The path to the HAProxy socket or HTTP URL. + enabled: true type: string haproxy.pid: description: process id (0 for first instance, 1 for second, ...) + enabled: true type: string haproxy.sid: description: server id (unique inside a proxy) + enabled: true type: string haproxy.iid: description: unique proxy id + enabled: true type: string haproxy.type: description: (0=frontend, 1=backend, 2=server, 3=socket/listener) + enabled: true type: string haproxy.addr: description: address:port or "unix". IPv6 has brackets around the address. + enabled: true type: string haproxy.algo: description: load balancing algorithm + enabled: true type: string attributes: diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics.go index 34e9d6ddca6f..8cf2c8718030 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics.go @@ -49,6 +49,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/cpu metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeState specifies the a value state attribute. type AttributeState int @@ -213,6 +240,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemCPUTime metricSystemCPUTime metricSystemCPUUtilization metricSystemCPUUtilization } @@ -227,11 +255,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemCPUTime: newMetricSystemCPUTime(ms.SystemCPUTime), metricSystemCPUUtilization: newMetricSystemCPUUtilization(ms.SystemCPUUtilization), } @@ -252,12 +288,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -289,8 +325,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { ils.Metrics().EnsureCapacity(mb.metricsCapacity) mb.metricSystemCPUTime.emit(ils.Metrics()) mb.metricSystemCPUUtilization.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics_test.go index 02be19a1547d..556221b421c9 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -75,7 +74,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics.go index 0f025c066ac6..53626dd65c7d 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics.go @@ -69,6 +69,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/disk metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -478,6 +505,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemDiskIo metricSystemDiskIo metricSystemDiskIoTime metricSystemDiskIoTime metricSystemDiskMerged metricSystemDiskMerged @@ -497,11 +525,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemDiskIo: newMetricSystemDiskIo(ms.SystemDiskIo), metricSystemDiskIoTime: newMetricSystemDiskIoTime(ms.SystemDiskIoTime), metricSystemDiskMerged: newMetricSystemDiskMerged(ms.SystemDiskMerged), @@ -527,12 +563,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -569,8 +605,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSystemDiskOperations.emit(ils.Metrics()) mb.metricSystemDiskPendingOperations.emit(ils.Metrics()) mb.metricSystemDiskWeightedIoTime.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics_test.go index 24f83a295d34..954ada7193c8 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -96,7 +95,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics.go index 4fa0746081c8..5a33f9ded3ef 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics.go @@ -53,6 +53,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/filesystem metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeState specifies the a value state attribute. type AttributeState int @@ -259,6 +286,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemFilesystemInodesUsage metricSystemFilesystemInodesUsage metricSystemFilesystemUsage metricSystemFilesystemUsage metricSystemFilesystemUtilization metricSystemFilesystemUtilization @@ -274,11 +302,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemFilesystemInodesUsage: newMetricSystemFilesystemInodesUsage(ms.SystemFilesystemInodesUsage), metricSystemFilesystemUsage: newMetricSystemFilesystemUsage(ms.SystemFilesystemUsage), metricSystemFilesystemUtilization: newMetricSystemFilesystemUtilization(ms.SystemFilesystemUtilization), @@ -300,12 +336,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -338,8 +374,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSystemFilesystemInodesUsage.emit(ils.Metrics()) mb.metricSystemFilesystemUsage.emit(ils.Metrics()) mb.metricSystemFilesystemUtilization.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics_test.go index 89e481f6332e..e3bba49b38b2 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -79,7 +78,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics.go index 8a3b387b6a1a..75e776069404 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics.go @@ -53,6 +53,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/load metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + type metricSystemCPULoadAverage15m struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -208,6 +235,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemCPULoadAverage15m metricSystemCPULoadAverage15m metricSystemCPULoadAverage1m metricSystemCPULoadAverage1m metricSystemCPULoadAverage5m metricSystemCPULoadAverage5m @@ -223,11 +251,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemCPULoadAverage15m: newMetricSystemCPULoadAverage15m(ms.SystemCPULoadAverage15m), metricSystemCPULoadAverage1m: newMetricSystemCPULoadAverage1m(ms.SystemCPULoadAverage1m), metricSystemCPULoadAverage5m: newMetricSystemCPULoadAverage5m(ms.SystemCPULoadAverage5m), @@ -249,12 +285,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -287,8 +323,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSystemCPULoadAverage15m.emit(ils.Metrics()) mb.metricSystemCPULoadAverage1m.emit(ils.Metrics()) mb.metricSystemCPULoadAverage5m.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics_test.go index 796fb338dabf..0bfaa374e801 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/loadscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -80,7 +79,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics.go index 0ccc0667e9aa..e71589dcfe10 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics.go @@ -49,6 +49,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/memory metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeState specifies the a value state attribute. type AttributeState int @@ -207,6 +234,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemMemoryUsage metricSystemMemoryUsage metricSystemMemoryUtilization metricSystemMemoryUtilization } @@ -221,11 +249,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemMemoryUsage: newMetricSystemMemoryUsage(ms.SystemMemoryUsage), metricSystemMemoryUtilization: newMetricSystemMemoryUtilization(ms.SystemMemoryUtilization), } @@ -246,12 +282,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -283,8 +319,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { ils.Metrics().EnsureCapacity(mb.metricsCapacity) mb.metricSystemMemoryUsage.emit(ils.Metrics()) mb.metricSystemMemoryUtilization.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics_test.go index 09d268db9760..9527e33a6bfe 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -75,7 +74,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics.go index bfc53e4e319a..04a184cd748b 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics.go @@ -69,6 +69,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/network metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -497,6 +524,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemNetworkConnections metricSystemNetworkConnections metricSystemNetworkConntrackCount metricSystemNetworkConntrackCount metricSystemNetworkConntrackMax metricSystemNetworkConntrackMax @@ -516,11 +544,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemNetworkConnections: newMetricSystemNetworkConnections(ms.SystemNetworkConnections), metricSystemNetworkConntrackCount: newMetricSystemNetworkConntrackCount(ms.SystemNetworkConntrackCount), metricSystemNetworkConntrackMax: newMetricSystemNetworkConntrackMax(ms.SystemNetworkConntrackMax), @@ -546,12 +582,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -588,8 +624,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSystemNetworkErrors.emit(ils.Metrics()) mb.metricSystemNetworkIo.emit(ils.Metrics()) mb.metricSystemNetworkPackets.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics_test.go index 30025ec39a8b..52cdddf4c025 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -94,7 +93,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics.go index 0c3a2840d401..61978477a4fd 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics.go @@ -57,6 +57,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/paging metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -360,6 +387,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemPagingFaults metricSystemPagingFaults metricSystemPagingOperations metricSystemPagingOperations metricSystemPagingUsage metricSystemPagingUsage @@ -376,11 +404,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemPagingFaults: newMetricSystemPagingFaults(ms.SystemPagingFaults), metricSystemPagingOperations: newMetricSystemPagingOperations(ms.SystemPagingOperations), metricSystemPagingUsage: newMetricSystemPagingUsage(ms.SystemPagingUsage), @@ -403,12 +439,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -442,8 +478,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSystemPagingOperations.emit(ils.Metrics()) mb.metricSystemPagingUsage.emit(ils.Metrics()) mb.metricSystemPagingUtilization.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics_test.go index f6c2e2a4e3a2..1ced79ffc83b 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/pagingscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -83,7 +82,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics.go index 1c628689952b..08d0a5981474 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics.go @@ -49,6 +49,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/processes metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeStatus specifies the a value status attribute. type AttributeStatus int @@ -231,6 +258,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSystemProcessesCount metricSystemProcessesCount metricSystemProcessesCreated metricSystemProcessesCreated } @@ -245,11 +273,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSystemProcessesCount: newMetricSystemProcessesCount(ms.SystemProcessesCount), metricSystemProcessesCreated: newMetricSystemProcessesCreated(ms.SystemProcessesCreated), } @@ -270,12 +306,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -307,8 +343,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { ils.Metrics().EnsureCapacity(mb.metricsCapacity) mb.metricSystemProcessesCount.emit(ils.Metrics()) mb.metricSystemProcessesCreated.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics_test.go index b1bfbfb6107d..15b723b861ea 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -76,7 +75,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md b/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md index 2e210a1a83f0..3eecbfab7273 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md @@ -172,12 +172,12 @@ Process threads count. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| process.command | The command used to launch the process (i.e. the command name). On Linux based systems, can be set to the zeroth string in proc/[pid]/cmdline. On Windows, can be set to the first parameter extracted from GetCommandLineW. | Any Str | -| process.command_line | The full command used to launch the process as a single string representing the full command. On Windows, can be set to the result of GetCommandLineW. Do not set this if you have to assemble it just for monitoring; use process.command_args instead. | Any Str | -| process.executable.name | The name of the process executable. On Linux based systems, can be set to the Name in proc/[pid]/status. On Windows, can be set to the base name of GetProcessImageFileNameW. | Any Str | -| process.executable.path | The full path to the process executable. On Linux based systems, can be set to the target of proc/[pid]/exe. On Windows, can be set to the result of GetProcessImageFileNameW. | Any Str | -| process.owner | The username of the user that owns the process. | Any Str | -| process.parent_pid | Parent Process identifier (PPID). | Any Int | -| process.pid | Process identifier (PID). | Any Int | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| process.command | The command used to launch the process (i.e. the command name). On Linux based systems, can be set to the zeroth string in proc/[pid]/cmdline. On Windows, can be set to the first parameter extracted from GetCommandLineW. | Any Str | true | +| process.command_line | The full command used to launch the process as a single string representing the full command. On Windows, can be set to the result of GetCommandLineW. Do not set this if you have to assemble it just for monitoring; use process.command_args instead. | Any Str | true | +| process.executable.name | The name of the process executable. On Linux based systems, can be set to the Name in proc/[pid]/status. On Windows, can be set to the base name of GetProcessImageFileNameW. | Any Str | true | +| process.executable.path | The full path to the process executable. On Linux based systems, can be set to the target of proc/[pid]/exe. On Windows, can be set to the result of GetProcessImageFileNameW. | Any Str | true | +| process.owner | The username of the user that owns the process. | Any Str | true | +| process.parent_pid | Parent Process identifier (PPID). | Any Int | true | +| process.pid | Process identifier (PID). | Any Int | true | diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics.go index 4c1b7f818255..f5547376d1ff 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics.go @@ -97,6 +97,62 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for hostmetricsreceiver/process metrics. +type ResourceAttributesSettings struct { + ProcessCommand ResourceAttributeSettings `mapstructure:"process.command"` + ProcessCommandLine ResourceAttributeSettings `mapstructure:"process.command_line"` + ProcessExecutableName ResourceAttributeSettings `mapstructure:"process.executable.name"` + ProcessExecutablePath ResourceAttributeSettings `mapstructure:"process.executable.path"` + ProcessOwner ResourceAttributeSettings `mapstructure:"process.owner"` + ProcessParentPid ResourceAttributeSettings `mapstructure:"process.parent_pid"` + ProcessPid ResourceAttributeSettings `mapstructure:"process.pid"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + ProcessCommand: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessCommandLine: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessExecutableName: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessExecutablePath: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessOwner: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessParentPid: ResourceAttributeSettings{ + Enabled: true, + }, + ProcessPid: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeContextSwitchType specifies the a value context_switch_type attribute. type AttributeContextSwitchType int @@ -935,6 +991,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricProcessContextSwitches metricProcessContextSwitches metricProcessCPUTime metricProcessCPUTime metricProcessCPUUtilization metricProcessCPUUtilization @@ -961,6 +1018,13 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { if ms.ProcessMemoryPhysicalUsage.enabledSetByUser { settings.Logger.Warn("[WARNING] `process.memory.physical_usage` should not be configured: The metric is deprecated and will be removed in v0.72.0. Please use `process.memory.usage` instead. See https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/hostmetricsreceiver#transition-to-process-memory-metric-names-aligned-with-opentelemetry-specification for more details.") @@ -972,6 +1036,7 @@ func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, opt startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricProcessContextSwitches: newMetricProcessContextSwitches(ms.ProcessContextSwitches), metricProcessCPUTime: newMetricProcessCPUTime(ms.ProcessCPUTime), metricProcessCPUUtilization: newMetricProcessCPUUtilization(ms.ProcessCPUUtilization), @@ -1004,61 +1069,75 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithProcessCommand sets provided value as "process.command" attribute for current resource. func WithProcessCommand(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("process.command", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessCommand.Enabled { + rm.Resource().Attributes().PutStr("process.command", val) + } } } // WithProcessCommandLine sets provided value as "process.command_line" attribute for current resource. func WithProcessCommandLine(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("process.command_line", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessCommandLine.Enabled { + rm.Resource().Attributes().PutStr("process.command_line", val) + } } } // WithProcessExecutableName sets provided value as "process.executable.name" attribute for current resource. func WithProcessExecutableName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("process.executable.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessExecutableName.Enabled { + rm.Resource().Attributes().PutStr("process.executable.name", val) + } } } // WithProcessExecutablePath sets provided value as "process.executable.path" attribute for current resource. func WithProcessExecutablePath(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("process.executable.path", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessExecutablePath.Enabled { + rm.Resource().Attributes().PutStr("process.executable.path", val) + } } } // WithProcessOwner sets provided value as "process.owner" attribute for current resource. func WithProcessOwner(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("process.owner", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessOwner.Enabled { + rm.Resource().Attributes().PutStr("process.owner", val) + } } } // WithProcessParentPid sets provided value as "process.parent_pid" attribute for current resource. func WithProcessParentPid(val int64) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutInt("process.parent_pid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessParentPid.Enabled { + rm.Resource().Attributes().PutInt("process.parent_pid", val) + } } } // WithProcessPid sets provided value as "process.pid" attribute for current resource. func WithProcessPid(val int64) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutInt("process.pid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ProcessPid.Enabled { + rm.Resource().Attributes().PutInt("process.pid", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1102,8 +1181,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricProcessPagingFaults.emit(ils.Metrics()) mb.metricProcessSignalsPending.emit(ils.Metrics()) mb.metricProcessThreads.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics_test.go index 197ba2990435..825be334269d 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -122,35 +121,65 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.ProcessCommand.Enabled attrVal, ok := rm.Resource().Attributes().Get("process.command") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ProcessCommandLine.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.command_line") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ProcessExecutableName.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.executable.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ProcessExecutablePath.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.executable.path") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ProcessOwner.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.owner") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ProcessParentPid.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.parent_pid") - assert.True(t, ok) - assert.EqualValues(t, 1, attrVal.Int()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, 1, attrVal.Int()) + } + enabled = mb.resourceAttributesSettings.ProcessPid.Enabled attrVal, ok = rm.Resource().Attributes().Get("process.pid") - assert.True(t, ok) - assert.EqualValues(t, 1, attrVal.Int()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, 1, attrVal.Int()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 7) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/metadata.yaml b/receiver/hostmetricsreceiver/internal/scraper/processscraper/metadata.yaml index 5696c0d4dec0..1b22eba2f7b2 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/metadata.yaml +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/metadata.yaml @@ -5,27 +5,32 @@ sem_conv_version: 1.9.0 resource_attributes: process.pid: description: Process identifier (PID). + enabled: true type: int process.parent_pid: description: Parent Process identifier (PPID). + enabled: true type: int process.executable.name: description: >- The name of the process executable. On Linux based systems, can be set to the Name in proc/[pid]/status. On Windows, can be set to the base name of GetProcessImageFileNameW. + enabled: true type: string process.executable.path: description: >- The full path to the process executable. On Linux based systems, can be set to the target of proc/[pid]/exe. On Windows, can be set to the result of GetProcessImageFileNameW. + enabled: true type: string process.command: description: >- The command used to launch the process (i.e. the command name). On Linux based systems, can be set to the zeroth string in proc/[pid]/cmdline. On Windows, can be set to the first parameter extracted from GetCommandLineW. + enabled: true type: string process.command_line: description: >- @@ -33,9 +38,11 @@ resource_attributes: full command. On Windows, can be set to the result of GetCommandLineW. Do not set this if you have to assemble it just for monitoring; use process.command_args instead. + enabled: true type: string process.owner: description: The username of the user that owns the process. + enabled: true type: string attributes: diff --git a/receiver/httpcheckreceiver/internal/metadata/generated_metrics.go b/receiver/httpcheckreceiver/internal/metadata/generated_metrics.go index 095b12d53054..6baec5ee07f3 100644 --- a/receiver/httpcheckreceiver/internal/metadata/generated_metrics.go +++ b/receiver/httpcheckreceiver/internal/metadata/generated_metrics.go @@ -52,6 +52,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for httpcheckreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + type metricHttpcheckDuration struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -216,14 +243,15 @@ func newMetricHttpcheckStatus(settings MetricSettings) metricHttpcheckStatus { // 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. 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 - metricHttpcheckDuration metricHttpcheckDuration - metricHttpcheckError metricHttpcheckError - metricHttpcheckStatus metricHttpcheckStatus + 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 + resourceAttributesSettings ResourceAttributesSettings + metricHttpcheckDuration metricHttpcheckDuration + metricHttpcheckError metricHttpcheckError + metricHttpcheckStatus metricHttpcheckStatus } // metricBuilderOption applies changes to default metrics builder. @@ -236,14 +264,22 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, - metricHttpcheckDuration: newMetricHttpcheckDuration(ms.HttpcheckDuration), - metricHttpcheckError: newMetricHttpcheckError(ms.HttpcheckError), - metricHttpcheckStatus: newMetricHttpcheckStatus(ms.HttpcheckStatus), + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), + metricHttpcheckDuration: newMetricHttpcheckDuration(ms.HttpcheckDuration), + metricHttpcheckError: newMetricHttpcheckError(ms.HttpcheckError), + metricHttpcheckStatus: newMetricHttpcheckStatus(ms.HttpcheckStatus), } for _, op := range options { op(mb) @@ -262,12 +298,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -299,8 +335,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricHttpcheckDuration.emit(ils.Metrics()) mb.metricHttpcheckError.emit(ils.Metrics()) mb.metricHttpcheckStatus.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/httpcheckreceiver/internal/metadata/generated_metrics_test.go b/receiver/httpcheckreceiver/internal/metadata/generated_metrics_test.go index 0457b19cd9aa..8772a0fb8ee8 100644 --- a/receiver/httpcheckreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/httpcheckreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -80,7 +79,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/iisreceiver/documentation.md b/receiver/iisreceiver/documentation.md index 3044ca7cf6d9..ffd2f5c92384 100644 --- a/receiver/iisreceiver/documentation.md +++ b/receiver/iisreceiver/documentation.md @@ -128,7 +128,7 @@ The amount of time the server has been up. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| iis.application_pool | The application pool, which is associated with worker processes of one or more applications. | Any Str | -| iis.site | The site of the web server. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| iis.application_pool | The application pool, which is associated with worker processes of one or more applications. | Any Str | true | +| iis.site | The site of the web server. | Any Str | true | diff --git a/receiver/iisreceiver/internal/metadata/generated_metrics.go b/receiver/iisreceiver/internal/metadata/generated_metrics.go index 6f54ae6d8fc0..bf907019181a 100644 --- a/receiver/iisreceiver/internal/metadata/generated_metrics.go +++ b/receiver/iisreceiver/internal/metadata/generated_metrics.go @@ -88,6 +88,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for iisreceiver metrics. +type ResourceAttributesSettings struct { + IisApplicationPool ResourceAttributeSettings `mapstructure:"iis.application_pool"` + IisSite ResourceAttributeSettings `mapstructure:"iis.site"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + IisApplicationPool: ResourceAttributeSettings{ + Enabled: true, + }, + IisSite: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -782,6 +818,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricIisConnectionActive metricIisConnectionActive metricIisConnectionAnonymous metricIisConnectionAnonymous metricIisConnectionAttemptCount metricIisConnectionAttemptCount @@ -806,11 +843,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricIisConnectionActive: newMetricIisConnectionActive(ms.IisConnectionActive), metricIisConnectionAnonymous: newMetricIisConnectionAnonymous(ms.IisConnectionAnonymous), metricIisConnectionAttemptCount: newMetricIisConnectionAttemptCount(ms.IisConnectionAttemptCount), @@ -841,26 +886,30 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithIisApplicationPool sets provided value as "iis.application_pool" attribute for current resource. func WithIisApplicationPool(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("iis.application_pool", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.IisApplicationPool.Enabled { + rm.Resource().Attributes().PutStr("iis.application_pool", val) + } } } // WithIisSite sets provided value as "iis.site" attribute for current resource. func WithIisSite(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("iis.site", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.IisSite.Enabled { + rm.Resource().Attributes().PutStr("iis.site", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -901,8 +950,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricIisRequestRejected.emit(ils.Metrics()) mb.metricIisThreadActive.emit(ils.Metrics()) mb.metricIisUptime.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/iisreceiver/internal/metadata/generated_metrics_test.go b/receiver/iisreceiver/internal/metadata/generated_metrics_test.go index c7198ee0c884..0b213ca13620 100644 --- a/receiver/iisreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/iisreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -116,15 +115,25 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.IisApplicationPool.Enabled attrVal, ok := rm.Resource().Attributes().Get("iis.application_pool") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.IisSite.Enabled attrVal, ok = rm.Resource().Attributes().Get("iis.site") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 2) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/iisreceiver/metadata.yaml b/receiver/iisreceiver/metadata.yaml index 701736f5e0cb..2e8525103b44 100644 --- a/receiver/iisreceiver/metadata.yaml +++ b/receiver/iisreceiver/metadata.yaml @@ -3,9 +3,11 @@ name: iisreceiver resource_attributes: iis.site: description: The site of the web server. + enabled: true type: string iis.application_pool: description: The application pool, which is associated with worker processes of one or more applications. + enabled: true type: string attributes: diff --git a/receiver/kafkametricsreceiver/internal/metadata/generated_metrics.go b/receiver/kafkametricsreceiver/internal/metadata/generated_metrics.go index efc94c53ef9e..ec84c777fe23 100644 --- a/receiver/kafkametricsreceiver/internal/metadata/generated_metrics.go +++ b/receiver/kafkametricsreceiver/internal/metadata/generated_metrics.go @@ -84,6 +84,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for kafkametricsreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + type metricKafkaBrokers struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -661,6 +688,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricKafkaBrokers metricKafkaBrokers metricKafkaConsumerGroupLag metricKafkaConsumerGroupLag metricKafkaConsumerGroupLagSum metricKafkaConsumerGroupLagSum @@ -684,11 +712,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricKafkaBrokers: newMetricKafkaBrokers(ms.KafkaBrokers), metricKafkaConsumerGroupLag: newMetricKafkaConsumerGroupLag(ms.KafkaConsumerGroupLag), metricKafkaConsumerGroupLagSum: newMetricKafkaConsumerGroupLagSum(ms.KafkaConsumerGroupLagSum), @@ -718,12 +754,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -763,8 +799,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricKafkaPartitionReplicas.emit(ils.Metrics()) mb.metricKafkaPartitionReplicasInSync.emit(ils.Metrics()) mb.metricKafkaTopicPartitions.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/kafkametricsreceiver/internal/metadata/generated_metrics_test.go b/receiver/kafkametricsreceiver/internal/metadata/generated_metrics_test.go index 61e5bc44fa97..1db093aecd4c 100644 --- a/receiver/kafkametricsreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/kafkametricsreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -112,7 +111,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/kubeletstatsreceiver/documentation.md b/receiver/kubeletstatsreceiver/documentation.md index e6927a5279d8..e569c3a30c49 100644 --- a/receiver/kubeletstatsreceiver/documentation.md +++ b/receiver/kubeletstatsreceiver/documentation.md @@ -378,20 +378,20 @@ The inodes used by the filesystem. This may not equal inodes - free because file ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| aws.volume.id | The id of the AWS Volume | Any Str | -| container.id | Container id used to identify container | Any Str | -| fs.type | The filesystem type of the Volume | Any Str | -| gce.pd.name | The name of the persistent disk in GCE | Any Str | -| glusterfs.endpoints.name | The endpoint name that details Glusterfs topology | Any Str | -| glusterfs.path | Glusterfs volume path | Any Str | -| k8s.container.name | Container name used by container runtime | Any Str | -| k8s.namespace.name | The name of the namespace that the pod is running in | Any Str | -| k8s.node.name | The name of the Node | Any Str | -| k8s.persistentvolumeclaim.name | The name of the Persistent Volume Claim | Any Str | -| k8s.pod.name | The name of the Pod | Any Str | -| k8s.pod.uid | The UID of the Pod | Any Str | -| k8s.volume.name | The name of the Volume | Any Str | -| k8s.volume.type | The type of the Volume | Any Str | -| partition | The partition in the Volume | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| aws.volume.id | The id of the AWS Volume | Any Str | true | +| container.id | Container id used to identify container | Any Str | true | +| fs.type | The filesystem type of the Volume | Any Str | true | +| gce.pd.name | The name of the persistent disk in GCE | Any Str | true | +| glusterfs.endpoints.name | The endpoint name that details Glusterfs topology | Any Str | true | +| glusterfs.path | Glusterfs volume path | Any Str | true | +| k8s.container.name | Container name used by container runtime | Any Str | true | +| k8s.namespace.name | The name of the namespace that the pod is running in | Any Str | true | +| k8s.node.name | The name of the Node | Any Str | true | +| k8s.persistentvolumeclaim.name | The name of the Persistent Volume Claim | Any Str | true | +| k8s.pod.name | The name of the Pod | Any Str | true | +| k8s.pod.uid | The UID of the Pod | Any Str | true | +| k8s.volume.name | The name of the Volume | Any Str | true | +| k8s.volume.type | The type of the Volume | Any Str | true | +| partition | The partition in the Volume | Any Str | true | diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go index 03b7ede748c2..76c1e435e055 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/metadata_test.go @@ -229,8 +229,9 @@ func TestSetExtraLabels(t *testing.T) { ro, err := tt.metadata.getExtraResources(stats.PodReference{UID: tt.args[0]}, MetadataLabel(tt.args[1]), tt.args[2]) r := pmetric.NewResourceMetrics() + ras := metadata.DefaultResourceAttributesSettings() for _, op := range ro { - op(r) + op(ras, r) } if tt.wantError == "" { @@ -374,6 +375,7 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { volName := "volume0" + ras := metadata.DefaultResourceAttributesSettings() metadata := NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, &v1.PodList{ Items: []v1.Pod{ { @@ -397,7 +399,7 @@ func TestSetExtraLabelsForVolumeTypes(t *testing.T) { rm := pmetric.NewResourceMetrics() for _, op := range ro { - op(rm) + op(ras, rm) } assert.Equal(t, tt.want, rm.Resource().Attributes().AsRaw()) diff --git a/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go b/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go index fa2e3c396ec3..27fc63c136fa 100644 --- a/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go +++ b/receiver/kubeletstatsreceiver/internal/kubelet/volume_test.go @@ -170,6 +170,7 @@ func TestDetailedPVCLabels(t *testing.T) { Namespace: tt.pod.namespace, }, } + ras := metadata.DefaultResourceAttributesSettings() metadata := NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, &v1.PodList{ Items: []v1.Pod{ { @@ -196,7 +197,7 @@ func TestDetailedPVCLabels(t *testing.T) { volumeResourceMetrics := pmetric.NewResourceMetrics() for _, op := range ro { - op(volumeResourceMetrics) + op(ras, volumeResourceMetrics) } require.Equal(t, tt.want, volumeResourceMetrics.Resource().Attributes().AsRaw()) diff --git a/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics.go b/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics.go index ff05e9113633..a0c94c7b7ed9 100644 --- a/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics.go +++ b/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics.go @@ -208,6 +208,94 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for kubeletstatsreceiver metrics. +type ResourceAttributesSettings struct { + AwsVolumeID ResourceAttributeSettings `mapstructure:"aws.volume.id"` + ContainerID ResourceAttributeSettings `mapstructure:"container.id"` + FsType ResourceAttributeSettings `mapstructure:"fs.type"` + GcePdName ResourceAttributeSettings `mapstructure:"gce.pd.name"` + GlusterfsEndpointsName ResourceAttributeSettings `mapstructure:"glusterfs.endpoints.name"` + GlusterfsPath ResourceAttributeSettings `mapstructure:"glusterfs.path"` + K8sContainerName ResourceAttributeSettings `mapstructure:"k8s.container.name"` + K8sNamespaceName ResourceAttributeSettings `mapstructure:"k8s.namespace.name"` + K8sNodeName ResourceAttributeSettings `mapstructure:"k8s.node.name"` + K8sPersistentvolumeclaimName ResourceAttributeSettings `mapstructure:"k8s.persistentvolumeclaim.name"` + K8sPodName ResourceAttributeSettings `mapstructure:"k8s.pod.name"` + K8sPodUID ResourceAttributeSettings `mapstructure:"k8s.pod.uid"` + K8sVolumeName ResourceAttributeSettings `mapstructure:"k8s.volume.name"` + K8sVolumeType ResourceAttributeSettings `mapstructure:"k8s.volume.type"` + Partition ResourceAttributeSettings `mapstructure:"partition"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + AwsVolumeID: ResourceAttributeSettings{ + Enabled: true, + }, + ContainerID: ResourceAttributeSettings{ + Enabled: true, + }, + FsType: ResourceAttributeSettings{ + Enabled: true, + }, + GcePdName: ResourceAttributeSettings{ + Enabled: true, + }, + GlusterfsEndpointsName: ResourceAttributeSettings{ + Enabled: true, + }, + GlusterfsPath: ResourceAttributeSettings{ + Enabled: true, + }, + K8sContainerName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sNamespaceName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sPersistentvolumeclaimName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sPodName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sPodUID: ResourceAttributeSettings{ + Enabled: true, + }, + K8sVolumeName: ResourceAttributeSettings{ + Enabled: true, + }, + K8sVolumeType: ResourceAttributeSettings{ + Enabled: true, + }, + Partition: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -2326,6 +2414,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricContainerCPUTime metricContainerCPUTime metricContainerCPUUtilization metricContainerCPUUtilization metricContainerFilesystemAvailable metricContainerFilesystemAvailable @@ -2380,11 +2469,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricContainerCPUTime: newMetricContainerCPUTime(ms.ContainerCPUTime), metricContainerCPUUtilization: newMetricContainerCPUUtilization(ms.ContainerCPUUtilization), metricContainerFilesystemAvailable: newMetricContainerFilesystemAvailable(ms.ContainerFilesystemAvailable), @@ -2445,117 +2542,147 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithAwsVolumeID sets provided value as "aws.volume.id" attribute for current resource. func WithAwsVolumeID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("aws.volume.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.AwsVolumeID.Enabled { + rm.Resource().Attributes().PutStr("aws.volume.id", val) + } } } // WithContainerID sets provided value as "container.id" attribute for current resource. func WithContainerID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("container.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ContainerID.Enabled { + rm.Resource().Attributes().PutStr("container.id", val) + } } } // WithFsType sets provided value as "fs.type" attribute for current resource. func WithFsType(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("fs.type", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.FsType.Enabled { + rm.Resource().Attributes().PutStr("fs.type", val) + } } } // WithGcePdName sets provided value as "gce.pd.name" attribute for current resource. func WithGcePdName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("gce.pd.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.GcePdName.Enabled { + rm.Resource().Attributes().PutStr("gce.pd.name", val) + } } } // WithGlusterfsEndpointsName sets provided value as "glusterfs.endpoints.name" attribute for current resource. func WithGlusterfsEndpointsName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("glusterfs.endpoints.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.GlusterfsEndpointsName.Enabled { + rm.Resource().Attributes().PutStr("glusterfs.endpoints.name", val) + } } } // WithGlusterfsPath sets provided value as "glusterfs.path" attribute for current resource. func WithGlusterfsPath(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("glusterfs.path", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.GlusterfsPath.Enabled { + rm.Resource().Attributes().PutStr("glusterfs.path", val) + } } } // WithK8sContainerName sets provided value as "k8s.container.name" attribute for current resource. func WithK8sContainerName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.container.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sContainerName.Enabled { + rm.Resource().Attributes().PutStr("k8s.container.name", val) + } } } // WithK8sNamespaceName sets provided value as "k8s.namespace.name" attribute for current resource. func WithK8sNamespaceName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.namespace.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sNamespaceName.Enabled { + rm.Resource().Attributes().PutStr("k8s.namespace.name", val) + } } } // WithK8sNodeName sets provided value as "k8s.node.name" attribute for current resource. func WithK8sNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sNodeName.Enabled { + rm.Resource().Attributes().PutStr("k8s.node.name", val) + } } } // WithK8sPersistentvolumeclaimName sets provided value as "k8s.persistentvolumeclaim.name" attribute for current resource. func WithK8sPersistentvolumeclaimName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.persistentvolumeclaim.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sPersistentvolumeclaimName.Enabled { + rm.Resource().Attributes().PutStr("k8s.persistentvolumeclaim.name", val) + } } } // WithK8sPodName sets provided value as "k8s.pod.name" attribute for current resource. func WithK8sPodName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.pod.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sPodName.Enabled { + rm.Resource().Attributes().PutStr("k8s.pod.name", val) + } } } // WithK8sPodUID sets provided value as "k8s.pod.uid" attribute for current resource. func WithK8sPodUID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.pod.uid", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sPodUID.Enabled { + rm.Resource().Attributes().PutStr("k8s.pod.uid", val) + } } } // WithK8sVolumeName sets provided value as "k8s.volume.name" attribute for current resource. func WithK8sVolumeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.volume.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sVolumeName.Enabled { + rm.Resource().Attributes().PutStr("k8s.volume.name", val) + } } } // WithK8sVolumeType sets provided value as "k8s.volume.type" attribute for current resource. func WithK8sVolumeType(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("k8s.volume.type", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.K8sVolumeType.Enabled { + rm.Resource().Attributes().PutStr("k8s.volume.type", val) + } } } // WithPartition sets provided value as "partition" attribute for current resource. func WithPartition(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("partition", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.Partition.Enabled { + rm.Resource().Attributes().PutStr("partition", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -2626,8 +2753,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricK8sVolumeInodes.emit(ils.Metrics()) mb.metricK8sVolumeInodesFree.emit(ils.Metrics()) mb.metricK8sVolumeInodesUsed.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics_test.go b/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics_test.go index e590f2a77dc9..a73172e72549 100644 --- a/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/kubeletstatsreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -236,67 +235,129 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.AwsVolumeID.Enabled attrVal, ok := rm.Resource().Attributes().Get("aws.volume.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ContainerID.Enabled attrVal, ok = rm.Resource().Attributes().Get("container.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.FsType.Enabled attrVal, ok = rm.Resource().Attributes().Get("fs.type") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.GcePdName.Enabled attrVal, ok = rm.Resource().Attributes().Get("gce.pd.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.GlusterfsEndpointsName.Enabled attrVal, ok = rm.Resource().Attributes().Get("glusterfs.endpoints.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.GlusterfsPath.Enabled attrVal, ok = rm.Resource().Attributes().Get("glusterfs.path") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sContainerName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.container.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sNamespaceName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.namespace.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sNodeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sPersistentvolumeclaimName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.persistentvolumeclaim.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sPodName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.pod.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sPodUID.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.pod.uid") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sVolumeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.volume.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.K8sVolumeType.Enabled attrVal, ok = rm.Resource().Attributes().Get("k8s.volume.type") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.Partition.Enabled attrVal, ok = rm.Resource().Attributes().Get("partition") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 15) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/kubeletstatsreceiver/metadata.yaml b/receiver/kubeletstatsreceiver/metadata.yaml index 406f4f1fbcad..170f8c90c8a6 100644 --- a/receiver/kubeletstatsreceiver/metadata.yaml +++ b/receiver/kubeletstatsreceiver/metadata.yaml @@ -3,48 +3,63 @@ name: kubeletstatsreceiver resource_attributes: k8s.node.name: description: "The name of the Node" + enabled: true type: string k8s.pod.uid: description: "The UID of the Pod" + enabled: true type: string k8s.pod.name: description: "The name of the Pod" + enabled: true type: string k8s.namespace.name: description: "The name of the namespace that the pod is running in" + enabled: true type: string k8s.container.name: description: "Container name used by container runtime" + enabled: true type: string container.id: description: "Container id used to identify container" + enabled: true type: string k8s.volume.name: description: "The name of the Volume" + enabled: true type: string k8s.volume.type: description: "The type of the Volume" + enabled: true type: string k8s.persistentvolumeclaim.name: description: "The name of the Persistent Volume Claim" + enabled: true type: string aws.volume.id: description: "The id of the AWS Volume" + enabled: true type: string fs.type: description: "The filesystem type of the Volume" + enabled: true type: string partition: description: "The partition in the Volume" + enabled: true type: string gce.pd.name: description: "The name of the persistent disk in GCE" + enabled: true type: string glusterfs.endpoints.name: description: "The endpoint name that details Glusterfs topology" + enabled: true type: string glusterfs.path: description: "Glusterfs volume path" + enabled: true type: string attributes: diff --git a/receiver/memcachedreceiver/internal/metadata/generated_metrics.go b/receiver/memcachedreceiver/internal/metadata/generated_metrics.go index 9e26ee98e1c0..84ca27c12b5b 100644 --- a/receiver/memcachedreceiver/internal/metadata/generated_metrics.go +++ b/receiver/memcachedreceiver/internal/metadata/generated_metrics.go @@ -84,6 +84,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for memcachedreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeCommand specifies the a value command attribute. type AttributeCommand int @@ -802,6 +829,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricMemcachedBytes metricMemcachedBytes metricMemcachedCommands metricMemcachedCommands metricMemcachedConnectionsCurrent metricMemcachedConnectionsCurrent @@ -825,11 +853,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricMemcachedBytes: newMetricMemcachedBytes(ms.MemcachedBytes), metricMemcachedCommands: newMetricMemcachedCommands(ms.MemcachedCommands), metricMemcachedConnectionsCurrent: newMetricMemcachedConnectionsCurrent(ms.MemcachedConnectionsCurrent), @@ -859,12 +895,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -904,8 +940,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricMemcachedOperationHitRatio.emit(ils.Metrics()) mb.metricMemcachedOperations.emit(ils.Metrics()) mb.metricMemcachedThreads.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/memcachedreceiver/internal/metadata/generated_metrics_test.go b/receiver/memcachedreceiver/internal/metadata/generated_metrics_test.go index 79660d3d49a8..e86d57f31e76 100644 --- a/receiver/memcachedreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/memcachedreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -112,7 +111,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/mongodbatlasreceiver/documentation.md b/receiver/mongodbatlasreceiver/documentation.md index a7a427b608a3..5f7725c02a5c 100644 --- a/receiver/mongodbatlasreceiver/documentation.md +++ b/receiver/mongodbatlasreceiver/documentation.md @@ -961,14 +961,14 @@ Aggregate of MongoDB Metrics MAX_SWAP_USAGE_FREE, MAX_SWAP_USAGE_USED ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| mongodb_atlas.db.name | Name of the Database | Any Str | -| mongodb_atlas.disk.partition | Name of a disk partition | Any Str | -| mongodb_atlas.host.name | Hostname of the process | Any Str | -| mongodb_atlas.org_name | Organization Name | Any Str | -| mongodb_atlas.process.id | ID of the process | Any Str | -| mongodb_atlas.process.port | Port process is bound to | Any Str | -| mongodb_atlas.process.type_name | Process type | Any Str | -| mongodb_atlas.project.id | Project ID | Any Str | -| mongodb_atlas.project.name | Project Name | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| mongodb_atlas.db.name | Name of the Database | Any Str | true | +| mongodb_atlas.disk.partition | Name of a disk partition | Any Str | true | +| mongodb_atlas.host.name | Hostname of the process | Any Str | true | +| mongodb_atlas.org_name | Organization Name | Any Str | true | +| mongodb_atlas.process.id | ID of the process | Any Str | true | +| mongodb_atlas.process.port | Port process is bound to | Any Str | true | +| mongodb_atlas.process.type_name | Process type | Any Str | true | +| mongodb_atlas.project.id | Project ID | Any Str | true | +| mongodb_atlas.project.name | Project Name | Any Str | true | diff --git a/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics.go b/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics.go index 2b9e844868d4..54a93d0657bc 100644 --- a/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics.go +++ b/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics.go @@ -292,6 +292,70 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for mongoatlasreceiver metrics. +type ResourceAttributesSettings struct { + MongodbAtlasDbName ResourceAttributeSettings `mapstructure:"mongodb_atlas.db.name"` + MongodbAtlasDiskPartition ResourceAttributeSettings `mapstructure:"mongodb_atlas.disk.partition"` + MongodbAtlasHostName ResourceAttributeSettings `mapstructure:"mongodb_atlas.host.name"` + MongodbAtlasOrgName ResourceAttributeSettings `mapstructure:"mongodb_atlas.org_name"` + MongodbAtlasProcessID ResourceAttributeSettings `mapstructure:"mongodb_atlas.process.id"` + MongodbAtlasProcessPort ResourceAttributeSettings `mapstructure:"mongodb_atlas.process.port"` + MongodbAtlasProcessTypeName ResourceAttributeSettings `mapstructure:"mongodb_atlas.process.type_name"` + MongodbAtlasProjectID ResourceAttributeSettings `mapstructure:"mongodb_atlas.project.id"` + MongodbAtlasProjectName ResourceAttributeSettings `mapstructure:"mongodb_atlas.project.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + MongodbAtlasDbName: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasDiskPartition: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasHostName: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasOrgName: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasProcessID: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasProcessPort: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasProcessTypeName: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasProjectID: ResourceAttributeSettings{ + Enabled: true, + }, + MongodbAtlasProjectName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeAssertType specifies the a value assert_type attribute. type AttributeAssertType int @@ -4224,6 +4288,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricMongodbatlasDbCounts metricMongodbatlasDbCounts metricMongodbatlasDbSize metricMongodbatlasDbSize metricMongodbatlasDiskPartitionIopsAverage metricMongodbatlasDiskPartitionIopsAverage @@ -4299,11 +4364,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricMongodbatlasDbCounts: newMetricMongodbatlasDbCounts(ms.MongodbatlasDbCounts), metricMongodbatlasDbSize: newMetricMongodbatlasDbSize(ms.MongodbatlasDbSize), metricMongodbatlasDiskPartitionIopsAverage: newMetricMongodbatlasDiskPartitionIopsAverage(ms.MongodbatlasDiskPartitionIopsAverage), @@ -4385,75 +4458,93 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithMongodbAtlasDbName sets provided value as "mongodb_atlas.db.name" attribute for current resource. func WithMongodbAtlasDbName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.db.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasDbName.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.db.name", val) + } } } // WithMongodbAtlasDiskPartition sets provided value as "mongodb_atlas.disk.partition" attribute for current resource. func WithMongodbAtlasDiskPartition(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.disk.partition", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasDiskPartition.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.disk.partition", val) + } } } // WithMongodbAtlasHostName sets provided value as "mongodb_atlas.host.name" attribute for current resource. func WithMongodbAtlasHostName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.host.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasHostName.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.host.name", val) + } } } // WithMongodbAtlasOrgName sets provided value as "mongodb_atlas.org_name" attribute for current resource. func WithMongodbAtlasOrgName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.org_name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasOrgName.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.org_name", val) + } } } // WithMongodbAtlasProcessID sets provided value as "mongodb_atlas.process.id" attribute for current resource. func WithMongodbAtlasProcessID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.process.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasProcessID.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.process.id", val) + } } } // WithMongodbAtlasProcessPort sets provided value as "mongodb_atlas.process.port" attribute for current resource. func WithMongodbAtlasProcessPort(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.process.port", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasProcessPort.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.process.port", val) + } } } // WithMongodbAtlasProcessTypeName sets provided value as "mongodb_atlas.process.type_name" attribute for current resource. func WithMongodbAtlasProcessTypeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.process.type_name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasProcessTypeName.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.process.type_name", val) + } } } // WithMongodbAtlasProjectID sets provided value as "mongodb_atlas.project.id" attribute for current resource. func WithMongodbAtlasProjectID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.project.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasProjectID.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.project.id", val) + } } } // WithMongodbAtlasProjectName sets provided value as "mongodb_atlas.project.name" attribute for current resource. func WithMongodbAtlasProjectName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mongodb_atlas.project.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MongodbAtlasProjectName.Enabled { + rm.Resource().Attributes().PutStr("mongodb_atlas.project.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -4545,8 +4636,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricMongodbatlasSystemPagingIoMax.emit(ils.Metrics()) mb.metricMongodbatlasSystemPagingUsageAverage.emit(ils.Metrics()) mb.metricMongodbatlasSystemPagingUsageMax.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics_test.go b/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics_test.go index c0d747070d88..97f62c31d345 100644 --- a/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/mongodbatlasreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -320,43 +319,81 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.MongodbAtlasDbName.Enabled attrVal, ok := rm.Resource().Attributes().Get("mongodb_atlas.db.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasDiskPartition.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.disk.partition") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasHostName.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.host.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasOrgName.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.org_name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasProcessID.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.process.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasProcessPort.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.process.port") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasProcessTypeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.process.type_name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasProjectID.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.project.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.MongodbAtlasProjectName.Enabled attrVal, ok = rm.Resource().Attributes().Get("mongodb_atlas.project.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 9) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/mongodbatlasreceiver/metadata.yaml b/receiver/mongodbatlasreceiver/metadata.yaml index 5de50099e70e..8e127cd79502 100644 --- a/receiver/mongodbatlasreceiver/metadata.yaml +++ b/receiver/mongodbatlasreceiver/metadata.yaml @@ -3,30 +3,39 @@ name: mongoatlasreceiver resource_attributes: mongodb_atlas.org_name: description: Organization Name + enabled: true type: string mongodb_atlas.project.name: description: Project Name + enabled: true type: string mongodb_atlas.project.id: description: Project ID + enabled: true type: string mongodb_atlas.host.name: description: Hostname of the process + enabled: true type: string mongodb_atlas.process.port: description: Port process is bound to + enabled: true type: string mongodb_atlas.process.type_name: description: Process type + enabled: true type: string mongodb_atlas.process.id: description: ID of the process + enabled: true type: string mongodb_atlas.db.name: description: Name of the Database + enabled: true type: string mongodb_atlas.disk.partition: description: Name of a disk partition + enabled: true type: string attributes: diff --git a/receiver/mongodbreceiver/documentation.md b/receiver/mongodbreceiver/documentation.md index dfbe6efe00f5..8b6c7b26038d 100644 --- a/receiver/mongodbreceiver/documentation.md +++ b/receiver/mongodbreceiver/documentation.md @@ -400,6 +400,6 @@ The amount of time that the server has been running. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| database | The name of a database. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| database | The name of a database. | Any Str | true | diff --git a/receiver/mongodbreceiver/internal/metadata/generated_metrics.go b/receiver/mongodbreceiver/internal/metadata/generated_metrics.go index d1d6e88f046c..4bc60bb95456 100644 --- a/receiver/mongodbreceiver/internal/metadata/generated_metrics.go +++ b/receiver/mongodbreceiver/internal/metadata/generated_metrics.go @@ -160,6 +160,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for mongodbreceiver metrics. +type ResourceAttributesSettings struct { + Database ResourceAttributeSettings `mapstructure:"database"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + Database: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeConnectionType specifies the a value connection_type attribute. type AttributeConnectionType int @@ -1984,6 +2016,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricMongodbCacheOperations metricMongodbCacheOperations metricMongodbCollectionCount metricMongodbCollectionCount metricMongodbConnectionCount metricMongodbConnectionCount @@ -2026,11 +2059,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricMongodbCacheOperations: newMetricMongodbCacheOperations(ms.MongodbCacheOperations), metricMongodbCollectionCount: newMetricMongodbCollectionCount(ms.MongodbCollectionCount), metricMongodbConnectionCount: newMetricMongodbConnectionCount(ms.MongodbConnectionCount), @@ -2079,19 +2120,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithDatabase sets provided value as "database" attribute for current resource. func WithDatabase(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("database", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.Database.Enabled { + rm.Resource().Attributes().PutStr("database", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -2150,8 +2193,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricMongodbSessionCount.emit(ils.Metrics()) mb.metricMongodbStorageSize.emit(ils.Metrics()) mb.metricMongodbUptime.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/mongodbreceiver/internal/metadata/generated_metrics_test.go b/receiver/mongodbreceiver/internal/metadata/generated_metrics_test.go index 28f0d1dd518c..6fc3668a1ac5 100644 --- a/receiver/mongodbreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/mongodbreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -180,11 +179,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.Database.Enabled attrVal, ok := rm.Resource().Attributes().Get("database") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/mongodbreceiver/metadata.yaml b/receiver/mongodbreceiver/metadata.yaml index f26dd8035065..2cf63d99c797 100644 --- a/receiver/mongodbreceiver/metadata.yaml +++ b/receiver/mongodbreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: mongodbreceiver resource_attributes: database: description: The name of a database. + enabled: true type: string attributes: diff --git a/receiver/mysqlreceiver/documentation.md b/receiver/mysqlreceiver/documentation.md index f0715815eb13..3621cdbf2096 100644 --- a/receiver/mysqlreceiver/documentation.md +++ b/receiver/mysqlreceiver/documentation.md @@ -599,6 +599,6 @@ The number of hits, misses or overflows for open tables cache lookups. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| mysql.instance.endpoint | Endpoint of the MySQL instance. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| mysql.instance.endpoint | Endpoint of the MySQL instance. | Any Str | true | diff --git a/receiver/mysqlreceiver/internal/metadata/generated_metrics.go b/receiver/mysqlreceiver/internal/metadata/generated_metrics.go index a63fd0f03d69..64245af960a8 100644 --- a/receiver/mysqlreceiver/internal/metadata/generated_metrics.go +++ b/receiver/mysqlreceiver/internal/metadata/generated_metrics.go @@ -214,6 +214,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for mysqlreceiver metrics. +type ResourceAttributesSettings struct { + MysqlInstanceEndpoint ResourceAttributeSettings `mapstructure:"mysql.instance.endpoint"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + MysqlInstanceEndpoint: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeBufferPoolData specifies the a value buffer_pool_data attribute. type AttributeBufferPoolData int @@ -3464,6 +3496,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricMysqlBufferPoolDataPages metricMysqlBufferPoolDataPages metricMysqlBufferPoolLimit metricMysqlBufferPoolLimit metricMysqlBufferPoolOperations metricMysqlBufferPoolOperations @@ -3519,11 +3552,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricMysqlBufferPoolDataPages: newMetricMysqlBufferPoolDataPages(ms.MysqlBufferPoolDataPages), metricMysqlBufferPoolLimit: newMetricMysqlBufferPoolLimit(ms.MysqlBufferPoolLimit), metricMysqlBufferPoolOperations: newMetricMysqlBufferPoolOperations(ms.MysqlBufferPoolOperations), @@ -3585,19 +3626,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithMysqlInstanceEndpoint sets provided value as "mysql.instance.endpoint" attribute for current resource. func WithMysqlInstanceEndpoint(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("mysql.instance.endpoint", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.MysqlInstanceEndpoint.Enabled { + rm.Resource().Attributes().PutStr("mysql.instance.endpoint", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -3669,8 +3712,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricMysqlTableOpenCache.emit(ils.Metrics()) mb.metricMysqlThreads.emit(ils.Metrics()) mb.metricMysqlTmpResources.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/mysqlreceiver/internal/metadata/generated_metrics_test.go b/receiver/mysqlreceiver/internal/metadata/generated_metrics_test.go index f90b34779358..20382b459603 100644 --- a/receiver/mysqlreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/mysqlreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -222,11 +221,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.MysqlInstanceEndpoint.Enabled attrVal, ok := rm.Resource().Attributes().Get("mysql.instance.endpoint") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/mysqlreceiver/metadata.yaml b/receiver/mysqlreceiver/metadata.yaml index 189a5be808fc..0f51b36edb76 100644 --- a/receiver/mysqlreceiver/metadata.yaml +++ b/receiver/mysqlreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: mysqlreceiver resource_attributes: mysql.instance.endpoint: description: Endpoint of the MySQL instance. + enabled: true type: string attributes: diff --git a/receiver/nginxreceiver/internal/metadata/generated_metrics.go b/receiver/nginxreceiver/internal/metadata/generated_metrics.go index f21b3eff6a83..a245788621fe 100644 --- a/receiver/nginxreceiver/internal/metadata/generated_metrics.go +++ b/receiver/nginxreceiver/internal/metadata/generated_metrics.go @@ -56,6 +56,33 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for nginxreceiver metrics. +type ResourceAttributesSettings struct { +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{} +} + // AttributeState specifies the a value state attribute. type AttributeState int @@ -302,6 +329,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricNginxConnectionsAccepted metricNginxConnectionsAccepted metricNginxConnectionsCurrent metricNginxConnectionsCurrent metricNginxConnectionsHandled metricNginxConnectionsHandled @@ -318,11 +346,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricNginxConnectionsAccepted: newMetricNginxConnectionsAccepted(ms.NginxConnectionsAccepted), metricNginxConnectionsCurrent: newMetricNginxConnectionsCurrent(ms.NginxConnectionsCurrent), metricNginxConnectionsHandled: newMetricNginxConnectionsHandled(ms.NginxConnectionsHandled), @@ -345,12 +381,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -383,8 +419,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricNginxConnectionsCurrent.emit(ils.Metrics()) mb.metricNginxConnectionsHandled.emit(ils.Metrics()) mb.metricNginxRequests.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/nginxreceiver/internal/metadata/generated_metrics_test.go b/receiver/nginxreceiver/internal/metadata/generated_metrics_test.go index 7d78ec6ac9ff..a76b90c5035a 100644 --- a/receiver/nginxreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/nginxreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -84,7 +83,9 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + enabledAttrCount := 0 + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 0) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/nsxtreceiver/documentation.md b/receiver/nsxtreceiver/documentation.md index 4783fef3eaa2..e864f6720bf9 100644 --- a/receiver/nsxtreceiver/documentation.md +++ b/receiver/nsxtreceiver/documentation.md @@ -95,9 +95,9 @@ The number of packets which have flowed through the network interface on the nod ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| device.id | The name of the network interface. | Any Str | -| nsxt.node.id | The ID of the NSX Node. | Any Str | -| nsxt.node.name | The name of the NSX Node. | Any Str | -| nsxt.node.type | The type of NSX Node. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| device.id | The name of the network interface. | Any Str | true | +| nsxt.node.id | The ID of the NSX Node. | Any Str | true | +| nsxt.node.name | The name of the NSX Node. | Any Str | true | +| nsxt.node.type | The type of NSX Node. | Any Str | true | diff --git a/receiver/nsxtreceiver/internal/metadata/generated_metrics.go b/receiver/nsxtreceiver/internal/metadata/generated_metrics.go index 1e5ef3924bf3..98788085e4d8 100644 --- a/receiver/nsxtreceiver/internal/metadata/generated_metrics.go +++ b/receiver/nsxtreceiver/internal/metadata/generated_metrics.go @@ -68,6 +68,50 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for nsxtreceiver metrics. +type ResourceAttributesSettings struct { + DeviceID ResourceAttributeSettings `mapstructure:"device.id"` + NsxtNodeID ResourceAttributeSettings `mapstructure:"nsxt.node.id"` + NsxtNodeName ResourceAttributeSettings `mapstructure:"nsxt.node.name"` + NsxtNodeType ResourceAttributeSettings `mapstructure:"nsxt.node.type"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + DeviceID: ResourceAttributeSettings{ + Enabled: true, + }, + NsxtNodeID: ResourceAttributeSettings{ + Enabled: true, + }, + NsxtNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + NsxtNodeType: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeClass specifies the a value class attribute. type AttributeClass int @@ -546,6 +590,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricNsxtNodeCPUUtilization metricNsxtNodeCPUUtilization metricNsxtNodeFilesystemUsage metricNsxtNodeFilesystemUsage metricNsxtNodeFilesystemUtilization metricNsxtNodeFilesystemUtilization @@ -565,11 +610,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricNsxtNodeCPUUtilization: newMetricNsxtNodeCPUUtilization(ms.NsxtNodeCPUUtilization), metricNsxtNodeFilesystemUsage: newMetricNsxtNodeFilesystemUsage(ms.NsxtNodeFilesystemUsage), metricNsxtNodeFilesystemUtilization: newMetricNsxtNodeFilesystemUtilization(ms.NsxtNodeFilesystemUtilization), @@ -595,40 +648,48 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithDeviceID sets provided value as "device.id" attribute for current resource. func WithDeviceID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("device.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.DeviceID.Enabled { + rm.Resource().Attributes().PutStr("device.id", val) + } } } // WithNsxtNodeID sets provided value as "nsxt.node.id" attribute for current resource. func WithNsxtNodeID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("nsxt.node.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.NsxtNodeID.Enabled { + rm.Resource().Attributes().PutStr("nsxt.node.id", val) + } } } // WithNsxtNodeName sets provided value as "nsxt.node.name" attribute for current resource. func WithNsxtNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("nsxt.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.NsxtNodeName.Enabled { + rm.Resource().Attributes().PutStr("nsxt.node.name", val) + } } } // WithNsxtNodeType sets provided value as "nsxt.node.type" attribute for current resource. func WithNsxtNodeType(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("nsxt.node.type", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.NsxtNodeType.Enabled { + rm.Resource().Attributes().PutStr("nsxt.node.type", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -664,8 +725,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricNsxtNodeMemoryUsage.emit(ils.Metrics()) mb.metricNsxtNodeNetworkIo.emit(ils.Metrics()) mb.metricNsxtNodeNetworkPacketCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/nsxtreceiver/internal/metadata/generated_metrics_test.go b/receiver/nsxtreceiver/internal/metadata/generated_metrics_test.go index aca3fbe20e50..e1add3c23983 100644 --- a/receiver/nsxtreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/nsxtreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -96,23 +95,41 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.DeviceID.Enabled attrVal, ok := rm.Resource().Attributes().Get("device.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.NsxtNodeID.Enabled attrVal, ok = rm.Resource().Attributes().Get("nsxt.node.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.NsxtNodeName.Enabled attrVal, ok = rm.Resource().Attributes().Get("nsxt.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.NsxtNodeType.Enabled attrVal, ok = rm.Resource().Attributes().Get("nsxt.node.type") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 4) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/nsxtreceiver/metadata.yaml b/receiver/nsxtreceiver/metadata.yaml index 9f707ceb197e..ada611fc9cfb 100644 --- a/receiver/nsxtreceiver/metadata.yaml +++ b/receiver/nsxtreceiver/metadata.yaml @@ -3,15 +3,19 @@ name: nsxtreceiver resource_attributes: nsxt.node.name: description: The name of the NSX Node. + enabled: true type: string nsxt.node.id: description: The ID of the NSX Node. + enabled: true type: string nsxt.node.type: description: The type of NSX Node. + enabled: true type: string device.id: description: The name of the network interface. + enabled: true type: string attributes: diff --git a/receiver/oracledbreceiver/documentation.md b/receiver/oracledbreceiver/documentation.md index 69550763226d..1ed1729d23a4 100644 --- a/receiver/oracledbreceiver/documentation.md +++ b/receiver/oracledbreceiver/documentation.md @@ -233,6 +233,6 @@ Number of times users manually issue the ROLLBACK statement or an error occurs d ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| oracledb.instance.name | The name of the instance that data is coming from. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| oracledb.instance.name | The name of the instance that data is coming from. | Any Str | true | diff --git a/receiver/oracledbreceiver/internal/metadata/generated_metrics.go b/receiver/oracledbreceiver/internal/metadata/generated_metrics.go index b3e846cbd1ae..fd33a5700c27 100644 --- a/receiver/oracledbreceiver/internal/metadata/generated_metrics.go +++ b/receiver/oracledbreceiver/internal/metadata/generated_metrics.go @@ -142,6 +142,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for oracledbreceiver metrics. +type ResourceAttributesSettings struct { + OracledbInstanceName ResourceAttributeSettings `mapstructure:"oracledb.instance.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + OracledbInstanceName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + type metricOracledbCPUTime struct { data pmetric.Metric // data buffer for generated metric. settings MetricSettings // metric settings provided by user. @@ -1404,6 +1436,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricOracledbCPUTime metricOracledbCPUTime metricOracledbDmlLocksLimit metricOracledbDmlLocksLimit metricOracledbDmlLocksUsage metricOracledbDmlLocksUsage @@ -1441,11 +1474,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricOracledbCPUTime: newMetricOracledbCPUTime(ms.OracledbCPUTime), metricOracledbDmlLocksLimit: newMetricOracledbDmlLocksLimit(ms.OracledbDmlLocksLimit), metricOracledbDmlLocksUsage: newMetricOracledbDmlLocksUsage(ms.OracledbDmlLocksUsage), @@ -1489,19 +1530,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithOracledbInstanceName sets provided value as "oracledb.instance.name" attribute for current resource. func WithOracledbInstanceName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("oracledb.instance.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.OracledbInstanceName.Enabled { + rm.Resource().Attributes().PutStr("oracledb.instance.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1555,8 +1598,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricOracledbTransactionsUsage.emit(ils.Metrics()) mb.metricOracledbUserCommits.emit(ils.Metrics()) mb.metricOracledbUserRollbacks.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/oracledbreceiver/internal/metadata/generated_metrics_test.go b/receiver/oracledbreceiver/internal/metadata/generated_metrics_test.go index 06e3b901fbc6..796d18493c82 100644 --- a/receiver/oracledbreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/oracledbreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -168,11 +167,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.OracledbInstanceName.Enabled attrVal, ok := rm.Resource().Attributes().Get("oracledb.instance.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/oracledbreceiver/metadata.yaml b/receiver/oracledbreceiver/metadata.yaml index 05effb312f33..3b6934cffebc 100644 --- a/receiver/oracledbreceiver/metadata.yaml +++ b/receiver/oracledbreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: oracledbreceiver resource_attributes: oracledb.instance.name: description: The name of the instance that data is coming from. + enabled: true type: string attributes: diff --git a/receiver/postgresqlreceiver/documentation.md b/receiver/postgresqlreceiver/documentation.md index c48a308abb24..14bd7f11a201 100644 --- a/receiver/postgresqlreceiver/documentation.md +++ b/receiver/postgresqlreceiver/documentation.md @@ -275,8 +275,8 @@ This metric requires WAL to be enabled with at least one replica. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| postgresql.database.name | The name of the database. | Any Str | -| postgresql.index.name | The name of the index on a table. | Any Str | -| postgresql.table.name | The schema name followed by the table name. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| postgresql.database.name | The name of the database. | Any Str | true | +| postgresql.index.name | The name of the index on a table. | Any Str | true | +| postgresql.table.name | The schema name followed by the table name. | Any Str | true | diff --git a/receiver/postgresqlreceiver/internal/metadata/generated_metrics.go b/receiver/postgresqlreceiver/internal/metadata/generated_metrics.go index f9d3c4f57440..a02c2a343c25 100644 --- a/receiver/postgresqlreceiver/internal/metadata/generated_metrics.go +++ b/receiver/postgresqlreceiver/internal/metadata/generated_metrics.go @@ -128,6 +128,46 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for postgresqlreceiver metrics. +type ResourceAttributesSettings struct { + PostgresqlDatabaseName ResourceAttributeSettings `mapstructure:"postgresql.database.name"` + PostgresqlIndexName ResourceAttributeSettings `mapstructure:"postgresql.index.name"` + PostgresqlTableName ResourceAttributeSettings `mapstructure:"postgresql.table.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + PostgresqlDatabaseName: ResourceAttributeSettings{ + Enabled: true, + }, + PostgresqlIndexName: ResourceAttributeSettings{ + Enabled: true, + }, + PostgresqlTableName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeBgBufferSource specifies the a value bg_buffer_source attribute. type AttributeBgBufferSource int @@ -1505,6 +1545,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricPostgresqlBackends metricPostgresqlBackends metricPostgresqlBgwriterBuffersAllocated metricPostgresqlBgwriterBuffersAllocated metricPostgresqlBgwriterBuffersWrites metricPostgresqlBgwriterBuffersWrites @@ -1539,11 +1580,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricPostgresqlBackends: newMetricPostgresqlBackends(ms.PostgresqlBackends), metricPostgresqlBgwriterBuffersAllocated: newMetricPostgresqlBgwriterBuffersAllocated(ms.PostgresqlBgwriterBuffersAllocated), metricPostgresqlBgwriterBuffersWrites: newMetricPostgresqlBgwriterBuffersWrites(ms.PostgresqlBgwriterBuffersWrites), @@ -1584,33 +1633,39 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithPostgresqlDatabaseName sets provided value as "postgresql.database.name" attribute for current resource. func WithPostgresqlDatabaseName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("postgresql.database.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.PostgresqlDatabaseName.Enabled { + rm.Resource().Attributes().PutStr("postgresql.database.name", val) + } } } // WithPostgresqlIndexName sets provided value as "postgresql.index.name" attribute for current resource. func WithPostgresqlIndexName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("postgresql.index.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.PostgresqlIndexName.Enabled { + rm.Resource().Attributes().PutStr("postgresql.index.name", val) + } } } // WithPostgresqlTableName sets provided value as "postgresql.table.name" attribute for current resource. func WithPostgresqlTableName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("postgresql.table.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.PostgresqlTableName.Enabled { + rm.Resource().Attributes().PutStr("postgresql.table.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1661,8 +1716,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricPostgresqlTableVacuumCount.emit(ils.Metrics()) mb.metricPostgresqlWalAge.emit(ils.Metrics()) mb.metricPostgresqlWalLag.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/postgresqlreceiver/internal/metadata/generated_metrics_test.go b/receiver/postgresqlreceiver/internal/metadata/generated_metrics_test.go index 3aa13e1525fc..b8f67f094c93 100644 --- a/receiver/postgresqlreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/postgresqlreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -156,19 +155,33 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.PostgresqlDatabaseName.Enabled attrVal, ok := rm.Resource().Attributes().Get("postgresql.database.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.PostgresqlIndexName.Enabled attrVal, ok = rm.Resource().Attributes().Get("postgresql.index.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.PostgresqlTableName.Enabled attrVal, ok = rm.Resource().Attributes().Get("postgresql.table.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 3) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/postgresqlreceiver/metadata.yaml b/receiver/postgresqlreceiver/metadata.yaml index 9d0381702292..75f1ef6bb85a 100644 --- a/receiver/postgresqlreceiver/metadata.yaml +++ b/receiver/postgresqlreceiver/metadata.yaml @@ -3,12 +3,15 @@ name: postgresqlreceiver resource_attributes: postgresql.database.name: description: The name of the database. + enabled: true type: string postgresql.table.name: description: The schema name followed by the table name. + enabled: true type: string postgresql.index.name: description: The name of the index on a table. + enabled: true type: string attributes: diff --git a/receiver/rabbitmqreceiver/documentation.md b/receiver/rabbitmqreceiver/documentation.md index 0b2763208d01..df4fff495ec2 100644 --- a/receiver/rabbitmqreceiver/documentation.md +++ b/receiver/rabbitmqreceiver/documentation.md @@ -68,8 +68,8 @@ The number of messages published to a queue. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| rabbitmq.node.name | The name of the RabbitMQ node. | Any Str | -| rabbitmq.queue.name | The name of the RabbitMQ queue. | Any Str | -| rabbitmq.vhost.name | The name of the RabbitMQ vHost. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| rabbitmq.node.name | The name of the RabbitMQ node. | Any Str | true | +| rabbitmq.queue.name | The name of the RabbitMQ queue. | Any Str | true | +| rabbitmq.vhost.name | The name of the RabbitMQ vHost. | Any Str | true | diff --git a/receiver/rabbitmqreceiver/internal/metadata/generated_metrics.go b/receiver/rabbitmqreceiver/internal/metadata/generated_metrics.go index dd972036b277..be5ae2c1122f 100644 --- a/receiver/rabbitmqreceiver/internal/metadata/generated_metrics.go +++ b/receiver/rabbitmqreceiver/internal/metadata/generated_metrics.go @@ -64,6 +64,46 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for rabbitmqreceiver metrics. +type ResourceAttributesSettings struct { + RabbitmqNodeName ResourceAttributeSettings `mapstructure:"rabbitmq.node.name"` + RabbitmqQueueName ResourceAttributeSettings `mapstructure:"rabbitmq.queue.name"` + RabbitmqVhostName ResourceAttributeSettings `mapstructure:"rabbitmq.vhost.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + RabbitmqNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + RabbitmqQueueName: ResourceAttributeSettings{ + Enabled: true, + }, + RabbitmqVhostName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeMessageState specifies the a value message.state attribute. type AttributeMessageState int @@ -406,6 +446,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricRabbitmqConsumerCount metricRabbitmqConsumerCount metricRabbitmqMessageAcknowledged metricRabbitmqMessageAcknowledged metricRabbitmqMessageCurrent metricRabbitmqMessageCurrent @@ -424,11 +465,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricRabbitmqConsumerCount: newMetricRabbitmqConsumerCount(ms.RabbitmqConsumerCount), metricRabbitmqMessageAcknowledged: newMetricRabbitmqMessageAcknowledged(ms.RabbitmqMessageAcknowledged), metricRabbitmqMessageCurrent: newMetricRabbitmqMessageCurrent(ms.RabbitmqMessageCurrent), @@ -453,33 +502,39 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithRabbitmqNodeName sets provided value as "rabbitmq.node.name" attribute for current resource. func WithRabbitmqNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("rabbitmq.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.RabbitmqNodeName.Enabled { + rm.Resource().Attributes().PutStr("rabbitmq.node.name", val) + } } } // WithRabbitmqQueueName sets provided value as "rabbitmq.queue.name" attribute for current resource. func WithRabbitmqQueueName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("rabbitmq.queue.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.RabbitmqQueueName.Enabled { + rm.Resource().Attributes().PutStr("rabbitmq.queue.name", val) + } } } // WithRabbitmqVhostName sets provided value as "rabbitmq.vhost.name" attribute for current resource. func WithRabbitmqVhostName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("rabbitmq.vhost.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.RabbitmqVhostName.Enabled { + rm.Resource().Attributes().PutStr("rabbitmq.vhost.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -514,8 +569,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricRabbitmqMessageDelivered.emit(ils.Metrics()) mb.metricRabbitmqMessageDropped.emit(ils.Metrics()) mb.metricRabbitmqMessagePublished.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/rabbitmqreceiver/internal/metadata/generated_metrics_test.go b/receiver/rabbitmqreceiver/internal/metadata/generated_metrics_test.go index c42b3773b488..0a1edc3c70d7 100644 --- a/receiver/rabbitmqreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/rabbitmqreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -92,19 +91,33 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.RabbitmqNodeName.Enabled attrVal, ok := rm.Resource().Attributes().Get("rabbitmq.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.RabbitmqQueueName.Enabled attrVal, ok = rm.Resource().Attributes().Get("rabbitmq.queue.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.RabbitmqVhostName.Enabled attrVal, ok = rm.Resource().Attributes().Get("rabbitmq.vhost.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 3) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/rabbitmqreceiver/metadata.yaml b/receiver/rabbitmqreceiver/metadata.yaml index 0afc35d1d7ab..bf156789796d 100644 --- a/receiver/rabbitmqreceiver/metadata.yaml +++ b/receiver/rabbitmqreceiver/metadata.yaml @@ -3,12 +3,15 @@ name: rabbitmqreceiver resource_attributes: rabbitmq.queue.name: description: The name of the RabbitMQ queue. + enabled: true type: string rabbitmq.node.name: description: The name of the RabbitMQ node. + enabled: true type: string rabbitmq.vhost.name: description: The name of the RabbitMQ vHost. + enabled: true type: string attributes: diff --git a/receiver/redisreceiver/documentation.md b/receiver/redisreceiver/documentation.md index 0705b2180b3a..ec7a25e9b649 100644 --- a/receiver/redisreceiver/documentation.md +++ b/receiver/redisreceiver/documentation.md @@ -330,6 +330,6 @@ Redis node's role ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| redis.version | Redis server's version. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| redis.version | Redis server's version. | Any Str | true | diff --git a/receiver/redisreceiver/internal/metadata/generated_metrics.go b/receiver/redisreceiver/internal/metadata/generated_metrics.go index 62c157df9cda..483c548c20f2 100644 --- a/receiver/redisreceiver/internal/metadata/generated_metrics.go +++ b/receiver/redisreceiver/internal/metadata/generated_metrics.go @@ -172,6 +172,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for redisreceiver metrics. +type ResourceAttributesSettings struct { + RedisVersion ResourceAttributeSettings `mapstructure:"redis.version"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + RedisVersion: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeRole specifies the a value role attribute. type AttributeRole int @@ -1915,6 +1947,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricRedisClientsBlocked metricRedisClientsBlocked metricRedisClientsConnected metricRedisClientsConnected metricRedisClientsMaxInputBuffer metricRedisClientsMaxInputBuffer @@ -1960,11 +1993,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricRedisClientsBlocked: newMetricRedisClientsBlocked(ms.RedisClientsBlocked), metricRedisClientsConnected: newMetricRedisClientsConnected(ms.RedisClientsConnected), metricRedisClientsMaxInputBuffer: newMetricRedisClientsMaxInputBuffer(ms.RedisClientsMaxInputBuffer), @@ -2016,19 +2057,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithRedisVersion sets provided value as "redis.version" attribute for current resource. func WithRedisVersion(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("redis.version", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.RedisVersion.Enabled { + rm.Resource().Attributes().PutStr("redis.version", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -2090,8 +2133,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricRedisRole.emit(ils.Metrics()) mb.metricRedisSlavesConnected.emit(ils.Metrics()) mb.metricRedisUptime.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/redisreceiver/internal/metadata/generated_metrics_test.go b/receiver/redisreceiver/internal/metadata/generated_metrics_test.go index 35a3c877ddc9..b2ddb6ece918 100644 --- a/receiver/redisreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/redisreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -196,11 +195,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.RedisVersion.Enabled attrVal, ok := rm.Resource().Attributes().Get("redis.version") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/redisreceiver/metadata.yaml b/receiver/redisreceiver/metadata.yaml index 9954a7294940..71797e9353c1 100644 --- a/receiver/redisreceiver/metadata.yaml +++ b/receiver/redisreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: redisreceiver resource_attributes: redis.version: description: Redis server's version. + enabled: true type: string attributes: diff --git a/receiver/riakreceiver/documentation.md b/receiver/riakreceiver/documentation.md index 7cf2c95850de..44af2d8f46e3 100644 --- a/receiver/riakreceiver/documentation.md +++ b/receiver/riakreceiver/documentation.md @@ -86,6 +86,6 @@ The number of operations performed by vnodes on the node. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| riak.node.name | The name this node uses to identify itself. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| riak.node.name | The name this node uses to identify itself. | Any Str | true | diff --git a/receiver/riakreceiver/internal/metadata/generated_metrics.go b/receiver/riakreceiver/internal/metadata/generated_metrics.go index 16cc22368aa5..e9507647a3bf 100644 --- a/receiver/riakreceiver/internal/metadata/generated_metrics.go +++ b/receiver/riakreceiver/internal/metadata/generated_metrics.go @@ -64,6 +64,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for riakreceiver metrics. +type ResourceAttributesSettings struct { + RiakNodeName ResourceAttributeSettings `mapstructure:"riak.node.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + RiakNodeName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeOperation specifies the a value operation attribute. type AttributeOperation int @@ -440,6 +472,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricRiakMemoryLimit metricRiakMemoryLimit metricRiakNodeOperationCount metricRiakNodeOperationCount metricRiakNodeOperationTimeMean metricRiakNodeOperationTimeMean @@ -458,11 +491,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricRiakMemoryLimit: newMetricRiakMemoryLimit(ms.RiakMemoryLimit), metricRiakNodeOperationCount: newMetricRiakNodeOperationCount(ms.RiakNodeOperationCount), metricRiakNodeOperationTimeMean: newMetricRiakNodeOperationTimeMean(ms.RiakNodeOperationTimeMean), @@ -487,19 +528,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithRiakNodeName sets provided value as "riak.node.name" attribute for current resource. func WithRiakNodeName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("riak.node.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.RiakNodeName.Enabled { + rm.Resource().Attributes().PutStr("riak.node.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -534,8 +577,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricRiakNodeReadRepairCount.emit(ils.Metrics()) mb.metricRiakVnodeIndexOperationCount.emit(ils.Metrics()) mb.metricRiakVnodeOperationCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/riakreceiver/internal/metadata/generated_metrics_test.go b/receiver/riakreceiver/internal/metadata/generated_metrics_test.go index 31910dd599cc..f7d9c07cd9bb 100644 --- a/receiver/riakreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/riakreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -92,11 +91,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.RiakNodeName.Enabled attrVal, ok := rm.Resource().Attributes().Get("riak.node.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/riakreceiver/metadata.yaml b/receiver/riakreceiver/metadata.yaml index 6d23dda5422b..952cf13715e1 100644 --- a/receiver/riakreceiver/metadata.yaml +++ b/receiver/riakreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: riakreceiver resource_attributes: riak.node.name: description: The name this node uses to identify itself. + enabled: true type: string attributes: diff --git a/receiver/saphanareceiver/documentation.md b/receiver/saphanareceiver/documentation.md index b252c2be2538..1b9ed55110a0 100644 --- a/receiver/saphanareceiver/documentation.md +++ b/receiver/saphanareceiver/documentation.md @@ -636,7 +636,7 @@ The time spent executing operations. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| db.system | The type of database system. | Any Str | -| saphana.host | The SAP HANA host. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| db.system | The type of database system. | Any Str | true | +| saphana.host | The SAP HANA host. | Any Str | true | diff --git a/receiver/saphanareceiver/internal/metadata/generated_metrics.go b/receiver/saphanareceiver/internal/metadata/generated_metrics.go index a7f365cf6143..58d15137989e 100644 --- a/receiver/saphanareceiver/internal/metadata/generated_metrics.go +++ b/receiver/saphanareceiver/internal/metadata/generated_metrics.go @@ -222,6 +222,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for saphanareceiver metrics. +type ResourceAttributesSettings struct { + DbSystem ResourceAttributeSettings `mapstructure:"db.system"` + SaphanaHost ResourceAttributeSettings `mapstructure:"saphana.host"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + DbSystem: ResourceAttributeSettings{ + Enabled: true, + }, + SaphanaHost: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeActivePendingRequestState specifies the a value active_pending_request_state attribute. type AttributeActivePendingRequestState int @@ -3135,6 +3171,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSaphanaAlertCount metricSaphanaAlertCount metricSaphanaBackupLatest metricSaphanaBackupLatest metricSaphanaColumnMemoryUsed metricSaphanaColumnMemoryUsed @@ -3192,11 +3229,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSaphanaAlertCount: newMetricSaphanaAlertCount(ms.SaphanaAlertCount), metricSaphanaBackupLatest: newMetricSaphanaBackupLatest(ms.SaphanaBackupLatest), metricSaphanaColumnMemoryUsed: newMetricSaphanaColumnMemoryUsed(ms.SaphanaColumnMemoryUsed), @@ -3260,26 +3305,30 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithDbSystem sets provided value as "db.system" attribute for current resource. func WithDbSystem(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("db.system", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.DbSystem.Enabled { + rm.Resource().Attributes().PutStr("db.system", val) + } } } // WithSaphanaHost sets provided value as "saphana.host" attribute for current resource. func WithSaphanaHost(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("saphana.host", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.SaphanaHost.Enabled { + rm.Resource().Attributes().PutStr("saphana.host", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -3353,8 +3402,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSaphanaVolumeOperationCount.emit(ils.Metrics()) mb.metricSaphanaVolumeOperationSize.emit(ils.Metrics()) mb.metricSaphanaVolumeOperationTime.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/saphanareceiver/internal/metadata/generated_metrics_test.go b/receiver/saphanareceiver/internal/metadata/generated_metrics_test.go index b272a64f027e..9716f3bd5011 100644 --- a/receiver/saphanareceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/saphanareceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -248,15 +247,25 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.DbSystem.Enabled attrVal, ok := rm.Resource().Attributes().Get("db.system") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.SaphanaHost.Enabled attrVal, ok = rm.Resource().Attributes().Get("saphana.host") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 2) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/saphanareceiver/metadata.yaml b/receiver/saphanareceiver/metadata.yaml index 90b4f3160180..d66437d10579 100644 --- a/receiver/saphanareceiver/metadata.yaml +++ b/receiver/saphanareceiver/metadata.yaml @@ -3,9 +3,11 @@ resource_attributes: saphana.host: type: string description: The SAP HANA host. + enabled: true db.system: type: string description: The type of database system. + enabled: true attributes: database: diff --git a/receiver/snowflakereceiver/metadata.yaml b/receiver/snowflakereceiver/metadata.yaml index a50e4c6981d6..1068bcd2a76a 100644 --- a/receiver/snowflakereceiver/metadata.yaml +++ b/receiver/snowflakereceiver/metadata.yaml @@ -4,6 +4,7 @@ name: snowflakereceiver resource_attributes: snowflake.account.name: description: Snowflake account being used by receiver. + enabled: true type: string attributes: diff --git a/receiver/sqlserverreceiver/documentation.md b/receiver/sqlserverreceiver/documentation.md index 1c067d6eec22..19cf96323617 100644 --- a/receiver/sqlserverreceiver/documentation.md +++ b/receiver/sqlserverreceiver/documentation.md @@ -180,6 +180,6 @@ Number of users connected to the SQL Server. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| sqlserver.database.name | The name of the SQL Server database. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| sqlserver.database.name | The name of the SQL Server database. | Any Str | true | diff --git a/receiver/sqlserverreceiver/internal/metadata/generated_metrics.go b/receiver/sqlserverreceiver/internal/metadata/generated_metrics.go index 7880ade932aa..9f1fb306e670 100644 --- a/receiver/sqlserverreceiver/internal/metadata/generated_metrics.go +++ b/receiver/sqlserverreceiver/internal/metadata/generated_metrics.go @@ -120,6 +120,38 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for sqlserverreceiver metrics. +type ResourceAttributesSettings struct { + SqlserverDatabaseName ResourceAttributeSettings `mapstructure:"sqlserver.database.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + SqlserverDatabaseName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributePageOperations specifies the a value page.operations attribute. type AttributePageOperations int @@ -1140,6 +1172,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricSqlserverBatchRequestRate metricSqlserverBatchRequestRate metricSqlserverBatchSQLCompilationRate metricSqlserverBatchSQLCompilationRate metricSqlserverBatchSQLRecompilationRate metricSqlserverBatchSQLRecompilationRate @@ -1172,11 +1205,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricSqlserverBatchRequestRate: newMetricSqlserverBatchRequestRate(ms.SqlserverBatchRequestRate), metricSqlserverBatchSQLCompilationRate: newMetricSqlserverBatchSQLCompilationRate(ms.SqlserverBatchSQLCompilationRate), metricSqlserverBatchSQLRecompilationRate: newMetricSqlserverBatchSQLRecompilationRate(ms.SqlserverBatchSQLRecompilationRate), @@ -1215,19 +1256,21 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithSqlserverDatabaseName sets provided value as "sqlserver.database.name" attribute for current resource. func WithSqlserverDatabaseName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("sqlserver.database.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.SqlserverDatabaseName.Enabled { + rm.Resource().Attributes().PutStr("sqlserver.database.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1276,8 +1319,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricSqlserverTransactionLogShrinkCount.emit(ils.Metrics()) mb.metricSqlserverTransactionLogUsage.emit(ils.Metrics()) mb.metricSqlserverUserConnectionCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/sqlserverreceiver/internal/metadata/generated_metrics_test.go b/receiver/sqlserverreceiver/internal/metadata/generated_metrics_test.go index d1849cb60ebf..7b5f820ada9b 100644 --- a/receiver/sqlserverreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/sqlserverreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -148,11 +147,17 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.SqlserverDatabaseName.Enabled attrVal, ok := rm.Resource().Attributes().Get("sqlserver.database.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 1) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/sqlserverreceiver/metadata.yaml b/receiver/sqlserverreceiver/metadata.yaml index d23f7587f48f..21abe34d6e79 100644 --- a/receiver/sqlserverreceiver/metadata.yaml +++ b/receiver/sqlserverreceiver/metadata.yaml @@ -3,6 +3,7 @@ name: sqlserverreceiver resource_attributes: sqlserver.database.name: description: The name of the SQL Server database. + enabled: true type: string attributes: diff --git a/receiver/vcenterreceiver/documentation.md b/receiver/vcenterreceiver/documentation.md index e7f146c42152..1c9ea620e20f 100644 --- a/receiver/vcenterreceiver/documentation.md +++ b/receiver/vcenterreceiver/documentation.md @@ -391,11 +391,11 @@ As measured over the most recent 20s interval. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| vcenter.cluster.name | The name of the vCenter Cluster. | Any Str | -| vcenter.datastore.name | The name of the vCenter datastore. | Any Str | -| vcenter.host.name | The hostname of the vCenter ESXi host. | Any Str | -| vcenter.resource_pool.name | The name of the resource pool. | Any Str | -| vcenter.vm.id | The instance UUID of the virtual machine. | Any Str | -| vcenter.vm.name | The name of the virtual machine. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| vcenter.cluster.name | The name of the vCenter Cluster. | Any Str | true | +| vcenter.datastore.name | The name of the vCenter datastore. | Any Str | true | +| vcenter.host.name | The hostname of the vCenter ESXi host. | Any Str | true | +| vcenter.resource_pool.name | The name of the resource pool. | Any Str | true | +| vcenter.vm.id | The instance UUID of the virtual machine. | Any Str | true | +| vcenter.vm.name | The name of the virtual machine. | Any Str | true | diff --git a/receiver/vcenterreceiver/internal/metadata/generated_metrics.go b/receiver/vcenterreceiver/internal/metadata/generated_metrics.go index 642d72e82ae4..01d393918e1d 100644 --- a/receiver/vcenterreceiver/internal/metadata/generated_metrics.go +++ b/receiver/vcenterreceiver/internal/metadata/generated_metrics.go @@ -184,6 +184,58 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for vcenterreceiver metrics. +type ResourceAttributesSettings struct { + VcenterClusterName ResourceAttributeSettings `mapstructure:"vcenter.cluster.name"` + VcenterDatastoreName ResourceAttributeSettings `mapstructure:"vcenter.datastore.name"` + VcenterHostName ResourceAttributeSettings `mapstructure:"vcenter.host.name"` + VcenterResourcePoolName ResourceAttributeSettings `mapstructure:"vcenter.resource_pool.name"` + VcenterVMID ResourceAttributeSettings `mapstructure:"vcenter.vm.id"` + VcenterVMName ResourceAttributeSettings `mapstructure:"vcenter.vm.name"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + VcenterClusterName: ResourceAttributeSettings{ + Enabled: true, + }, + VcenterDatastoreName: ResourceAttributeSettings{ + Enabled: true, + }, + VcenterHostName: ResourceAttributeSettings{ + Enabled: true, + }, + VcenterResourcePoolName: ResourceAttributeSettings{ + Enabled: true, + }, + VcenterVMID: ResourceAttributeSettings{ + Enabled: true, + }, + VcenterVMName: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeDiskDirection specifies the a value disk_direction attribute. type AttributeDiskDirection int @@ -2167,6 +2219,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricVcenterClusterCPUEffective metricVcenterClusterCPUEffective metricVcenterClusterCPULimit metricVcenterClusterCPULimit metricVcenterClusterHostCount metricVcenterClusterHostCount @@ -2215,11 +2268,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricVcenterClusterCPUEffective: newMetricVcenterClusterCPUEffective(ms.VcenterClusterCPUEffective), metricVcenterClusterCPULimit: newMetricVcenterClusterCPULimit(ms.VcenterClusterCPULimit), metricVcenterClusterHostCount: newMetricVcenterClusterHostCount(ms.VcenterClusterHostCount), @@ -2274,54 +2335,66 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithVcenterClusterName sets provided value as "vcenter.cluster.name" attribute for current resource. func WithVcenterClusterName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.cluster.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterClusterName.Enabled { + rm.Resource().Attributes().PutStr("vcenter.cluster.name", val) + } } } // WithVcenterDatastoreName sets provided value as "vcenter.datastore.name" attribute for current resource. func WithVcenterDatastoreName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.datastore.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterDatastoreName.Enabled { + rm.Resource().Attributes().PutStr("vcenter.datastore.name", val) + } } } // WithVcenterHostName sets provided value as "vcenter.host.name" attribute for current resource. func WithVcenterHostName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.host.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterHostName.Enabled { + rm.Resource().Attributes().PutStr("vcenter.host.name", val) + } } } // WithVcenterResourcePoolName sets provided value as "vcenter.resource_pool.name" attribute for current resource. func WithVcenterResourcePoolName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.resource_pool.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterResourcePoolName.Enabled { + rm.Resource().Attributes().PutStr("vcenter.resource_pool.name", val) + } } } // WithVcenterVMID sets provided value as "vcenter.vm.id" attribute for current resource. func WithVcenterVMID(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.vm.id", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterVMID.Enabled { + rm.Resource().Attributes().PutStr("vcenter.vm.id", val) + } } } // WithVcenterVMName sets provided value as "vcenter.vm.name" attribute for current resource. func WithVcenterVMName(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("vcenter.vm.name", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.VcenterVMName.Enabled { + rm.Resource().Attributes().PutStr("vcenter.vm.name", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -2386,8 +2459,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricVcenterVMNetworkPacketCount.emit(ils.Metrics()) mb.metricVcenterVMNetworkThroughput.emit(ils.Metrics()) mb.metricVcenterVMNetworkUsage.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/vcenterreceiver/internal/metadata/generated_metrics_test.go b/receiver/vcenterreceiver/internal/metadata/generated_metrics_test.go index ae46a9014a5c..087e38b73a17 100644 --- a/receiver/vcenterreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/vcenterreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -212,31 +211,57 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.VcenterClusterName.Enabled attrVal, ok := rm.Resource().Attributes().Get("vcenter.cluster.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.VcenterDatastoreName.Enabled attrVal, ok = rm.Resource().Attributes().Get("vcenter.datastore.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.VcenterHostName.Enabled attrVal, ok = rm.Resource().Attributes().Get("vcenter.host.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.VcenterResourcePoolName.Enabled attrVal, ok = rm.Resource().Attributes().Get("vcenter.resource_pool.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.VcenterVMID.Enabled attrVal, ok = rm.Resource().Attributes().Get("vcenter.vm.id") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.VcenterVMName.Enabled attrVal, ok = rm.Resource().Attributes().Get("vcenter.vm.name") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 6) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/vcenterreceiver/metadata.yaml b/receiver/vcenterreceiver/metadata.yaml index 81abd4cb2dd1..7891098485db 100644 --- a/receiver/vcenterreceiver/metadata.yaml +++ b/receiver/vcenterreceiver/metadata.yaml @@ -3,21 +3,27 @@ name: vcenterreceiver resource_attributes: vcenter.cluster.name: description: The name of the vCenter Cluster. + enabled: true type: string vcenter.host.name: description: The hostname of the vCenter ESXi host. + enabled: true type: string vcenter.resource_pool.name: description: The name of the resource pool. + enabled: true type: string vcenter.datastore.name: description: The name of the vCenter datastore. + enabled: true type: string vcenter.vm.name: description: The name of the virtual machine. + enabled: true type: string vcenter.vm.id: description: The instance UUID of the virtual machine. + enabled: true type: string attributes: diff --git a/receiver/zookeeperreceiver/documentation.md b/receiver/zookeeperreceiver/documentation.md index 549e9ac9550e..3688d7922566 100644 --- a/receiver/zookeeperreceiver/documentation.md +++ b/receiver/zookeeperreceiver/documentation.md @@ -146,7 +146,7 @@ Number of z-nodes that a ZooKeeper server has in its data tree. ## Resource Attributes -| Name | Description | Values | -| ---- | ----------- | ------ | -| server.state | State of the Zookeeper server (leader, standalone or follower). | Any Str | -| zk.version | Zookeeper version of the instance. | Any Str | +| Name | Description | Values | Enabled | +| ---- | ----------- | ------ | ------- | +| server.state | State of the Zookeeper server (leader, standalone or follower). | Any Str | true | +| zk.version | Zookeeper version of the instance. | Any Str | true | diff --git a/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go b/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go index 00d67f921e60..6037f24b66c2 100644 --- a/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go +++ b/receiver/zookeeperreceiver/internal/metadata/generated_metrics.go @@ -100,6 +100,42 @@ func DefaultMetricsSettings() MetricsSettings { } } +// ResourceAttributeSettings provides common settings for a particular metric. +type ResourceAttributeSettings struct { + Enabled bool `mapstructure:"enabled"` + + enabledProvidedByUser bool +} + +func (ras *ResourceAttributeSettings) Unmarshal(parser *confmap.Conf) error { + if parser == nil { + return nil + } + err := parser.Unmarshal(ras, confmap.WithErrorUnused()) + if err != nil { + return err + } + ras.enabledProvidedByUser = parser.IsSet("enabled") + return nil +} + +// ResourceAttributesSettings provides settings for zookeeperreceiver metrics. +type ResourceAttributesSettings struct { + ServerState ResourceAttributeSettings `mapstructure:"server.state"` + ZkVersion ResourceAttributeSettings `mapstructure:"zk.version"` +} + +func DefaultResourceAttributesSettings() ResourceAttributesSettings { + return ResourceAttributesSettings{ + ServerState: ResourceAttributeSettings{ + Enabled: true, + }, + ZkVersion: ResourceAttributeSettings{ + Enabled: true, + }, + } +} + // AttributeDirection specifies the a value direction attribute. type AttributeDirection int @@ -921,6 +957,7 @@ type MetricsBuilder struct { resourceCapacity int // maximum observed number of resource attributes. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information + resourceAttributesSettings ResourceAttributesSettings metricZookeeperConnectionActive metricZookeeperConnectionActive metricZookeeperDataTreeEphemeralNodeCount metricZookeeperDataTreeEphemeralNodeCount metricZookeeperDataTreeSize metricZookeeperDataTreeSize @@ -948,11 +985,19 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { } } +// WithResourceAttributesSettings sets ResourceAttributeSettings on the metrics builder. +func WithResourceAttributesSettings(ras ResourceAttributesSettings) metricBuilderOption { + return func(mb *MetricsBuilder) { + mb.resourceAttributesSettings = ras + } +} + func NewMetricsBuilder(ms MetricsSettings, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ startTime: pcommon.NewTimestampFromTime(time.Now()), metricsBuffer: pmetric.NewMetrics(), buildInfo: settings.BuildInfo, + resourceAttributesSettings: DefaultResourceAttributesSettings(), metricZookeeperConnectionActive: newMetricZookeeperConnectionActive(ms.ZookeeperConnectionActive), metricZookeeperDataTreeEphemeralNodeCount: newMetricZookeeperDataTreeEphemeralNodeCount(ms.ZookeeperDataTreeEphemeralNodeCount), metricZookeeperDataTreeSize: newMetricZookeeperDataTreeSize(ms.ZookeeperDataTreeSize), @@ -986,26 +1031,30 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) { } // ResourceMetricsOption applies changes to provided resource metrics. -type ResourceMetricsOption func(pmetric.ResourceMetrics) +type ResourceMetricsOption func(ResourceAttributesSettings, pmetric.ResourceMetrics) // WithServerState sets provided value as "server.state" attribute for current resource. func WithServerState(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("server.state", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ServerState.Enabled { + rm.Resource().Attributes().PutStr("server.state", val) + } } } // WithZkVersion sets provided value as "zk.version" attribute for current resource. func WithZkVersion(val string) ResourceMetricsOption { - return func(rm pmetric.ResourceMetrics) { - rm.Resource().Attributes().PutStr("zk.version", val) + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { + if ras.ZkVersion.Enabled { + rm.Resource().Attributes().PutStr("zk.version", val) + } } } // 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(rm pmetric.ResourceMetrics) { + return func(ras ResourceAttributesSettings, rm pmetric.ResourceMetrics) { var dps pmetric.NumberDataPointSlice metrics := rm.ScopeMetrics().At(0).Metrics() for i := 0; i < metrics.Len(); i++ { @@ -1049,8 +1098,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { mb.metricZookeeperSyncPending.emit(ils.Metrics()) mb.metricZookeeperWatchCount.emit(ils.Metrics()) mb.metricZookeeperZnodeCount.emit(ils.Metrics()) + for _, op := range rmo { - op(rm) + op(mb.resourceAttributesSettings, rm) } if ils.Metrics().Len() > 0 { mb.updateCapacity(rm) diff --git a/receiver/zookeeperreceiver/internal/metadata/generated_metrics_test.go b/receiver/zookeeperreceiver/internal/metadata/generated_metrics_test.go index aabac35e87f9..965de2df3d65 100644 --- a/receiver/zookeeperreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/zookeeperreceiver/internal/metadata/generated_metrics_test.go @@ -1,5 +1,4 @@ // Code generated by mdatagen. DO NOT EDIT. - package metadata import ( @@ -128,15 +127,25 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, 1, metrics.ResourceMetrics().Len()) rm := metrics.ResourceMetrics().At(0) attrCount := 0 - attrCount++ + enabledAttrCount := 0 + enabled := mb.resourceAttributesSettings.ServerState.Enabled attrVal, ok := rm.Resource().Attributes().Get("server.state") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + enabled = mb.resourceAttributesSettings.ZkVersion.Enabled attrVal, ok = rm.Resource().Attributes().Get("zk.version") - assert.True(t, ok) - assert.EqualValues(t, "attr-val", attrVal.Str()) - assert.Equal(t, attrCount, rm.Resource().Attributes().Len()) + attrCount++ + assert.Equal(t, enabled, ok) + if enabled { + enabledAttrCount++ + assert.EqualValues(t, "attr-val", attrVal.Str()) + } + assert.Equal(t, enabledAttrCount, rm.Resource().Attributes().Len()) + assert.Equal(t, attrCount, 2) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() diff --git a/receiver/zookeeperreceiver/metadata.yaml b/receiver/zookeeperreceiver/metadata.yaml index b6c1dd0e5569..9d2bae0c5f65 100644 --- a/receiver/zookeeperreceiver/metadata.yaml +++ b/receiver/zookeeperreceiver/metadata.yaml @@ -3,9 +3,11 @@ name: zookeeperreceiver resource_attributes: server.state: description: State of the Zookeeper server (leader, standalone or follower). + enabled: true type: string zk.version: description: Zookeeper version of the instance. + enabled: true type: string attributes: