Skip to content

Commit 6ac5285

Browse files
[cmd/mdatagen] Document metadata.yaml schema (open-telemetry#22962)
* Start adding the status information to the metadata schema documentation * Remove cmd from the list of possible classes * Fix spelling mistake Co-authored-by: Antoine Toulme <[email protected]> * Update the value used for the development stability status Co-authored-by: Antoine Toulme <[email protected]> * Fix the tests after updating the example metadata file * Document how to use mdatagen and the schema of metadata.yaml * Add the stability levels for connectors * Change the receiver type so the tests pass * Update the generated documentation * Change the syntax used for string array types so that it is valid YAML --------- Co-authored-by: Antoine Toulme <[email protected]>
1 parent 061eb3e commit 6ac5285

File tree

7 files changed

+89
-47
lines changed

7 files changed

+89
-47
lines changed

cmd/mdatagen/README.md

+37-23
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
# Metadata Generator
22

3-
Components must contain a `metadata.yaml` file that documents various aspects of the component including:
3+
Every component's documentation should include a brief description of the component and guidance on how to use it.
4+
There is also some information about the component (or metadata) that should be included to help end-users understand the current state of the component and whether it is right for their use case.
5+
Examples of this metadata about a component are:
46

57
* its stability level
68
* the distributions containing it
79
* the types of pipelines it supports
810
* metrics emitted in the case of a scraping receiver
911

10-
Current examples:
12+
The metadata generator defines a schema for specifying this information to ensure it is complete and well-formed.
13+
The metadata generator is then able to ingest the metadata, validate it against the schema and produce documentation in a standardized format.
14+
An example of how this generated documentation looks can be found in [documentation.md](./documentation.md).
1115

12-
* hostmetricsreceiver scrapers like the [cpuscraper](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/metadata.yaml)
16+
## Using the Metadata Generator
1317

14-
See [metric-metadata.yaml](metric-metadata.yaml) for file format documentation.
18+
In order for a component to benefit from the metadata generator (`mdatagen`) these requirements need to be met:
19+
1. A `metadata.yaml` file containing the metadata needs to be included in the component
20+
2. The component should declare a `go:generate mdatagen` directive which tells `mdatagen` what to generate
1521

16-
If adding a new receiver a `doc.go` file should also be added to trigger the generation. See below for details.
22+
As an example, here is a minimal `metadata.yaml` for the [OTLP receiver](https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver):
23+
```yaml
24+
type: otlp
25+
status:
26+
class: receiver
27+
stability:
28+
beta: [logs]
29+
stable: [metrics, traces]
30+
```
1731
18-
## Generating
32+
Detailed information about the schema of `metadata.yaml` can be found in [metadata-schema.yaml](./metadata-schema.yaml).
1933

20-
`make generate` triggers the following actions:
34+
The `go:generate mdatagen` directive is usually defined in a `doc.go` file in the same package as the component, for example:
35+
```go
36+
//go:generate mdatagen metadata.yaml
2137
22-
1. `cd cmd/mdatagen && $(GOCMD) install .` to install `mdatagen` tool in`GOBIN`
38+
package main
39+
```
2340

24-
2. All `go:generate mdatagen` directives that are usually defined in `doc.go` files of the metric scrapers will be
25-
executed. For example,
26-
[/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/doc.go](../../receiver/hostmetricsreceiver/internal/scraper/cpuscraper/doc.go)
27-
runs `mdatagen` for the [metadata.yaml](../../receiver/hostmetricsreceiver/internal/scraper/cpuscraper/metadata.yaml)
28-
which generates the metrics builder code in
29-
[internal/metadata](../../receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata)
30-
and [documentation.md](../../receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata) with
31-
generated documentation about emitted metrics.
41+
Below are some more examples that can be used for reference:
3242

33-
## Development
43+
* The ElasticSearch receiver has an extensive [metadata.yaml](../../receiver/elasticsearchreceiver/metadata.yaml)
44+
* The host metrics receiver has internal subcomponents, each with their own `metadata.yaml` and `doc.go`. See [cpuscraper](../../receiver/hostmetricsreceiver/internal/scraper/cpuscraper) for example.
3445

35-
In order to introduce support of a new functionality in metadata.yaml:
46+
You can run `cd cmd/mdatagen && $(GOCMD) install .` to install the `mdatagen` tool in `GOBIN` and then run `mdatagen metadata.yaml` to generate documentation for a specific component or you can run `make generate` to generate documentation for all components.
3647

37-
1. Make code changes in the (generating code)[./loader.test] and (templates)[./templates/].
38-
2. Add usage of the new functionality in (metadata.yaml)[./metadata.yaml].
39-
3. Run `make mdatagen-test`.
40-
4. Make sure all tests are passing including (generated tests)[./internal/metadata/generated_metrics_test.go].
41-
5. Run `make generate`.
48+
## Contributing to the Metadata Generator
4249

50+
The code for generating the documentation can be found in [loader.go](./loader.go) and the templates for rendering the documentation can be found in [templates](./templates).
51+
When making updates to the metadata generator or introducing support for new functionality:
52+
53+
1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [./metadata.yaml](metadata.yaml) files reflect the changes.
54+
2. Run `make mdatagen-test`.
55+
3. Make sure all tests are passing including [generated tests](./internal/metadata/generated_metrics_test.go).
56+
4. Run `make generate`.

cmd/mdatagen/documentation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)
22

