-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing go mod replace for collector
- Loading branch information
Showing
11 changed files
with
580 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
The code was copied from: | ||
https://github.com/open-telemetry/opentelemetry-collector/tree/v0.114.0/otelcol/internal/configunmarshaler | ||
|
||
This forked package exists because the "forked_otelcol" package depends on it. | ||
|
||
There is no need to update it every time Alloy's OTel version is updated: | ||
* This is very fundamental code which doesn't change often. | ||
* As long as Static mode's code still behaves as expected and the converter tests pass, that's fine. | ||
* The version of OTel which Agent's repo uses is different from the one which Alloy uses anyway. | ||
|
||
The only time when this forked package may need updating is when it fails to build due to refactoring in the OTel repo. |
76 changes: 76 additions & 0 deletions
76
internal/static/traces/forked_configunmarshaler/configs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package forked_configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"golang.org/x/exp/maps" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/confmap" | ||
) | ||
|
||
type Configs[F component.Factory] struct { | ||
cfgs map[component.ID]component.Config | ||
|
||
factories map[component.Type]F | ||
} | ||
|
||
func NewConfigs[F component.Factory](factories map[component.Type]F) *Configs[F] { | ||
return &Configs[F]{factories: factories} | ||
} | ||
|
||
func (c *Configs[F]) Unmarshal(conf *confmap.Conf) error { | ||
rawCfgs := make(map[component.ID]map[string]any) | ||
if err := conf.Unmarshal(&rawCfgs); err != nil { | ||
return err | ||
} | ||
|
||
// Prepare resulting map. | ||
c.cfgs = make(map[component.ID]component.Config) | ||
// Iterate over raw configs and create a config for each. | ||
for id := range rawCfgs { | ||
// Find factory based on component kind and type that we read from config source. | ||
factory, ok := c.factories[id.Type()] | ||
if !ok { | ||
return errorUnknownType(id, maps.Keys(c.factories)) | ||
} | ||
|
||
// Get the configuration from the confmap.Conf to preserve internal representation. | ||
sub, err := conf.Sub(id.String()) | ||
if err != nil { | ||
return errorUnmarshalError(id, err) | ||
} | ||
|
||
// Create the default config for this component. | ||
cfg := factory.CreateDefaultConfig() | ||
|
||
// Now that the default config struct is created we can Unmarshal into it, | ||
// and it will apply user-defined config on top of the default. | ||
if err := sub.Unmarshal(&cfg); err != nil { | ||
return errorUnmarshalError(id, err) | ||
} | ||
|
||
c.cfgs[id] = cfg | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Configs[F]) Configs() map[component.ID]component.Config { | ||
return c.cfgs | ||
} | ||
|
||
func errorUnknownType(id component.ID, factories []component.Type) error { | ||
if id.Type().String() == "logging" { | ||
return errors.New("the logging exporter has been deprecated, use the debug exporter instead") | ||
} | ||
return fmt.Errorf("unknown type: %q for id: %q (valid values: %v)", id.Type(), id, factories) | ||
} | ||
|
||
func errorUnmarshalError(id component.ID, err error) error { | ||
return fmt.Errorf("error reading configuration for %q: %w", id, err) | ||
} |
145 changes: 145 additions & 0 deletions
145
internal/static/traces/forked_configunmarshaler/configs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package forked_configunmarshaler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/connector/connectortest" | ||
"go.opentelemetry.io/collector/exporter/exportertest" | ||
"go.opentelemetry.io/collector/extension/extensiontest" | ||
"go.opentelemetry.io/collector/processor/processortest" | ||
"go.opentelemetry.io/collector/receiver/receivertest" | ||
) | ||
|
||
var nopType = component.MustNewType("nop") | ||
|
||
var testKinds = []struct { | ||
kind string | ||
factories map[component.Type]component.Factory | ||
}{ | ||
{ | ||
kind: "receiver", | ||
factories: map[component.Type]component.Factory{ | ||
nopType: receivertest.NewNopFactory(), | ||
}, | ||
}, | ||
{ | ||
kind: "processor", | ||
factories: map[component.Type]component.Factory{ | ||
nopType: processortest.NewNopFactory(), | ||
}, | ||
}, | ||
{ | ||
kind: "exporter", | ||
factories: map[component.Type]component.Factory{ | ||
nopType: exportertest.NewNopFactory(), | ||
}, | ||
}, | ||
{ | ||
kind: "connector", | ||
factories: map[component.Type]component.Factory{ | ||
nopType: connectortest.NewNopFactory(), | ||
}, | ||
}, | ||
{ | ||
kind: "extension", | ||
factories: map[component.Type]component.Factory{ | ||
nopType: extensiontest.NewNopFactory(), | ||
}, | ||
}, | ||
} | ||
|
||
func TestUnmarshal(t *testing.T) { | ||
for _, tk := range testKinds { | ||
t.Run(tk.kind, func(t *testing.T) { | ||
cfgs := NewConfigs(tk.factories) | ||
conf := confmap.NewFromStringMap(map[string]any{ | ||
"nop": nil, | ||
"nop/my" + tk.kind: nil, | ||
}) | ||
require.NoError(t, cfgs.Unmarshal(conf)) | ||
|
||
assert.Equal(t, map[component.ID]component.Config{ | ||
component.NewID(nopType): tk.factories[nopType].CreateDefaultConfig(), | ||
component.NewIDWithName(nopType, "my"+tk.kind): tk.factories[nopType].CreateDefaultConfig(), | ||
}, cfgs.Configs()) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnmarshalError(t *testing.T) { | ||
for _, tk := range testKinds { | ||
t.Run(tk.kind, func(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
conf *confmap.Conf | ||
// string that the error must contain | ||
expectedError string | ||
}{ | ||
{ | ||
name: "invalid-type", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nop": nil, | ||
"/custom": nil, | ||
}), | ||
expectedError: "the part before / should not be empty", | ||
}, | ||
{ | ||
name: "invalid-name-after-slash", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nop": nil, | ||
"nop/": nil, | ||
}), | ||
expectedError: "the part after / should not be empty", | ||
}, | ||
{ | ||
name: "unknown-type", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nosuch" + tk.kind: nil, | ||
}), | ||
expectedError: "unknown type: \"nosuch" + tk.kind + "\" for id: \"nosuch" + tk.kind + "\" (valid values: [nop])", | ||
}, | ||
{ | ||
name: "duplicate", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nop /my" + tk.kind + " ": nil, | ||
" nop/ my" + tk.kind: nil, | ||
}), | ||
expectedError: "duplicate name", | ||
}, | ||
{ | ||
name: "invalid-section", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nop": map[string]any{ | ||
"unknown_section": tk.kind, | ||
}, | ||
}), | ||
expectedError: "error reading configuration for \"nop\"", | ||
}, | ||
{ | ||
name: "invalid-sub-config", | ||
conf: confmap.NewFromStringMap(map[string]any{ | ||
"nop": "tests", | ||
}), | ||
expectedError: "'[nop]' expected a map, got 'string'", | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cfgs := NewConfigs(tk.factories) | ||
err := cfgs.Unmarshal(tt.conf) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.expectedError) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
The code was copied from: | ||
https://github.com/open-telemetry/opentelemetry-collector/blob/v0.114.0/otelcol/unmarshaler.go | ||
https://github.com/open-telemetry/opentelemetry-collector/blob/v0.114.0/otelcol/unmarshaler_test.go | ||
https://github.com/open-telemetry/opentelemetry-collector/blob/v0.114.0/otelcol/factories_test.go | ||
|
||
The purpose of this forked package is to allow Static mode to use the "Unmarshal(...)" function. | ||
In the original OTel repo it is internal (it's lowercase - "unmarshal(...)"). | ||
|
||
There is no need to update this forked package every time Alloy's OTel version is updated: | ||
* This is very fundamental code which doesn't change often. | ||
* As long as Static mode's code still behaves as expected and the converter tests pass, that's fine. | ||
* The version of OTel which Agent's repo uses is different from the one which Alloy uses anyway. | ||
|
||
The only time when this forked package may need updating is when it fails to build due to refactoring in the OTel repo. |
Oops, something went wrong.