Skip to content

Commit

Permalink
[chore] Move mdatagen code to internal, no public API needed (open-te…
Browse files Browse the repository at this point in the history
…lemetry#11181)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Sep 16, 2024
1 parent 8db93c2 commit 2c0941f
Show file tree
Hide file tree
Showing 83 changed files with 120 additions and 117 deletions.
4 changes: 2 additions & 2 deletions cmd/mdatagen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ You can run `cd cmd/mdatagen && $(GOCMD) install .` to install the `mdatagen` to

## Contributing to the Metadata Generator

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).
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).
When making updates to the metadata generator or introducing support for new functionality:

1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [./metadata.yaml](metadata.yaml) files reflect the changes.
1. Ensure the [metadata-schema.yaml](./metadata-schema.yaml) and [metadata.yaml](./metadata.yaml) files reflect the changes.
2. Run `make mdatagen-test`.
3. Make sure all tests are passing including [generated tests](./internal/samplereceiver/internal/metadata/generated_metrics_test.go).
4. Run `make generate`.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main
package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"

import "embed"

// templateFS ensures that the files needed
// TemplateFS ensures that the files needed
// to generate metadata as an embedded filesystem since
// `go get` doesn't require these files to be downloaded.
//
//go:embed templates/*.tmpl templates/testdata/*.tmpl
var templateFS embed.FS
var TemplateFS embed.FS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main
package internal

