Skip to content

Commit

Permalink
Enhance builder to capture go module for each component type (#10599)
Browse files Browse the repository at this point in the history
Resolves #10570

Alternative to #10598

This implementation is more reliable that #10598 because it gets
information directly from the builder manifest. It relies on additional
structure in the `component.go` file, ultimately encoded in
`otelcol.Factories`.

An alternative would be to push the enhancements deeper, into the
`<kind>.Factories` implementations, so that the module information is
available directly alongside the Factory.
  • Loading branch information
djaglowski authored Jul 16, 2024
1 parent 2d7dea6 commit f2c56a3
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 10 deletions.
25 changes: 25 additions & 0 deletions .chloggen/module-info-builder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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/builder

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add go module info the builder generated code.

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
25 changes: 25 additions & 0 deletions .chloggen/module-info-components.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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: otelcol

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add go module to components subcommand.

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
15 changes: 10 additions & 5 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Distribution struct {
OtelColVersion string `mapstructure:"otelcol_version"`
RequireOtelColModule bool `mapstructure:"-"` // required for backwards-compatibility with builds older than 0.86.0
SupportsConfmapFactories bool `mapstructure:"-"` // Required for backwards-compatibility with builds older than 0.99.0
SupportsComponentModules bool `mapstructure:"-"` // Required for backwards-compatibility with builds older than 0.105.0
OutputPath string `mapstructure:"output_path"`
Version string `mapstructure:"version"`
BuildTags string `mapstructure:"build_tags"`
Expand Down Expand Up @@ -144,13 +145,14 @@ func (c *Config) SetGoPath() error {
}

func (c *Config) SetBackwardsCompatibility() error {
// check whether we need to adjust the core API module import
constraint, err := version.NewConstraint(">= 0.86.0")
// Get the version of the collector
otelColVersion, err := version.NewVersion(c.Distribution.OtelColVersion)
if err != nil {
return err
}

otelColVersion, err := version.NewVersion(c.Distribution.OtelColVersion)
// check whether we need to adjust the core API module import
constraint, err := version.NewConstraint(">= 0.86.0")
if err != nil {
return err
}
Expand All @@ -163,12 +165,15 @@ func (c *Config) SetBackwardsCompatibility() error {
return err
}

otelColVersion, err = version.NewVersion(c.Distribution.OtelColVersion)
c.Distribution.SupportsConfmapFactories = constraint.Check(otelColVersion)

// check whether go modules are recorded for components
constraint, err = version.NewConstraint(">= 0.105.0")
if err != nil {
return err
}

c.Distribution.SupportsConfmapFactories = constraint.Check(otelColVersion)
c.Distribution.SupportsComponentModules = constraint.Check(otelColVersion)

return nil
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/builder/internal/builder/templates/components.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package main

import (
{{- if .Distribution.SupportsComponentModules}}
"go.opentelemetry.io/collector/component"
{{- end}}
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/extension"
Expand Down Expand Up @@ -38,6 +41,12 @@ func components() (otelcol.Factories, error) {
if err != nil {
return otelcol.Factories{}, err
}
{{- if .Distribution.SupportsComponentModules}}
factories.ExtensionModules = make(map[component.Type]string, len(factories.Extensions))
{{- range .Extensions}}
factories.ExtensionModules[{{.Name}}.NewFactory().Type()] = "{{.GoMod}}"
{{- end}}
{{- end}}

factories.Receivers, err = receiver.MakeFactoryMap(
{{- range .Receivers}}
Expand All @@ -47,6 +56,12 @@ func components() (otelcol.Factories, error) {
if err != nil {
return otelcol.Factories{}, err
}
{{- if .Distribution.SupportsComponentModules}}
factories.ReceiverModules = make(map[component.Type]string, len(factories.Receivers))
{{- range .Receivers}}
factories.ReceiverModules[{{.Name}}.NewFactory().Type()] = "{{.GoMod}}"
{{- end}}
{{- end}}

factories.Exporters, err = exporter.MakeFactoryMap(
{{- range .Exporters}}
Expand All @@ -56,6 +71,12 @@ func components() (otelcol.Factories, error) {
if err != nil {
return otelcol.Factories{}, err
}
{{- if .Distribution.SupportsComponentModules}}
factories.ExporterModules = make(map[component.Type]string, len(factories.Exporters))
{{- range .Exporters}}
factories.ExporterModules[{{.Name}}.NewFactory().Type()] = "{{.GoMod}}"
{{- end}}
{{- end}}

factories.Processors, err = processor.MakeFactoryMap(
{{- range .Processors}}
Expand All @@ -65,6 +86,12 @@ func components() (otelcol.Factories, error) {
if err != nil {
return otelcol.Factories{}, err
}
{{- if .Distribution.SupportsComponentModules}}
factories.ProcessorModules = make(map[component.Type]string, len(factories.Processors))
{{- range .Processors}}
factories.ProcessorModules[{{.Name}}.NewFactory().Type()] = "{{.GoMod}}"
{{- end}}
{{- end}}

factories.Connectors, err = connector.MakeFactoryMap(
{{- range .Connectors}}
Expand All @@ -74,6 +101,12 @@ func components() (otelcol.Factories, error) {
if err != nil {
return otelcol.Factories{}, err
}
{{- if .Distribution.SupportsComponentModules}}
factories.ConnectorModules = make(map[component.Type]string, len(factories.Connectors))
{{- range .Connectors}}
factories.ConnectorModules[{{.Name}}.NewFactory().Type()] = "{{.GoMod}}"
{{- end}}
{{- end}}

return factories, nil
}
16 changes: 11 additions & 5 deletions otelcol/command_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type componentWithStability struct {
Name component.Type
Module string
Stability map[string]string
}

Expand Down Expand Up @@ -49,7 +50,8 @@ func newComponentsCommand(set CollectorSettings) *cobra.Command {
components := componentsOutput{}
for _, con := range sortFactoriesByType[connector.Factory](factories.Connectors) {
components.Connectors = append(components.Connectors, componentWithStability{
Name: con.Type(),
Name: con.Type(),
Module: factories.ConnectorModules[con.Type()],
Stability: map[string]string{
"logs-to-logs": con.LogsToLogsStability().String(),
"logs-to-metrics": con.LogsToMetricsStability().String(),
Expand All @@ -67,15 +69,17 @@ func newComponentsCommand(set CollectorSettings) *cobra.Command {
}
for _, ext := range sortFactoriesByType[extension.Factory](factories.Extensions) {
components.Extensions = append(components.Extensions, componentWithStability{
Name: ext.Type(),
Name: ext.Type(),
Module: factories.ExtensionModules[ext.Type()],
Stability: map[string]string{
"extension": ext.ExtensionStability().String(),
},
})
}
for _, prs := range sortFactoriesByType[processor.Factory](factories.Processors) {
components.Processors = append(components.Processors, componentWithStability{
Name: prs.Type(),
Name: prs.Type(),
Module: factories.ProcessorModules[prs.Type()],
Stability: map[string]string{
"logs": prs.LogsProcessorStability().String(),
"metrics": prs.MetricsProcessorStability().String(),
Expand All @@ -85,7 +89,8 @@ func newComponentsCommand(set CollectorSettings) *cobra.Command {
}
for _, rcv := range sortFactoriesByType[receiver.Factory](factories.Receivers) {
components.Receivers = append(components.Receivers, componentWithStability{
Name: rcv.Type(),
Name: rcv.Type(),
Module: factories.ReceiverModules[rcv.Type()],
Stability: map[string]string{
"logs": rcv.LogsReceiverStability().String(),
"metrics": rcv.MetricsReceiverStability().String(),
Expand All @@ -95,7 +100,8 @@ func newComponentsCommand(set CollectorSettings) *cobra.Command {
}
for _, exp := range sortFactoriesByType[exporter.Factory](factories.Exporters) {
components.Exporters = append(components.Exporters, componentWithStability{
Name: exp.Type(),
Name: exp.Type(),
Module: factories.ExporterModules[exp.Type()],
Stability: map[string]string{
"logs": exp.LogsExporterStability().String(),
"metrics": exp.MetricsExporterStability().String(),
Expand Down
15 changes: 15 additions & 0 deletions otelcol/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ type Factories struct {

// Connectors maps connector type names in the config to the respective factory.
Connectors map[component.Type]connector.Factory

// ReceiverModules maps receiver types to their respective go modules.
ReceiverModules map[component.Type]string

// ProcessorModules maps processor types to their respective go modules.
ProcessorModules map[component.Type]string

// ExporterModules maps exporter types to their respective go modules.
ExporterModules map[component.Type]string

// ExtensionModules maps extension types to their respective go modules.
ExtensionModules map[component.Type]string

// ConnectorModules maps connector types to their respective go modules.
ConnectorModules map[component.Type]string
}
20 changes: 20 additions & 0 deletions otelcol/factories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,42 @@ func nopFactories() (Factories, error) {
if factories.Connectors, err = connector.MakeFactoryMap(connectortest.NewNopFactory()); err != nil {
return Factories{}, err
}
factories.ConnectorModules = make(map[component.Type]string, len(factories.Connectors))
for _, con := range factories.Connectors {
factories.ConnectorModules[con.Type()] = "go.opentelemetry.io/collector/connector/connectortest v1.2.3"
}

if factories.Extensions, err = extension.MakeFactoryMap(extensiontest.NewNopFactory()); err != nil {
return Factories{}, err
}
factories.ExtensionModules = make(map[component.Type]string, len(factories.Extensions))
for _, ext := range factories.Extensions {
factories.ExtensionModules[ext.Type()] = "go.opentelemetry.io/collector/extension/extensiontest v1.2.3"
}

if factories.Receivers, err = receiver.MakeFactoryMap(receivertest.NewNopFactory(), receivertest.NewNopFactoryForType(component.DataTypeLogs)); err != nil {
return Factories{}, err
}
factories.ReceiverModules = make(map[component.Type]string, len(factories.Receivers))
for _, rec := range factories.Receivers {
factories.ReceiverModules[rec.Type()] = "go.opentelemetry.io/collector/receiver/receivertest v1.2.3"
}

if factories.Exporters, err = exporter.MakeFactoryMap(exportertest.NewNopFactory()); err != nil {
return Factories{}, err
}
factories.ExporterModules = make(map[component.Type]string, len(factories.Exporters))
for _, exp := range factories.Exporters {
factories.ExporterModules[exp.Type()] = "go.opentelemetry.io/collector/exporter/exportertest v1.2.3"
}

if factories.Processors, err = processor.MakeFactoryMap(processortest.NewNopFactory()); err != nil {
return Factories{}, err
}
factories.ProcessorModules = make(map[component.Type]string, len(factories.Processors))
for _, proc := range factories.Processors {
factories.ProcessorModules[proc.Type()] = "go.opentelemetry.io/collector/processor/processortest v1.2.3"
}

return factories, err
}
6 changes: 6 additions & 0 deletions otelcol/testdata/components-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ buildinfo:
version: latest
receivers:
- name: nop
module: go.opentelemetry.io/collector/receiver/receivertest v1.2.3
stability:
logs: Stable
metrics: Stable
traces: Stable
- name: nop_logs
module: go.opentelemetry.io/collector/receiver/receivertest v1.2.3
stability:
logs: Stable
metrics: Undefined
traces: Undefined
processors:
- name: nop
module: go.opentelemetry.io/collector/processor/processortest v1.2.3
stability:
logs: Stable
metrics: Stable
traces: Stable
exporters:
- name: nop
module: go.opentelemetry.io/collector/exporter/exportertest v1.2.3
stability:
logs: Stable
metrics: Stable
traces: Stable
connectors:
- name: nop
module: go.opentelemetry.io/collector/connector/connectortest v1.2.3
stability:
logs-to-logs: Development
logs-to-metrics: Development
Expand All @@ -39,5 +44,6 @@ connectors:
traces-to-traces: Development
extensions:
- name: nop
module: go.opentelemetry.io/collector/extension/extensiontest v1.2.3
stability:
extension: Stable

0 comments on commit f2c56a3

Please sign in to comment.