| 1 |
# Metadata Generator |
| 2 |
|
| 3 |
<!-- status autogenerated section --> |
| 4 |
| Status | | |
| 5 |
| ------------- |-----------| |
| 6 |
| Stability | [alpha]: metrics | |
| 7 |
| Issues | [](https://github.com/open-telemetry/opentelemetry-collector/issues?q=is%3Aopen+is%3Aissue+label%3Acmd%2Fmdatagen) [](https://github.com/open-telemetry/opentelemetry-collector/issues?q=is%3Aclosed+is%3Aissue+label%3Acmd%2Fmdatagen) | |
| 8 |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@dmitryax](https://www.github.com/dmitryax) | |
| 9 |
|
| 10 |
[alpha]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#alpha |
| 11 |
<!-- end autogenerated section --> |
| 12 |
|
| 13 |
Every component's documentation should include a brief description of the component and guidance on how to use it. |
| 14 |
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. |
| 15 |
Examples of this metadata about a component are: |
| 16 |
|
| 17 |
* its stability level |
| 18 |
* the distributions containing it |
| 19 |
* the types of pipelines it supports |
| 20 |
* metrics emitted in the case of a scraping receiver, a scraper, or a connector |
| 21 |
|
| 22 |
The metadata generator defines a schema for specifying this information to ensure it is complete and well-formed. |
| 23 |
The metadata generator is then able to ingest the metadata, validate it against the schema and produce documentation in a standardized format. |
| 24 |
An example of how this generated documentation looks can be found in [documentation.md](https://github.com/open-telemetry/opentelemetry-collector/blob/main/cmd/mdatagen/internal/samplereceiver/documentation.md). |
| 25 |
|
| 26 |
## Using the Metadata Generator |
| 27 |
|
| 28 |
In order for a component to benefit from the metadata generator (`mdatagen`) these requirements need to be met: |
| 29 |
1. A yaml file containing the metadata that needs to be included in the component |
| 30 |
2. The component should declare a `go:generate mdatagen` directive which tells `mdatagen` what to generate |
| 31 |
|
| 32 |
As an example, here is a minimal `metadata.yaml` for the [OTLP receiver](https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver): |
| 33 |
```yaml |
| 34 |
type: otlp |
| 35 |
status: |
| 36 |
class: receiver |
| 37 |
stability: |
| 38 |
beta: [logs] |
| 39 |
stable: [metrics, traces] |
| 40 |
``` |
| 41 |
|
| 42 |
Detailed information about the schema of `metadata.yaml` can be found in [metadata-schema.yaml](./metadata-schema.yaml). |
| 43 |
|
| 44 |
The `go:generate mdatagen` directive is usually defined in a `doc.go` file in the same package as the component, for example: |
| 45 |
```go |
| 46 |
//go:generate mdatagen metadata.yaml |
| 47 |
|
| 48 |
package main |
| 49 |
``` |
| 50 |
|
| 51 |
Below are some more examples that can be used for reference: |
| 52 |
|
| 53 |
* The ElasticSearch receiver has an extensive [metadata.yaml](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/elasticsearchreceiver/metadata.yaml) |
| 54 |
* The host metrics receiver has internal subcomponents, each with their own `metadata.yaml` and `doc.go`. See [cpuscraper](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/hostmetricsreceiver/internal/scraper/cpuscraper) for example. |
| 55 |
|
| 56 |
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. |
| 57 |
|
| 58 |
### Generate multiple metadata packages |
| 59 |
|
| 60 |
By default, `mdatagen` will generate a package called `metadata` in the `internal` directory. If you want to generate a package with a different name, you can use the `generated_package_name` configuration field to provide an alternate name. |
| 61 |
|
| 62 |
```yaml |
| 63 |
type: otlp |
| 64 |
generated_package_name: customname |
| 65 |
status: |
| 66 |
class: receiver |
| 67 |
stability: |
| 68 |
beta: [logs] |
| 69 |
stable: [metrics, traces] |
| 70 |
``` |
| 71 |
|
| 72 |
The most common scenario for this would be making major changes to a receiver's metadata without breaking what exists. In this scenario, `mdatagen` could produce separate packages for different metadata specs in the same receiver: |
| 73 |
|
| 74 |
```go |
| 75 |
//go:generate mdatagen metadata.yaml |
| 76 |
//go:generate mdatagen custom.yaml |
| 77 |
|
| 78 |
package main |
| 79 |
``` |
| 80 |
|
| 81 |
With two different packages generated, the behaviour for which metadata is used can be easily controlled via featuregate or a similar mechanism. |
| 82 |
|
| 83 |
## Contributing to the Metadata Generator |
| 84 |
|
| 85 |
The code for generating the documentation can be found in [loader.go](./internal/loader.go) and the templates for rendering the documentation can be found in [templates](./internal/templates). |
| 86 |
When making updates to the metadata generator or introducing support for new functionality: |
| 87 |
|
| 88 |
1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [metadata.yaml](./metadata.yaml) files reflect the changes. |
| 89 |
2. Run `make mdatagen-test`. |
| 90 |
3. Make sure all tests are passing including [generated tests](./internal/samplereceiver/internal/metadata/generated_metrics_test.go). |
| 91 |
4. Run `make generate`. |