Skip to content

Commit 6a56478

Browse files
[mdatagen] Add semconv reference for attributes in metadata schema (#14646)
#### Description This PR adds support in mdatagen for defining semantic conventions references for attributes. Updating your attribute in `metadata.yaml`: ```yaml attributes: state: description: Breakdown of memory usage by type. type: string enum: [buffered, cached, inactive, free, slab_reclaimable, slab_unreclaimable, used] semantic_convention: ref: system.md#system-memory-state ``` > [!IMPORTANT] > This work also updates the `semantic_convention: ref:` to use a relative URL, this was done as we want to ensure the same semconv version from the metadata schema is used, otherwise the end user could have a full url in `ref` with one semconv version and the schema version being a different version. A new method has been introduced in mdatagen that builds the semconv url at runtime. Creates this table in the generated docs for that component: #### Attributes | Name | Description | Values | Requirement Level | Semantic Convention | | ---- | ----------- | ------ | ----------------- | ------------------- | | state | Breakdown of memory usage by type. | Str: ``buffered``, ``cached``, ``inactive``, ``free``, ``slab_reclaimable``, ``slab_unreclaimable``, ``used`` | Recommended | [state](https://github.com/open-telemetry/semantic-conventions/blob/v1.38.0/docs/registry/attributes/system.md#system-memory-state) | <!-- Issue number if applicable --> #### Link to tracking issue This work is linked to #13297 but doesn't close it. <!--Describe what testing was performed and which tests were added.--> #### Testing - loader_test.go was updated with attributes for `cpu` and `state`. - Also `cmd/mdatagen/internal/samplereciever` was updated and generate code created which creates tests. <!--Describe the documentation added.--> #### Documentation The documentation template has been updated to reference the semconv definition for attributes if provided in the schema.
1 parent 9860d1b commit 6a56478

26 files changed

Lines changed: 919 additions & 208 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
7+
component: cmd/mdatagen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add semconv reference for attributes
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13297]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [user]

.github/workflows/utils/cspell.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@
464464
"subpackages",
465465
"swiatekm",
466466
"syft",
467+
"systemcputime",
468+
"systemdiskio",
467469
"tailsampling",
468470
"tchannel",
469471
"telemetrygen",
@@ -498,6 +500,7 @@
498500
"unmarshal",
499501
"unmarshalling",
500502
"unmarshalls",
503+
"unreclaimable",
501504
"unredacted",
502505
"unshallow",
503506
"unstarted",

cmd/mdatagen/internal/loader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func LoadMetadata(filePath string) (Metadata, error) {
6262
md.GeneratedPackageName = "metadata"
6363
}
6464

65+
if err := md.expandSemConvRefs(); err != nil {
66+
return md, err
67+
}
68+
6569
if err := md.Validate(); err != nil {
6670
return md, err
6771
}

cmd/mdatagen/internal/loader_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ func TestLoadMetadata(t *testing.T) {
183183
},
184184

