func()

in collector/receiver/prometheusreceiver/config.go [278:329]


func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
	if componentParser == nil {
		return nil
	}
	// We need custom unmarshaling because prometheus "config" subkey defines its own
	// YAML unmarshaling routines so we need to do it explicitly.

	err := componentParser.Unmarshal(cfg)
	if err != nil {
		return fmt.Errorf("prometheus receiver failed to parse config: %w", err)
	}

	// Unmarshal prometheus's config values. Since prometheus uses `yaml` tags, so use `yaml`.
	promCfg, err := componentParser.Sub(prometheusConfigKey)
	if err != nil || len(promCfg.ToStringMap()) == 0 {
		return err
	}
	out, err := yaml.Marshal(promCfg.ToStringMap())
	if err != nil {
		return fmt.Errorf("prometheus receiver failed to marshal config to yaml: %w", err)
	}

	err = yaml.UnmarshalStrict(out, &cfg.PrometheusConfig)
	if err != nil {
		return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %w", err)
	}

	// Unmarshal targetAllocator configs
	targetAllocatorCfg, err := componentParser.Sub(targetAllocatorConfigKey)
	if err != nil {
		return err
	}
	targetAllocatorHTTPSDCfg, err := targetAllocatorCfg.Sub(targetAllocatorHTTPSDConfigKey)
	if err != nil {
		return err
	}

	targetAllocatorHTTPSDMap := targetAllocatorHTTPSDCfg.ToStringMap()
	if len(targetAllocatorHTTPSDMap) != 0 {
		targetAllocatorHTTPSDMap["url"] = "http://placeholder" // we have to set it as else the marshal will fail
		httpSDConf, err := yaml.Marshal(targetAllocatorHTTPSDMap)
		if err != nil {
			return fmt.Errorf("prometheus receiver failed to marshal config to yaml: %w", err)
		}
		err = yaml.UnmarshalStrict(httpSDConf, &cfg.TargetAllocator.HTTPSDConfig)
		if err != nil {
			return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %w", err)
		}
	}

	return nil
}