From 9cb9e356bd12e93ff56af6d530a51cfc4a1f0947 Mon Sep 17 00:00:00 2001 From: Hyunsoo Kim Date: Fri, 11 Oct 2024 16:33:44 -0400 Subject: [PATCH] add marshaler to otel prometheus receiver to avoid any failures due to yaml tags --- receiver/prometheusreceiver/config.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/receiver/prometheusreceiver/config.go b/receiver/prometheusreceiver/config.go index c4269efde038..79e368d3282e 100644 --- a/receiver/prometheusreceiver/config.go +++ b/receiver/prometheusreceiver/config.go @@ -74,6 +74,7 @@ func (cfg *TargetAllocator) Validate() error { type PromConfig promconfig.Config var _ confmap.Unmarshaler = (*PromConfig)(nil) +var _ confmap.Marshaler = (*PromConfig)(nil) func (cfg *PromConfig) Unmarshal(componentParser *confmap.Conf) error { cfgMap := componentParser.ToStringMap() @@ -83,6 +84,20 @@ func (cfg *PromConfig) Unmarshal(componentParser *confmap.Conf) error { return unmarshalYAML(cfgMap, (*promconfig.Config)(cfg)) } +func (cfg PromConfig) Marshal(componentParser *confmap.Conf) error { + yamlOut, err := yaml.Marshal(cfg) + if err != nil { + return fmt.Errorf("prometheus receiver: failed to marshal config to yaml: %w", err) + } + + var result map[string]any + err = yaml.UnmarshalStrict(yamlOut, &result) + if err != nil { + return fmt.Errorf("prometheus receiver: failed to unmarshal yaml to prometheus config object: %w", err) + } + return componentParser.Merge(confmap.NewFromStringMap(result)) +} + func (cfg *PromConfig) Validate() error { // Reject features that Prometheus supports but that the receiver doesn't support: // See: