Skip to content

Commit 7cd8f58

Browse files
authored
[cmd/mdatagen] [chore] Fix entity-scoped MetricsBuilder to respect resource_attributes config (#14762)
This fixes feature brought in #14660. No need for changelog if we can merge it before the release The `Emit()` method in generated entity builders was calling `entity.CopyToResource()` unconditionally, ignoring the `resource_attributes` configuration set by the user. Fix the code generator so that entity structs store attribute values in plain unexported fields instead of eagerly building an `entity.Entity`. At `Emit()` time, each entity struct's `copyToResource()` method applies the ResourceAttributesConfig: - Disabled identity attributes: suppress the entity entirely; other enabled attributes are still written directly to the resource without entity association. - Disabled descriptive/extra attributes: entity is produced normally but the disabled attribute is omitted.
1 parent 12a8fe0 commit 7cd8f58

7 files changed

Lines changed: 521 additions & 123 deletions

File tree

cmd/mdatagen/internal/command.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,10 @@ func getTemplateFuncMap(md Metadata) template.FuncMap {
339339
"casesTitle": cases.Title(language.English).String,
340340
"toLowerCase": strings.ToLower,
341341
"toCamelCase": func(s string) string {
342-
caser := cases.Title(language.English).String
343-
parts := strings.Split(s, "_")
344-
var result strings.Builder
345-
for _, part := range parts {
346-
fmt.Fprintf(&result, "%s", caser(part))
347-
}
348-
return result.String()
342+
return joinCamelCase(strings.Split(s, "_"), true)
343+
},
344+
"toLowerCamelCase": func(s string) string {
345+
return joinCamelCase(strings.Split(s, "_"), false)
349346
},
350347
"inc": func(i int) int { return i + 1 },
351348
"distroURL": distroURL,
@@ -570,3 +567,16 @@ func generateConfigGoStruct(md Metadata, outputDir string) error {
570567
fns := cfggen.WithCfgFns(getTemplateFuncMap(md), rootPkg, md.PackageName)
571568
return generateFileWithFns(tmplFile, dstFile, md, packageName, fns)
572569
}
570+
571+
func joinCamelCase(parts []string, exported bool) string {
572+
caser := cases.Title(language.English).String
573+
var result strings.Builder
574+
for i, part := range parts {
575+
if i == 0 && !exported {
576+
fmt.Fprintf(&result, "%s", strings.ToLower(part))
577+
} else {
578+
fmt.Fprintf(&result, "%s", caser(part))
579+
}
580+
}
581+
return result.String()
582+
}

cmd/mdatagen/internal/sampleconnector/internal/metadata/generated_entity_metrics.go

Lines changed: 31 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/sampleconnector/internal/metadata/generated_entity_metrics_test.go

Lines changed: 62 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/sampleentityreceiver/internal/metadata/generated_entity_metrics.go

Lines changed: 74 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)