3-
# test
3+
# file
44

55
## Default Metrics
66

cmd/mdatagen/internal/metadata/generated_config.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/metadata/generated_status.go

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/loader_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@ func Test_loadMetadata(t *testing.T) {
2020
{
2121
name: "metadata.yaml",
2222
want: metadata{
23-
Type: "test",
23+
Type: "file",
2424
SemConvVersion: "1.9.0",
25+
Status: &Status{
26+
Class: "receiver",
27+
Stability: map[string][]string{
28+
"development": {"logs"},
29+
"beta": {"traces"},
30+
"stable": {"metrics"},
31+
},
32+
Distributions: []string{"contrib"},
33+
Warnings: []string{"Any additional information that should be brought to the consumer's attention"},
34+
},
2535
ResourceAttributes: map[attributeName]attribute{
2636
"string.resource.attr": {
2737
Description: "Resource attribute with any string value.",
@@ -146,12 +156,6 @@ func Test_loadMetadata(t *testing.T) {
146156
},
147157
},
148158
ScopeName: "otelcol",
149-
Status: &Status{
150-
Class: "receiver",
151-
Stability: map[string][]string{
152-
"development": {"metrics"},
153-
},
154-
},
155159
},
156160
},
157161
{

cmd/mdatagen/metric-metadata.yaml renamed to cmd/mdatagen/metadata-schema.yaml

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
# Required: type of the receiver.
1+
# Required: The type of the component - Usually the name. The type and class combined uniquely identify the component (eg. receiver/otlp) or subcomponent (eg. receiver/hostmetricsreceiver/cpu)
22
type:
33

4+
# Required for components (Optional for subcomponents): A high-level view of the development status and use of this component
5+
status:
6+
# Required: The class of the component (For example receiver)
7+
class: <receiver|processor|exporter|connector|extension>
8+
# Required: The stability of the component - See https://github.com/open-telemetry/opentelemetry-collector#stability-levels
9+
stability:
10+
development: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
11+
alpha: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
12+
beta: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
13+
stable: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
14+
deprecated: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
15+
unmaintained: [<metrics|traces|logs|traces_to_metrics|metrics_to_metrics|logs_to_metrics>]
16+
# Optional: The distributions that this component is bundled with (For example core or contrib). See statusdata.go for a list of common distros.
17+
distributions: [string]
18+
# Optional: A list of warnings that should be brought to the attention of users looking to use this component
19+
warnings: [string]
20+
421
# Optional: OTel Semantic Conventions version that will be associated with the scraped metrics.
522
# This attribute should be set for metrics compliant with OTel Semantic Conventions.
623
sem_conv_version: 1.9.0
@@ -13,7 +30,7 @@ resource_attributes:
1330
# Required: description of the attribute.
1431
description:
1532
# Optional: array of attribute values if they are static values (currently, only string type is supported).
16-
enum:
33+
enum: [string]
1734
# Required: attribute value type.
1835
type: <string|int|double|bool|bytes|slice|map>
1936

@@ -27,16 +44,16 @@ attributes:
2744
# Required: description of the attribute.
2845
description:
2946
# Optional: array of attribute values if they are static values (currently, only string type is supported).
30-
enum:
47+
enum: [string]
3148
# Required: attribute value type.
3249
type: <string|int|double|bool|bytes|slice|map>
3350

34-
# Required: map of metric names with the key being the metric name and value
51+
# Optional: map of metric names with the key being the metric name and value
3552
# being described below.
3653
metrics:
3754
<metric.name>:
3855
# Required: whether the metric is collected by default.
39-
enabled: # true | false
56+
enabled: bool
4057
# Required: metric description.
4158
description:
4259
# Optional: extended documentation of the metric.
@@ -57,11 +74,11 @@ metrics:
5774
# Required: metric type with its settings.
5875
<sum|gauge>:
5976
# Required for sum and gauge metrics: type of number data point values.
60-
value_type: # int | double
77+
value_type: <int|double>
6178
# Required for sum metric: whether the metric is monotonic (no negative delta values).
62-
monotonic: # true | false
79+
monotonic: bool
6380
# Required for sum metric: whether reported values incorporate previous measurements
6481
# (cumulative) or not (delta).
65-
aggregation: # delta | cumulative
82+
aggregation: <delta|cumulative>
6683
# Optional: array of attributes that were defined in the attributes section that are emitted by this metric.
67-
attributes:
84+
attributes: [string]

cmd/mdatagen/metadata.yaml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Sample metric metadata file with all available configurations.
22

3-
type: test
3+
type: file
44

55
sem_conv_version: 1.9.0
66

77
status:
88
class: receiver
99
stability:
10-
development: [metrics]
10+
development: [logs]
11+
beta: [traces]
12+
stable: [metrics]
13+
distributions: [contrib]
14+
warnings:
15+
- Any additional information that should be brought to the consumer's attention
1116

1217
resource_attributes:
1318
string.resource.attr:

0 commit comments

Comments
 (0)