Skip to content

Commit c122c91

Browse files
authored
feat: add nrdot component to config (#485)
* add nrdot component to config * fix nrModule url * add tests * if there are populated version flags, use those instead of usual update method * address comments, rename variables, and testing * add null check, rename more variables for consistency
1 parent c06432f commit c122c91

File tree

6 files changed

+258
-9
lines changed

6 files changed

+258
-9
lines changed

cmd/nrdot-collector-builder/cmd/manifest/update.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ var UpdateCmd = &cobra.Command{
3131
jsonOutput, _ := cmd.Root().PersistentFlags().GetBool("json")
3232
verbose, _ := cmd.Root().PersistentFlags().GetBool("verbose")
3333

34+
// Get nrdot version information from persistent flags
35+
var nrdotVersion, nrdotUsage string
36+
if nrdotVersionFlag := cmd.Root().PersistentFlags().Lookup("nrdot-version"); nrdotVersionFlag != nil {
37+
nrdotVersion = nrdotVersionFlag.Value.String()
38+
nrdotUsage = nrdotVersionFlag.Usage
39+
}
40+
41+
var coreStable, coreStableUsage string
42+
if coreStableFlag := cmd.Root().PersistentFlags().Lookup("core-stable"); coreStableFlag != nil {
43+
coreStable = coreStableFlag.Value.String()
44+
coreStableUsage = coreStableFlag.Usage
45+
}
46+
47+
var contribBeta, contribBetaUsage string
48+
if contribBetaFlag := cmd.Root().PersistentFlags().Lookup("contrib-beta"); contribBetaFlag != nil {
49+
contribBeta = contribBetaFlag.Value.String()
50+
contribBetaUsage = contribBetaFlag.Usage
51+
}
52+
53+
// Create a map with module path (usage) as key and version array as value for updates
54+
nrdotUpdates := make(map[string][]string)
55+
if nrdotVersion != "" {
56+
nrdotUpdates[nrdotUsage] = []string{nrdotVersion}
57+
}
58+
if coreStable != "" {
59+
nrdotUpdates[coreStableUsage] = []string{coreStable}
60+
}
61+
if contribBeta != "" {
62+
nrdotUpdates[contribBetaUsage] = []string{contribBeta}
63+
}
64+
3465
matches, _ := filepath.Glob(configPath)
3566

3667
if len(matches) == 0 {
@@ -68,9 +99,17 @@ var UpdateCmd = &cobra.Command{
6899
currentVersions = cfg.Versions
69100
}
70101

71-
updatedCfg, err := manifest.UpdateConfigModules(cfg)
72-
if err != nil {
73-
return fmt.Errorf("failed to update configuration: %w", err)
102+
var updatedCfg *manifest.Config
103+
if len(nrdotUpdates) > 0 {
104+
updatedCfg, err = manifest.CopyAndUpdateConfigModules(cfg, nrdotUpdates)
105+
if err != nil {
106+
return fmt.Errorf("failed to update configuration with nrdot versions: %w", err)
107+
}
108+
} else {
109+
updatedCfg, err = manifest.UpdateConfigModules(cfg)
110+
if err != nil {
111+
return fmt.Errorf("failed to update configuration: %w", err)
112+
}
74113
}
75114

76115
if err = manifest.WriteConfigFile(updatedCfg); err != nil {
@@ -91,6 +130,7 @@ var UpdateCmd = &cobra.Command{
91130
NextVersions: nextVersions,
92131
CurrentVersions: currentVersions,
93132
}
133+
94134
b, err := json.Marshal(output)
95135
if err != nil {
96136
return fmt.Errorf("failed to marshal JSON output: %w", err)

cmd/nrdot-collector-builder/cmd/root.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111

1212
var jsonOutput bool
1313
var verbose bool
14+
var nrdotVersion string
15+
var coreStableVersion string
16+
var contribBetaVersion string
1417

1518
// rootCmd represents the base command when called without any subcommands
1619
var rootCmd = &cobra.Command{
@@ -26,6 +29,24 @@ It simplifies the process of building and deploying the collector with NRDOT-spe
2629
func init() {
2730
rootCmd.PersistentFlags().BoolVar(&jsonOutput, "json", false, "Output results in JSON format")
2831
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Verbose output")
32+
rootCmd.PersistentFlags().StringVar(
33+
&nrdotVersion,
34+
"nrdot-version",
35+
"",
36+
"github.com/newrelic/nrdot-collector-components/exporter/nrdot",
37+
)
38+
rootCmd.PersistentFlags().StringVar(
39+
&coreStableVersion,
40+
"core-stable",
41+
"",
42+
"go.opentelemetry.io/collector",
43+
)
44+
rootCmd.PersistentFlags().StringVar(
45+
&contribBetaVersion,
46+
"contrib-beta",
47+
"",
48+
"github.com/open-telemetry/opentelemetry-collector-contrib",
49+
)
2950
}
3051

3152
// Execute adds all child commands to the root command and sets flags appropriately.

cmd/nrdot-collector-builder/internal/manifest/config.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ var errMissingGoMod = errors.New("missing gomod specification for module")
2323

2424
const coreModule = "go.opentelemetry.io/collector"
2525
const contribModule = "github.com/open-telemetry/opentelemetry-collector-contrib"
26+
const nrModule = "github.com/newrelic/nrdot-collector-components"
2627

2728
type Versions struct {
2829
BetaCoreVersion string `json:"betaCoreVersion"`
2930
BetaContribVersion string `json:"betaContribVersion"`
3031
StableCoreVersion string `json:"stableCoreVersion"`
32+
NrdotVersion string `json:"nrdotVersion"`
3133
}
3234

3335
// Config holds the builder's configuration
@@ -95,6 +97,14 @@ func isOtelContribComponent(mod string) bool {
9597
return false
9698
}
9799

100+
func isNrdotComponent(component Module) bool {
101+
// Check if the component is part of the NRDOT Collector
102+
if strings.HasPrefix(component.GoMod, nrModule) {
103+
return true
104+
}
105+
return false
106+
}
107+
98108
func isOtelComponent(component Module) bool {
99109
// Check if the component is part of the OpenTelemetry Collector
100110
if isOtelCoreComponent(component.GoMod) || isOtelContribComponent(component.GoMod) {
@@ -111,22 +121,40 @@ func isStableVersion(version string) bool {
111121
return false
112122
}
113123

124+
func isCompatibleWithNrdotComponent(nrdotVersion string, betaVersion string) bool {
125+
if semver.Compare(nrdotVersion, betaVersion) >= 0 {
126+
return true
127+
}
128+
return false
129+
}
130+
114131
func (c *Config) SetVersions() error {
115132

116133
versions := Versions{}
117134

135+
for _, component := range c.allNrdotComponents() {
136+
if isNrdotComponent(component) {
137+
componentVersion := strings.Split(component.GoMod, " ")[1]
138+
versions.NrdotVersion = componentVersion
139+
}
140+
141+
if versions.NrdotVersion != "" {
142+
break
143+
}
144+
}
145+
118146
for _, component := range c.allOtelComponents() {
119147
if isOtelComponent(component) {
120148
componentVersion := strings.Split(component.GoMod, " ")[1]
121149
if isOtelCoreComponent(component.GoMod) {
122150
if isStableVersion(componentVersion) {
123151
versions.StableCoreVersion = componentVersion
124-
} else {
152+
} else if isCompatibleWithNrdotComponent(versions.NrdotVersion, componentVersion) {
125153
versions.BetaCoreVersion = componentVersion
126154
}
127155
}
128156

129-
if isOtelContribComponent(component.GoMod) && !isStableVersion(componentVersion) {
157+
if isOtelContribComponent(component.GoMod) && !isStableVersion(componentVersion) && isCompatibleWithNrdotComponent(versions.NrdotVersion, componentVersion) {
130158
versions.BetaContribVersion = componentVersion
131159
}
132160

@@ -228,6 +256,16 @@ func (cfg *Config) allOtelComponents() []Module {
228256
return allOtelComponents
229257
}
230258

259+
func (cfg *Config) allNrdotComponents() []Module {
260+
allNrdotComponents := []Module{}
261+
for _, component := range cfg.allComponents() {
262+
if isNrdotComponent(component) {
263+
allNrdotComponents = append(allNrdotComponents, component)
264+
}
265+
}
266+
return allNrdotComponents
267+
}
268+
231269
func validateModules(name string, mods []Module) error {
232270
for i, mod := range mods {
233271
if mod.GoMod == "" {

cmd/nrdot-collector-builder/internal/manifest/config_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ func TestConfig_IsOtelContribComponent(t *testing.T) {
8787
assert.False(t, isOtelContribComponent("github.com/some/other/module"))
8888
}
8989

90+
func TestConfig_IsNrdotComponent(t *testing.T) {
91+
assert.True(t, isNrdotComponent(Module{GoMod: "github.com/newrelic/nrdot-collector-components/component v1.0.0"}))
92+
assert.True(t, isNrdotComponent(Module{GoMod: "github.com/newrelic/nrdot-collector-components/component"}))
93+
assert.False(t, isNrdotComponent(Module{GoMod: "github.com/some/other/module"}))
94+
}
95+
9096
func TestConfig_SetVersions(t *testing.T) {
9197
cfg := &Config{
9298
Extensions: []Module{
9399
{GoMod: "github.com/open-telemetry/opentelemetry-collector-contrib/component v0.1.0"},
100+
{GoMod: "github.com/newrelic/nrdot-collector-components/component v0.1.0"},
94101
},
95102
Receivers: []Module{
96103
{GoMod: "go.opentelemetry.io/collector v1.0.0"},
@@ -104,6 +111,7 @@ func TestConfig_SetVersions(t *testing.T) {
104111
assert.Equal(t, "v1.0.0", cfg.Versions.StableCoreVersion)
105112
assert.Equal(t, "v0.1.0", cfg.Versions.BetaCoreVersion)
106113
assert.Equal(t, "v0.1.0", cfg.Versions.BetaContribVersion)
114+
assert.Equal(t, "v0.1.0", cfg.Versions.NrdotVersion)
107115
}
108116

109117
func TestConfig_SetVersions_MissingCore(t *testing.T) {
@@ -121,3 +129,58 @@ func TestConfig_SetVersions_MissingCore(t *testing.T) {
121129
assert.Contains(t, err.Error(), "missing beta core version")
122130

123131
}
132+
133+
func TestIsCompatibleWithNrdotComponent(t *testing.T) {
134+
tests := []struct {
135+
name string
136+
nrdotVersion string
137+
betaVersion string
138+
expectedMatch bool
139+
}{
140+
{
141+
name: "nrdot version equal to beta version",
142+
nrdotVersion: "v0.142.0",
143+
betaVersion: "v0.142.0",
144+
expectedMatch: true,
145+
},
146+
{
147+
name: "different minor versions - nrdot higher",
148+
nrdotVersion: "v0.143.0",
149+
betaVersion: "v0.142.5",
150+
expectedMatch: true,
151+
},
152+
{
153+
name: "different minor versions - beta higher",
154+
nrdotVersion: "v0.142.0",
155+
betaVersion: "v0.142.1",
156+
expectedMatch: false,
157+
},
158+
{
159+
name: "different patch versions - nrdot higher",
160+
nrdotVersion: "v0.142.5",
161+
betaVersion: "v0.142.3",
162+
expectedMatch: true,
163+
},
164+
{
165+
name: "different patch versions - beta higher",
166+
nrdotVersion: "v0.142.3",
167+
betaVersion: "v0.142.5",
168+
expectedMatch: false,
169+
},
170+
{
171+
name: "edge case - large version numbers",
172+
nrdotVersion: "v0.999.999",
173+
betaVersion: "v0.999.998",
174+
expectedMatch: true,
175+
},
176+
}
177+
178+
for _, tt := range tests {
179+
t.Run(tt.name, func(t *testing.T) {
180+
result := isCompatibleWithNrdotComponent(tt.nrdotVersion, tt.betaVersion)
181+
assert.Equal(t, tt.expectedMatch, result,
182+
"isCompatibleWithNrdotComponent(%s, %s) = %v, want %v",
183+
tt.nrdotVersion, tt.betaVersion, result, tt.expectedMatch)
184+
})
185+
}
186+
}

cmd/nrdot-collector-builder/internal/manifest/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"os/exec"
12+
"slices"
1213
"sort"
1314
"strings"
1415

@@ -77,8 +78,9 @@ func fetchAllModuleVersions(cfg *Config, modules []string) (map[string][]string,
7778
func fetchLatestModuleVersions(cfg *Config) (map[string][]string, error) {
7879
updates := make(map[string][]string)
7980

81+
var components = slices.Concat(cfg.allOtelComponents(), cfg.allNrdotComponents())
8082
var modules []string
81-
for _, component := range cfg.allOtelComponents() {
83+
for _, component := range components {
8284
module, _, _ := strings.Cut(component.GoMod, " ")
8385
modules = append(modules, module)
8486
}
@@ -90,7 +92,7 @@ func fetchLatestModuleVersions(cfg *Config) (map[string][]string, error) {
9092
}
9193

9294
// Iterate over all components in cfg.allComponents()
93-
for _, component := range cfg.allOtelComponents() {
95+
for _, component := range components {
9496
// Extract the module name from the component's GoMod field
9597
module, currentVersion, _ := strings.Cut(component.GoMod, " ")
9698
// Log the current version

0 commit comments

Comments
 (0)