Glue: Add DB migration & support provisioning for user-defined correlations config (#55560)

* Allow provisioning correlation config

* Simplify code

* Fix reading correlations test

* Fix linting errors

* Fix linting errors

* remove simpleJson

* Clean up

* Fix tests

* Update swagger docs

* Fix linting

* Fix linting

* Clean up swagger definitions

Co-authored-by: Elfo404 <me@giordanoricci.com>
This commit is contained in:
Piotr Jamróz
2022-09-27 11:08:02 +02:00
committed by GitHub
co-authored by Elfo404
parent a49fcbdbbc
commit becdf10f0e
10 changed files with 207 additions and 8 deletions
+1
View File
@@ -16,6 +16,7 @@ func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCo
TargetUID: cmd.TargetUID,
Label: cmd.Label,
Description: cmd.Description,
Config: cmd.Config,
}
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *sqlstore.DBSession) error {
+21
View File
@@ -13,7 +13,22 @@ var (
ErrUpdateCorrelationEmptyParams = errors.New("not enough parameters to edit correlation")
)
// CorrelationConfigTarget is the target data query specific to target data source (Correlation.TargetUID)
// swagger:model
type CorrelationConfigTarget struct{}
// swagger:model
type CorrelationConfig struct {
// Field used to attach the correlation link
// required:true
Field string `json:"field"`
// Target data query
// required:true
Target CorrelationConfigTarget `json:"target"`
}
// Correlation is the model for correlations definitions
// swagger:model
type Correlation struct {
// Unique identifier of the correlation
// example: 50xhMlg9k
@@ -30,6 +45,9 @@ type Correlation struct {
// Description of the correlation
// example: Logs to Traces
Description string `json:"description" xorm:"description"`
// Correlation Configuration
// example: { field: "job", target: { query: "job=app" } }
Config CorrelationConfig `json:"config" xorm:"jsonb config"`
}
// CreateCorrelationResponse is the response struct for CreateCorrelationCommand
@@ -56,6 +74,9 @@ type CreateCorrelationCommand struct {
// Optional description of the correlation
// example: Logs to Traces
Description string `json:"description"`
// Arbitrary configuration object handled in frontend
// example: { field: "job", target: { query: "job=app" } }
Config CorrelationConfig `json:"config"`
}
// swagger:model
@@ -5,6 +5,8 @@ import (
"errors"
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/datasources"
@@ -134,20 +136,37 @@ func (dc *DatasourceProvisioner) applyChanges(ctx context.Context, configPath st
return nil
}
func makeCreateCorrelationCommand(correlation map[string]interface{}, SourceUid string, OrgId int64) (correlations.CreateCorrelationCommand, error) {
func makeCreateCorrelationCommand(correlation map[string]interface{}, SourceUID string, OrgId int64) (correlations.CreateCorrelationCommand, error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
targetUID, ok := correlation["targetUID"].(string)
if !ok {
return correlations.CreateCorrelationCommand{}, fmt.Errorf("correlation missing targetUID")
}
return correlations.CreateCorrelationCommand{
SourceUID: SourceUid,
createCommand := correlations.CreateCorrelationCommand{
SourceUID: SourceUID,
TargetUID: targetUID,
Label: correlation["label"].(string),
Description: correlation["description"].(string),
OrgId: OrgId,
SkipReadOnlyCheck: true,
}, nil
}
if correlation["config"] != nil {
jsonbody, err := json.Marshal(correlation["config"])
if err != nil {
return correlations.CreateCorrelationCommand{}, err
}
config := correlations.CorrelationConfig{}
if err := json.Unmarshal(jsonbody, &config); err != nil {
return correlations.CreateCorrelationCommand{}, err
}
createCommand.Config = config
}
return createCommand, nil
}
func (dc *DatasourceProvisioner) deleteDatasources(ctx context.Context, dsToDelete []*deleteDatasourceConfig) error {
@@ -25,4 +25,8 @@ func addCorrelationsMigrations(mg *Migrator) {
mg.AddMigration("add index correlations.uid", NewAddIndexMigration(correlationsV1, correlationsV1.Indices[0]))
mg.AddMigration("add index correlations.source_uid", NewAddIndexMigration(correlationsV1, correlationsV1.Indices[1]))
mg.AddMigration("add correlation config column", NewAddColumnMigration(correlationsV1, &Column{
Name: "config", Type: DB_Text, Nullable: true,
}))
}