-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
940 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
### 🚩 Deprecations 🚩 | ||
|
||
### 💡 Enhancements 💡 | ||
- Add config marshaler (#5566) | ||
|
||
### 🧰 Bug fixes 🧰 | ||
|
||
|
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
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
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,158 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package configmarshaler // import "go.opentelemetry.io/collector/service/configmarshaler" | ||
|
||
import ( | ||
"bytes" | ||
|
||
"go.uber.org/multierr" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/service" | ||
"go.opentelemetry.io/collector/service/configmarshaler/encoder/mapstructure" | ||
) | ||
|
||
// YAML top-level configuration keys. | ||
const ( | ||
// extensionsKeyName is the configuration key name for extensions section. | ||
extensionsKeyName = "extensions" | ||
|
||
// receiversKeyName is the configuration key name for receivers section. | ||
receiversKeyName = "receivers" | ||
|
||
// exportersKeyName is the configuration key name for exporters section. | ||
exportersKeyName = "exporters" | ||
|
||
// processorsKeyName is the configuration key name for processors section. | ||
processorsKeyName = "processors" | ||
|
||
// serviceKeyName is the configuration key name for service section. | ||
serviceKeyName = "service" | ||
|
||
defaultIndent = 2 | ||
) | ||
|
||
var ( | ||
// emptyNode is a workaround to omit empty maps | ||
emptyNode = &yaml.Node{ | ||
Kind: yaml.ScalarNode, | ||
Value: "", | ||
Style: yaml.FlowStyle, | ||
} | ||
) | ||
|
||
type ConfigMarshaler struct { | ||
} | ||
|
||
// New creates a new default ConfigMarshaler. | ||
func New() ConfigMarshaler { | ||
return ConfigMarshaler{} | ||
} | ||
|
||
// Marshal converts a service.Config into YAML bytes. | ||
func (ConfigMarshaler) Marshal(cfg *service.Config) ([]byte, error) { | ||
output := make(map[string]interface{}) | ||
var errs, err error | ||
if output[extensionsKeyName], err = marshalExtensions(cfg.Extensions); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
if output[receiversKeyName], err = marshalReceivers(cfg.Receivers); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
if output[processorsKeyName], err = marshalProcessors(cfg.Processors); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
if output[exportersKeyName], err = marshalExporters(cfg.Exporters); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
if output[serviceKeyName], err = mapstructure.Encode(cfg.Service); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
if errs != nil { | ||
return nil, errs | ||
} | ||
var buffer bytes.Buffer | ||
enc := yaml.NewEncoder(&buffer) | ||
enc.SetIndent(defaultIndent) | ||
if err = enc.Encode(output); err != nil { | ||
return nil, err | ||
} | ||
if err = enc.Close(); err != nil { | ||
return nil, err | ||
} | ||
return buffer.Bytes(), nil | ||
} | ||
|
||
func marshalExtensions(exts map[config.ComponentID]config.Extension) (interface{}, error) { | ||
if len(exts) == 0 { | ||
return emptyNode, nil | ||
} | ||
extensions := make(map[string]interface{}) | ||
for id, cfg := range exts { | ||
conf := confmap.New() | ||
if err := config.MarshalExtension(conf, cfg); err != nil { | ||
return nil, err | ||
} | ||
extensions[id.String()] = conf.ToStringMap() | ||
} | ||
return extensions, nil | ||
} | ||
|
||
func marshalReceivers(recvs map[config.ComponentID]config.Receiver) (interface{}, error) { | ||
if len(recvs) == 0 { | ||
return emptyNode, nil | ||
} | ||
receivers := make(map[string]interface{}) | ||
for id, cfg := range recvs { | ||
conf := confmap.New() | ||
if err := config.MarshalReceiver(conf, cfg); err != nil { | ||
return nil, err | ||
} | ||
receivers[id.String()] = conf.ToStringMap() | ||
} | ||
return receivers, nil | ||
} | ||
|
||
func marshalExporters(exps map[config.ComponentID]config.Exporter) (interface{}, error) { | ||
if len(exps) == 0 { | ||
return emptyNode, nil | ||
} | ||
exporters := make(map[string]interface{}) | ||
for id, cfg := range exps { | ||
conf := confmap.New() | ||
if err := config.MarshalExporter(conf, cfg); err != nil { | ||
return nil, err | ||
} | ||
exporters[id.String()] = conf.ToStringMap() | ||
} | ||
return exporters, nil | ||
} | ||
|
||
func marshalProcessors(procs map[config.ComponentID]config.Processor) (interface{}, error) { | ||
if len(procs) == 0 { | ||
return emptyNode, nil | ||
} | ||
processors := make(map[string]interface{}) | ||
for id, cfg := range procs { | ||
conf := confmap.New() | ||
if err := config.MarshalProcessor(conf, cfg); err != nil { | ||
return nil, err | ||
} | ||
processors[id.String()] = conf.ToStringMap() | ||
} | ||
return processors, nil | ||
} |
Oops, something went wrong.