185185
Attributes: map[AttributeName]Attribute{
186+
"cpu": {
187+
Description: "Logical CPU number starting at 0.",
188+
Type: ValueType{
189+
ValueType: pcommon.ValueTypeStr,
190+
},
191+
FullName: "cpu",
192+
RequirementLevel: AttributeRequirementLevelRecommended,
193+
},
186194
"enum_attr": {
187195
Description: "Attribute with a known set of string values.",
188196
NameOverride: "",
@@ -275,6 +283,18 @@ func TestLoadMetadata(t *testing.T) {
275283
FullName: "required_string_attr",
276284
RequirementLevel: AttributeRequirementLevelRequired,
277285
},
286+
"state": {
287+
Description: "Breakdown of memory usage by type.",
288+
Enum: []string{"buffered", "cached", "inactive", "free", "slab_reclaimable", "slab_unreclaimable", "used"},
289+
Type: ValueType{
290+
ValueType: pcommon.ValueTypeStr,
291+
},
292+
FullName: "state",
293+
RequirementLevel: AttributeRequirementLevelRecommended,
294+
SemanticConvention: &SemanticConvention{
295+
SemanticConventionRef: "https://github.com/open-telemetry/semantic-conventions/blob/v1.38.0/docs/registry/attributes/system.md#system-memory-state",
296+
},
297+
},
278298
},
279299
Metrics: map[MetricName]Metric{
280300
"default.metric": {
@@ -330,6 +350,7 @@ func TestLoadMetadata(t *testing.T) {
330350
SemanticConvention: &SemanticConvention{SemanticConventionRef: "https://github.com/open-telemetry/semantic-conventions/blob/v1.38.0/docs/system/system-metrics.md#metric-systemcputime"},
331351
Description: "Monotonic cumulative sum int metric enabled by default.",
332352
ExtendedDocumentation: "The metric will be become optional soon.",
353+
Attributes: []AttributeName{"cpu"},
333354
},
334355
Unit: strPtr("s"),
335356
Sum: &Sum{
@@ -338,6 +359,20 @@ func TestLoadMetadata(t *testing.T) {
338359
Mono: Mono{Monotonic: true},
339360
},
340361
},
362+
"system.memory.usage": {
363+
Signal: Signal{
364+
Enabled: true,
365+
Stability: component.StabilityLevelDevelopment,
366+
Description: "Bytes of memory in use.",
367+
Attributes: []AttributeName{"state"},
368+
},
369+
Unit: strPtr("By"),
370+
Sum: &Sum{
371+
MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeInt},
372+
AggregationTemporality: AggregationTemporality{Aggregation: pmetric.AggregationTemporalityCumulative},
373+
Mono: Mono{Monotonic: false},
374+
},
375+
},
341376
"optional.metric": {
342377
Signal: Signal{
343378
Enabled: false,
@@ -629,7 +664,7 @@ func TestLoadMetadata(t *testing.T) {
629664
{
630665
name: "testdata/invalid_metric_semconvref.yaml",
631666
want: Metadata{},
632-
wantErr: "metric \"default.metric\": invalid semantic-conventions URL: want https://github.com/open-telemetry/semantic-conventions/blob/v1.37.2/*#metric-defaultmetric, got \"https://github.com/open-telemetry/semantic-conventions/blob/v1.38.0/docs/system/system-metrics.md#metric-systemcputime\"",
667+
wantErr: "metric \"default.metric\": invalid semantic-conventions URL: want https://github.com/open-telemetry/semantic-conventions/blob/v1.37.2/*#metric-defaultmetric, got \"https://github.com/open-telemetry/semantic-conventions/blob/v1.37.2/docs/system/system-metrics.md#metric-systemcputime\"",
633668
},
634669
{
635670
name: "testdata/no_metric_stability.yaml",
@@ -646,6 +681,16 @@ func TestLoadMetadata(t *testing.T) {
646681
want: Metadata{},
647682
wantErr: "config type must be \"object\", got \"string\"",
648683
},
684+
{
685+
name: "testdata/invalid_metric_semconv_url_full.yaml",
686+
want: Metadata{},
687+
wantErr: "metric \"default.metric\", use relative path for URL, not the full URL",
688+
},
689+
{
690+
name: "testdata/invalid_attribute_semconv_url_full.yaml",
691+
want: Metadata{},
692+
wantErr: "attribute \"used_attr\", use relative path for URL, not the full URL",
693+
},
649694
{
650695
name: "testdata/~~this file doesn't exist~~.yaml",
651696
wantErr: "unable to read the file file:testdata/~~this file doesn't exist~~.yaml",

cmd/mdatagen/internal/metadata.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"go.opentelemetry.io/collector/pdata/pcommon"
1919
)
2020

21+
const semConvURL = "https://github.com/open-telemetry/semantic-conventions/blob"
22+
2123
type Metadata struct {
2224
// Type of the component.
2325
Type string `mapstructure:"type"`
@@ -581,6 +583,8 @@ type Attribute struct {
581583
Warnings Warnings `mapstructure:"warnings"`
582584
// RequirementLevel defines the requirement level of the attribute.
583585
RequirementLevel AttributeRequirementLevel `mapstructure:"requirement_level"`
586+
// The semantic convention reference of the attribute.
587+
SemanticConvention *SemanticConvention `mapstructure:"semantic_convention"`
584588
}
585589

586590
// IsConditional returns true if the attribute is conditionally required.
@@ -785,3 +789,39 @@ type FeatureGate struct {
785789
// ReferenceURL is the URL with contextual information about the feature gate.
786790
ReferenceURL string `mapstructure:"reference_url"`
787791
}
792+
793+
func (md *Metadata) expandSemConvRefs() error {
794+
for k, v := range md.Attributes {
795+
if v.SemanticConvention != nil {
796+
if strings.HasPrefix(v.SemanticConvention.SemanticConventionRef, "http") {
797+
return fmt.Errorf("attribute %q, use relative path for URL, not the full URL", k)
798+
}
799+
url := fmt.Sprintf(
800+
"%s/v%s/docs/registry/attributes/%s",
801+
semConvURL,
802+
md.SemConvVersion,
803+
v.SemanticConvention.SemanticConventionRef,
804+
)
805+
v.SemanticConvention.SemanticConventionRef = url
806+
}
807+
md.Attributes[k] = v
808+
}
809+
810+
for k, v := range md.Metrics {
811+
if v.SemanticConvention != nil {
812+
if strings.HasPrefix(v.SemanticConvention.SemanticConventionRef, "http") {
813+
return fmt.Errorf("metric %q, use relative path for URL, not the full URL", k)
814+
}
815+
url := fmt.Sprintf(
816+
"%s/v%s/docs/%s",
817+
semConvURL,
818+
md.SemConvVersion,
819+
v.SemanticConvention.SemanticConventionRef,
820+
)
821+
v.SemanticConvention.SemanticConventionRef = url
822+
}
823+
md.Metrics[k] = v
824+
}
825+
826+
return nil
827+
}

cmd/mdatagen/internal/sampleconnector/documentation.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ The metric will be become optional soon.
2424
2525
#### Attributes
2626
27-
| Name | Description | Values | Requirement Level |
28-
| ---- | ----------- | ------ | -------- |
29-
| string_attr | Attribute with any string value. | Any Str | Recommended |
30-
| state | Integer attribute with overridden name. | Any Int | Recommended |
31-
| enum_attr | Attribute with a known set of string values. | Str: ``red``, ``green``, ``blue`` | Recommended |
32-
| slice_attr | Attribute with a slice value. | Any Slice | Recommended |
33-
| map_attr | Attribute with a map value. | Any Map | Recommended |
27+
| Name | Description | Values | Requirement Level | Semantic Convention |
28+
| ---- | ----------- | ------ | ----------------- | ------------------- |
29+
| string_attr | Attribute with any string value. | Any Str | Recommended | - |
30+
| state | Integer attribute with overridden name. | Any Int | Recommended | - |
31+
| enum_attr | Attribute with a known set of string values. | Str: ``red``, ``green``, ``blue`` | Recommended | - |
32+
| slice_attr | Attribute with a slice value. | Any Slice | Recommended | - |
33+
| map_attr | Attribute with a map value. | Any Map | Recommended | - |
3434
3535
### default.metric.to_be_removed
3636
@@ -54,13 +54,13 @@ Monotonic cumulative sum int metric with string input_type enabled by default.
5454
5555
#### Attributes
5656
57-
| Name | Description | Values | Requirement Level |
58-
| ---- | ----------- | ------ | -------- |
59-
| string_attr | Attribute with any string value. | Any Str | Recommended |
60-
| state | Integer attribute with overridden name. | Any Int | Recommended |
61-
| enum_attr | Attribute with a known set of string values. | Str: ``red``, ``green``, ``blue`` | Recommended |
62-
| slice_attr | Attribute with a slice value. | Any Slice | Recommended |
63-
| map_attr | Attribute with a map value. | Any Map | Recommended |
57+
| Name | Description | Values | Requirement Level | Semantic Convention |
58+
| ---- | ----------- | ------ | ----------------- | ------------------- |
59+
| string_attr | Attribute with any string value. | Any Str | Recommended | - |
60+
| state | Integer attribute with overridden name. | Any Int | Recommended | - |
61+
| enum_attr | Attribute with a known set of string values. | Str: ``red``, ``green``, ``blue`` | Recommended | - |
62+
| slice_attr | Attribute with a slice value. | Any Slice | Recommended | - |
63+
| map_attr | Attribute with a map value. | Any Map | Recommended | - |
6464
6565
### reaggregate.metric
6666
@@ -72,10 +72,10 @@ Metric for testing spatial reaggregation
7272
7373
#### Attributes
7474
75-
| Name | Description | Values | Requirement Level |
76-
| ---- | ----------- | ------ | -------- |
77-
| string_attr | Attribute with any string value. | Any Str | Recommended |
78-
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended |
75+
| Name | Description | Values | Requirement Level | Semantic Convention |
76+
| ---- | ----------- | ------ | ----------------- | ------------------- |
77+
| string_attr | Attribute with any string value. | Any Str | Recommended | - |
78+
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended | - |
7979
8080
## Optional Metrics
8181
@@ -99,11 +99,11 @@ metrics:
9999
100100
#### Attributes
101101
102-
| Name | Description | Values | Requirement Level |
103-
| ---- | ----------- | ------ | -------- |
104-
| string_attr | Attribute with any string value. | Any Str | Recommended |
105-
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended |
106-
| boolean_attr2 | Another attribute with a boolean value. | Any Bool | Recommended |
102+
| Name | Description | Values | Requirement Level | Semantic Convention |
103+
| ---- | ----------- | ------ | ----------------- | ------------------- |
104+
| string_attr | Attribute with any string value. | Any Str | Recommended | - |
105+
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended | - |
106+
| boolean_attr2 | Another attribute with a boolean value. | Any Bool | Recommended | - |
107107
108108
### optional.metric.empty_unit
109109
@@ -117,23 +117,23 @@ metrics:
117117
118118
#### Attributes
119119
120-
| Name | Description | Values | Requirement Level |
121-
| ---- | ----------- | ------ | -------- |
122-
| string_attr | Attribute with any string value. | Any Str | Recommended |
123-
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended |
120+
| Name | Description | Values | Requirement Level | Semantic Convention |
121+
| ---- | ----------- | ------ | ----------------- | ------------------- |
122+
| string_attr | Attribute with any string value. | Any Str | Recommended | - |
123+
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended | - |
124124
125125
## Resource Attributes
126126
127-
| Name | Description | Values | Enabled |
128-
| ---- | ----------- | ------ | ------- |
129-
| map.resource.attr | Resource attribute with a map value. | Any Map | true |
130-
| optional.resource.attr | Explicitly disabled ResourceAttribute. | Any Str | false |
131-
| slice.resource.attr | Resource attribute with a slice value. | Any Slice | true |
132-
| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true |
133-
| string.resource.attr | Resource attribute with any string value. | Any Str | true |
134-
| string.resource.attr_disable_warning | Resource attribute with any string value. | Any Str | true |
135-
| string.resource.attr_remove_warning | Resource attribute with any string value. | Any Str | false |
136-
| string.resource.attr_to_be_removed | Resource attribute with any string value. | Any Str | true |
127+
| Name | Description | Values | Enabled | Semantic Convention |
128+
| ---- | ----------- | ------ | ------- | ------------------- |
129+
| map.resource.attr | Resource attribute with a map value. | Any Map | true | - |
130+
| optional.resource.attr | Explicitly disabled ResourceAttribute. | Any Str | false | - |
131+
| slice.resource.attr | Resource attribute with a slice value. | Any Slice | true | - |
132+
| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true | - |
133+
| string.resource.attr | Resource attribute with any string value. | Any Str | true | - |
134+
| string.resource.attr_disable_warning | Resource attribute with any string value. | Any Str | true | - |
135+
| string.resource.attr_remove_warning | Resource attribute with any string value. | Any Str | false | - |
136+
| string.resource.attr_to_be_removed | Resource attribute with any string value. | Any Str | true | - |
137137
138138
## Entities
139139

cmd/mdatagen/internal/sampleentityreceiver/documentation.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Current phase of the pod
3030
3131
#### Attributes
3232
33-
| Name | Description | Values | Requirement Level |
34-
| ---- | ----------- | ------ | -------- |
35-
| phase | The phase of the pod (Pending, Running, Succeeded, Failed, Unknown) | Str: ``Pending``, ``Running``, ``Succeeded``, ``Failed``, ``Unknown`` | Recommended |
33+
| Name | Description | Values | Requirement Level | Semantic Convention |
34+
| ---- | ----------- | ------ | ----------------- | ------------------- |
35+
| phase | The phase of the pod (Pending, Running, Succeeded, Failed, Unknown) | Str: ``Pending``, ``Running``, ``Succeeded``, ``Failed``, ``Unknown`` | Recommended | - |
3636
3737
### k8s.replicaset.desired
3838
@@ -44,13 +44,13 @@ Number of desired replicas
4444
4545
## Resource Attributes
4646
47-
| Name | Description | Values | Enabled |
48-
| ---- | ----------- | ------ | ------- |
49-
| k8s.namespace.name | The name of the Kubernetes Namespace | Any Str | true |
50-
| k8s.pod.name | The name of the Kubernetes Pod | Any Str | true |
51-
| k8s.pod.uid | The UID of the Kubernetes Pod | Any Str | true |
52-
| k8s.replicaset.name | The name of the Kubernetes ReplicaSet | Any Str | true |
53-
| k8s.replicaset.uid | The UID of the Kubernetes ReplicaSet | Any Str | true |
47+
| Name | Description | Values | Enabled | Semantic Convention |
48+
| ---- | ----------- | ------ | ------- | ------------------- |
49+
| k8s.namespace.name | The name of the Kubernetes Namespace | Any Str | true | - |
50+
| k8s.pod.name | The name of the Kubernetes Pod | Any Str | true | - |
51+
| k8s.pod.uid | The UID of the Kubernetes Pod | Any Str | true | - |
52+
| k8s.replicaset.name | The name of the Kubernetes ReplicaSet | Any Str | true | - |
53+
| k8s.replicaset.uid | The UID of the Kubernetes ReplicaSet | Any Str | true | - |
5454
5555
## Entities
5656

cmd/mdatagen/internal/sampleprocessor/documentation.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
## Resource Attributes
66

7-
| Name | Description | Values | Enabled |
8-
| ---- | ----------- | ------ | ------- |
9-
| map.resource.attr | Resource attribute with a map value. | Any Map | true |
10-
| optional.resource.attr | Explicitly disabled ResourceAttribute. | Any Str | false |
11-
| slice.resource.attr | Resource attribute with a slice value. | Any Slice | true |
12-
| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true |
13-
| string.resource.attr | Resource attribute with any string value. | Any Str | true |
14-
| string.resource.attr_disable_warning | Resource attribute with any string value. | Any Str | true |
15-
| string.resource.attr_remove_warning | Resource attribute with any string value. | Any Str | false |
16-
| string.resource.attr_to_be_removed | Resource attribute with any string value. | Any Str | true |
7+
| Name | Description | Values | Enabled | Semantic Convention |
8+
| ---- | ----------- | ------ | ------- | ------------------- |
9+
| map.resource.attr | Resource attribute with a map value. | Any Map | true | - |
10+
| optional.resource.attr | Explicitly disabled ResourceAttribute. | Any Str | false | - |
11+
| slice.resource.attr | Resource attribute with a slice value. | Any Slice | true | - |
12+
| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true | - |
13+
| string.resource.attr | Resource attribute with any string value. | Any Str | true | - |
14+
| string.resource.attr_disable_warning | Resource attribute with any string value. | Any Str | true | - |
15+
| string.resource.attr_remove_warning | Resource attribute with any string value. | Any Str | false | - |
16+
| string.resource.attr_to_be_removed | Resource attribute with any string value. | Any Str | true | - |

0 commit comments

Comments
 (0)