import (
"io/fs"
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
}
count = 0
)
assert.NoError(t, fs.WalkDir(templateFS, ".", func(path string, d fs.DirEntry, _ error) error {
assert.NoError(t, fs.WalkDir(TemplateFS, ".", func(path string, d fs.DirEntry, _ error) error {
if d != nil && d.IsDir() {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/mdatagen/lint.go → cmd/mdatagen/internal/lint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main
package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"

import (
"errors"
Expand All @@ -11,8 +11,8 @@ import (
"go.opentelemetry.io/collector/cmd/mdatagen/third_party/golint"
)

// formatIdentifier variable in a go-safe way
func formatIdentifier(s string, exported bool) (string, error) {
// FormatIdentifier variable in a go-safe way
func FormatIdentifier(s string, exported bool) (string, error) {
if s == "" {
return "", errors.New("string cannot be empty")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main
package internal

import (
"testing"
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestFormatIdentifier(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := formatIdentifier(tt.input, tt.exported)
got, err := FormatIdentifier(tt.input, tt.exported)

if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
Expand Down
64 changes: 32 additions & 32 deletions cmd/mdatagen/loader.go → cmd/mdatagen/internal/loader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package main
package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"

import (
"context"
Expand All @@ -19,24 +19,24 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
)

type metricName string
type MetricName string

func (mn metricName) Render() (string, error) {
return formatIdentifier(string(mn), true)
func (mn MetricName) Render() (string, error) {
return FormatIdentifier(string(mn), true)
}

func (mn metricName) RenderUnexported() (string, error) {
return formatIdentifier(string(mn), false)
func (mn MetricName) RenderUnexported() (string, error) {
return FormatIdentifier(string(mn), false)
}

type attributeName string
type AttributeName string

func (mn attributeName) Render() (string, error) {
return formatIdentifier(string(mn), true)
func (mn AttributeName) Render() (string, error) {
return FormatIdentifier(string(mn), true)
}

func (mn attributeName) RenderUnexported() (string, error) {
return formatIdentifier(string(mn), false)
func (mn AttributeName) RenderUnexported() (string, error) {
return FormatIdentifier(string(mn), false)
}

// ValueType defines an attribute value type.
Expand Down Expand Up @@ -97,7 +97,7 @@ func (mvt ValueType) Primitive() string {
}
}

type metric struct {
type Metric struct {
// Enabled defines whether the metric is enabled by default.
Enabled bool `mapstructure:"enabled"`

Expand Down Expand Up @@ -126,21 +126,21 @@ type metric struct {
Histogram *histogram `mapstructure:"histogram,omitempty"`

// Attributes is the list of attributes that the metric emits.
Attributes []attributeName `mapstructure:"attributes"`
Attributes []AttributeName `mapstructure:"attributes"`

// Level specifies the minimum `configtelemetry.Level` for which
// the metric will be emitted. This only applies to internal telemetry
// configuration.
Level configtelemetry.Level `mapstructure:"level"`
}

func (m *metric) Unmarshal(parser *confmap.Conf) error {
func (m *Metric) Unmarshal(parser *confmap.Conf) error {
if !parser.IsSet("enabled") {
return errors.New("missing required field: `enabled`")
}
return parser.Unmarshal(m)
}
func (m metric) Data() MetricData {
func (m Metric) Data() MetricData {
if m.Sum != nil {
return m.Sum
}
Expand All @@ -162,7 +162,7 @@ type warnings struct {
IfConfigured string `mapstructure:"if_configured"`
}

type attribute struct {
type Attribute struct {
// Description describes the purpose of the attribute.
Description string `mapstructure:"description"`
// NameOverride can be used to override the attribute name.
Expand All @@ -178,20 +178,20 @@ type attribute struct {
// Type is an attribute type.
Type ValueType `mapstructure:"type"`
// FullName is the attribute name populated from the map key.
FullName attributeName `mapstructure:"-"`
FullName AttributeName `mapstructure:"-"`
// Warnings that will be shown to user under specified conditions.
Warnings warnings `mapstructure:"warnings"`
}

// Name returns actual name of the attribute that is set on the metric after applying NameOverride.
func (a attribute) Name() attributeName {
func (a Attribute) Name() AttributeName {
if a.NameOverride != "" {
return attributeName(a.NameOverride)
return AttributeName(a.NameOverride)
}
return a.FullName
}

func (a attribute) TestValue() string {
func (a Attribute) TestValue() string {
if a.Enum != nil {
return fmt.Sprintf(`"%s"`, a.Enum[0])
}
Expand Down Expand Up @@ -239,7 +239,7 @@ type tests struct {

type telemetry struct {
Level configtelemetry.Level `mapstructure:"level"`
Metrics map[metricName]metric `mapstructure:"metrics"`
Metrics map[MetricName]Metric `mapstructure:"metrics"`
}

func (t telemetry) Levels() map[string]interface{} {
Expand All @@ -250,7 +250,7 @@ func (t telemetry) Levels() map[string]interface{} {
return levels
}

type metadata struct {
type Metadata struct {
// Type of the component.
Type string `mapstructure:"type"`
// Type of the parent component (applicable to subcomponents).
Expand All @@ -262,11 +262,11 @@ type metadata struct {
// SemConvVersion is a version number of OpenTelemetry semantic conventions applied to the scraped metrics.
SemConvVersion string `mapstructure:"sem_conv_version"`
// ResourceAttributes that can be emitted by the component.
ResourceAttributes map[attributeName]attribute `mapstructure:"resource_attributes"`
ResourceAttributes map[AttributeName]Attribute `mapstructure:"resource_attributes"`
// Attributes emitted by one or more metrics.
Attributes map[attributeName]attribute `mapstructure:"attributes"`
Attributes map[AttributeName]Attribute `mapstructure:"attributes"`
// Metrics that can be emitted by the component.
Metrics map[metricName]metric `mapstructure:"metrics"`
Metrics map[MetricName]Metric `mapstructure:"metrics"`
// GithubProject is the project where the component README lives in the format of org/repo, defaults to open-telemetry/opentelemetry-collector-contrib
GithubProject string `mapstructure:"github_project"`
// ScopeName of the metrics emitted by the component.
Expand All @@ -277,31 +277,31 @@ type metadata struct {
Tests tests `mapstructure:"tests"`
}

func setAttributesFullName(attrs map[attributeName]attribute) {
func setAttributesFullName(attrs map[AttributeName]Attribute) {
for k, v := range attrs {
v.FullName = k
attrs[k] = v
}
}

type templateContext struct {
metadata
type TemplateContext struct {
Metadata
// Package name for generated code.
Package string
}

func loadMetadata(filePath string) (metadata, error) {
func LoadMetadata(filePath string) (Metadata, error) {
cp, err := fileprovider.NewFactory().Create(confmaptest.NewNopProviderSettings()).Retrieve(context.Background(), "file:"+filePath, nil)
if err != nil {
return metadata{}, err
return Metadata{}, err
}

conf, err := cp.AsConf()
if err != nil {
return metadata{}, err
return Metadata{}, err
}

md := metadata{ShortFolderName: shortFolderName(filePath), Tests: tests{Host: "componenttest.NewNopHost()"}}
md := Metadata{ShortFolderName: shortFolderName(filePath), Tests: tests{Host: "componenttest.NewNopHost()"}}
if err = conf.Unmarshal(&md); err != nil {
return md, err
}
Expand Down
Loading

0 comments on commit 2c0941f

Please sign in to comment.