Skip to content

Commit d03583d

Browse files
committed
[cmd/mdatagen] Update the scope name generation method
Don't use hardcoded "go.opentelemetry.io/collector" prefix. Provide a way to specify the scope_name in metadat.yaml. If not provided, try to use the go package name.
1 parent b75fe36 commit d03583d

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

.chloggen/mdatagen-scope-name.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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. otlpreceiver)
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: Use go package name for the scope name by default and add an option to provide the scope name in metadata.yaml.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: []

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_status.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/metadata.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Sample metadata file with all available configurations for a receiver.
22

33
type: sample
4+
scope_name: go.opentelemetry.io/collector/internal/receiver/samplereceiver
45

56
sem_conv_version: 1.9.0
67

cmd/mdatagen/loader.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"os"
10+
"os/exec"
1111
"path/filepath"
1212
"strings"
1313

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

@@ -253,10 +253,16 @@ func loadMetadata(filePath string) (metadata, error) {
253253
return metadata{}, err
254254
}
255255

256-
md := metadata{ScopeName: scopeName(filePath), ShortFolderName: shortFolderName(filePath)}
256+
md := metadata{ShortFolderName: shortFolderName(filePath)}
257257
if err := conf.Unmarshal(&md); err != nil {
258258
return md, err
259259
}
260+
if md.ScopeName == "" {
261+
md.ScopeName, err = packageName()
262+
if err != nil {
263+
return md, err
264+
}
265+
}
260266

261267
if err := md.Validate(); err != nil {
262268
return md, err
@@ -287,15 +293,11 @@ func shortFolderName(filePath string) string {
287293
return parentFolder
288294
}
289295

290-
func scopeName(filePath string) string {
291-
sn := "go.opentelemetry.io/collector"
292-
dirs := strings.Split(filepath.Dir(filePath), string(os.PathSeparator))
293-
for _, dir := range dirs {
294-
for _, cType := range componentTypes {
295-
if strings.HasSuffix(dir, cType) {
296-
sn += "/" + dir
297-
}
298-
}
296+
func packageName() (string, error) {
297+
cmd := exec.Command("go", "list", "-f", "{{.ImportPath}}")
298+
output, err := cmd.Output()
299+
if err != nil {
300+
return "", err
299301
}
300-
return sn
302+
return strings.TrimSpace(string(output)), nil
301303
}

cmd/mdatagen/metadata-schema.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ type:
44
# Required for subcomponents: The type of the parent component.
55
parent: string
66

7+
# Optional: Scope name for the telemetry generated by the component. If not set, name of the go package will be used.
8+
scope_name: string
9+
710
# Required for components (Optional for subcomponents): A high-level view of the development status and use of this component
811
status:
912
# Required: The class of the component (For example receiver)

0 commit comments

Comments
 (0)