Skip to content

Commit

Permalink
[cmd/mdatagen] Update the scope name generation method (#9693)
Browse files Browse the repository at this point in the history
Don't use hardcoded "go.opentelemetry.io/collector" prefix. Provide a
way to specify the `scope_name` in metadata.yaml. If not provided, try
to use the go package name.

Updates
#9494
  • Loading branch information
dmitryax authored Mar 5, 2024
1 parent b75fe36 commit 6e61c9b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
13 changes: 13 additions & 0 deletions .chloggen/mdatagen-scope-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Use this changelog template to create an entry for release notes.

# 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. otlpreceiver)
component: cmd/mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use go package name for the scope name by default and add an option to provide the scope name in metadata.yaml.

# One or more tracking issues or pull requests related to the change
issues: [9693]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/mdatagen/internal/samplereceiver/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Sample metadata file with all available configurations for a receiver.

type: sample
scope_name: go.opentelemetry.io/collector/internal/receiver/samplereceiver

sem_conv_version: 1.9.0

Expand Down
32 changes: 17 additions & 15 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -222,7 +222,7 @@ type metadata struct {
// Metrics that can be emitted by the component.
Metrics map[metricName]metric `mapstructure:"metrics"`
// ScopeName of the metrics emitted by the component.
ScopeName string `mapstructure:"-"`
ScopeName string `mapstructure:"scope_name"`
// ShortFolderName is the shortened folder name of the component, removing class if present
ShortFolderName string `mapstructure:"-"`

Expand Down Expand Up @@ -253,12 +253,18 @@ func loadMetadata(filePath string) (metadata, error) {
return metadata{}, err
}

md := metadata{ScopeName: scopeName(filePath), ShortFolderName: shortFolderName(filePath)}
if err := conf.Unmarshal(&md); err != nil {
md := metadata{ShortFolderName: shortFolderName(filePath)}
if err = conf.Unmarshal(&md); err != nil {
return md, err
}
if md.ScopeName == "" {
md.ScopeName, err = packageName()
if err != nil {
return md, err
}
}

if err := md.Validate(); err != nil {
if err = md.Validate(); err != nil {
return md, err
}

Expand Down Expand Up @@ -287,15 +293,11 @@ func shortFolderName(filePath string) string {
return parentFolder
}

func scopeName(filePath string) string {
sn := "go.opentelemetry.io/collector"
dirs := strings.Split(filepath.Dir(filePath), string(os.PathSeparator))
for _, dir := range dirs {
for _, cType := range componentTypes {
if strings.HasSuffix(dir, cType) {
sn += "/" + dir
}
}
func packageName() (string, error) {
cmd := exec.Command("go", "list", "-f", "{{.ImportPath}}")
output, err := cmd.Output()
if err != nil {
return "", err
}
return sn
return strings.TrimSpace(string(output)), nil
}
4 changes: 2 additions & 2 deletions cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestLoadMetadata(t *testing.T) {
},
},
},
ScopeName: "go.opentelemetry.io/collector/samplereceiver",
ScopeName: "go.opentelemetry.io/collector/internal/receiver/samplereceiver",
ShortFolderName: "sample",
},
},
Expand All @@ -227,7 +227,7 @@ func TestLoadMetadata(t *testing.T) {
want: metadata{
Type: "subcomponent",
Parent: "parentComponent",
ScopeName: "go.opentelemetry.io/collector",
ScopeName: "go.opentelemetry.io/collector/cmd/mdatagen",
ShortFolderName: "testdata",
},
},
Expand Down
3 changes: 3 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ type:
# Required for subcomponents: The type of the parent component.
parent: string

# Optional: Scope name for the telemetry generated by the component. If not set, name of the go package will be used.
scope_name: string

# Required for components (Optional for subcomponents): A high-level view of the development status and use of this component
status:
# Required: The class of the component (For example receiver)
Expand Down

0 comments on commit 6e61c9b

Please sign in to comment.