grafana/pkg/plugins/config/config.go
Giuseppe Guerra 09078b14e1
Plugins: Support for distributed tracing in backend plugins SDK (#63714)
* Tracing: Pass OTLP address and propagation format to plugins

* Fix unit tests

* Fix indentation

* Fix plugin manager integration tests

* Goimports

* Pass plugin version to plugins

* Do not add GF_PLUGIN_VERSION if plugin version is not set, add tests

* Allow disabling plugins distributed tracing on a per-plugin basis

* Moved disabled plugins to tracing.opentelemetry config section

* Pre-allocate DisabledPlugins map to the correct size

* Moved disable tracing setting flags in plugin settings

* Renamed plugin env vars for tracing endpoint and propagation

* Fix plugin initializer tests

* Refactoring: Moved OpentelemetryCfg from pkg/infra to pkg/plugins

* Changed GetSection to Section in parseSettingsOpentelemetry

* Add tests for NewOpentelemetryCfg

* Fix test case names in TestNewOpentelemetryCfg

* OpenTelemetry: Remove redundant error checks
2023-03-30 23:31:14 +02:00

96 lines
2.8 KiB
Go

package config
import (
"fmt"
"strings"
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/setting"
)
type Cfg struct {
log log.Logger
DevMode bool
PluginsPath string
PluginSettings setting.PluginSettings
PluginsAllowUnsigned []string
// AWS Plugin Auth
AWSAllowedAuthProviders []string
AWSAssumeRoleEnabled bool
// Azure Cloud settings
Azure *azsettings.AzureSettings
BuildVersion string // TODO Remove
LogDatasourceRequests bool
PluginsCDNURLTemplate string
Opentelemetry OpentelemetryCfg
}
func ProvideConfig(settingProvider setting.Provider, grafanaCfg *setting.Cfg) (*Cfg, error) {
return NewCfg(settingProvider, grafanaCfg)
}
func NewCfg(settingProvider setting.Provider, grafanaCfg *setting.Cfg) (*Cfg, error) {
logger := log.New("plugin.cfg")
aws := settingProvider.Section("aws")
plugins := settingProvider.Section("plugins")
allowedUnsigned := grafanaCfg.PluginsAllowUnsigned
if len(plugins.KeyValue("allow_loading_unsigned_plugins").Value()) > 0 {
allowedUnsigned = strings.Split(plugins.KeyValue("allow_loading_unsigned_plugins").Value(), ",")
}
allowedAuth := grafanaCfg.AWSAllowedAuthProviders
if len(aws.KeyValue("allowed_auth_providers").Value()) > 0 {
allowedUnsigned = strings.Split(settingProvider.KeyValue("plugins", "allow_loading_unsigned_plugins").Value(), ",")
}
otelCfg, err := NewOpentelemetryCfg(grafanaCfg)
if err != nil {
return nil, fmt.Errorf("new opentelemetry cfg: %w", err)
}
return &Cfg{
log: logger,
PluginsPath: grafanaCfg.PluginsPath,
BuildVersion: grafanaCfg.BuildVersion,
DevMode: settingProvider.KeyValue("", "app_mode").MustBool(grafanaCfg.Env == setting.Dev),
PluginSettings: extractPluginSettings(settingProvider),
PluginsAllowUnsigned: allowedUnsigned,
AWSAllowedAuthProviders: allowedAuth,
AWSAssumeRoleEnabled: aws.KeyValue("assume_role_enabled").MustBool(grafanaCfg.AWSAssumeRoleEnabled),
Azure: grafanaCfg.Azure,
LogDatasourceRequests: grafanaCfg.PluginLogBackendRequests,
PluginsCDNURLTemplate: grafanaCfg.PluginsCDNURLTemplate,
Opentelemetry: otelCfg,
}, nil
}
func extractPluginSettings(settingProvider setting.Provider) setting.PluginSettings {
ps := setting.PluginSettings{}
for sectionName, sectionCopy := range settingProvider.Current() {
if !strings.HasPrefix(sectionName, "plugin.") {
continue
}
// Calling Current() returns a redacted version of section. We need to replace the map values with the unredacted values.
section := settingProvider.Section(sectionName)
for k := range sectionCopy {
sectionCopy[k] = section.KeyValue(k).MustString("")
}
pluginID := strings.Replace(sectionName, "plugin.", "", 1)
ps[pluginID] = sectionCopy
}
return ps
}