[release-12.0.3] Unified storage: Respect GF_DATABASE_URL override (#107566)

Unified storage: Respect GF_DATABASE_URL override (#105331)

* Database for unified storage resources now reuses DB code that respects URL override.

Access instrument_queries via section getter.

(cherry picked from commit 041c343a86)
This commit is contained in:
Peter Štibraný
2025-07-03 14:35:37 +02:00
committed by GitHub
parent 716cf170fb
commit fb59ff62a8
3 changed files with 86 additions and 31 deletions
+16 -8
View File
@@ -10,6 +10,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"github.com/grafana/grafana/pkg/services/sqlstore"
"xorm.io/xorm"
infraDB "github.com/grafana/grafana/pkg/infra/db"
@@ -31,8 +33,8 @@ const grafanaDBInstrumentQueriesKey = "instrument_queries"
var errGrafanaDBInstrumentedNotSupported = errors.New("the Resource API is " +
"attempting to leverage the database from core Grafana defined in the" +
" [database] INI section since a database configuration was not provided" +
" in the [resource_api] section. But we detected that the key `" +
grafanaDBInstrumentQueriesKey + "` is enabled in [database], and that" +
" in the [resource_api] section. But we detected that the key" +
" `instrument_queries` is enabled in [database], and that" +
" setup is currently unsupported. Please, consider disabling that flag")
func ProvideResourceDB(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.Tracer) (db.DBProvider, error) {
@@ -76,7 +78,11 @@ func newResourceDBProvider(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.
// as fallback, and as it uses a dedicated INI section, then keys are not
// prefixed with "db_"
getter := newConfGetter(cfg.SectionWithEnvOverrides("resource_api"), "db_")
fallbackGetter := newConfGetter(cfg.SectionWithEnvOverrides("database"), "")
fallbackConfig, fallbackErr := sqlstore.NewDatabaseConfig(cfg, nil)
if fallbackErr != nil {
// Ignore error here and keep going.
fallbackConfig = nil
}
p = &resourceDBProvider{
cfg: cfg,
@@ -87,7 +93,6 @@ func newResourceDBProvider(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.
}
dbType := getter.String("type")
grafanaDBType := fallbackGetter.String("type")
switch {
// Deprecated: First try with the config in the "resource_api" section, which is specific to Unified Storage
case dbType == dbTypePostgres:
@@ -104,19 +109,22 @@ func newResourceDBProvider(grafanaDB infraDB.DB, cfg *setting.Cfg, tracer trace.
return p, fmt.Errorf("invalid db type specified: %s", dbType)
// If we have an empty Resource API db config, try with the core Grafana database config
case grafanaDBType != "":
case fallbackConfig != nil && fallbackConfig.Type != "":
p.registerMetrics = true
p.engine, err = getEngine(cfg)
p.engine, err = getEngine(fallbackConfig)
return p, err
case grafanaDB != nil:
// try to use the grafana db connection (should only happen in tests)
if fallbackGetter.Bool(grafanaDBInstrumentQueriesKey) {
if newConfGetter(cfg.SectionWithEnvOverrides("database"), "").Bool(grafanaDBInstrumentQueriesKey) {
return nil, errGrafanaDBInstrumentedNotSupported
}
p.engine = grafanaDB.GetEngine()
return p, nil
default:
return p, fmt.Errorf("no database type specified")
if fallbackErr != nil {
return nil, fallbackErr
}
return nil, fmt.Errorf("no database type specified")
}
}