// NewProvider generates an appropriate provider according to analysis provider config.
NewProvider generates an appropriate provider according to analysis provider config.
// NewProvider generates an appropriate provider according to analysis provider config .
func NewProvider(analysisTempCfg *config.TemplatableAnalysisMetrics, providerCfg *config.PipedAnalysisProvider, logger *zap.Logger) (metrics.Provider, error) {
	switch providerCfg.Type {
	case model.AnalysisProviderPrometheus:
		options := []prometheus.Option{
			prometheus.WithLogger(logger),
			prometheus.WithTimeout(analysisTempCfg.Timeout.Duration()),
		}
		cfg := providerCfg.PrometheusConfig
		if cfg.UsernameFile != "" && cfg.PasswordFile != "" {
			username, err := os.ReadFile(cfg.UsernameFile)
			if err != nil {
				return nil, fmt.Errorf("failed to read the username file: %w", err)
			}
			password, err := os.ReadFile(cfg.PasswordFile)
			if err != nil {
				return nil, fmt.Errorf("failed to read the password file: %w", err)
			}
			options = append(options, prometheus.WithBasicAuth(strings.TrimSpace(string(username)), strings.TrimSpace(string(password))))
		}
		return prometheus.NewProvider(providerCfg.PrometheusConfig.Address, options...)
	case model.AnalysisProviderDatadog:
		var apiKey, applicationKey string
		cfg := providerCfg.DatadogConfig
		if cfg.APIKeyFile != "" {
			a, err := os.ReadFile(cfg.APIKeyFile)
			if err != nil {
				return nil, fmt.Errorf("failed to read the api-key file: %w", err)
			}
			apiKey = strings.TrimSpace(string(a))
		}
		if cfg.ApplicationKeyFile != "" {
			a, err := os.ReadFile(cfg.ApplicationKeyFile)
			if err != nil {
				return nil, fmt.Errorf("failed to read the application-key file: %w", err)
			}
			applicationKey = strings.TrimSpace(string(a))
		}
		options := []datadog.Option{
			datadog.WithLogger(logger),
			datadog.WithTimeout(analysisTempCfg.Timeout.Duration()),
		}
		if cfg.Address != "" {
			options = append(options, datadog.WithAddress(cfg.Address))
		}
		return datadog.NewProvider(apiKey, applicationKey, options...)
	default:
		return nil, fmt.Errorf("any of providers config not found")
	}
}
func NewProvider ( analysisTempCfg * config . TemplatableAnalysisMetrics , providerCfg * config . PipedAnalysisProvider , logger * zap . Logger ) ( metrics . Provider , error ) { switch providerCfg . Type { case model . AnalysisProviderPrometheus : options := [ ] prometheus . Option { prometheus . WithLogger ( logger ) , prometheus . WithTimeout ( analysisTempCfg . Timeout . Duration ( ) ) , } cfg := providerCfg . PrometheusConfig if cfg . UsernameFile != " " && cfg . PasswordFile != " " { username , err := os . ReadFile ( cfg . UsernameFile ) if err != nil { return nil , fmt . Errorf ( " " , err ) } password , err := os . ReadFile ( cfg . PasswordFile ) if err != nil { return nil , fmt . Errorf ( " " , err ) } options = append ( options , prometheus . WithBasicAuth ( strings . TrimSpace ( string ( username ) ) , strings . TrimSpace ( string ( password ) ) ) ) } return prometheus . NewProvider ( providerCfg . PrometheusConfig . Address , options ... ) case model . AnalysisProviderDatadog : var apiKey , applicationKey string cfg := providerCfg . DatadogConfig if cfg . APIKeyFile != " " { a , err := os . ReadFile ( cfg . APIKeyFile ) if err != nil { return nil , fmt . Errorf ( " " , err ) } apiKey = strings . TrimSpace ( string ( a ) ) } if cfg . ApplicationKeyFile != " " { a , err := os . ReadFile ( cfg . ApplicationKeyFile ) if err != nil { return nil , fmt . Errorf ( " " , err ) } applicationKey = strings . TrimSpace ( string ( a ) ) } options := [ ] datadog . Option { datadog . WithLogger ( logger ) , datadog . WithTimeout ( analysisTempCfg . Timeout . Duration ( ) ) , } if cfg . Address != " " { options = append ( options , datadog . WithAddress ( cfg . Address ) ) } return datadog . NewProvider ( apiKey , applicationKey , options ... ) default : return nil , fmt . Errorf ( " " ) } }
NewProvider generates an appropriate provider according to analysis provider config.
NewProvider generates an appropriate provider according to analysis provider config .